def __init__(self, data=None): ''' Constructor for the FDU Descriptor :param data dictionary containing the FDU descriptor :return the FDU object ''' self.e = user_entity.user_entity() self.encoder = pybindJSONEncoder() self.id = None self.uuid = None self.atomic_entities = [] self.virtual_links = [] if data is not None: pybindJSONDecoder.load_ietf_json({'entity_descriptor': data}, None, None, obj=self.e, skip_unknown=True) self.enforce() self.id = self.e.entity_descriptor.id self.name = data.get('name') self.description = data.get('description') self.uuid = data.get('uuid', None) self.atomic_entities = data.get('atomic_entities') self.virtual_links = data.get('virtual_links')
def __init__(self, data=None): ''' Constructor for the FDU Descriptor :param data dictionary containing the FDU descriptor :return the FDU object ''' self.e = infra_entity.infra_entity() self.encoder = pybindJSONEncoder() self.entity_id = None self.uuid = None self.atomic_entities = [] self.virtual_links = [] if data is not None: pybindJSONDecoder.load_ietf_json({'entity_record': data}, None, None, obj=self.e) self.enforce() self.entity_id = self.e.entity_record.entity_id self.uuid = self.e.entity_record.uuid self.atomic_entities = data.get('atomic_entities') self.virtual_links = data.get('virtual_links')
def __init__(self, data=None): ''' Constructor for the FDU Record :param data dictionary containing the FDU Record :return the FDU object ''' self.fdu = infra_fdu.infra_fdu() self.encoder = pybindJSONEncoder() self.uuid = None self.fdu_id = None self.status = None self.image = {} self.command = {} self.storage = [] self.computation_requirements = {} self.geographical_requirements = {} self.energy_requirements = {} self.hypervisor = None self.migration_kind = None self.configuration = {} self.interfaces = [] self.io_ports = [] self.connection_points = [] self.depends_on = [] self.error_code = None self.error_msg = None self.migration_properties = {} self.hypervisor_info = {} if data is not None: # data = json.loads(data) if isinstance(data['hypervisor_info'], dict): data.update({'hypervisor_info':json.dumps(data['hypervisor_info'])}) pybindJSONDecoder.load_ietf_json({'fdu_record':data}, None, None, obj=self.fdu) self.enforce() while isinstance(data['hypervisor_info'], str): data.update({'hypervisor_info':json.loads(data['hypervisor_info'])}) self.uuid = self.fdu.fdu_record.uuid self.fdu_id = self.fdu.fdu_record.fdu_id self.image = data.get('image', None) self.command = data.get('command', None) self.status = data.get('status') self.storage = data.get('storage') self.computation_requirements = data.get('computation_requirements') self.geographical_requirements = data.get('geographical_requirements', None) self.energy_requirements = data.get('energy_requirements', None) self.hypervisor = data.get('hypervisor') self.migration_kind = data.get('migration_kind') self.configuration = data.get('configuration', None) self.interfaces = data.get('interfaces') self.io_ports = data.get('io_ports') self.connection_points = data.get('connection_points') self.depends_on = data.get('depends_on') self.error_code = data.get('error_code', None) self.error_msg = data.get('error_msg', None) self.migration_properties = data.get('migration_properties', None) self.hypervisor_info = data.get('hypervisor_info')
def __init__(self, data=None): ''' Constructor for the FDU Descriptor :param data dictionary containing the FDU descriptor :return the FDU object ''' self.ae = infra_atomic_entity.infra_atomic_entity() self.encoder = pybindJSONEncoder() self.atomic_entity_id = None self.uuid = None self.fdus = [] self.internal_virtual_links = [] self.connection_points = [] self.depends_on = [] if data is not None: def conv(x): x['properties'] = json.dumps(x['properties']) return x cdata = copy.deepcopy(data) xs = list(map(conv, cdata['connection_points'])) cdata['connection_points'] = xs pybindJSONDecoder.load_ietf_json({'ae_record': cdata}, None, None, obj=self.ae, skip_unknown=True) self.enforce() self.atomic_entity_id = self.ae.ae_record.atomic_entity_id self.uuid = self.ae.ae_record.uuid self.fdus = [InfraFDU(x) for x in data.get('fdus')] self.internal_virtual_links = data.get('internal_virtual_links') self.connection_points = data.get('connection_points') self.depends_on = data.get('depends_on')
def __init__(self, data=None): ''' Constructor for the FDU Descriptor :param data dictionary containing the FDU descriptor :return the FDU object ''' self.ae = user_atomic_entity.user_atomic_entity() self.encoder = pybindJSONEncoder() self.id = None self.uuid = None self.name = None self.description = None self.fdus = [] self.internal_virtual_links = [] self.connection_points = [] self.depends_on = [] if data is not None: pybindJSONDecoder.load_ietf_json({'ae_descriptor': data}, None, None, obj=self.ae, skip_unknown=True) self.enforce() self.id = self.ae.ae_descriptor.id self.name = data.get('name') self.description = data.get('description') self.uuid = data.get('uuid', None) self.fdus = [FDU(x) for x in data.get('fdus')] self.internal_virtual_links = data.get('internal_virtual_links') self.connection_points = data.get('connection_points') self.depends_on = data.get('depends_on')
def service_set_request(request, path_helper, logger): response_msg = pynms_rpc_pb2.SetResponse(request_id=request.request_id) prefix = request.prefix if len(request.prefix) else None logger.debug("Starting set routine with input message as %s" % request) if pynms_rpc_pb2.EncodingType.Name(request.encoding) == 'JSON_IETF': decoder = pybindJSONDecoder.load_ietf_json elif pynms_rpc_pb2.EncodingType.Name(request.encoding) == 'JSON_PYBIND': decoder = pybindJSONDecoder.load_json else: response_msg.response_code = pynms_rpc_pb2.UNSUPPORTED_ENCODING response_msg.message = "Unsupported encoding" return response_msg logger.debug("Determind the encoding to be %s from message" % request.encoding) logger.debug("Starting checkpointing...") checkpoint = {} encoder = pybindJSONEncoder() for operation in request.operation: logger.debug("Looking for %s" % str(operation)) path = operation.path if prefix is None else prefix + "/" + operation.path logger.debug("Trying to checkpoint %s" % path) # TODO: do we assume that the request.operation dataset is ordered # for us? chk_objects = path_helper.get(path) checkpoint[operation.path] = PyNMSServerGRPCMethods.get_encoded_object_set(path_helper, path, "JSON_PYBIND", logger) logger.debug("Checkpointed %s successfully" % operation.path) error_paths = [] completed_paths = [] for operation in request.operation: try: existing_obj = path_helper.get_unique(operation.path) except YANGPathHelperException as m: logger.debug("Hit a YANGPathException when getting %s" % operation.path) error_paths.append({ 'path': path, 'error': pynms_rpc_pb2.INVALID_PATH, 'message': 'Invalid Path' }) break if pynms_rpc_pb2.SetDataCommand.Name(operation.opcode) in ['UPDATE_CONFIG', 'REPLACE_CONFIG']: logger.debug("Running parsing for %s with method %s" % (operation.path, pynms_rpc_pb2.SetDataCommand.Name(operation.opcode))) try: parsed_json = json.loads(operation.value) except ValueError as m: logger.debug("Hit an exception when loading the JSON for %s -> %s" % (operation.path, str(m))) error_paths.append({ 'path': operation.path, 'error': pynms_rpc_pb2.INVALID_CONFIGURATION, 'message': 'Invalid JSON' }) break except Exception as e: logger.debug("Hit unknown exception %s @ %s:152" % (str(e), __file__)) break overwrite = True if pynms_rpc_pb2.SetDataCommand.Name(operation.opcode) == 'REPLACE_CONFIG' else False try: decoder(parsed_json, None, None, obj=existing_obj, path_helper=path_helper, overwrite=overwrite) except ValueError as e: logger.debug("Hit a ValueError when loading the JSON for %s (specified to %s)" % (operation.path, existing_obj._path())) logger.debug("ValueError: %s" % str(msg)) error_paths.append({ 'path': operation.path, 'error': pynms_rpc_pb2.INVALID_CONFIGURATION, 'message': 'Invalid configuration' }) break except AttributeError as msg: logger.debug("Hit a AttributeError when loading the JSON for %s (specified to %s)" % (operation.path, existing_obj._path())) logger.debug("AttributeError: %s" % str(msg)) error_paths.append({ 'path': operation.path, 'error': pynms_rpc_pb2.INVALID_CONFIGURATION, 'message': 'Invalid configuration' }) break except KeyError as msg: logger.debug("Hit a KeyError when loading the JSON for %s" % (operation.path)) error_paths.append({ 'path': operation.path, 'error': pynms_rpc_pb2.INVALID_CONFIGURATION, 'message': 'Attempted to update an existing leaf entry', }) except Exception as e: logger.debug("Hit unknown exception %s @ %s:152" % (str(e), __file__)) break elif pynms_rpc_pb2.SetDataCommand.Name(operation.opcode) in ['DELETE_CONFIG']: if "[" in existing_obj._path()[-1]: path = existing_obj._path()[:-1] containing_obj = path_helper.get_unique(path) parent = getattr(containing_obj,existing_obj._path()[-1].split("[")[0]) else: parent = existing_obj._parent if hasattr(parent, "delete"): keyval = parent._extract_key(existing_obj) # todo, error handling if hasattr(keyval, "_get_ptr"): keyval = keyval._get_ptr() parent.delete(keyval) else: # if the value is not a list entry unset_method = getattr(parent, "_unset_%s" % safe_name(operation.path.split("/")[-1]), None) if unset_method is None: error_paths.append({ 'path': operation.path, 'error': pynms_rpc_pb2.NOK, 'message': "Could not delete %s" % path }) break unset_method() completed_paths.append(operation.path) if len(error_paths) == 0: response_msg.response_code = pynms_rpc_pb2.OK return response_msg # need to rollback # # We need to rollback by applying changes in reverse order to the transaction that # was specified. completed_paths.reverse() for path in completed_paths: original_content = checkpoint[path] existing_object = path_helper.get_unique(path) pybindJSONDecoder.load_json(original_content, None, None, obj=existing_object, path_helper=path_helper, overwrite=True) response_msg.response_code = error_paths[0]['error'] response_msg.message = error_paths[0]['message'] return response_msg
def __init__(self, data=None): ''' Constructor for the FDU Descriptor Parameters ---------- data : dictionary optional dictionart containing the FDU descriptor returns ------- FDU ''' self.fdu = user_fdu.user_fdu() self.encoder = pybindJSONEncoder() self.id = None self.uuid = None self.name = None self.description = None self.image = None self.command = None self.storage = [] self.computation_requirements = None self.geographical_requirements = None self.energy_requirements = None self.hypervisor = None self.migration_kind = None self.configuration = None self.interfaces = [] self.io_ports = [] self.connection_points = [] self.depends_on = [] if data is not None: pybindJSONDecoder.load_ietf_json({'fdu_descriptor': data}, None, None, obj=self.fdu, skip_unknown=True) self.enforce() self.id = self.fdu.fdu_descriptor.id self.uuid = data.get('uuid', None) self.image = data.get('image', None) self.command = data.get('command', None) self.name = data.get('name') self.description = data.get('description', None) self.storage = data.get('storage') self.computation_requirements = data.get( 'computation_requirements') self.geographical_requirements = data.get( 'geographical_requirements', None) self.energy_requirements = data.get('energy_requirements', None) self.hypervisor = data.get('hypervisor') self.migration_kind = data.get('migration_kind') self.configuration = data.get('configuration', None) self.interfaces = data.get('interfaces') self.io_ports = data.get('io_ports') self.connection_points = data.get('connection_points') self.depends_on = data.get('depends_on')
new_rt = other_root.devices.device.add('TEST3') new_rt.mgmt_ip = "10.10.10.11" new_rt.config.commands.cmd.append('net add swp1 access vlan 10') t1 = json.loads(pybindJSON.dumps(test_root)) t2 = json.loads(pybindJSON.dumps(other_root)) diff1 = (diff(t1, t2)) print(diff1) #if 'devices' in diff1.keys() and 'device' in diff1['devices'].keys(): # for device in diff1['devices']['device'].keys(): # if 'config' in diff1['devices']['device'][device]: ## pass #print ((diff1['devices']['device'])) #ddiff = DeepDiff(t1, t2, ignore_order=True) #print (ddiff) print(test_root.devices.device.keys()) print(dir(pybindJSONDecoder)) print(rt.mgmt_ip) print((json.loads(pybindJSON.dumps(test_root.devices.device['TEST'])))) print(pybindJSONEncoder(test_root))
def service_set_request(request, path_helper, logger): response_msg = pynms_rpc_pb2.SetResponse(request_id=request.request_id) prefix = request.prefix if len(request.prefix) else None logger.debug("Starting set routine with input message as %s" % request) if pynms_rpc_pb2.EncodingType.Name(request.encoding) == 'JSON_IETF': decoder = pybindJSONDecoder.load_ietf_json elif pynms_rpc_pb2.EncodingType.Name( request.encoding) == 'JSON_PYBIND': decoder = pybindJSONDecoder.load_json else: response_msg.response_code = pynms_rpc_pb2.UNSUPPORTED_ENCODING response_msg.message = "Unsupported encoding" return response_msg logger.debug("Determind the encoding to be %s from message" % request.encoding) logger.debug("Starting checkpointing...") checkpoint = {} encoder = pybindJSONEncoder() for operation in request.operation: logger.debug("Looking for %s" % str(operation)) path = operation.path if prefix is None else prefix + "/" + operation.path logger.debug("Trying to checkpoint %s" % path) # TODO: do we assume that the request.operation dataset is ordered # for us? chk_objects = path_helper.get(path) checkpoint[operation. path] = PyNMSServerGRPCMethods.get_encoded_object_set( path_helper, path, "JSON_PYBIND", logger) logger.debug("Checkpointed %s successfully" % operation.path) error_paths = [] completed_paths = [] for operation in request.operation: try: existing_obj = path_helper.get_unique(operation.path) except YANGPathHelperException as m: logger.debug("Hit a YANGPathException when getting %s" % operation.path) error_paths.append({ 'path': path, 'error': pynms_rpc_pb2.INVALID_PATH, 'message': 'Invalid Path' }) break if pynms_rpc_pb2.SetDataCommand.Name( operation.opcode) in ['UPDATE_CONFIG', 'REPLACE_CONFIG']: logger.debug( "Running parsing for %s with method %s" % (operation.path, pynms_rpc_pb2.SetDataCommand.Name(operation.opcode))) try: parsed_json = json.loads(operation.value) except ValueError as m: logger.debug( "Hit an exception when loading the JSON for %s -> %s" % (operation.path, str(m))) error_paths.append({ 'path': operation.path, 'error': pynms_rpc_pb2.INVALID_CONFIGURATION, 'message': 'Invalid JSON' }) break except Exception as e: logger.debug("Hit unknown exception %s @ %s:152" % (str(e), __file__)) break overwrite = True if pynms_rpc_pb2.SetDataCommand.Name( operation.opcode) == 'REPLACE_CONFIG' else False try: decoder(parsed_json, None, None, obj=existing_obj, path_helper=path_helper, overwrite=overwrite) except ValueError as e: logger.debug( "Hit a ValueError when loading the JSON for %s (specified to %s)" % (operation.path, existing_obj._path())) logger.debug("ValueError: %s" % str(msg)) error_paths.append({ 'path': operation.path, 'error': pynms_rpc_pb2.INVALID_CONFIGURATION, 'message': 'Invalid configuration' }) break except AttributeError as msg: logger.debug( "Hit a AttributeError when loading the JSON for %s (specified to %s)" % (operation.path, existing_obj._path())) logger.debug("AttributeError: %s" % str(msg)) error_paths.append({ 'path': operation.path, 'error': pynms_rpc_pb2.INVALID_CONFIGURATION, 'message': 'Invalid configuration' }) break except KeyError as msg: logger.debug( "Hit a KeyError when loading the JSON for %s" % (operation.path)) error_paths.append({ 'path': operation.path, 'error': pynms_rpc_pb2.INVALID_CONFIGURATION, 'message': 'Attempted to update an existing leaf entry', }) except Exception as e: logger.debug("Hit unknown exception %s @ %s:152" % (str(e), __file__)) break elif pynms_rpc_pb2.SetDataCommand.Name( operation.opcode) in ['DELETE_CONFIG']: if "[" in existing_obj._path()[-1]: path = existing_obj._path()[:-1] containing_obj = path_helper.get_unique(path) parent = getattr(containing_obj, existing_obj._path()[-1].split("[")[0]) else: parent = existing_obj._parent if hasattr(parent, "delete"): keyval = parent._extract_key(existing_obj) # todo, error handling if hasattr(keyval, "_get_ptr"): keyval = keyval._get_ptr() parent.delete(keyval) else: # if the value is not a list entry unset_method = getattr( parent, "_unset_%s" % safe_name(operation.path.split("/")[-1]), None) if unset_method is None: error_paths.append({ 'path': operation.path, 'error': pynms_rpc_pb2.NOK, 'message': "Could not delete %s" % path }) break unset_method() completed_paths.append(operation.path) if len(error_paths) == 0: response_msg.response_code = pynms_rpc_pb2.OK return response_msg # need to rollback # # We need to rollback by applying changes in reverse order to the transaction that # was specified. completed_paths.reverse() for path in completed_paths: original_content = checkpoint[path] existing_object = path_helper.get_unique(path) pybindJSONDecoder.load_json(original_content, None, None, obj=existing_object, path_helper=path_helper, overwrite=True) response_msg.response_code = error_paths[0]['error'] response_msg.message = error_paths[0]['message'] return response_msg