def test_004_serialise(self): self.instance_a.root_tc04_a.a = "emigration" self.instance_b.root_tc04_b.b = "alpine-fork" expected_json = json.load(open(os.path.join(os.path.dirname(__file__), "json", "04-serialise.json"))) v = json.loads(pbJ.dumps(self.path_helper.get_unique("/"))) self.assertEqual(v, expected_json) expected_ietf_json = json.load(open(os.path.join(os.path.dirname(__file__), "json", "04b-ietf-serialise.json"))) v = json.loads(pbJ.dumps(self.path_helper.get_unique("/"), mode="ietf")) self.assertEqual(v, expected_ietf_json)
def test_004_serialise(self): self.instance_a.root_tc04_a.a = "emigration" self.instance_b.root_tc04_b.b = "alpine-fork" expected_json = json.load(open(os.path.join(this_dir, "json", "04-serialise.json"))) v = json.loads(pbJ.dumps(self.path_helper.get_unique("/"))) self.assertEqual(v,expected_json) expected_ietf_json = json.load(open(os.path.join(this_dir, "json", "04b-ietf-serialise.json"))) v = json.loads(pbJ.dumps(self.path_helper.get_unique("/"), mode="ietf")) self.assertEqual(v, expected_ietf_json)
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 createTransceiver(slot_width, constellation, bandwidth, fec): trans = sliceable_transceiver_sdm() trans.transceiver.transceiverid = "1" sliceobj = trans.transceiver.slice.add("1") counter = 1 modes = ["LP01", "LP11a", "LP11b", "LP21a", "LP21b", "LP02"] channels = [ "137", "129", "121", "113", "105", "97", "89", "81", "73", "65", "57", "49", "41", "33", "25", "17" ] for i in range(0, len(modes)): for j in range(0, len(channels)): optical_channel = sliceobj.optical_channel.add('%s' % counter) optical_channel.coreid = '%s' % counter optical_channel.modeid = '%s' % modes[i] fs = optical_channel.frequency_slot fs.ncf = '%s' % channels[j] fs.slot_width = slot_width counter += 1 for i in range(1, len(channels) * len(modes) + 1): optical_signal = sliceobj.optical_signal.add('%s' % i) optical_signal.constellation = constellation optical_signal.bandwidth = bandwidth optical_signal.fec = fec equalization = optical_signal.equalization equalization.equalizationid = 1 equalization.mimo = "true" equalization.num_taps = "500" print(pybindJSON.dumps(trans)) return trans
def test_example_vnf_pps_dump_json(self): example_vnf_pps = os.listdir(EXAMPLE_DIR) # check each vnf_pp file against the model print("Validating: {} VNF-PP files.".format(len(example_vnf_pps))) for ep in example_vnf_pps: parsed_vnfpp = check_against_model(os.path.join(EXAMPLE_DIR, ep)) print(pybindJSON.dumps(parsed_vnfpp))
def Provision(ap): """Triggers the provisioning workflow. Args: ap: AP Class object. This populates the conig object (from PyangBind, from YANG model) for day-0 provisioning. """ paths = gnmi_lib.ParsePath(gnmi_lib.PathNames(( '/provision-aps/provision-ap[mac=%s]/' % ap.mac))) provision_apconfigs = ap_manager_configs.provision_aps.provision_ap day0 = provision_apconfigs.add(ap.mac) day0.config.mac = ap.mac day0.config.hostname = ap.ap_name day0.config.country_code = 'US' json_value = json.loads(pybindJSON.dumps(day0, mode='ietf', indent=2)) if FLAGS.dry_run: print('Here\'s the JSON that was created, for sending to the Target:') print('*'*25, '\n\n', json.dumps(json_value, indent=2),'\n\n', '*'*25) print('JSON written as dry_run_provision.json') f = six.moves.builtins.open('dry_run_provision.json', 'w') f.write(json.dumps(json_value, indent=2) + '\n') sys.exit() else: r = gnmi_lib.Set(ap.stub, paths, ap.targetuser, ap.targetpass, json_value, 'update') print('provisioning succesfull, with the following gNMI SetResponse:\n', r)
def test_005_deserialise(self): root = self.path_helper.get_unique("/") fh = open(os.path.join(this_dir, "json", "05-deserialise.json"), 'r') pybindJSONDecoder.load_json(json.load(fh), None, None, obj=root) v = json.loads(pbJ.dumps(self.path_helper.get_unique("/"))) x = json.load(open(os.path.join(this_dir, "json", "05-deserialise.json"), 'r')) self.assertEqual(v, x)
def edit_device(device): session_id = request.cookies.get('SessionCookie') snoDB = ConfigDB.active_sessions[session_id] deviceObj = snoDB.devices.device[device] if request.method == 'GET': device_dict = json.loads(pybindJSON.dumps(deviceObj)) device_type = deviceObj.device_type jtox_cmd_config_yang = "pyang -f jtox ../yang/sno.yang ../device/yang/device.yang ../device/yang/" + str( device_type) + "/cumulus-nclu.yang" jtox_output, jtox_error = subprocess_cmd(jtox_cmd_config_yang) tree_output = json.loads(jtox_output.decode('utf-8')) device_dict['tree'] = tree_output['tree'] print(device_dict) return render_template('editDevice.html', device_dict=device_dict) device_dict = request.form.to_dict() correct_nested_dict(device_dict) if "_method" in device_dict.keys(): snoDB.devices.device.delete(device) ConfigDB.active_sessions[session_id] = snoDB flash("Device deleted successfully") return redirect(url_for('add_device')) #session_id = request.cookies.get('SessionCookie') #snoDB = ConfigDB.active_sessions[session_id] #device = snoDB.devices.device[device] pybindJSONDecoder.load_json(device_dict, None, None, deviceObj) ConfigDB.active_sessions[session_id] = snoDB flash("Device edited successfully") return redirect(url_for('add_device'))
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_json_generation(self): json_dir = os.path.join(os.path.dirname(__file__), "json") for file_name in os.listdir(json_dir): with self.subTest(json_file=file_name), open( os.path.join(json_dir, file_name), "r") as file_handle: parameters = regex.sub( "interfaces\_ph:(?P<pathhelper>[a-zA-Z]+)\-flt:(?P<filter>[a-zA-Z]+)\-m:(?P<mode>[a-zA-Z]+)\.json", "\g<pathhelper>||\g<filter>||\g<mode>", file_name, ).split("||") path_helper, config_filter, mode = (YANGBool(parameters[0]), YANGBool(parameters[1]), parameters[2]) if path_helper: instance = self.ocbind.openconfig_interfaces( path_helper=self.yang_helper) else: instance = self.ocbind.openconfig_interfaces() instance.interfaces.interface.add("eth0") expected_json = json.load(file_handle) actual_json = json.loads( dumps(instance, filter=bool(config_filter), mode=mode)) self.assertEqual( expected_json, actual_json, "Generated JSON did not match expected object for %s" % file_name)
def test0100(self): self.assertArgs(["interface", "address", "prefix_length"]) xpath = "/interfaces/interface[name=%s]" % self.interface _json_value = json.loads(pybindJSON.dumps(self.iface, mode='ietf')) schema.fixSubifIndex(_json_value) self.assertTrue(self.gNMISetUpdate(xpath, _json_value), "gNMI Set did not succeed.")
def CycleChannels(ap, radio_obj, five_g=True, width=20): """Cycles through all available channels. Channels are set on the object and config is sent to the AP. Config leaf is then verified, followed by comparing all values in the state leaf vs config. It takes roughly ~50 secs per channel or ~22 minutes for all 20MHz. Varies per vendor. 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. width: (int) Channel width, eg. 20, 40, 80. """ ap.radio_id = '0' if five_g else '1' ap.radio_freq = 'FREQ_5GHZ' if five_g else 'FREQ_2GHZ' path = _GetPathByContainer(ap, 'radios') channels = _GetChannelSet(five_g, width) for channel in channels: logging.info('Setting radio id %s to channel %s', ap.radio_id, channel) if not five_g: radio_obj.id = ap.radio_id radio_obj.operating_frequency = ap.radio_freq radio_obj.channel_width = width radio_obj.channel = channel radio_obj.id = ap.radio_id json_str = pybindJSON.dumps(radio_obj, mode='ietf') SetConfig(ap, xpath=path, json_str=json_str) _VerifyRadioContainer(ap, radio_obj, five_g)
def _rpc_worker(self, operation_type=None, pybind_object=None, resource_depth=None, timeout=''): """ This is an auto-generated method for the PySwitchLib. """ rest_operation = 'POST' rest_uri = pybind_object._rest_uri_for_post() rest_data = '' rest_commands = [] if rest_uri == '/': rest_uri = pybind_object._rest_uri() label_list_items = lambda x: x pybind_object = pybind_object._parent rest_data = dicttoxml(json.loads(pybindJSON.dumps(pybind_object, mode='rest'), object_pairs_hook=OrderedDict), root=False, attr_type=False, item_func=label_list_items) rest_commands.append( [rest_operation, rest_uri, rest_data, 'rpc', resource_depth]) return (rest_commands, '', timeout)
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 CycleTransmitPowers(ap, radio_obj, power_levels, five_g=True): """Cycles through the given power_levels. Powers are set on the object and config is sent to the AP. Config leaf is then verified, followed by comparing all values in the state leaf vs config. Args: ap: (object) chido_test.ApObject containing all AP attributes. radio_obj: (YANGBaseClass) OC radio container object. power_levels: (list) integers representing the radio transmit-power. five_g: (bool) Whether target is 5GHz radio. """ ap.radio_id = '0' if five_g else '1' ap.radio_freq = 'FREQ_5GHZ' if five_g else 'FREQ_2GHZ' path = _GetPathByContainer(ap, 'radios') for power in power_levels: logging.info('Setting radio id %s to transmit-power %s', ap.radio_id, power) if not five_g: radio_obj.id = ap.radio_id radio_obj.operating_frequency = ap.radio_freq radio_obj.transmit_power = power json_str = pybindJSON.dumps(radio_obj, mode='ietf') SetConfig(ap, xpath=path, json_str=json_str) logging.info('Sent power of %s as %s to %s', power, json_str, path) _VerifyRadioContainer(ap, radio_obj, five_g)
def pyangbind_validation(self, item, data, force=False): try: if item == "vnfds": myvnfd = vnfd_im() pybindJSONDecoder.load_ietf_json( {'vnfd:vnfd-catalog': { 'vnfd': [data] }}, None, None, obj=myvnfd, path_helper=True, skip_unknown=force) out = pybindJSON.dumps(myvnfd, mode="ietf") elif item == "nsds": mynsd = nsd_im() pybindJSONDecoder.load_ietf_json( {'nsd:nsd-catalog': { 'nsd': [data] }}, None, None, obj=mynsd, path_helper=True, skip_unknown=force) out = pybindJSON.dumps(mynsd, mode="ietf") elif item == "nsts": mynst = nst_im() pybindJSONDecoder.load_ietf_json({'nst': [data]}, None, None, obj=mynst, path_helper=True, skip_unknown=force) out = pybindJSON.dumps(mynst, mode="ietf") else: raise EngineException( "Not possible to validate '{}' item".format(item), http_code=HTTPStatus.INTERNAL_SERVER_ERROR) desc_out = self._remove_envelop(yaml.safe_load(out)) return desc_out except Exception as e: raise EngineException("Error in pyangbind validation: {}".format( str(e)), http_code=HTTPStatus.UNPROCESSABLE_ENTITY)
def ConfigPhyMac(ap, student_ssids): """Triggers the configuration of PHY and MAC layer. Args: ap: AP Class object. student_ssids: (list) SSIDs to configure on the AP. This populates the conig object (from PyangBind, from YANG model) for day-1+ configuration. """ paths = gnmi_lib.ParsePath(gnmi_lib.PathNames(( '/access-points/access-point[hostname=%s]/' % ap.ap_name))) ap_configs = access_point_configs.access_points.access_point ap_configs.add(ap.ap_name) open_ssid = ap_configs[ap.ap_name].ssids.ssid.add(ap.openssid) open_ssid.config.name = ap.openssid open_ssid.config.enabled = True open_ssid.config.hidden = False open_ssid.config.operating_frequency = 'FREQ_5GHZ' open_ssid.config.opmode = 'OPEN' open_ssid.wmm.config.trust_dscp = True psk_ssid = ap_configs[ap.ap_name].ssids.ssid.add(ap.pskssid) psk_ssid.config.enabled = True psk_ssid.config.name = ap.pskssid psk_ssid.config.hidden = False psk_ssid.config.operating_frequency = 'FREQ_2_5_GHZ' psk_ssid.config.opmode = 'WPA2_PERSONAL' psk_ssid.config.wpa2_psk = 'testing123' psk_ssid.wmm.config.trust_dscp = True # PHY Layer stuff. fiveg = ap_configs[ap.ap_name].radios.radio.add(0) fiveg.config.id = 0 fiveg.config.operating_frequency = 'FREQ_5GHZ' fiveg.config.enabled = True fiveg.config.dca = False fiveg.config.transmit_power = 3 fiveg.config.channel_width = 20 fiveg.config.channel = 165 twog = ap_configs[ap.ap_name].radios.radio.add(1) twog.config.id = 1 twog.config.operating_frequency = 'FREQ_2GHZ' twog.config.enabled = True twog.config.dca = False twog.config.transmit_power = 3 twog.config.channel_width = 20 twog.config.channel = 6 json_value = _int_fixer(json.loads(pybindJSON.dumps( access_point_configs, mode='ietf', indent=2))['openconfig-access-points:access-points']['access-point'][0]) if FLAGS.dry_run: print('Here\'s the JSON that was created, for sending to the Target:') print('*'*25, '\n\n', json.dumps(json_value, indent=2),'\n\n', '*'*25) print('JSON written as dry_run_configs.json') f = six.moves.builtins.open('dry_run_configs.json', 'w') f.write(json.dumps(json_value, indent=2) + '\n') sys.exit() else: r = gnmi_lib.Set(ap.stub, paths, ap.targetuser, ap.targetpass, json_value, 'update') print('Configuration succesfull, with the following gNMI SetResponse:\n', r) return json.dumps(json_value)
def test_serialise_container(self): self.serialise_obj.two.string_test = "twenty-two" with open(os.path.join(os.path.dirname(__file__), "json", "container.json"), 'r') as fp: self.assertEqual( json.loads(dumps(self.yang_helper.get("/two")[0])), json.load(fp), "Invalid output returned when serialising a container." )
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 test_example_vnf_bds_dump_json(self): example_vnf_bds = os.listdir(EXAMPLE_DIR) # check each vnf_bd file against the model print("Validating: {} VNF-BD files.".format(len(example_vnf_bds))) for ep in example_vnf_bds: parsed_vnfbd = check_against_model(os.path.join(EXAMPLE_DIR, ep)) print(pybindJSON.dumps(parsed_vnfbd)) self.assertIsNotNone(parsed_vnfbd)
def test_serialise_container(self): self.serialise_obj.two.string_test = "twenty-two" with open(os.path.join(os.path.dirname(__file__), "json", "container.json"), "r") as fp: self.assertEqual( json.loads(dumps(self.yang_helper.get("/two")[0])), json.load(fp), "Invalid output returned when serialising a container.", )
def convertToYangJsonStr(jsonObj): equipdata = jsonObj['equipmentDtlData'] for equipment in equipdata['equipmentLst']: for equipmentContainer in equipment['equipmentContainer']: for virtualNE in equipmentContainer['virtualNE']: for equipTag in virtualNE: if (type(virtualNE[equipTag]) == list): walkList(virtualNE[equipTag], equipTag) return (pybindJSON.dumps(op))
def Get_LispAddress_Noeid_JSON(eid_string, vni=None): """ Description: Returns lisp address dictionary Returns: python dictionary Params: eid_string: type of lisp address vni: virtual network id """ out_dump = pbJ.dumps(Get_LispAddress_Object(eid_string, vni), filter=True, mode="ietf") return Clean_JSON(out_dump)
def test_010_filtered_json_output(self): self.instance.bgp.global_.config.as_ = 2856 self.instance.bgp.global_.config.router_id = "192.0.2.1" n = self.instance.bgp.neighbors.neighbor.add("192.1.1.1") n.config.peer_as = 5400 n.config.description = "a fictional transit session" json_out = pbj.dumps(self.instance) testdata = open(this_dir + "/testdata/tc010.json").read() self.assertEqual(json_out, testdata)
def test_040_unfiltered_ietf_json_output(self): self.instance.bgp.global_.config.as_ = 2856 self.instance.bgp.global_.config.router_id = "192.0.2.1" n = self.instance.bgp.neighbors.neighbor.add("192.1.1.1") n.config.peer_as = 5400 n.config.description = "a fictional transit session" json_out = pbj.dumps(self.instance, filter=False, mode="ietf") testdata = open(this_dir + "/testdata/tc040.json").read() self.assertEqual(json.loads(json_out), json.loads(testdata))
def test_009_presence_serialise(self): self.instance.parent.child._set_present() expectedJ = """ { "parent": { "child": {} } }""" self.assertEqual(json.loads(pbJ.dumps(self.instance)), json.loads(expectedJ))
def calculate_diff(left, right, snoObject=sno()): left_dict = json.loads(pybindJSON.dumps(left)) right_dict = json.loads(pybindJSON.dumps(right)) right_side_recursion(left_dict, right_dict) left_after_right_recursion = snoObject pybindJSONDecoder.load_json(left_dict, None, None, left_after_right_recursion) left_xml = (pybindIETFXMLEncoder.serialise(left_after_right_recursion)) right_xml = (pybindIETFXMLEncoder.serialise(right)) root_left = etree.fromstring(left_xml) root_right = etree.fromstring(right_xml) left_side_recursion(root_left, root_right) return (etree.tostring(root_left).decode())
def update(self, snoRoot, vars): if self.name not in snoRoot.devices.device.keys(): raise KeyError(self.name + "not present in device list") else: DeviceObj = snoRoot.devices.device[self.name] DeviceDict = json.loads(pybindJSON.dumps(DeviceObj, filter=True)) DeviceDict.update(vars) pybindJSONDecoder.load_json(DeviceDict, None, None, DeviceObj) return snoRoot
def serialise(self, cls_instance): data_json = pbJ.dumps( cls_instance, indent=4, filter=True, skip_subtrees=[], select=False, mode="default", ) return data_json
def make_json(yml): model = ietf_interfaces() for intf, conf in yml['interface'].iteritems(): print("Instantiating model for {}".format(intf)) intf_model = model.interfaces.interface.add(intf) intf_model.description = conf['description'] ip_model = intf_model.ipv4.address.add(conf['address']['prefix']) ip_model.netmask = conf['address']['netmask'] print("Done") return pybindJSON.dumps(model, mode='ietf')
def test_example_demo_ports_dump_json(self): example_demo_ports = os.listdir(EXAMPLE_DIR) # check each demo_port file against the model print("Validating: {} demo-port files.".format( len(example_demo_ports))) for ep in example_demo_ports: parsed_demo_port = check_against_model( os.path.join(EXAMPLE_DIR, ep)) print(pybindJSON.dumps(parsed_demo_port)) self.assertIsNotNone(parsed_demo_port)
def test_030_filtered_ietf_json_output(self): self.instance.bgp.global_.config.as_ = 2856 self.instance.bgp.global_.config.router_id = "192.0.2.1" n = self.instance.bgp.neighbors.neighbor.add("192.1.1.1") n.config.peer_as = 5400 n.config.description = "a fictional transit session" json_out = pbj.dumps(self.instance, mode="ietf") with open("{}/testdata/tc030.json".format(this_dir)) as f: testdata = f.read() self.assertEqual(json.loads(json_out), json.loads(testdata))
def Get_MappingAuthkey_JSON(key_string="password", key_type=1): """ Description: Returns mapping auth key dictionary Returns: python dictionary Params: key_string: key string key_type: key type """ pbj_dump = pbJ.dumps(Get_MappingAuthkey_Object(key_string, key_type), filter=True, mode="default") out_dump = '{"mapping-authkey":' + pbj_dump + '}' return Clean_JSON(out_dump)
def Get_LispAddress_JSON(eid_string, vni=None): """ Description: Returns lisp address dictionary with eid wrapped Returns: python dictionary Params: eid_string: type of lisp address vni: virtual network id """ pbj_dump = pbJ.dumps(Get_LispAddress_Object(eid_string, vni), filter=True, mode="ietf") out_dump = '{"eid":' + pbj_dump + '}' return Clean_JSON(out_dump)
def test_040_unfiltered_ietf_json_output(self): self.instance.bgp.global_.config.as_ = 2856 self.instance.bgp.global_.config.router_id = "192.0.2.1" n = self.instance.bgp.neighbors.neighbor.add("192.1.1.1") n.config.peer_as = 5400 n.config.description = "a fictional transit session" json_out = pbj.dumps(self.instance, filter=False, mode="ietf") with open(os.path.join(os.path.dirname(__file__), "testdata", "tc040.json")) as fp: testdata = fp.read() self.assertEqual(json.loads(json_out), json.loads(testdata))
def SetContainer(ap, container, config_obj): """Sets a container config and verifies it based on config obj provided. This function works where setting and verifying the container does not require any special logic. It simply sets configuration at a container level and verifies all the configured leafs' state. All leafs are checked against schema. Args: ap: (object) chido_test.ApObject containing all AP attributes. container: (str) a supported container within the model. config_obj: (YANGBaseClass) OC config object matching the container. Raises: ConfigError: If the config leaf does not match config sent. """ path = _GetPathByContainer(ap, container) json_str = pybindJSON.dumps(config_obj, mode='ietf') SetConfig(ap, xpath=path, json_str=json_str) expected_config = pybindJSON.dumps(config_obj, mode='ietf') 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=config_obj) retrieved_config = pybindJSON.dumps(retrieved_config_obj, mode='ietf') if expected_config != retrieved_config: logging.info('Expected:\n%s', expected_config) logging.info('Retrieved:\n%s', retrieved_config) raise ConfigError('Container config does not match config sent') # Now verify the state. path = path.replace('/config', '/state') configured_keys = json.loads(expected_config).keys() # We replace the 'openconfig-*' prefix in JSON to get the actual leaf names. # TODO(xavier): Regex match prefix replace so it works for all models. leafs = [ l.replace('openconfig-access-points:', '').replace('openconfig-ap-manager:', '').replace('-', '_') for l in configured_keys ] _VerifyContainerState(ap, container, path, leafs, retrieved_config_obj)
def ExtractJson(oc_obj, leaf_str): # sometimes leaf can not be dumped, # so dump the parent for safe # set filter True to filter attributes which are not configured if hasattr(oc_obj, 'get'): tmp_json = json.loads(pybindJSON.dumps(oc_obj, filter=True)) else: tmp_json = json.loads(pybindJSON.dumps(oc_obj._parent, filter=True)) leaf_name = oc_obj.yang_name() if leaf_name in tmp_json: tmp_json = tmp_json[leaf_name] else: tmp_json = None if leaf_str and leaf_str in tmp_json: tmp_json = tmp_json[leaf_str] return tmp_json
def Get_MappingRecord_JSON(eid, locators, ttl=1440, authoritative=True, action='NoAction'): """ Description: Returns mapping record dictionary Returns: python dictionary Params: eid: lisp address object locators: list of locator record objects ttl: recordTtl authoritative: authoritative action: action """ pbj_dump = pbJ.dumps(Get_MappingRecord_Object(eid, locators, ttl, authoritative, action), filter=True, mode="ietf") out_dump = '{"mapping-record":' + pbj_dump + '}' return Clean_JSON(out_dump)
def test_full_serialise(self): self.serialise_obj.c1.l1.add(1) for signed in ["int", "uint"]: for size in [8, 16, 32, 64]: name = "%s%s" % (signed, size) setter = getattr(self.serialise_obj.c1.l1[1], "_set_%s" % name) setter(1) self.serialise_obj.c1.l1[1].restricted_integer = 6 self.serialise_obj.c1.l1[1].string = "bear" self.serialise_obj.c1.l1[1].restricted_string = "aardvark" self.serialise_obj.c1.l1[1].union = 16 self.serialise_obj.c1.l1[1].union_list.append(16) self.serialise_obj.c1.l1[1].union_list.append("chicken") self.serialise_obj.c1.t1.add(16) self.serialise_obj.c1.t1.add(32) self.serialise_obj.c1.l1[1].leafref = 16 self.serialise_obj.c1.l1[1].binary = bitarray("010101") self.serialise_obj.c1.l1[1].boolean = True self.serialise_obj.c1.l1[1].enumeration = "one" self.serialise_obj.c1.l1[1].identityref = "idone" self.serialise_obj.c1.l1[1].typedef_one = "test" self.serialise_obj.c1.l1[1].typedef_two = 8 self.serialise_obj.c1.l1[1].one_leaf = "hi" for i in range(1, 5): self.serialise_obj.c1.l1[1].ll.append(six.text_type(i)) self.serialise_obj.c1.l1[1].next_hop.append("DROP") self.serialise_obj.c1.l1[1].next_hop.append("192.0.2.1") self.serialise_obj.c1.l1[1].next_hop.append("fish") self.serialise_obj.c1.l1[1].typedef_decimal = Decimal("21.21") self.serialise_obj.c1.l1[1].range_decimal = Decimal("4.44443322") self.serialise_obj.c1.l1[1].typedef_decimalrange = Decimal("42.42") self.serialise_obj.c1.l1[1].decleaf = Decimal("42.4422") for i in range(1, 10): self.serialise_obj.c1.l2.add(i) pybind_json = json.loads(dumps(self.serialise_obj)) with open(os.path.join(os.path.dirname(__file__), "json", "expected-output.json"), 'r') as fp: external_json = json.load(fp) self.assertEqual(pybind_json, external_json, "JSON did not match expected output.")
def test_json_generation(self): json_dir = os.path.join(os.path.dirname(__file__), 'json') for file_name in os.listdir(json_dir): with self.subTest(json_file=file_name), open(os.path.join(json_dir, file_name), 'r') as file_handle: parameters = regex.sub( 'interfaces\_ph:(?P<pathhelper>[a-zA-Z]+)\-flt:(?P<filter>[a-zA-Z]+)\-m:(?P<mode>[a-zA-Z]+)\.json', '\g<pathhelper>||\g<filter>||\g<mode>', file_name ).split("||") path_helper, config_filter, mode = YANGBool(parameters[0]), YANGBool(parameters[1]), parameters[2] if path_helper: instance = self.ocbind.openconfig_interfaces(path_helper=self.yang_helper) else: instance = self.ocbind.openconfig_interfaces() instance.interfaces.interface.add("eth0") expected_json = json.load(file_handle) actual_json = json.loads(dumps(instance, filter=bool(config_filter), mode=mode)) self.assertEqual(expected_json, actual_json, "Generated JSON did not match expected object for %s" % file_name)
for i in [(-452.6729, False), (-444.44, True), (-443.22, False), (-330, True), (-222.21, False), (111.2, False), (111.1, True), (446.56, True), (555.55559282, False)]: passed = False try: q.container.dec64LeafWithRange = i[0] passed = True except ValueError, m: pass assert passed == i[1], \ "decimal64 leaf with range was not correctly set (%f -> %s != %s)" \ % (i[0], passed, i[1]) testdata = ''' { "container": { "d1": 12.34 } } ''' import pyangbind.lib.pybindJSON as pbj doc = pbj.loads(testdata, bindings, 'decimal') print pbj.dumps(doc) if not k: os.system("/bin/rm %s/bindings.py" % this_dir) os.system("/bin/rm %s/bindings.pyc" % this_dir) if __name__ == '__main__': main()
#!/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))
# Load an instance from an IETF-JSON string string_to_load = open(os.path.join('json', 'simple-instance-ietf.json'), 'r') string_to_load = string_to_load.read().replace('\n', '') loaded_ietf_obj_two = pbJ.loads_ietf(string_to_load, sbindings, "simple_serialise") pp.pprint(loaded_ietf_obj_two.get(filter=True)) # Load into an existing instance from pyangbind.lib.serialise import pybindJSONDecoder import json # Create a new instance existing_instance = sbindings.simple_serialise() existing_instance.a_list.add("entry-one") existing_instance.a_list.add("entry-two") fn = os.path.join("json", "simple-instance-additional.json") data_to_load = json.load(open(fn, 'r')) pybindJSONDecoder.load_json(data_to_load, None, None, obj=existing_instance) pp.pprint(existing_instance.a_list.keys()) pp.pprint(existing_instance.get(filter=True)) # Serialise objects to JSON print(pbJ.dumps(existing_instance, filter=True)) print(pbJ.dumps(existing_instance, filter=True, mode="ietf")) print(pbJ.dumps(existing_instance.a_list, select={'the-key': 'entry-one'})) print(pbJ.dumps(existing_instance.a_list, select={'the-key': 'entry-one'}, mode="ietf"))
lrecord_obj[loc_id].localLocator = flags % 10 lrecord_obj[loc_id].rlocProbed = (flags / 10) % 10 lrecord_obj[loc_id].routed = (flags / 100) % 10 return lrecord_obj def Get_LocatorRecord_JSON(rloc, weights='1/1/255/0', flags=001, loc_id="ISP1"): """ Description: Returns locator record dictionary Returns: python dictionary Params: rloc: eid_string for lisp address object weights: priority/weight/multicastPriority/multicastWeight flags: Three bit parameter in the sequence routed->rlocProbed->routed loc_id: id of locator record object """ pbj_dump = pbJ.dumps(Get_LocatorRecord_Object(rloc, weights, flags, loc_id), filter=True, mode="default") pbj_dict = json.loads(pbj_dump) pbj_dict[loc_id]['rloc'] = Get_LispAddress_Noeid_JSON(rloc) out_dump = '{"LocatorRecord":' + str(pbj_dict) + '}' return Clean_JSON(out_dump) def Get_MappingRecord_Object(eid, locators, ttl=1440, authoritative=True, action='NoAction'): """ Description: Returns mapping record object from pyangbind generated classes. Returns: mapping record object Params: eid: lisp address object locators: list of locator record objects ttl: recordTtl authoritative: authoritative action: action
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 += " --use-xpathhelper" cmd += " %s/%s.yang" % (this_dir, TESTNAME) os.system(cmd) from bindings import json_serialise from bitarray import bitarray from pyangbind.lib.xpathhelper import YANGPathHelper y = YANGPathHelper() js = json_serialise(path_helper=y) js.c1.l1.add(1) for s in ["int", "uint"]: for l in [8, 16, 32, 64]: name = "%s%s" % (s, l) x = getattr(js.c1.l1[1], "_set_%s" % name) x(1) js.c1.l1[1].restricted_integer = 6 js.c1.l1[1].string = "bear" js.c1.l1[1].restricted_string = "aardvark" js.c1.l1[1].union = 16 js.c1.l1[1].union_list.append(16) js.c1.l1[1].union_list.append("chicken") js.c1.t1.add(16) js.c1.t1.add(32) js.c1.l1[1].leafref = 16 js.c1.l1[1].binary = bitarray("010101") js.c1.l1[1].boolean = True js.c1.l1[1].enumeration = "one" js.c1.l1[1].identityref = "idone" js.c1.l1[1].typedef_one = "test" js.c1.l1[1].typedef_two = 8 js.c1.l1[1].one_leaf = "hi" for i in range(1, 5): js.c1.l1[1].ll.append(unicode(i)) js.c1.l1[1].next_hop.append("DROP") js.c1.l1[1].next_hop.append("192.0.2.1") js.c1.l1[1].next_hop.append("fish") js.c1.l1[1].typedef_decimal = Decimal("21.21") js.c1.l1[1].range_decimal = Decimal("4.44443322") js.c1.l1[1].typedef_decimalrange = Decimal("42.42") js.c1.l1[1].decleaf = Decimal("42.4422") for i in range(1, 10): js.c1.l2.add(i) pybind_json = json.loads(dumps(js)) external_json = json.load(open(os.path.join(this_dir, "json", "expected-output.json"), 'r')) assert pybind_json == external_json, "JSON did not match the expected output" yph = YANGPathHelper() new_obj = json_serialise(path_helper=yph) new_obj.two.string_test = "twenty-two" assert json.loads(dumps(yph.get("/two")[0])) == \ json.load(open(os.path.join(this_dir, "json", "container.json"), 'r')), \ "Invalid output returned when serialising a container" if not k: os.system("/bin/rm %s/bindings.py" % this_dir) os.system("/bin/rm %s/bindings.pyc" % this_dir)
pp.pprint(loaded_ietf_obj.get(filter=True)) # Load an instance from an IETF-JSON string string_to_load = open(os.path.join("json", "simple-instance-ietf.json"), "r") string_to_load = string_to_load.read().replace("\n", "") loaded_ietf_obj_two = pbJ.loads_ietf(string_to_load, sbindings, "simple_serialise") pp.pprint(loaded_ietf_obj_two.get(filter=True)) # Load into an existing instance from pyangbind.lib.serialise import pybindJSONDecoder import json # Create a new instance existing_instance = sbindings.simple_serialise() existing_instance.a_list.add("entry-one") existing_instance.a_list.add("entry-two") fn = os.path.join("json", "simple-instance-additional.json") data_to_load = json.load(open(fn, "r")) pybindJSONDecoder.load_json(data_to_load, None, None, obj=existing_instance) pp.pprint(existing_instance.a_list.keys()) pp.pprint(existing_instance.get(filter=True)) # Serialise objects to JSON print(pbJ.dumps(existing_instance, filter=True)) print(pbJ.dumps(existing_instance, filter=True, mode="ietf")) print(pbJ.dumps(existing_instance.a_list, select={"the-key": "entry-one"})) print(pbJ.dumps(existing_instance.a_list, select={"the-key": "entry-one"}, mode="ietf"))
def test_roundtrip_simple(self): self.rt_obj.a.idref = "VALUE_TWO" j = pbJ.dumps(self.rt_obj) pbS.pybindJSONDecoder.load_json(json.loads(j), None, None, obj=self.rt_obj)
for nhop in [(0, "192.168.0.1"), (1, "10.0.0.1")]: nh = rt.next_hops.next_hop.add(nhop[0]) nh.config.next_hop = nhop[1] # Iterate through the next-hops added for index, nh in rt.next_hops.next_hop.iteritems(): print("%s: %s" % (index, nh.config.next_hop)) # Try and set an invalid tag type try: rt.config.set_tag = "INVALID-TAG" except ValueError as m: print("Cannot set tag: %s" % m) # Dump the entire instance as JSON in PyangBind format print(pybindJSON.dumps(oclr)) # 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 \
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/robshakir/" + \ "yang/master/standard/ietf/RFC/" FETCH_FILES = [ (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 += " --use-xpathhelper " cmd += files_str os.system(cmd) from ocbind import openconfig_interfaces for fn in os.listdir(os.path.join(this_dir, "json")): jobj = json.load(open(os.path.join(this_dir, "json", fn), 'r')) parameters = re.sub('interfaces\_ph:(?P<pathhelper>[a-zA-Z]+)\-flt:(?P<filter>[a-zA-Z]+)\-m:(?P<mode>[a-zA-Z]+)\.json', '\g<pathhelper>||\g<filter>||\g<mode>', fn).split("||") path_helper,filter,mode = YANGBool(parameters[0]), YANGBool(parameters[1]), parameters[2] if path_helper: ph = YANGPathHelper() i = openconfig_interfaces(path_helper=ph) else: i = openconfig_interfaces() i.interfaces.interface.add("eth0") jstr = json.loads(dumps(i, filter=bool(filter), mode=mode)) assert jstr == jobj, "Generated JSON did not match expected object for %s" % fn \ + " %s != %s" % (jstr, jobj) passed = True try: jstr = json.loads(json.dumps(pybindIETFJSONEncoder.generate_element(i), cls=pybindIETFJSONEncoder)) except Exception as e: passed = False assert passed, "Serialisation test for object with pybindIETFJSONEncoder threw an error" jstr = json.loads(json.dumps(i, cls=pybindJSONEncoder)) print jstr 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)