def generate_set_message(operations, request_id=0): try: msg_reqid = int(request_id) except ValueError: raise PyNMSGRPCClientException("request_id must be an integer") setreq = pynms_rpc_pb2.SetRequest(request_id=msg_reqid, encoding=pynms_rpc_pb2.JSON_IETF) req = 0 for operation in operations: if not isinstance(operation, PyNMSConfigOperation): raise PyNMSGRPCClientException("operations must be PyNMSConfigOperation instances") setop = setreq.operation.add() if operation.method == 'UPDATE_CONFIG': setop.opcode = pynms_rpc_pb2.UPDATE_CONFIG elif operation.method == 'REPLACE_CONFIG': setop.opcode = pynms_rpc_pb2.REPLACE_CONFIG elif operation.method == 'DELETE_CONFIG': setop.opcode = pynms_rpc_pb2.DELETE_CONFIG else: # default to UPDATE_CONFIG setop.opcode = pynms_rpc_pb2.UPDATE_CONFIG setop.path = operation.path # always use IETF JSON for now if hasattr(operation.content, "_pyangbind_elements"): tree = pybindIETFJSONEncoder.generate_element(operation.content, flt=True) else: tree = operation.content encoder = pybindIETFJSONEncoder setop.value = json.dumps(tree, cls=encoder) return setreq
def test_serialise_single_object(self): self.serialise_obj.c1.l1.add(1) self.serialise_obj.c1.l1[1].string = "bear" actual_json = json.loads(json.dumps( pybindIETFJSONEncoder.generate_element(self.serialise_obj.c1.l1[1].string, flt=True), cls=pybindIETFJSONEncoder, indent=4 )) self.assertEqual(actual_json, "bear", "Single element JSON did not match the expected output")
def test_pybind_ietf_json_encoder_serialisation_without_path_helper(self): instance = self.ocbind.openconfig_interfaces() instance.interfaces.interface.add("eth0") passed = True try: json.loads(json.dumps(pybindIETFJSONEncoder.generate_element(instance), cls=pybindIETFJSONEncoder)) except Exception: passed = False self.assertTrue(passed, "Serialisation test for object with pybindIETFJSONEncoder threw an error")
def test_serialise_single_object(self): self.serialise_obj.c1.l1.add(1) self.serialise_obj.c1.l1[1].string = "bear" actual_json = json.loads( json.dumps(pybindIETFJSONEncoder.generate_element( self.serialise_obj.c1.l1[1].string, flt=True), cls=pybindIETFJSONEncoder, indent=4)) self.assertEqual( actual_json, "bear", "Single element JSON did not match the expected output")
def test_serialise_full_container(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.l1[1].empty = True 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].remote_identityref = "stilton" 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" self.serialise_obj.c1.l1[1].uint64type = 2**22 self.serialise_obj.c1.l1[1].typedef_decimal = 32.29 self.serialise_obj.c1.l1[1].typedef_decimalrange = Decimal("33.44") self.serialise_obj.c1.l1[1].range_decimal = Decimal("4.44443322") 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("TEST") self.serialise_obj.augtarget.augleaf = "teststring" 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( json.dumps(pybindIETFJSONEncoder.generate_element( self.serialise_obj, flt=True), cls=pybindIETFJSONEncoder, indent=4)) with open(os.path.join(os.path.dirname(__file__), "json", "obj.json"), 'r') as fp: external_json = json.load(fp) self.assertEqual(pybind_json, external_json, "JSON did not match the expected output.")
def test_serialise_full_container(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.l1[1].empty = True 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].remote_identityref = "stilton" 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" self.serialise_obj.c1.l1[1].uint64type = 2 ** 22 self.serialise_obj.c1.l1[1].typedef_decimal = 32.29 self.serialise_obj.c1.l1[1].typedef_decimalrange = Decimal("33.44") self.serialise_obj.c1.l1[1].range_decimal = Decimal("4.44443322") 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("TEST") self.serialise_obj.augtarget.augleaf = "teststring" 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( json.dumps( pybindIETFJSONEncoder.generate_element(self.serialise_obj, flt=True), cls=pybindIETFJSONEncoder, indent=4, ) ) with open(os.path.join(os.path.dirname(__file__), "json", "obj.json"), "r") as fp: external_json = json.load(fp) self.assertEqual(pybind_json, external_json, "JSON did not match the expected output.")
def get_encoded_object_set(path_helper, path, encoding, logger, select_filter=False): # TODO: filtering generate_element = False if encoding == 'JSON_IETF': encoder = pybindIETFJSONEncoder generate_element = True elif encoding == 'JSON_PYBIND': encoder = pybindJSONEncoder logger.debug("Getting objects at %s..." % path) objects = path_helper.get(path) logger.debug("Objects returned were: %s" % objects) logger.debug("Generate element was %s" % generate_element) if generate_element is True: try: tmp_obj = [pybindIETFJSONEncoder.generate_element(i, flt=True) for i in objects] except Exception as e: logger.debug("Received an exception whilst generating elements: %s: %s" % (type(e), str(e))) raise Exception(str(e)) objects = tmp_obj else: tmp_obj = [i.get(filter=True) for i in objects] objects = tmp_obj logger.debug("Objects are %s" % objects) logger.debug("Objects length is %d" % len(objects)) if len(objects) > 1: try: ret_obj = [json.dumps(i, cls=encoder) for i in objects] except Exception as e: logger.debug("Received an exception whilst encoding(multi-obj): %s" % str(e)) raise Exception(str(e)) elif len(objects) == 0: ret_obj = "" else: try: ret_obj = json.dumps(objects[0], cls=encoder) except Exception as e: logger.debug("Received an exception whilst encoding (one object): %s" % str(e)) raise Exception(str(e)) logger.debug("returned object is %s" % ret_obj) return ret_obj
def generate_set_message(operations, request_id=0): try: msg_reqid = int(request_id) except ValueError: raise PyNMSGRPCClientException("request_id must be an integer") setreq = pynms_rpc_pb2.SetRequest(request_id=msg_reqid, encoding=pynms_rpc_pb2.JSON_IETF) req = 0 for operation in operations: if not isinstance(operation, PyNMSConfigOperation): raise PyNMSGRPCClientException( "operations must be PyNMSConfigOperation instances") setop = setreq.operation.add() if operation.method == 'UPDATE_CONFIG': setop.opcode = pynms_rpc_pb2.UPDATE_CONFIG elif operation.method == 'REPLACE_CONFIG': setop.opcode = pynms_rpc_pb2.REPLACE_CONFIG elif operation.method == 'DELETE_CONFIG': setop.opcode = pynms_rpc_pb2.DELETE_CONFIG else: # default to UPDATE_CONFIG setop.opcode = pynms_rpc_pb2.UPDATE_CONFIG setop.path = operation.path # always use IETF JSON for now if hasattr(operation.content, "_pyangbind_elements"): tree = pybindIETFJSONEncoder.generate_element( operation.content, flt=True) else: tree = operation.content encoder = pybindIETFJSONEncoder setop.value = json.dumps(tree, cls=encoder) return setreq
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) cmd += " %s/augment.yang" % this_dir os.system(cmd) from bindings import ietf_json_serialise from bitarray import bitarray from pyangbind.lib.xpathhelper import YANGPathHelper y = YANGPathHelper() js = ietf_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.l1[1].empty = True 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].remote_identityref = "stilton" js.c1.l1[1].typedef_one = "test" js.c1.l1[1].typedef_two = 8 js.c1.l1[1].one_leaf = "hi" js.c1.l1[1].uint64type = 2**22 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("TEST") js.augtarget.augleaf = "teststring" js.c1.l1[1].decleaf = Decimal('42.4422') for i in range(1, 10): js.c1.l2.add(i) pybind_json = json.loads(json.dumps( pybindIETFJSONEncoder.generate_element(js, flt=True), cls=pybindIETFJSONEncoder, indent=4)) external_json = json.load( open(os.path.join(this_dir, "json", "obj.json"), 'r')) # from pprint import PrettyPrinter # pp = PrettyPrinter(indent=4) # pp.pprint(pybind_json) assert pybind_json == external_json, "JSON did not match the expected output" if not k: os.system("/bin/rm %s/bindings.py" % this_dir) os.system("/bin/rm %s/bindings.pyc" % this_dir)
def dumps(obj, indent=4, filter=True, skip_subtrees=[], select=False, mode="default"): def lookup_subdict(dictionary, key): if not isinstance(key, list): raise AttributeError('keys should be a list') unresolved_dict = {} for k, v in dictionary.iteritems(): if ":" in k: k = k.split(":")[1] unresolved_dict[k] = v if not key[0] in unresolved_dict: raise KeyError("requested non-existent key (%s)" % key[0]) if len(key) == 1: return unresolved_dict[key[0]] current = key.pop(0) return lookup_subdict(dictionary[current], key) if not isinstance(skip_subtrees, list): raise AttributeError('the subtrees to be skipped should be a list') if mode == 'ietf': tree = pybindIETFJSONEncoder.generate_element(obj, flt=filter) else: tree = obj.get(filter=filter) for p in skip_subtrees: pp = p.split("/")[1:] # Iterate through the skip path and the object's own path to determine # whether they match, then skip the relevant subtrees. match = True trimmed_path = copy.deepcopy(pp) for i, j in zip(obj._path(), pp): # paths may have attributes in them, but the skip dictionary does # not, so we ensure that the object's absolute path is attribute # free, if "[" in i: i = i.split("[")[0] if not i == j: match = False break trimmed_path.pop(0) if match and len(trimmed_path): tree = remove_path(tree, trimmed_path) if select: key_del = [] for t in tree: keep = True for k, v in select.iteritems(): if mode == 'default' or isinstance(tree, dict): if keep and not \ unicode(lookup_subdict(tree[t], k.split("."))) == unicode(v): keep = False else: # handle ietf case where we have a list and might have namespaces if keep and not \ unicode(lookup_subdict(t, k.split("."))) == unicode(v): keep = False if not keep: key_del.append(t) if mode == 'default' or isinstance(tree, dict): for k in key_del: if mode == 'default': del tree[k] else: for i in key_del: tree.remove(i) if mode == "ietf": cls = pybindIETFJSONEncoder else: cls = pybindJSONEncoder return json.dumps(tree, cls=cls, indent=indent)
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) cmd += " %s/augment.yang" % this_dir os.system(cmd) from bindings import ietf_json_serialise from bitarray import bitarray from pyangbind.lib.xpathhelper import YANGPathHelper y = YANGPathHelper() js = ietf_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.l1[1].empty = True 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].remote_identityref = "stilton" js.c1.l1[1].typedef_one = "test" js.c1.l1[1].typedef_two = 8 js.c1.l1[1].one_leaf = "hi" js.c1.l1[1].uint64type = 2**22 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("TEST") js.augtarget.augleaf = "teststring" js.c1.l1[1].decleaf = Decimal('42.4422') for i in range(1, 10): js.c1.l2.add(i) pybind_json = json.loads( json.dumps(pybindIETFJSONEncoder.generate_element(js, flt=True), cls=pybindIETFJSONEncoder, indent=4)) external_json = json.load( open(os.path.join(this_dir, "json", "obj.json"), 'r')) # from pprint import PrettyPrinter # pp = PrettyPrinter(indent=4) # pp.pprint(pybind_json) assert pybind_json == external_json, "JSON did not match the expected output" if not k: os.system("/bin/rm %s/bindings.py" % this_dir) os.system("/bin/rm %s/bindings.pyc" % this_dir)
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)
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 + "types/openconfig-yang-types.yang", "include"), (OC + "types/openconfig-inet-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)) sys.stdout.flush() 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)) 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)
def dumps(obj, indent=4, filter=True, skip_subtrees=[], select=False, mode="default", with_defaults=None): def lookup_subdict(dictionary, key): if not isinstance(key, list): raise AttributeError("keys should be a list") unresolved_dict = {} for k, v in six.iteritems(dictionary): if ":" in k: k = k.split(":")[1] unresolved_dict[k] = v if not key[0] in unresolved_dict: raise KeyError("requested non-existent key (%s)" % key[0]) if len(key) == 1: return unresolved_dict[key[0]] current = key.pop(0) return lookup_subdict(dictionary[current], key) if not isinstance(skip_subtrees, list): raise AttributeError("the subtrees to be skipped should be a list") if mode == "ietf": tree = pybindIETFJSONEncoder.generate_element(obj, flt=filter, with_defaults=with_defaults) else: tree = obj.get(filter=filter) for p in skip_subtrees: pp = p.split("/")[1:] # Iterate through the skip path and the object's own path to determine # whether they match, then skip the relevant subtrees. match = True trimmed_path = copy.deepcopy(pp) for i, j in zip(obj._path(), pp): # paths may have attributes in them, but the skip dictionary does # not, so we ensure that the object's absolute path is attribute # free, if "[" in i: i = i.split("[")[0] if not i == j: match = False break trimmed_path.pop(0) if match and len(trimmed_path): tree = remove_path(tree, trimmed_path) if select: key_del = [] for t in tree: keep = True for k, v in six.iteritems(select): v = six.text_type(v) if mode == "default" or isinstance(tree, dict): if keep and not six.text_type(lookup_subdict(tree[t], k.split("."))) == v: keep = False else: # handle ietf case where we have a list and might have namespaces if keep and not six.text_type(lookup_subdict(t, k.split("."))) == v: keep = False if not keep: key_del.append(t) if mode == "default" or isinstance(tree, dict): for k in key_del: if mode == "default": del tree[k] else: for i in key_del: tree.remove(i) if mode == "ietf": cls = pybindIETFJSONEncoder else: cls = pybindJSONEncoder return json.dumps(tree, cls=cls, indent=indent)
def get_encoded_object_set(path_helper, path, encoding, logger, select_filter=False): # TODO: filtering generate_element = False if encoding == 'JSON_IETF': encoder = pybindIETFJSONEncoder generate_element = True elif encoding == 'JSON_PYBIND': encoder = pybindJSONEncoder logger.debug("Getting objects at %s..." % path) objects = path_helper.get(path) logger.debug("Objects returned were: %s" % objects) logger.debug("Generate element was %s" % generate_element) if generate_element is True: try: tmp_obj = [ pybindIETFJSONEncoder.generate_element(i, flt=True) for i in objects ] except Exception as e: logger.debug( "Received an exception whilst generating elements: %s: %s" % (type(e), str(e))) raise Exception(str(e)) objects = tmp_obj else: tmp_obj = [i.get(filter=True) for i in objects] objects = tmp_obj logger.debug("Objects are %s" % objects) logger.debug("Objects length is %d" % len(objects)) if len(objects) > 1: try: ret_obj = [json.dumps(i, cls=encoder) for i in objects] except Exception as e: logger.debug( "Received an exception whilst encoding(multi-obj): %s" % str(e)) raise Exception(str(e)) elif len(objects) == 0: ret_obj = "" else: try: ret_obj = json.dumps(objects[0], cls=encoder) except Exception as e: logger.debug( "Received an exception whilst encoding (one object): %s" % str(e)) raise Exception(str(e)) logger.debug("returned object is %s" % ret_obj) return ret_obj