def evc_from_dict(self, evc_dict): """Convert some dict values to instance of EVC classes. This method will convert: [UNI, Link] """ data = evc_dict.copy() # Do not modify the original dict for attribute, value in data.items(): if 'uni' in attribute: try: data[attribute] = self.uni_from_dict(value) except ValueError as exc: raise ValueError(f'Error creating UNI: {exc}') if attribute == 'circuit_scheduler': data[attribute] = [] for schedule in value: data[attribute].append(CircuitSchedule.from_dict(schedule)) if 'link' in attribute: if value: data[attribute] = self.link_from_dict(value) if 'path' in attribute and attribute != 'dynamic_backup_path': if value: data[attribute] = [ self.link_from_dict(link) for link in value ] return EVC(self.controller, **data)
def test_from_dict(self): """Test create circuit schedule from dict.""" circuit_schedule_dict = { "id": 52342432, "action": "create", "frequency": "1 * * * *" } circuit_schedule = CircuitSchedule.from_dict(circuit_schedule_dict) self.assertEqual(circuit_schedule.id, circuit_schedule_dict["id"]) self.assertEqual(circuit_schedule.action, circuit_schedule_dict["action"]) self.assertEqual(circuit_schedule.frequency, circuit_schedule_dict["frequency"])
def update_schedule(self, schedule_id): """Update a schedule. Change all attributes from the given schedule from a EVC circuit. The schedule ID is preserved as default. Payload example: { "date": "2019-08-07T14:52:10.967Z", "interval": "string", "frequency": "1 * * *", "action": "create" } """ log.debug('update_schedule /v2/evc/schedule/%s', schedule_id) # Try to find a circuit schedule evc, found_schedule = self._find_evc_by_schedule_id(schedule_id) # Can not modify circuits deleted and archived if not found_schedule: result = f'schedule_id {schedule_id} not found' log.debug('update_schedule result %s %s', result, 404) raise NotFound(result) if evc.archived: result = f'Circuit {evc.id} is archived. Update is forbidden.' log.debug('update_schedule result %s %s', result, 403) raise Forbidden(result) data = self.json_from_request('update_schedule') new_schedule = CircuitSchedule.from_dict(data) new_schedule.id = found_schedule.id # Remove the old schedule evc.circuit_scheduler.remove(found_schedule) # Append the modified schedule evc.circuit_scheduler.append(new_schedule) # Cancel all schedule jobs self.sched.cancel_job(found_schedule.id) # Add the new circuit schedule self.sched.add_circuit_job(evc, new_schedule) # Save EVC to the storehouse evc.sync() result = new_schedule.as_dict() status = 200 log.debug('update_schedule result %s %s', result, status) return jsonify(result), status
def _evc_dict_with_instances(self, evc_dict): """Convert some dict values to instance of EVC classes. This method will convert: [UNI, Link] """ data = evc_dict.copy() # Do not modify the original dict for attribute, value in data.items(): # Get multiple attributes. # Ex: uni_a, uni_z if "uni" in attribute: try: data[attribute] = self._uni_from_dict(value) except ValueError: result = "Error creating UNI: Invalid value" raise BadRequest(result) from BadRequest if attribute == "circuit_scheduler": data[attribute] = [] for schedule in value: data[attribute].append(CircuitSchedule.from_dict(schedule)) # Get multiple attributes. # Ex: primary_links, # backup_links, # current_links_cache, # primary_links_cache, # backup_links_cache if "links" in attribute: data[attribute] = [ self._link_from_dict(link) for link in value ] # Ex: current_path, # primary_path, # backup_path if "path" in attribute and attribute != "dynamic_backup_path": data[attribute] = Path( [self._link_from_dict(link) for link in value] ) return data
def _evc_dict_with_instances(self, evc_dict): """Convert some dict values to instance of EVC classes. This method will convert: [UNI, Link] """ data = evc_dict.copy() # Do not modify the original dict for attribute, value in data.items(): # Get multiple attributes. # Ex: uni_a, uni_z if 'uni' in attribute: try: data[attribute] = self._uni_from_dict(value) except ValueError as exc: raise ValueError(f'Error creating UNI: {exc}') if attribute == 'circuit_scheduler': data[attribute] = [] for schedule in value: data[attribute].append(CircuitSchedule.from_dict(schedule)) # Get multiple attributes. # Ex: primary_links, # backup_links, # current_links_cache, # primary_links_cache, # backup_links_cache if 'links' in attribute: data[attribute] = [ self._link_from_dict(link) for link in value ] # Get multiple attributes. # Ex: current_path, # primary_path, # backup_path if 'path' in attribute and attribute != 'dynamic_backup_path': data[attribute] = Path( [self._link_from_dict(link) for link in value]) return data
def create_schedule(self): """ Create a new schedule for a given circuit. This service do no check if there are conflicts with another schedule. Payload example: { "circuit_id":"aa:bb:cc", "schedule": { "date": "2019-08-07T14:52:10.967Z", "interval": "string", "frequency": "1 * * * *", "action": "create" } } """ log.debug('create_schedule /v2/evc/schedule/') json_data = self.json_from_request('create_schedule') try: circuit_id = json_data['circuit_id'] except TypeError: result = 'The payload should have a dictionary.' log.debug('create_schedule result %s %s', result, 400) raise BadRequest(result) except KeyError: result = 'Missing circuit_id.' log.debug('create_schedule result %s %s', result, 400) raise BadRequest(result) try: schedule_data = json_data['schedule'] except KeyError: result = 'Missing schedule data.' log.debug('create_schedule result %s %s', result, 400) raise BadRequest(result) # Get EVC from circuits buffer circuits = self._get_circuits_buffer() # get the circuit evc = circuits.get(circuit_id) # get the circuit if not evc: result = f'circuit_id {circuit_id} not found' log.debug('create_schedule result %s %s', result, 404) raise NotFound(result) # Can not modify circuits deleted and archived if evc.archived: result = f'Circuit {circuit_id} is archived. Update is forbidden.' log.debug('create_schedule result %s %s', result, 403) raise Forbidden(result) # new schedule from dict new_schedule = CircuitSchedule.from_dict(schedule_data) # If there is no schedule, create the list if not evc.circuit_scheduler: evc.circuit_scheduler = [] # Add the new schedule evc.circuit_scheduler.append(new_schedule) # Add schedule job self.sched.add_circuit_job(evc, new_schedule) # save circuit to storehouse evc.sync() result = new_schedule.as_dict() status = 201 log.debug('create_schedule result %s %s', result, status) return jsonify(result), status
def test_as_dict(self): """Test the method as_dict.""" attributes = { "controller": get_controller_mock(), "id": "custom_id", "name": "custom_name", "uni_a": get_uni_mocked(is_valid=True), "uni_z": get_uni_mocked(is_valid=True), "start_date": "2018-08-21T18:44:54", "end_date": "2018-08-21T18:44:55", "primary_links": [], "request_time": "2018-08-21T19:10:41", "creation_time": "2018-08-21T18:44:54", "owner": "my_name", "circuit_scheduler": [ CircuitSchedule.from_dict({ "id": 234243247, "action": "create", "frequency": "1 * * * *", }), CircuitSchedule.from_dict({ "id": 234243239, "action": "create", "interval": { "hours": 2 }, }), ], "enabled": True, "priority": 2, } evc = EVC(**attributes) expected_dict = { "id": "custom_id", "name": "custom_name", "uni_a": attributes["uni_a"].as_dict(), "uni_z": attributes["uni_z"].as_dict(), "start_date": "2018-08-21T18:44:54", "end_date": "2018-08-21T18:44:55", "bandwidth": 0, "primary_links": [], "backup_links": [], "current_path": [], "primary_path": [], "backup_path": [], "dynamic_backup_path": False, "request_time": "2018-08-21T19:10:41", "creation_time": "2018-08-21T18:44:54", "circuit_scheduler": [ { "id": 234243247, "action": "create", "frequency": "1 * * * *", }, { "id": 234243239, "action": "create", "interval": { "hours": 2 }, }, ], "active": False, "enabled": True, "priority": 2, } actual_dict = evc.as_dict() for name, value in expected_dict.items(): actual = actual_dict.get(name) self.assertEqual(value, actual)
def test_as_dict(self): """Test the method as_dict.""" attributes = { "controller": get_controller_mock(), "id": "custom_id", "name": "custom_name", "uni_a": get_uni_mocked(is_valid=True), "uni_z": get_uni_mocked(is_valid=True), "start_date": '2018-08-21T18:44:54', "end_date": '2018-08-21T18:44:55', 'primary_links': [], 'request_time': '2018-08-21T19:10:41', 'creation_time': '2018-08-21T18:44:54', 'owner': "my_name", 'circuit_scheduler': [ CircuitSchedule.from_dict({"id": 234243247, "action": "create", "frequency": "1 * * * *"}), CircuitSchedule.from_dict({"id": 234243239, "action": "create", "interval": {"hours": 2}}) ], 'enabled': True, 'priority': 2 } evc = EVC(**attributes) expected_dict = { 'id': 'custom_id', 'name': 'custom_name', 'uni_a': attributes['uni_a'].as_dict(), 'uni_z': attributes['uni_z'].as_dict(), 'start_date': '2018-08-21T18:44:54', 'end_date': '2018-08-21T18:44:55', 'bandwidth': 0, 'primary_links': [], 'backup_links': [], 'current_path': [], 'primary_path': [], 'backup_path': [], 'dynamic_backup_path': False, 'request_time': '2018-08-21T19:10:41', 'creation_time': '2018-08-21T18:44:54', 'circuit_scheduler': [ { "id": 234243247, "action": "create", "frequency": "1 * * * *" }, { "id": 234243239, "action": "create", "interval": { "hours": 2 } } ], 'active': False, 'enabled': True, 'priority': 2 } actual_dict = evc.as_dict() for name, value in expected_dict.items(): actual = actual_dict.get(name) self.assertEqual(value, actual)