Skip to content

Commit

Permalink
Method to redeploy an EVC
Browse files Browse the repository at this point in the history
New method to redeploy an EVC. The action is
taken only if the EVC is enabled.
Fixes kytos#31
  • Loading branch information
ajoaoff committed Aug 12, 2021
1 parent 15707f7 commit bf3e18e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
28 changes: 25 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,28 @@ def delete_circuit(self, circuit_id):
emit_event(self.controller, 'deleted', evc_id=evc.id)
return jsonify(result), status

@rest('/v2/evc/<circuit_id>/redeploy', methods=['PATCH'])
def redeploy(self, circuit_id):
"""Endpoint to force the redeployment of an EVC
"""
log.debug('redeploy /v2/evc/%s/redeploy', circuit_id)
try:
evc = self.circuits[circuit_id]
except KeyError:
result = f'circuit_id {circuit_id} not found'
raise NotFound(result)
if evc.is_enabled():
with evc.lock:
evc.remove_current_flows()
evc.deploy()
result = {'response': f'Circuit {circuit_id} redeploy received.'}
status = 202
else:
result = {'response': f'Circuit {circuit_id} is disabled.'}
status = 409

return jsonify(result), status

@rest('/v2/evc/schedule', methods=['GET'])
def list_schedules(self):
"""Endpoint to return all schedules stored for all circuits.
Expand Down Expand Up @@ -320,7 +342,7 @@ def create_schedule(self):
"""
log.debug('create_schedule /v2/evc/schedule/')

json_data = self.json_from_request('create_schedule')
json_data = self._json_from_request('create_schedule')
try:
circuit_id = json_data['circuit_id']
except TypeError:
Expand Down Expand Up @@ -407,7 +429,7 @@ def update_schedule(self, schedule_id):
log.debug('update_schedule result %s %s', result, 403)
raise Forbidden(result)

data = self.json_from_request('update_schedule')
data = self._json_from_request('update_schedule')

new_schedule = CircuitSchedule.from_dict(data)
new_schedule.id = found_schedule.id
Expand Down Expand Up @@ -702,7 +724,7 @@ def _get_circuits_buffer(self):
return self.circuits

@staticmethod
def json_from_request(caller):
def _json_from_request(caller):
"""Return a json from request.
If it was not possible to get a json from the request, log, for debug,
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def test_verify_api_urls(self):
({'circuit_id': '[circuit_id]'}, {'OPTIONS', 'PATCH'},
'/api/kytos/mef_eline/v2/evc/<circuit_id>'),

({'circuit_id': '[circuit_id]'}, {'OPTIONS', 'PATCH'},
'/api/kytos/mef_eline/v2/evc/<circuit_id>/redeploy'),

({}, {'OPTIONS', 'GET', 'HEAD'},
'/api/kytos/mef_eline/v2/evc/schedule'),

Expand Down Expand Up @@ -712,6 +715,39 @@ def test_load_circuits_by_interface(self):
# pylint: disable=protected-access
self.assertEqual(self.napp._circuits_by_interface, expected_result)

def test_redeploy_evc(self):
"""Test endpoint to redeploy an EVC."""
evc1 = MagicMock()
evc1.is_enabled.return_value = True
self.napp.circuits = {'1': evc1,
'2': MagicMock()}
api = self.get_app_test_client(self.napp)
url = f'{self.server_name_url}/v2/evc/1/redeploy'
response = api.patch(url)
self.assertEqual(response.status_code, 202, response.data)

def test_redeploy_evc_disabled(self):
"""Test endpoint to redeploy an EVC."""
evc1 = MagicMock()
evc1.is_enabled.return_value = False
self.napp.circuits = {'1': evc1,
'2': MagicMock()}
api = self.get_app_test_client(self.napp)
url = f'{self.server_name_url}/v2/evc/1/redeploy'
response = api.patch(url)
self.assertEqual(response.status_code, 409, response.data)

def test_redeploy_evc_deleted(self):
"""Test endpoint to redeploy an EVC."""
evc1 = MagicMock()
evc1.is_enabled.return_value = True
self.napp.circuits = {'1': evc1,
'2': MagicMock()}
api = self.get_app_test_client(self.napp)
url = f'{self.server_name_url}/v2/evc/3/redeploy'
response = api.patch(url)
self.assertEqual(response.status_code, 404, response.data)

def test_list_schedules__no_data(self):
"""Test list of schedules."""
api = self.get_app_test_client(self.napp)
Expand Down

0 comments on commit bf3e18e

Please sign in to comment.