def test_011_presence_deserialise(self): inputJ = """ { "presence:parent": { "child": {} } }""" x = pbJ.loads_ietf(inputJ, bindings, "presence") self.assertIs(x.parent.child._present(), True)
def check_against_model(yaml_path): """ Open a given YAML file and tries to load it into model that was generated by pyangbind. Fails if the YAML file is not compatible to the model. """ with open(yaml_path, "r") as f: vnf_pp_data = yaml.load(f) vnf_pp_model = pybindJSON.loads_ietf(vnf_pp_data, vnf_pp, MODEL_NAME) # print(ped_model.ped.name) # example how to use model return vnf_pp_model
def add_route(current_routes, route, nh, name): try: model = pybindJSON.loads_ietf(current_routes, binding, "cisco_route_static") route = model.route.ip_route_interface_forwarding_list.add(route) nexthop = route.fwd_list.add(nh) nexthop.name = name json_data = pybindJSON.dumps(model, mode='ietf') write_file('{}.json'.format(NEW_FN), json_data) except Exception: print(str(sys.exc_info()[0])) return -1 return json_data
def modify_route(current_routes, route, nh_old, nh_new): try: model = pybindJSON.loads_ietf(current_routes, binding, "cisco_route_static") route = model.route.ip_route_interface_forwarding_list[route] route.fwd_list.delete(nh_old) route.fwd_list.add(nh_new) json_data = pybindJSON.dumps(model, mode='ietf') write_file('{}.json'.format(NEW_FN), json_data) except Exception: print(str(sys.exc_info()[0])) return -1 return json_data
def check_against_model(yaml_path): """ Open a given YAML file and tries to load it into model that was generated by pyangbind. Fails if the YAML file is not compatible to the model. """ with open(yaml_path, "r") as f: demo_port_data = yaml.load(f, Loader=yaml.SafeLoader) demo_port_model = pybindJSON.loads_ietf(demo_port_data, demo_port, MODEL_NAME) return demo_port_model
def validate(self, data, model, model_name): valid = False try: loaded_model = pbJ.loads_ietf(data, model, model_name) # valid = True # except AttributeError as e: # valid = False # logger.debug(f"Invalid model - Exception: {e}") except Exception as e: valid = False logger.debug(f"Invalid model - Exception: {e}") else: valid = True logger.debug(f"Validated model {loaded_model}") finally: return valid
def load_serialize_model(): """ Open a given YAML file and tries to load it into model that was generated by pyangbind. Fails if the YAML file is not compatible to the model. """ example_demo_ports = os.listdir(EXAMPLE_DIR) ep = example_demo_ports[0] yaml_path = os.path.join(EXAMPLE_DIR, ep) with open(yaml_path, "r") as f: demo_port_data = yaml.load(f, Loader=yaml.FullLoader) demo_port_model = pybindJSON.loads_ietf(demo_port_data, demo_port, MODEL_NAME) # Modifying the source model - adds a new port and sets its config port = demo_port_model.ports.port.add(2) port.config.speed = "SPEED_10GB" print(pybindJSON.dumps(demo_port_model))
#!/usr/bin/env python import requests import binding import pyangbind.lib.pybindJSON as pybindJSON import json headers = {'Accept': 'application/yang-data+json'} url = 'http://*****:*****@localhost:8080/restconf/data/devices/device=rusim0/live-status/xran-usermgmt:xran-users' # example getting live-status of RU resp = requests.get(url, headers=headers) ietf_json = resp.content string_to_load = ietf_json.replace('\n', '') live_users = pybindJSON.loads_ietf(string_to_load, binding, "xran_usermgmt") json_users = json.loads(pybindJSON.dumps(live_users)) json_users = json_users['xran-users']['user'] users = [] i = 0 for k in json_users.keys(): users.append(str(k)) for user in users: print "user", i, live_users.xran_users.user[user].get() i += 1
string_to_load = open(os.path.join('json', 'simple-instance.json'), 'r') string_to_load = string_to_load.read().replace('\n', '') loaded_object_two = pbJ.loads(string_to_load, sbindings, "simple_serialise") pp.pprint(loaded_object_two.get(filter=True)) # Load an instance from an IETF-JSON file loaded_ietf_obj = \ pbJ.load_ietf(os.path.join("json", "simple-instance-ietf.json"), sbindings, "simple_serialise") 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)
# Load an instance from a corresponding string using the native JSON format string_to_load = open(os.path.join("json", "simple-instance.json"), "r") string_to_load = string_to_load.read().replace("\n", "") loaded_object_two = pbJ.loads(string_to_load, sbindings, "simple_serialise") pp.pprint(loaded_object_two.get(filter=True)) # Load an instance from an IETF-JSON file loaded_ietf_obj = pbJ.load_ietf(os.path.join("json", "simple-instance-ietf.json"), sbindings, "simple_serialise") 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)