def test_gi_keyspec_string_method(self): #create a keyspec that points to a leaf node xpath = "D,/rw-base:colony[rw-base:name=\'abc\']/rw-fpath:bundle-ether[rw-fpath:name=\'xyz\']/rw-base:mtu" schema = RwFpathDYang.get_schema() keyspec = RwKeyspec.path_from_xpath(schema, xpath, RwKeyspec.RwXpathType.KEYSPEC) self.assertFalse(keyspec) xpath = "D,/rw-base:colony[rw-base:name=\'abc\']/rw-fpath:bundle-ether[rw-fpath:name=\'xyz\']/rw-fpath:mtu" keyspec = RwKeyspec.path_from_xpath(schema, xpath, RwKeyspec.RwXpathType.KEYSPEC) self.assertTrue(keyspec) v_create_string = keyspec.create_string(schema) v_create_string_no_schema = keyspec.create_string() print("create_string = %s" % (v_create_string,)) print("create_string_no_schema = %s" % (v_create_string_no_schema,)) self.assertEqual(v_create_string, xpath)
def rpc(self, input_obj, module): if self.session.ncclient_manager is None: yield from self.session.connect() request_xml = input_obj.to_xml_v2(self.model) netconf_response = yield from self.session.ncclient_manager.dispatch( etree.fromstring(request_xml)) self._log.info("netconf rpc response: %s", netconf_response.xml) output_obj = None if netconf_response.ok is True: in_desc = input_obj.retrieve_descriptor() rpc_output_xpath = 'O,/{}'.format(in_desc.xml_element_name()) out_desc = RwKeyspec.get_pbcmd_from_xpath(rpc_output_xpath, self.schema) (module_name, obj_type) = out_desc.get_gi_typename().split(".", 1) create_object = getattr(module, obj_type) output_obj = create_object() output_obj.from_xml_v2(self.model, Proxy._rpc_fix_root(xml=Proxy._xml_strip_rpc_reply(netconf_response.xml), rpc_name=out_desc.xml_element_name(), prefix=out_desc.xml_prefix(), namespace=out_desc.xml_ns())) return output_obj
def rpc(self, input_obj, module): if self.session.ncclient_manager is None: yield from self.session.connect() request_xml = input_obj.to_xml_v2(self.model) netconf_response = yield from self.session.ncclient_manager.dispatch( etree.fromstring(request_xml)) self._log.info("netconf rpc response: %s", netconf_response.xml) output_obj = None if netconf_response.ok is True: in_desc = input_obj.retrieve_descriptor() rpc_output_xpath = 'O,/{}'.format(in_desc.xml_element_name()) out_desc = RwKeyspec.get_pbcmd_from_xpath(rpc_output_xpath, self.schema) (module_name, obj_type) = out_desc.get_gi_typename().split(".", 1) create_object = getattr(module, obj_type) output_obj = create_object() output_obj.from_xml_v2( self.model, Proxy._rpc_fix_root(xml=Proxy._xml_strip_rpc_reply( netconf_response.xml), rpc_name=out_desc.xml_element_name(), prefix=out_desc.xml_prefix(), namespace=out_desc.xml_ns())) return output_obj
def test_gi_xpath_for_leaf(self): # some augment testcases for leaf yang nodes xpath = "D,/taugb1:b1_c/tauga1:a1_f1" schema = TestAugmentA1Yang.get_schema() keyspec = RwKeyspec.path_from_xpath(schema, xpath, RwKeyspec.RwXpathType.KEYSPEC) self.assertTrue(keyspec) v_create_string = keyspec.create_string(schema) self.assertEqual(v_create_string, xpath) xpath = "D,/taugb1:b1_c/taugb1:b1_c1/taugb1:b1_f1" keyspec = RwKeyspec.path_from_xpath(schema, xpath, RwKeyspec.RwXpathType.KEYSPEC) self.assertTrue(keyspec) v_create_string = keyspec.create_string(schema) self.assertEqual(v_create_string, xpath) xpath = "D,/taugb1:b1_c/taugb1:b1_c1/tauga1:a1_c1/tauga1:a1_f1" keyspec = RwKeyspec.path_from_xpath(schema, xpath, RwKeyspec.RwXpathType.KEYSPEC) self.assertTrue(keyspec) v_create_string = keyspec.create_string(schema) self.assertEqual(v_create_string, xpath)
def test_keyspec_xpath(self): xpath = "/ps:person/ps:phone[ps:number=\'1234\']" schema = RwpersonDbYang.get_schema() keyspec = RwKeyspec.path_from_xpath(schema, xpath, RwKeyspec.RwXpathType.KEYSPEC) self.assertTrue(keyspec) xpath_r = keyspec.to_xpath(schema) self.assertTrue(xpath_r) self.assertEqual(xpath_r, xpath) print(xpath_r)
def test_gi_get_pbcmd_from_xpath(self): xpath = "D,/rw-base:colony[rw-base:name='trafgen']" schema = RwFpathDYang.get_schema() pbcmd = RwKeyspec.get_pbcmd_from_xpath(xpath, schema) self.assertTrue(pbcmd) self.assertEqual(pbcmd, RwFpathDYang.ConfigColony().retrieve_descriptor()) xpath = "/rw-base:colony[rw-base:name='trafgen']/rw-fpath:bundle-ether[rw-fpath:name='bundle1']" pbcmd = RwKeyspec.get_pbcmd_from_xpath(xpath, schema) self.assertTrue(pbcmd) self.assertEqual(pbcmd, RwFpathDYang.ConfigColony_BundleEther().retrieve_descriptor()) #container xpath = "/rw-base:colony[rw-base:name='trafgen']/rw-fpath:bundle-ether[rw-fpath:name='bundle1']/rw-fpath:lacp" pbcmd = RwKeyspec.get_pbcmd_from_xpath(xpath, schema) self.assertTrue(pbcmd) self.assertEqual(pbcmd, RwFpathDYang.ConfigColony_BundleEther_Lacp().retrieve_descriptor()) #leaf, should give error xpath = "/rw-base:colony[rw-base:name='trafgen']/rw-fpath:bundle-ether[rw-fpath:name='bundle1']/rw-fpath:open" pbcmd = RwKeyspec.get_pbcmd_from_xpath(xpath, schema) self.assertFalse(pbcmd);
def test_conversion_url_to_dts_xpath_2(self): self.maxDiff = None url = "/api/running/rwrestconf-configuration/" # trailing / on purpose expected = "" schema = load_multiple_schema_root(["rw-restconf"]) actual = create_dts_xpath_from_url(url, schema) value = RwKeyspec.path_from_xpath(RwRestconfYang.get_schema(), actual, RwKeyspec.RwXpathType.KEYSPEC, None)
def test_conversion_url_to_dts_xpath(self): self.maxDiff = None url = "/api/operational/car/toyota/models" expected = "" schema = load_multiple_schema_root(["vehicle-a", "vehicle-augment-a"]) actual = create_dts_xpath_from_url(url, schema) xpath = "D,/vehicle-a:car[vehicle-a:key_name = 'toyota']" value = RwKeyspec.path_from_xpath(VehicleAYang.get_schema(), actual, RwKeyspec.RwXpathType.KEYSPEC, None)
def test_gi_schema_context_methods(self): #create a keyspec that points to a leaf node xpath = "/rw-base:colony[rw-base:name=\'trafgen\']/rw-fpath:bundle-ether[rw-fpath:name=\'bundle1\']/rw-fpath:mtu" schema = RwFpathDYang.get_schema() keyspec = RwKeyspec.path_from_xpath(schema, xpath, RwKeyspec.RwXpathType.KEYSPEC) self.assertTrue(keyspec) gi_desc = RwFpathDYang.ConfigColony_BundleEther.schema() self.assertTrue(gi_desc); lname = gi_desc.keyspec_leaf(keyspec) self.assertTrue(lname) print("Leaf " + lname) self.assertEqual(lname, "mtu") #Error case not a leaf keyspec xpath = "/rw-base:colony[rw-base:name=\'trafgen\']/rw-fpath:bundle-ether[rw-fpath:name=\'bundle1\']" keyspec = RwKeyspec.path_from_xpath(schema, xpath, RwKeyspec.RwXpathType.KEYSPEC) self.assertTrue(keyspec) lname = gi_desc.keyspec_leaf(keyspec) self.assertFalse(lname); #Error wrong schema gi_desc = RwFpathDYang.ConfigColony.schema() lname = gi_desc.keyspec_leaf(keyspec) self.assertFalse(lname)
def get_protobuf_type(xpath, protobuf_schema): ''' Given the protobuf schema, return the python type corresponding to the given xpath. ''' desc = RwKeyspec.get_pbcmd_from_xpath(xpath, protobuf_schema) module_name, obj_type = desc.get_gi_typename().split(".", 1) try: gi_module = getattr(gi.repository, module_name) except AttributeError: # first time we've tried to construct something from this module, so load the python overrides gi_module_name = "gi.repository.%s" % module_name importlib.import_module(gi_module_name) gi_module = getattr(gi.repository, module_name) protobuf_type = getattr(gi_module, obj_type) return protobuf_type