def test_006_ietf_deserialise(self): root = self.path_helper.get_unique("/") fh = open(os.path.join(os.path.dirname(__file__), "json", "06-deserialise-ietf.json"), 'r') pybindJSONDecoder.load_ietf_json(json.load(fh), None, None, obj=root) v = json.loads(pbJ.dumps(self.path_helper.get_unique("/"), mode="ietf")) x = json.load(open(os.path.join(os.path.dirname(__file__), "json", "06-deserialise-ietf.json"), 'r')) self.assertEqual(v, x)
def to_json(self): ''' Converts the FDU into a dictionary returns ------- dictionary ''' data = { 'uuid': self.uuid, 'id': self.id, 'name': self.name, 'description': self.description, 'image': self.image, 'command': self.command, 'storage': self.storage, 'computation_requirements': self.computation_requirements, 'geographical_requirements': self.geographical_requirements, 'energy_requirements': self.energy_requirements, 'hypervisor': self.hypervisor, 'migration_kind': self.migration_kind, 'configuration': self.configuration, 'interfaces': self.interfaces, 'io_ports': self.io_ports, 'connection_points': self.connection_points, 'depends_on': self.depends_on, } check_obj = user_fdu.user_fdu() pybindJSONDecoder.load_ietf_json({'fdu_descriptor': data}, None, None, obj=check_obj, skip_unknown=True) return data
def to_json(self): data = { 'uuid': self.uuid, 'atomic_entity_id': self.atomic_entity_id, 'fdus': [x.to_json() for x in self.fdus], 'internal_virtual_links': self.internal_virtual_links, 'connection_points': self.connection_points, 'depends_on': self.depends_on, } check_obj = infra_atomic_entity.infra_atomic_entity() 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=check_obj, skip_unknown=True) return data
def pyangbind_validation(self, item, data, force=False): ''' item: vnfd, nst, nsd data: dict object loaded from the descriptor file force: True to skip unknown fields in the descriptor ''' if item == "vnfd": myobj = vnfd_im() elif item == "nsd": myobj = nsd_im() elif item == "nst": myobj = nst_im() else: raise ValidationException( "Not possible to validate '{}' item".format(item)) try: pybindJSONDecoder.load_ietf_json(data, None, None, obj=myobj, path_helper=True, skip_unknown=force) out = pybindJSON.dumps(myobj, mode="ietf") desc_out = yaml.safe_load(out) return desc_out except Exception as e: raise ValidationException( "Error in pyangbind validation: {}".format(str(e)))
def test_006_ietf_deserialise(self): root = self.path_helper.get_unique("/") fh = open(os.path.join(this_dir, "json", "06-deserialise-ietf.json"), 'r') pybindJSONDecoder.load_ietf_json(json.load(fh), None, None, obj=root) v = json.loads(pbJ.dumps(self.path_helper.get_unique("/"), mode="ietf")) x = json.load(open(os.path.join(this_dir, "json", "06-deserialise-ietf.json"), 'r')) self.assertEqual(v, x)
def to_json(self): data = { 'uuid': self.uuid, 'fdu_id': self.fdu_id, 'status': self.status, 'image': self.image, 'command': self.command, 'storage': self.storage, 'computation_requirements': self.computation_requirements, 'geographical_requirements': self.geographical_requirements, 'energy_requirements': self.energy_requirements, 'hypervisor': self.hypervisor, 'migration_kind': self.migration_kind, 'configuration': self.configuration, 'interfaces': self.interfaces, 'io_ports': self.io_ports, 'connection_points': self.connection_points, 'depends_on': self.depends_on, 'error_code': self.error_code, 'error_msg': self.error_msg, 'migration_properties': self.migration_properties, 'hypervisor_info': self.hypervisor_info } check_obj = infra_fdu.infra_fdu() pybindJSONDecoder.load_ietf_json({'fdu_record':data.update({'hypervisor_info':json.dumps(data['hypervisor_info'])})}, None, None, obj=check_obj) data.update({'hypervisor_info':json.loads(data['hypervisor_info'])}) return data
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 ValidateContainer(ap, container): """Validates a container adheres to schema and returns a state object. # TODO(xavier): This function should aim to replace ValidateJoinedAPs. Args: ap: (object) chido_test.ApObject containing all AP attributes. container: (str) a supported container within the model. Returns: YANGBaseClass object with data from the JSON state response. """ container_obj = _GetContainer(ap, container) path = _GetPathByContainer(ap, container) has_state = False if '/config' in path: has_state = True path = path.replace('/config', '/state') gnmi_response = GetPath(ap, path) json_bytes = gnmi_response.notification[0].update[0].val.json_ietf_val json_bytes = json_bytes.replace(b'openconfig-wifi-types:', b'') if has_state: state = pybindJSONDecoder.load_ietf_json(json.loads(json_bytes), None, None, obj=container_obj.state) else: state = pybindJSONDecoder.load_ietf_json(json.loads(json_bytes), None, None, obj=container_obj) # print(pybindJSON.dumps(state, mode='ietf')) return state
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 test_dont_skip_unknown_keys(self): allowed = True try: with open(os.path.join(os.path.dirname(__file__), "json", "nonexist.json"), "r") as fp: pybindJSONDecoder.load_ietf_json(json.load(fp), self.bindings, "json_deserialise", skip_unknown=False) except AttributeError: allowed = False self.assertFalse(allowed, "Skipping keys that did not exist was not successfully handled.")
def test_006_ietf_deserialise(self): root = self.path_helper.get_unique("/") with open(os.path.join(os.path.dirname(__file__), "json", "06-deserialise-ietf.json"), 'r') as fp: pybindJSONDecoder.load_ietf_json(json.load(fp), None, None, obj=root) v = json.loads(pbJ.dumps(self.path_helper.get_unique("/"), mode="ietf")) with open(os.path.join(os.path.dirname(__file__), "json", "06-deserialise-ietf.json"), 'r') as fp: x = json.load(fp) self.assertEqual(v, x)
def DisableRadio(ap, radio_obj, five_g=True): """Disables radio. Args: ap: (object) chido_test.ApObject containing all AP attributes. radio_obj: (YANGBaseClass) OC radio container object. five_g: (bool) Whether target is 5GHz radio. Raises: ConfigError: if radio config does not match. StateMismatchError: if the radio did not get disabled. """ ap.radio_id = '0' if five_g else '1' ap.radio_freq = 'FREQ_5GHZ' if five_g else 'FREQ_2GHZ' path = _GetPathByContainer(ap, 'radios') logging.info('Setting radio id %s to disabled', ap.radio_id) if not five_g: radio_obj.id = ap.radio_id radio_obj.operating_frequency = ap.radio_freq radio_obj.channel = 1 radio_obj.enabled = False json_str = pybindJSON.dumps(radio_obj, mode='ietf') SetConfig(ap, xpath=path, json_str=json_str) expected_config = pybindJSON.dumps(radio_obj, mode='ietf') # string format. # We reset path in case some parameters changed based above. path = _GetPathByContainer(ap, 'radios') gnmi_response = GetPath(ap, path) json_bytes = gnmi_response.notification[0].update[0].val.json_ietf_val json_bytes = json_bytes.replace(b'openconfig-wifi-types:', b'') retrieved_config_obj = pybindJSONDecoder.load_ietf_json( json.loads(json_bytes), None, None, obj=radio_obj) retrieved_config = pybindJSON.dumps(retrieved_config_obj, mode='ietf') if expected_config != retrieved_config: logging.info(expected_config) logging.info(retrieved_config) raise ConfigError('Radio "%s" config does not match config sent' % ap.radio_id) path = path.replace('/config', '/state') radio = _GetContainer(ap, 'radios') gnmi_response = GetPath(ap, path) json_bytes = gnmi_response.notification[0].update[0].val.json_ietf_val json_bytes = json_bytes.replace(b'openconfig-wifi-types:', b'') radio_state = pybindJSONDecoder.load_ietf_json(json.loads(json_bytes), None, None, obj=radio.state) if radio_state.enabled: raise StateMismatchError('Radio %s not disabled') logging.info('Radio "%s" was disabled', ap.radio_id)
def osm_dep_validator(self,descriptor): input_file_name = descriptor try: data = input_file_name if "vnfd:vnfd-catalog" in data or "vnfd-catalog" in data: descriptor = "VNF" # Check if mgmt-interface is defined: self.remove_prefix(data, "vnfd:") vnfd_descriptor = data["vnfd-catalog"] vnfd_list = vnfd_descriptor["vnfd"] mgmt_iface = False for vnfd in vnfd_list: vdu_list = vnfd["vdu"] for vdu in vdu_list: interface_list = [] external_interface_list = vdu.pop("external-interface", ()) for external_interface in external_interface_list: if external_interface.get("virtual-interface", {}).get("type") == "OM-MGMT": raise KeyError( "Wrong 'Virtual-interface type': Deprecated 'OM-MGMT' value. Please, use 'PARAVIRT' instead") interface_list = vdu.get("interface", ()) for interface in interface_list: if interface.get("virtual-interface", {}).get("type") == "OM-MGMT": raise KeyError( "Wrong 'Virtual-interface type': Deprecated 'OM-MGMT' value. Please, use 'PARAVIRT' instead") if vnfd.get("mgmt-interface"): mgmt_iface = True if vnfd["mgmt-interface"].get("vdu-id"): raise KeyError("'mgmt-iface': Deprecated 'vdu-id' field. Please, use 'cp' field instead") if not mgmt_iface: raise KeyError("'mgmt-iface' is a mandatory field and it is not defined") myvnfd = vnfd_catalog() pybindJSONDecoder.load_ietf_json(data, None, None, obj=myvnfd) return True elif "nsd:nsd-catalog" in data or "nsd-catalog" in data: descriptor = "NS" mynsd = nsd_catalog() pybindJSONDecoder.load_ietf_json(data, None, None, obj=mynsd) return True else: descriptor = None raise KeyError("This is not neither nsd-catalog nor vnfd-catalog descriptor") except Exception as e: if input_file_name: print("Error loading file: {}".format(pprint.pformat(str(e))), file=sys.stderr) else: if descriptor: print("Error. Invalid {} descriptor format in '{}': {}".format(descriptor, input_file_name, str(e)), file=sys.stderr) else: print("Error. Invalid descriptor format in '{}': {}".format(input_file_name, str(e)), file=sys.stderr)
def test_dont_skip_unknown_keys(self): allowed = True try: with open(os.path.join(os.path.dirname(__file__), "json", "nonexistkey.json")) as fp: pybindJSONDecoder.load_ietf_json( json.load(fp), self.bindings, "ietf_json_deserialise", skip_unknown=False ) except AttributeError: allowed = False self.assertFalse(allowed, "Skipping keys that did not exist was not successfully handled.")
def to_json(self): data = { 'uuid': self.uuid, 'entity_id': self.entity_id, 'atomic_entities': self.atomic_entities, 'virtual_links': self.virtual_links } check_obj = infra_entity.infra_entity() pybindJSONDecoder.load_ietf_json({'entity_record': data}, None, None, obj=check_obj) return data
def decodeJson(json_text: Union[str, bytes], obj: PybindBase): """Decode a JSON text into a PybindBase object. This method is used to validate the JSON text adheres to the OC schema. Args: json_text: The JSON-IETF text to decode. obj: The PybindBase object to decode the texto into. Raises: Error if unable to parse the JSON text. """ json_text = removeOpenConfigPrefix(json_text) pybindJSONDecoder.load_ietf_json(json.loads(json_text), None, None, obj)
def test_006_ietf_deserialise(self): root = self.path_helper.get_unique("/") with open(os.path.join(this_dir, "json", "06-deserialise-ietf.json"), 'r') as fh: pybindJSONDecoder.load_ietf_json(json.load(fh), None, None, obj=root) v = json.loads(pbJ.dumps(self.path_helper.get_unique("/"), mode="ietf")) with open(os.path.join(this_dir, "json", "06-deserialise-ietf.json"), 'r') as f: x = json.load(f) self.assertEqual(v, x)
def to_json(self): data = { 'uuid': self.uuid, 'id': self.id, 'name': self.name, 'description': self.description, 'atomic_entities': self.atomic_entities, 'virtual_links': self.virtual_links } check_obj = user_entity.user_entity() pybindJSONDecoder.load_ietf_json({'entity_descriptor': data}, None, None, obj=check_obj) return data
def test_load_graceful_restart(self): expected_json = { "bgp": { "neighbors": { "neighbor": { "12.12.12.12": { "config": { "peer-group": "g1" }, "neighbor-address": "12.12.12.12" }, "13.13.13.13": { "neighbor-address": "13.13.13.13", "config": { "peer-group": "g2" } } } } } } with open(os.path.join(os.path.dirname(__file__), "json", "bgp-gr-ex.json"), 'r') as fp: graceful_restart_neighbors = json.load(fp) actual_json = pybindJSONDecoder.load_ietf_json( graceful_restart_neighbors['configuration'], self.ocbind, 'openconfig_bgp', path_helper=self.yang_helper ).get(filter=True) self.assertEqual(actual_json, expected_json, "Graceful restart example was not loaded correctly.")
def test_list_with_children_load(self): expected_json = { "chlist": { 1: { "keyleaf": 1, "child": { "number": 1, "string": "one" } }, 2: { "keyleaf": 2, "child": { "number": 2, "string": "two" } }, } } with open( os.path.join(os.path.dirname(__file__), "json", "chlist.json")) as fp: actual_json = pybindJSONDecoder.load_ietf_json( json.load(fp), self.bindings, "ietf_json_deserialise").get(filter=True) self.assertEqual( actual_json, expected_json, "List with children load did not return expected JSON")
def test_load_global_config(self): expected_json = { "bgp": { "global": { "confederation": { "config": { "identifier": 65517, "member-as": [ 65518, 65519, 65520 ] } } } } } with open(os.path.join(os.path.dirname(__file__), "json", "bgp-global-ex.json"), 'r') as fp: bgp_global_ex = json.load(fp) actual_json = pybindJSONDecoder.load_ietf_json( bgp_global_ex['configuration'], self.ocbind, 'openconfig_bgp', path_helper=self.yang_helper ).get(filter=True) self.assertEqual(actual_json, expected_json, "Invalid JSON loaded for global config")
def test_load_graceful_restart(self): expected_json = { "bgp": { "neighbors": { "neighbor": { "12.12.12.12": { "config": { "peer-group": "g1" }, "neighbor-address": "12.12.12.12" }, "13.13.13.13": { "neighbor-address": "13.13.13.13", "config": { "peer-group": "g2" } } } } } } with open( os.path.join(os.path.dirname(__file__), "json", "bgp-gr-ex.json"), 'r') as fp: graceful_restart_neighbors = json.load(fp) actual_json = pybindJSONDecoder.load_ietf_json( graceful_restart_neighbors['configuration'], self.ocbind, 'openconfig_bgp', path_helper=self.yang_helper).get(filter=True) self.assertEqual(actual_json, expected_json, "Graceful restart example was not loaded correctly.")
def test_load_neighbor_list(self): expected_json = { "bgp": { "neighbors": { "neighbor": { "13.13.13.13": { "neighbor-address": "13.13.13.13", "config": { "peer-group": "g1" } }, "12.12.12.12": { "neighbor-address": "12.12.12.12", "config": { "peer-group": "g1" } } } } } } with open( os.path.join(os.path.dirname(__file__), "json", "bgp-neighbor-list-ex.json"), 'r') as fp: bgp_neighbor_list = json.load(fp) actual_json = pybindJSONDecoder.load_ietf_json( bgp_neighbor_list['configuration'], self.ocbind, 'openconfig_bgp', path_helper=self.yang_helper).get(filter=True) self.assertEqual(actual_json, expected_json, "Invalid JSON returned when loading neighbor list")
def GetContainerFromJson(ap, json_path, container): """Returns an OC object based on provided json file. Json must adhere to schema. Args: ap: (object) chido_test.ApObject containing all AP attributes. json_path: (str) full path to JSON file. container: (str) a supported container within the model. Returns: Serialized OC Object (YANGBaseClass). Raises: UnsupportedContainerError: If container is not supported. """ container = _GetContainer(ap, container) json_data = '' with open(json_path, 'rt') as data_file: json_data = data_file.read() return pybindJSONDecoder.load_ietf_json(json.loads(json_data), None, None, obj=container.config)
def Deserialize(ap, gnmi_response, del_messages=True): """Checks if a given json can be deserialized by adhering to schema.""" # TODO(xavier): Figure out if an option to pull high level containers of # hostname, radios, ssids, system, assigned_ap_managers makes sense. ap_base = v020binding.openconfig_access_points() if ap.vendor == 'arista': ap_base = arista_aps.openconfig_access_points() ap_obj = ap_base.access_points.access_point.add(ap.ap_name) # TODO(xavier): Figure out why pybindJSONDecoder needs this workaround. json_bytes = gnmi_response.notification[0].update[0].val.json_ietf_val json_bytes = json_bytes.replace(b'openconfig-aaa:', b'') json_bytes = json_bytes.replace(b'openconfig-wifi-types:', b'') json_dict = json.loads(json_bytes) if del_messages: # Delete inconsistently implemented model. Test separately. json_dict['openconfig-access-points:system'].pop('messages', None) binded_obj = pybindJSONDecoder.load_ietf_json(json_dict, None, None, obj=ap_obj) # print(pybindJSON.dumps(binded_obj, mode='ietf')) return binded_obj
def test_load_neighbor_list(self): expected_json = { "bgp": { "neighbors": { "neighbor": { "13.13.13.13": { "neighbor-address": "13.13.13.13", "config": { "peer-group": "g1" } }, "12.12.12.12": { "neighbor-address": "12.12.12.12", "config": { "peer-group": "g1" } } } } } } with open(os.path.join(os.path.dirname(__file__), "json", "bgp-neighbor-list-ex.json"), 'r') as fp: bgp_neighbor_list = json.load(fp) actual_json = pybindJSONDecoder.load_ietf_json( bgp_neighbor_list['configuration'], self.ocbind, 'openconfig_bgp', path_helper=self.yang_helper ).get(filter=True) self.assertEqual(actual_json, expected_json, "Invalid JSON returned when loading neighbor list")
def loads_ietf(d, parent_pymod, yang_base, path_helper=None, extmethods=None, overwrite=False): # Same as above, to allow for load_ietf to work the same way if isinstance(d, six.string_types + (six.text_type,)): d = json.loads(d, object_pairs_hook=OrderedDict) return pybindJSONDecoder.load_ietf_json( d, parent_pymod, yang_base, path_helper=path_helper, extmethods=extmethods, overwrite=overwrite )
def test_load_deactivated(self): expected_json = {"bgp": {"global": {"config": {"router-id": "10.10.10.10"}}}} with open(os.path.join(os.path.dirname(__file__), "json", "bgp-deactivated-config-ex.json"), "r") as fp: deactivated = json.load(fp) actual_json = pybindJSONDecoder.load_ietf_json( deactivated["configuration"], self.ocbind, "openconfig_bgp", path_helper=self.yang_helper ).get(filter=True) self.assertEqual(actual_json, expected_json, "Router ID configuration example not loaded correctly.")
def test_all_the_types(self): expected_json = { 'c1': { 'l1': { 1: { 'one-leaf': 'hi', 'typedef-one': 'test', 'boolean': True, 'binary': bitarray('111111'), 'union': '16', 'identityref': 'idone', 'enumeration': 'one', 'k1': 1, 'uint16': 1, 'union-list': [16, 'chicken'], 'uint32': 1, 'int32': 1, 'int16': 1, 'string': 'bear', 'typedef-two': 8, 'uint8': 1, 'restricted-integer': 6, 'leafref': '16', 'int8': 1, 'uint64': 1, 'remote-identityref': 'remote:stilton', 'int64': 1, 'restricted-string': 'aardvark', 'decimal': Decimal('16.32'), 'empty': True } }, 'l2': OrderedDict( [ (1, {'k1': 1}), (2, {'k1': 2}), (3, {'k1': 3}), (4, {'k1': 4}), (5, {'k1': 5}), (6, {'k1': 6}), (7, {'k1': 7}), (8, {'k1': 8}), (9, {'k1': 9}) ]), 't1': { '32': {'target': '32'}, '16': {'target': '16'} } } } with open(os.path.join(os.path.dirname(__file__), "json", "complete-obj.json")) as fp: actual_json = pybindJSONDecoder.load_ietf_json( json.load(fp), self.bindings, "ietf_json_deserialise" ).get(filter=True) self.assertEqual(actual_json, expected_json, "Deserialisation of complete object not as expected.")
def test_all_the_types(self): expected_json = { 'c1': { 'l1': { 1: { 'one-leaf': 'hi', 'typedef-one': 'test', 'boolean': True, 'binary': bitarray('010101'), 'union': '16', 'identityref': 'idone', 'enumeration': 'one', 'k1': 1, 'uint16': 1, 'union-list': [16, 'chicken'], 'uint32': 1, 'int32': 1, 'int16': 1, 'string': 'bear', 'typedef-two': 8, 'uint8': 1, 'restricted-integer': 6, 'leafref': '16', 'int8': 1, 'uint64': 1, 'remote-identityref': 'remote:stilton', 'int64': 1, 'restricted-string': 'aardvark', 'decimal': Decimal('16.32'), 'empty': True } }, 'l2': OrderedDict( [ (1, {'k1': 1}), (2, {'k1': 2}), (3, {'k1': 3}), (4, {'k1': 4}), (5, {'k1': 5}), (6, {'k1': 6}), (7, {'k1': 7}), (8, {'k1': 8}), (9, {'k1': 9}) ]), 't1': { '32': {'target': '32'}, '16': {'target': '16'} } } } with open(os.path.join(os.path.dirname(__file__), "json", "complete-obj.json")) as fp: actual_json = pybindJSONDecoder.load_ietf_json( json.load(fp), self.bindings, "ietf_json_deserialise" ).get(filter=True) self.assertEqual(actual_json, expected_json, "Deserialisation of complete object not as expected.")
def test_single_key_list_load(self): expected_json = { "skey": {"one": {"leaf-one": "one"}, "three": {"leaf-one": "three"}, "two": {"leaf-one": "two"}} } with open(os.path.join(os.path.dirname(__file__), "json", "skeylist.json")) as fp: actual_json = pybindJSONDecoder.load_ietf_json(json.load(fp), self.bindings, "ietf_json_deserialise").get( filter=True ) self.assertEqual(actual_json, expected_json, "Single key list load did not return expected JSON")
def to_json(self): data = { 'uuid': self.uuid, 'id': self.id, 'name': self.name, 'description': self.description, 'fdus': [x.to_json() for x in self.fdus], 'internal_virtual_links': self.internal_virtual_links, 'connection_points': self.connection_points, 'depends_on': self.depends_on, } check_obj = user_atomic_entity.user_atomic_entity() pybindJSONDecoder.load_ietf_json({'ae_descriptor': data}, None, None, obj=check_obj, skip_unknown=True) return data
def test_multi_key_list_load(self): expected_json = {'mkey': {'one 1': {'leaf-two': 1, 'leaf-one': 'one'}, 'three 2': {'leaf-two': 2, 'leaf-one': 'three'}}} with open(os.path.join(os.path.dirname(__file__), "json", "mkeylist.json")) as fp: actual_json = pybindJSONDecoder.load_ietf_json( json.load(fp), self.bindings, "ietf_json_deserialise" ).get(filter=True) self.assertEqual(actual_json, expected_json, "Multikey list load did not return expected JSON")
def test_list_with_children_load(self): expected_json = {'chlist': {1: {'keyleaf': 1, 'child': {'number': 1, 'string': 'one'}}, 2: {'keyleaf': 2, 'child': {'number': 2, 'string': 'two'}}}} with open(os.path.join(os.path.dirname(__file__), "json", "chlist.json")) as fp: actual_json = pybindJSONDecoder.load_ietf_json( json.load(fp), self.bindings, "ietf_json_deserialise" ).get(filter=True) self.assertEqual(actual_json, expected_json, "List with children load did not return expected JSON")
def test_load_deactivated_metadata(self): with open(os.path.join(os.path.dirname(__file__), "json", "bgp-deactivated-config-ex.json"), "r") as fp: deactivated = json.load(fp) deactivated_instance = pybindJSONDecoder.load_ietf_json( deactivated["configuration"], self.ocbind, "openconfig_bgp", path_helper=self.yang_helper ) self.assertTrue( deactivated_instance.bgp.global_.config.router_id._metadata["inactive"], "Metadata for router-id element not set correctly.", )
def test_python_compatibility(self): """A simple test to verify Python compatibility. This test exercises basic IM interoperability with supported versions of Python in order to verify the IM libraries compatibility. As of 30 Nov 2017, the IM library fails with Python3. This invokes that failing code so that it can be repeatably tested: ValueError: '_pybind_generated_by' in __slots__ conflicts with class variable """ try: data = yaml.load(VNFD_YAML) myvnfd = vnfd_catalog.vnfd() pybindJSONDecoder.load_ietf_json(data, None, None, obj=myvnfd) except ValueError: assert False
def test_load_graceful_restart_metadata(self): with open(os.path.join(os.path.dirname(__file__), "json", "bgp-gr-ex.json"), "r") as fp: graceful_restart_neighbors = json.load(fp) neighbors_instance = pybindJSONDecoder.load_ietf_json( graceful_restart_neighbors["configuration"], self.ocbind, "openconfig_bgp", path_helper=self.yang_helper ) self.assertEqual( neighbors_instance.bgp.neighbors.neighbor["12.12.12.12"]._metadata, {"inactive": True}, "Metadata for graceful restart example was not loaded correctly.", )
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 test_list_with_children_load(self): expected_json = { "chlist": { 1: {"keyleaf": 1, "child": {"number": 1, "string": "one"}}, 2: {"keyleaf": 2, "child": {"number": 2, "string": "two"}}, } } with open(os.path.join(os.path.dirname(__file__), "json", "chlist.json")) as fp: actual_json = pybindJSONDecoder.load_ietf_json(json.load(fp), self.bindings, "ietf_json_deserialise").get( filter=True ) self.assertEqual(actual_json, expected_json, "List with children load did not return expected JSON")
def test_all_the_types(self): expected_json = { "c1": { "l1": { 1: { "one-leaf": "hi", "typedef-one": "test", "boolean": True, "binary": bitarray("010101"), "union": "16", "identityref": "idone", "enumeration": "one", "k1": 1, "uint16": 1, "union-list": [16, "chicken"], "uint32": 1, "int32": 1, "int16": 1, "string": "bear", "typedef-two": 8, "uint8": 1, "restricted-integer": 6, "leafref": "16", "int8": 1, "uint64": 1, "remote-identityref": "stilton", "int64": 1, "restricted-string": "aardvark", "decimal": Decimal("16.32"), "empty": True, } }, "l2": OrderedDict( [ (1, {"k1": 1}), (2, {"k1": 2}), (3, {"k1": 3}), (4, {"k1": 4}), (5, {"k1": 5}), (6, {"k1": 6}), (7, {"k1": 7}), (8, {"k1": 8}), (9, {"k1": 9}), ] ), "t1": {"32": {"target": "32"}, "16": {"target": "16"}}, } } with open(os.path.join(os.path.dirname(__file__), "json", "complete-obj.json")) as fp: actual_json = pybindJSONDecoder.load_ietf_json(json.load(fp), self.bindings, "ietf_json_deserialise").get( filter=True ) self.assertEqual(actual_json, expected_json, "Deserialisation of complete object not as expected.")
def test_load_graceful_restart_metadata(self): with open( os.path.join(os.path.dirname(__file__), "json", "bgp-gr-ex.json"), 'r') as fp: graceful_restart_neighbors = json.load(fp) neighbors_instance = pybindJSONDecoder.load_ietf_json( graceful_restart_neighbors['configuration'], self.ocbind, 'openconfig_bgp', path_helper=self.yang_helper) self.assertEqual( neighbors_instance.bgp.neighbors.neighbor["12.12.12.12"]._metadata, {'inactive': True}, "Metadata for graceful restart example was not loaded correctly.")
def test_load_deactivated_metadata(self): with open( os.path.join(os.path.dirname(__file__), "json", "bgp-deactivated-config-ex.json"), 'r') as fp: deactivated = json.load(fp) deactivated_instance = pybindJSONDecoder.load_ietf_json( deactivated['configuration'], self.ocbind, 'openconfig_bgp', path_helper=self.yang_helper) self.assertTrue( deactivated_instance.bgp.global_.config.router_id. _metadata['inactive'], "Metadata for router-id element not set correctly.")
#!/usr/bin/env python from __future__ import print_function, unicode_literals from rbindings.simple_rpc_rpc.test.input import input from rbindings.simple_rpc_rpc.test.output import output from pyangbind.lib.serialise import pybindJSONDecoder from pyangbind.lib.pybindJSON import dumps import pprint import os import json pp = pprint.PrettyPrinter(indent=4) # Create an input instance rpc_input = input() rpc_input.input_container.argument_one = "test_call" rpc_input.input_container.argument_two = 32 print(dumps(rpc_input, mode="ietf")) # Load an output from IETF JSON rpc_output = output() fn = os.path.join("json", "rpc-output.json") json_obj = json.load(open(fn, "r")) pybindJSONDecoder.load_ietf_json(json_obj, None, None, obj=rpc_output) print(rpc_output.response_id) pp.pprint(rpc_output.get(filter=True))
def main(): try: opts, args = getopt.getopt(sys.argv[1:], "k", ["keepfiles"]) except getopt.GetoptError as e: print str(e) sys.exit(127) k = False for o, a in opts: if o in ["-k", "--keepfiles"]: k = True pythonpath = os.environ.get("PATH_TO_PYBIND_TEST_PYTHON") if \ os.environ.get('PATH_TO_PYBIND_TEST_PYTHON') is not None \ else sys.executable pyangpath = os.environ.get('PYANGPATH') if \ os.environ.get('PYANGPATH') is not None else False pyangbindpath = os.environ.get('PYANGBINDPATH') if \ os.environ.get('PYANGBINDPATH') is not None else False assert pyangpath is not False, "could not find path to pyang" assert pyangbindpath is not False, "could not resolve pyangbind directory" this_dir = os.path.dirname(os.path.realpath(__file__)) cmd = "%s " % pythonpath cmd += "%s --plugindir %s/pyangbind/plugin" % (pyangpath, pyangbindpath) cmd += " -f pybind -o %s/bindings.py" % this_dir cmd += " -p %s" % this_dir cmd += " %s/%s.yang" % (this_dir, TESTNAME) os.system(cmd) import bindings from bindings import ietf_json_deserialise pth = os.path.join(this_dir, "json", "mkeylist.json") nobj = pybindJSONDecoder.load_ietf_json(json.load(open(pth, 'r')), bindings, "ietf_json_deserialise") expected_get = {'mkey': {u'one 1': {'leaf-two': 1, 'leaf-one': u'one'}, u'three 2': {'leaf-two': 2, 'leaf-one': u'three'}}} assert nobj.get(filter=True) == expected_get, \ "Multikey list load did not return expected JSON" del nobj pth = os.path.join(this_dir, "json", "skeylist.json") nobj = pybindJSONDecoder.load_ietf_json(json.load(open(pth, 'r')), bindings, "ietf_json_deserialise") expected_get = {'skey': {u'one': {'leaf-one': u'one'}, u'three': {'leaf-one': u'three'}, u'two': {'leaf-one': u'two'}}} assert nobj.get(filter=True) == expected_get, "Single key list load did " + \ "not return expected JSON" del nobj pth = os.path.join(this_dir, "json", "chlist.json") nobj = pybindJSONDecoder.load_ietf_json(json.load(open(pth, 'r')), bindings, "ietf_json_deserialise") expected_get = {'chlist': {1: {'keyleaf': 1, 'child': {'number': 1, 'string': u'one'}}, 2: {'keyleaf': 2, 'child': {'number': 2, 'string': u'two'}}}} assert nobj.get(filter=True) == expected_get, "List with children load " + \ "did not return expected JSON" pth = os.path.join(this_dir, "json", "complete-obj.json") nobj = pybindJSONDecoder.load_ietf_json(json.load(open(pth, 'r')), bindings, "ietf_json_deserialise") expected_get = { "c1": { "l1": { 1: { "one-leaf": "hi", "typedef-one": "test", "boolean": True, "binary": bitarray("111111"), "union": "16", "identityref": "idone", "enumeration": "one", "k1": 1, "uint16": 1, "union-list": [16, "chicken"], "uint32": 1, "int32": 1, "int16": 1, "string": "bear", "typedef-two": 8, "uint8": 1, "restricted-integer": 6, "leafref": "16", "int8": 1, "uint64": 1, "remote-identityref": "remote:stilton", "int64": 1, "restricted-string": "aardvark", "decimal": Decimal('16.32') } }, "l2": OrderedDict( [ (1, {"k1": 1}), (2, {"k1": 2}), (3, {"k1": 3}), (4, {"k1": 4}), (5, {"k1": 5}), (6, {"k1": 6}), (7, {"k1": 7}), (8, {"k1": 8}), (9, {"k1": 9}) ]), "t1": { "32": {"target": "32"}, "16": {"target": "16"} } } } assert nobj.get(filter=True) == expected_get, "Deserialisation of " + \ "complete object not as expected" pth = os.path.join(this_dir, "json", "nonexistkey.json") for i in [True, False]: nobj = None success = True try: nobj = pybindJSONDecoder.load_ietf_json(json.load(open(pth, 'r')), bindings, "ietf_json_deserialise", skip_unknown=i) except AttributeError: success = False assert success is i, "Skipping keys that did not exist was not" + \ " successfully handled" if not k: os.system("/bin/rm %s/bindings.py" % this_dir) os.system("/bin/rm %s/bindings.pyc" % this_dir)
# Dump the static routes instance as JSON in IETF format print(pybindJSON.dumps(oclr.local_routes, mode="ietf")) # Load the "json/oc-lr.json" file into a new instance of # "openconfig_local_routing". We import the module here, such that a new # instance of the class can be created by the deserialisation code import binding new_oclr = pybindJSON.load(os.path.join("json", "oc-lr.json"), binding, "openconfig_local_routing") # Manipulate the data loaded print("Current tag: %d" % new_oclr.local_routes.static_routes.static[u"192.0.2.1/32"].config.set_tag) new_oclr.local_routes.static_routes.static[u"192.0.2.1/32"].config.set_tag \ += 1 print("New tag: %d" % new_oclr.local_routes.static_routes.static[u"192.0.2.1/32"].config.set_tag) # Load JSON into an existing class structure from pyangbind.lib.serialise import pybindJSONDecoder import json ietf_json = json.load(open(os.path.join("json", "oc-lr_ietf.json"), 'r')) pybindJSONDecoder.load_ietf_json(ietf_json, None, None, obj=new_oclr.local_routes) # Iterate through the classes - both the 192.0.2.1/32 prefix and 192.0.2.2/32 # prefix are now in the objects for prefix, route in new_oclr.local_routes.static_routes.static.iteritems(): print("Prefix: %s, tag: %d" % (prefix, route.config.set_tag))
def main(): try: opts, args = getopt.getopt(sys.argv[1:], "k", ["keepfiles"]) except getopt.GetoptError as e: print str(e) sys.exit(127) k = False for o, a in opts: if o in ["-k", "--keepfiles"]: k = True pythonpath = os.environ.get("PATH_TO_PYBIND_TEST_PYTHON") if \ os.environ.get('PATH_TO_PYBIND_TEST_PYTHON') is not None \ else sys.executable pyangpath = os.environ.get('PYANGPATH') if \ os.environ.get('PYANGPATH') is not None else False pyangbindpath = os.environ.get('PYANGBINDPATH') if \ os.environ.get('PYANGBINDPATH') is not None else False assert pyangpath is not False, "could not find path to pyang" assert pyangbindpath is not False, "could not resolve pyangbind directory" OC = "https://raw.githubusercontent.com/openconfig/" + \ "public/master/release/models/" RFC = "https://raw.githubusercontent.com/YangModels/" + \ "yang/master/standard/ietf/RFC/" FETCH_FILES = [ (OC + "bgp/openconfig-bgp-multiprotocol.yang", "openconfig"), (OC + "bgp/openconfig-bgp-operational.yang", "openconfig"), (OC + "bgp/openconfig-bgp-policy.yang", "openconfig"), (OC + "bgp/openconfig-bgp-types.yang", "openconfig"), (OC + "bgp/openconfig-bgp.yang", "openconfig"), (OC + "policy/openconfig-routing-policy.yang", "openconfig"), (OC + "policy/openconfig-policy-types.yang", "openconfig"), (OC + "openconfig-extensions.yang", "include"), (OC + "openconfig-types.yang", "include"), (OC + "interfaces/openconfig-interfaces.yang", "openconfig"), (RFC + "ietf-inet-types.yang", "include"), (RFC + "ietf-yang-types.yang", "include"), ] this_dir = os.path.dirname(os.path.realpath(__file__)) del_dirs = [] for fn in FETCH_FILES: wrdir = os.path.join(this_dir, fn[1]) if not os.path.exists(wrdir): os.mkdir(wrdir) if wrdir not in del_dirs: del_dirs.append(wrdir) wrpath = os.path.join(this_dir, fn[1], fn[0].split("/")[-1]) if not os.path.exists(wrpath): got = False count = 0 for i in range(0,4): response = requests.get(fn[0]) if response.status_code != 200: time.sleep(2) else: got = True f = open(wrpath, 'w') f.write(response.content) f.close() break assert got is True, "Could not get file %s from GitHub (response: %s)" \ % (response.status_code, fn[0]) files_str = " ".join([os.path.join(this_dir, "openconfig", i) for i in os.listdir(os.path.join(this_dir, "openconfig"))]) cmd = "%s " % pythonpath cmd += "%s --plugindir %s/pyangbind/plugin" % (pyangpath, pyangbindpath) cmd += " -f pybind --split-class-dir %s/ocbind" % this_dir cmd += " -p %s" % this_dir cmd += " -p %s" % os.path.join(this_dir, "include") cmd += files_str # NB: use-xpathhelper is NOT specified here, so we don't try and do anything # with leafrefs os.system(cmd) import ocbind yh = YANGPathHelper() json_dir = os.path.join(this_dir, "json") jbgp_global_ex = json.load( open(os.path.join(json_dir, "bgp-global-ex.json"), 'r')) ljs = pybindJSONDecoder.load_ietf_json(jbgp_global_ex["configuration"], ocbind, "openconfig_bgp", path_helper=yh) expected_ljs = \ { "bgp": { "global": { "confederation": { "config": { "identifier": 65517, "enabled": True, "member-as": [ 65518, 65519, 65520 ] } } } } } assert ljs.get(filter=True) == expected_ljs, \ "Invalid JSON loaded for global config" jbgp_neigh_list = json.load(open(os.path.join(json_dir, "bgp-neighbor-list-ex.json"), 'r')) ljs = pybindJSONDecoder.load_ietf_json(jbgp_neigh_list["configuration"], ocbind, "openconfig_bgp", path_helper=yh) expected_ljs = \ { "bgp": { "neighbors": { "neighbor": { "13.13.13.13": { "neighbor-address": "13.13.13.13", "config": { "peer-group": "g1" } }, "12.12.12.12": { "neighbor-address": "12.12.12.12", "config": { "peer-group": "g1" } } } } } } assert ljs.get(filter=True) == expected_ljs, \ "Invalid JSON returned when loading neighbor list" jbgp_gr = json.load(open(os.path.join(json_dir, "bgp-gr-ex.json"), 'r')) ljs = pybindJSONDecoder.load_ietf_json(jbgp_gr["configuration"], ocbind, "openconfig_bgp", path_helper=yh) expected_ljs = \ { "bgp": { "neighbors": { "neighbor": { "12.12.12.12": { "config": { "peer-group": "g1" }, "neighbor-address": "12.12.12.12" }, "13.13.13.13": { "neighbor-address": "13.13.13.13", "config": { "peer-group": "g2" } } } } } } assert ljs.get(filter=True) == expected_ljs, \ "Graceful restart example was not loaded correctly" assert ljs.bgp.neighbors.neighbor[u"12.12.12.12"]._metadata == \ {u"inactive": True}, \ "Metadata for GR example was not loaded correctly" jbgp_deactivated = json.load(open(os.path.join(json_dir, "bgp-deactivated-config-ex.json"), 'r')) ljs = pybindJSONDecoder.load_ietf_json(jbgp_deactivated["configuration"], ocbind, "openconfig_bgp", path_helper=yh) expected_ljs = \ { "bgp": { "global": { "config": { "router-id": "10.10.10.10" } } } } assert ljs.get(filter=True) == expected_ljs, \ "Router ID configuration example not loaded correctly" assert ljs.bgp.global_.config.router_id._metadata["inactive"] == True, \ "Metadata for router-id element not set correctly" if not k: del_dirs.append(os.path.join(this_dir, "ocbind")) for dirname in del_dirs: for root, dirs, files in os.walk(os.path.join(dirname), topdown=False): for name in files: os.remove(os.path.join(root, name)) for name in dirs: os.rmdir(os.path.join(root, name)) os.rmdir(dirname)