def setUpClass(self): self.ncc = NetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) self.netconf_service = NetconfService()
def test_netconf_delete_container(self): # Build loopback configuration address = Native.Interface.Loopback.Ipv4.Address() address.ip = "2.2.2.2" address.netmask = "255.255.255.255" loopback = Native.Interface.Loopback() loopback.name = 2222 loopback.ipv4.address.append(address) native = Native() native.interface.loopback.append(loopback) ns = NetconfService() result = ns.edit_config(self.ncc, Datastore.candidate, native) self.assertTrue(result) # Read ipv4 configuration native = Native() loopback = Native.Interface.Loopback() loopback.name = 2222 native.interface.loopback.append(loopback) ipv4_config = ns.get_config(self.ncc, Datastore.candidate, loopback.ipv4) self.assertIsNotNone(ipv4_config) self.assertEqual(ipv4_config.address['2.2.2.2'].netmask, "255.255.255.255") # Delete configuration result = ns.discard_changes(self.ncc) self.assertEqual(result, True)
def test_netconf_get_running_config(self): self.logger.setLevel(logging.ERROR) ns = NetconfService() config = ns.get_config(self.ncc) self.assertNotEqual(len(config), 0) print("\n==== Retrieved entities:") for entity in config: print(entity.path())
def setUpClass(self): if SanityNetconf.PROVIDER_TYPE == "native": self.ncc = NativeNetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) else: self.ncc = NetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) self.netconf_service = NetconfService()
def setUpClass(self): self.ydk_client = NativeNetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) self.netconf_service = NetconfService()
def connect_and_provision(self, device): from ydk.providers import NetconfServiceProvider from ydk.services import NetconfService, Datastore provider = NetconfServiceProvider(address=device['hostname'], port=device['port'], username=device['username'], password=device['password'], protocol='ssh') netconf = NetconfService() if "configuration" in self.root_yang_model_names: netconf.lock(provider, Datastore.candidate) result = netconf.edit_config(provider, Datastore.candidate, self.bindings) netconf.commit(provider) netconf.unlock(provider, Datastore.candidate) return result
def setUpClass(cls): cls.ncc = NetconfServiceProvider( cls.hostname, cls.username, cls.password, cls.port, cls.protocol, not cls.on_demand, cls.common_cache, cls.timeout) cls.netconf_service = NetconfService()
def run(args): ''' This function uses PET Stack (via YDK) and configures the device ''' # setup the protocol and transport ncapi = NetconfService() ncsession = NetconfServiceProvider(address="198.18.1.11", port=830, username="******", password="******", protocol="ssh") # setup the encoded content data = prepare_config() # Perform an action - send to the device rsp = ncapi.edit_config(ncsession, Datastore.candidate, data) # Perform an action - commit the content ncapi.commit(ncsession)
def setUpClass(cls): cls.ncc = NetconfServiceProvider(cls.hostname, cls.username, cls.password, cls.port, cls.protocol, not cls.on_demand, cls.common_cache, cls.timeout) cls.netconf_service = NetconfService() cls.logger = logging.getLogger("ydk") handler = logging.StreamHandler() formatter = logging.Formatter( ("%(asctime)s - %(name)s - %(levelname)s - %(message)s")) handler.setFormatter(formatter) cls.logger.addHandler(handler) cls.logger.setLevel(logging.ERROR)
# log debug messages if verbose argument specified if args.verbose: logger = logging.getLogger("ydk") logger.setLevel(logging.INFO) handler = logging.StreamHandler() formatter = logging.Formatter(("%(asctime)s - %(name)s - " "%(levelname)s - %(message)s")) handler.setFormatter(formatter) logger.addHandler(handler) # create NETCONF provider provider = NetconfServiceProvider(address=device.hostname, port=device.port, username=device.username, password=device.password, protocol=device.scheme) # create NETCONF service netconf = NetconfService() locale = xr_infra_infra_locale_cfg.Locale() # create object config_locale(locale) # add object configuration # edit configuration on NETCONF device # netconf.lock(provider, Datastore.candidate) netconf.edit_config(provider, Datastore.candidate, locale) netconf.commit(provider) # netconf.unlock(provider, Datastore.candidate) exit() # End of script
class SanityTest(unittest.TestCase): PROVIDER_TYPE = "non-native" @classmethod def setUpClass(self): self.ydk_client = NativeNetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) self.netconf_service = NetconfService() @classmethod def tearDownClass(self): self.ydk_client.close() pass def setUp(self): pass def tearDown(self): pass def test_payload(self): result = self.ydk_client.execute(''' <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <edit-config> <target><candidate/></target> <config> <runner xmlns="http://cisco.com/ns/yang/ydktest-sanity"><ytypes><built-in-t><number8>12</number8></built-in-t></ytypes></runner> </config> </edit-config> </rpc>''', '') self.assertIn('ok', result) def test_server_error(self): try: result = self.ydk_client.execute(''' <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <edit-config> <target><candidate/></target> <config> <runner xmlns="http://invalid.com"></runner> </config> </edit-config> </rpc>''', '') self.assertIn('ok', result) except Exception as e: self.assertIsInstance(e, YPYServiceProviderError) msg = str(e) self.assertEqual(msg, 'Server rejected request.\n\terror-type: protocol\n\terror-tag: unknown-namespace\n\t' 'error-severity: error\n\terror-path: /rpc/edit-config/config\n\tbad-element: runner\n' '\tbad-namespace: http://invalid.com') def test_compare_clients(self): ncc = NetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) import time start_time = time.time() native_result = self.netconf_service.get(self.ydk_client, None) native_end_time = time.time() ncc_result = self.netconf_service.get(ncc, None) ncc_end_time = time.time() print 'Native client time taken: ' + str(native_end_time - start_time) + ' seconds' print 'NCClient time taken: ' + str(ncc_end_time - native_end_time) + ' seconds' self.assertEqual(True, is_equal(native_result, ncc_result))
# log debug messages if verbose argument specified if args.verbose: logger = logging.getLogger("ydk") logger.setLevel(logging.INFO) handler = logging.StreamHandler() formatter = logging.Formatter(("%(asctime)s - %(name)s - " "%(levelname)s - %(message)s")) handler.setFormatter(formatter) logger.addHandler(handler) # create NETCONF provider provider = NetconfServiceProvider(address=device.hostname, port=device.port, username=device.username, password=device.password, protocol=device.scheme) # create NETCONF service netconf = NetconfService() native = xe_native.Native() # create object config_native(native) # add object configuration # edit configuration on NETCONF device # netconf.lock(provider, Datastore.running) netconf.edit_config(provider, Datastore.running, native) # netconf.unlock(provider, Datastore.running) exit() # End of script
class SanityNetconf(unittest.TestCase): PROVIDER_TYPE = "non-native" @classmethod def setUpClass(self): from ydk.providers import NetconfServiceProvider from ydk.services import NetconfService if SanityNetconf.PROVIDER_TYPE == "native": self.ncc = NativeNetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) else: self.ncc = NetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) self.netconf_service = NetconfService() @classmethod def tearDownClass(self): self.ncc.close() def setUp(self): from ydk.services import CRUDService crud = CRUDService() runner = ysanity.Runner() crud.delete(self.ncc, runner) def tearDown(self): pass def _create_runner(self): runner = ysanity.Runner() runner.one.number = 1 runner.one.name = 'runner:one:name' return runner def test_copy_config_invalid_1(self): try: from ydk.services import Datastore op = self.netconf_service.copy_config(self.ncc, target=None, source=Datastore.running) except YPYServiceError as err: expected_msg = "'target' and 'source' cannot be None" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_copy_config_invalid_2(self): try: from ydk.services import Datastore op = self.netconf_service.copy_config(self.ncc, target=Datastore.candidate, source=None) except YPYServiceError as err: expected_msg = "'target' and 'source' cannot be None" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_copy_config_invalid_3(self): try: op = self.netconf_service.copy_config(self.ncc, target=None, source=None) except YPYServiceError as err: expected_msg = "'target' and 'source' cannot be None" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_copy_config_invalid_4(self): try: from ydk.services import Datastore op = self.netconf_service.copy_config( self.ncc, target=Datastore.candidate, source=Datastore.running, with_defaults_option=1) except YPYServiceError as err: expected_msg = "optional arg 'with_defaults_option' must be of type ietf_netconf_with_defaults.WithDefaultsModeEnum" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_delete_config_invalid(self): try: op = self.netconf_service.delete_config(self.ncc, target=None) except YPYServiceError as err: expected_msg = "'target' cannot be None" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_edit_config_invalid_1(self): try: runner = self._create_runner() op = self.netconf_service.edit_config(self.ncc, None, runner) except YPYServiceError as err: expected_msg = "'target' and 'config' cannot be None" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_edit_config_invalid_2(self): try: from ydk.services import Datastore op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, None) except YPYServiceError as err: expected_msg = "'target' and 'config' cannot be None" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_edit_config_invalid_3(self): try: op = self.netconf_service.edit_config(self.ncc, None, None) except YPYServiceError as err: expected_msg = "'target' and 'config' cannot be None" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_edit_config_invalid_4(self): try: from ydk.services import Datastore runner = self._create_runner() op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner, default_operation=1) except YPYServiceError as err: expected_msg = "optional arg 'default_operation' must be of type ietf_netconf.EditConfigRpc.Input.DefaultOperationEnum" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_edit_config_invalid_5(self): try: from ydk.services import Datastore runner = self._create_runner() op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner, error_option=1) except YPYServiceError as err: expected_msg = "optional arg 'error_option' must be of type ietf_netconf.EditConfigRpc.Input.ErrorOptionEnum" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_edit_config_invalid_6(self): try: from ydk.services import Datastore runner = self._create_runner() op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner, test_option=1) except YPYServiceError as err: expected_msg = "optional arg 'test_option' must be of type ietf_netconf.EditConfigRpc.Input.TestOptionEnum" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_get_config_invalid_1(self): try: from ydk.services import Datastore runner = self._create_runner() get_filter = ysanity.Runner() op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) result = self.netconf_service.get_config(self.ncc, None, get_filter) except YPYServiceError as err: expected_msg = "'source' cannot be None" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_get_config_invalid_2(self): try: from ydk.services import Datastore runner = self._create_runner() get_filter = ysanity.Runner() op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) result = self.netconf_service.get_config(self.ncc, Datastore.candidate, get_filter, with_defaults_option=1) except YPYServiceError as err: expected_msg = "optional arg 'with_defaults_option' must be of type ietf_netconf_with_defaults.WithDefaultsModeEnum" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_get_invalid(self): try: from ydk.services import Datastore runner = self._create_runner() get_filter = ysanity.Runner() op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) self.assertIn('ok', op) op = self.netconf_service.discard_changes(self.ncc) self.assertIn('ok', op) op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) self.assertIn('ok', op) op = self.netconf_service.commit(self.ncc) self.assertIn('ok', op) result = self.netconf_service.get(self.ncc, get_filter, with_defaults_option=1) self.assertEqual(is_equal(runner, result), True) except YPYServiceError as err: expected_msg = "optional arg 'with_defaults_option' must be of type ietf_netconf_with_defaults.WithDefaultsModeEnum" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_lock_invalid(self): try: op = self.netconf_service.lock(self.ncc, None) except YPYServiceError as err: expected_msg = "'target' cannot be None" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_unlock_invalid(self): try: from ydk.services import Datastore op = self.netconf_service.lock(self.ncc, Datastore.candidate) op = self.netconf_service.unlock(self.ncc, None) except YPYServiceError as err: expected_msg = "'target' cannot be None" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised') def test_validate_invalid(self): try: op = self.netconf_service.validate(self.ncc) except YPYServiceError as err: expected_msg = "'source' cannot be None" self.assertEqual(err.message, expected_msg) else: raise Exception('YPYServiceError not raised')
args = parser.parse_args() device = urlparse(args.device) # log debug messages if verbose argument specified if args.verbose: logger = logging.getLogger("ydk") logger.setLevel(logging.INFO) handler = logging.StreamHandler() formatter = logging.Formatter(("%(asctime)s - %(name)s - " "%(levelname)s - %(message)s")) handler.setFormatter(formatter) logger.addHandler(handler) # create NETCONF provider provider = NetconfServiceProvider(address=device.hostname, port=device.port, username=device.username, password=device.password, protocol=device.scheme) # create NETCONF service netconf = NetconfService() mpls_lsd = xr_mpls_lsd_cfg.MplsLsd() # create object # get configuration from NETCONF device # mpls_lsd = netconf.get_config(provider, Datastore.running) process_mpls_lsd(mpls_lsd) # process object data exit() # End of script
class SanityNetconf(unittest.TestCase): PROVIDER_TYPE = "non-native" @classmethod def setUpClass(self): if SanityNetconf.PROVIDER_TYPE == "native": self.ncc = NativeNetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) else: self.ncc = NetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) self.netconf_service = NetconfService() @classmethod def tearDownClass(self): self.ncc.close() def setUp(self): from ydk.services import CRUDService crud = CRUDService() runner = ysanity.Runner() crud.delete(self.ncc, runner) def tearDown(self): pass def test_edit_commit_get(self): runner = ysanity.Runner() runner.one.number = 1 runner.one.name = 'runner:one:name' get_filter = ysanity.Runner() op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) self.assertEqual(None, op) result = self.netconf_service.get_config(self.ncc, Datastore.candidate, get_filter) self.assertEqual(is_equal(runner, result), True) op = self.netconf_service.commit(self.ncc) self.assertEqual(None, op) result = self.netconf_service.get(self.ncc, get_filter) self.assertEqual(is_equal(runner, result), True) def test_lock_unlock(self): op = self.netconf_service.lock(self.ncc, Datastore.running) self.assertEqual(None, op) op = self.netconf_service.unlock(self.ncc, Datastore.running) self.assertEqual(None, op) def test_lock_unlock_fail(self): op = self.netconf_service.lock(self.ncc, Datastore.candidate) self.assertEqual(None, op) try: op = self.netconf_service.unlock(self.ncc, Datastore.running) except Exception as e: self.assertIsInstance(e, YPYError) def test_validate(self): op = self.netconf_service.validate(self.ncc, source=Datastore.candidate) self.assertEqual(None, op) runner = ysanity.Runner() runner.one.number = 1 runner.one.name = 'runner:one:name' op = self.netconf_service.validate(self.ncc, source=runner) self.assertEqual(None, op) def test_validate_fail(self): # should have been handled by YDK local validation pass def test_commit_discard(self): runner = ysanity.Runner() runner.two.number = 2 runner.two.name = 'runner:two:name' get_filter = ysanity.Runner() op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) self.assertEqual(None, op) op = self.netconf_service.discard_changes(self.ncc) self.assertEqual(None, op) op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) self.assertEqual(None, op) op = self.netconf_service.commit(self.ncc) self.assertEqual(None, op) result = self.netconf_service.get(self.ncc, get_filter) self.assertEqual(is_equal(runner, result), True) def test_confirmed_commit(self): runner = ysanity.Runner() runner.two.number = 2 runner.two.name = 'runner:two:name' get_filter = ysanity.Runner() op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) self.assertEqual(None, op) op = self.netconf_service.commit(self.ncc, confirmed=True, confirm_timeout=120) self.assertEqual(None, op) result = self.netconf_service.get(self.ncc, get_filter) self.assertEqual(is_equal(runner, result), True) op = self.netconf_service.cancel_commit(self.ncc) self.assertEqual(None, op) def test_copy_config(self): op = self.netconf_service.copy_config(self.ncc, Datastore.candidate, Datastore.running) self.assertEqual(None, op) runner = ysanity.Runner() runner.two.number = 2 runner.two.name = 'runner:two:name' get_filter = ysanity.Runner() op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) self.assertEqual(None, op) op = self.netconf_service.copy_config(self.ncc, Datastore.running, Datastore.candidate) self.assertEqual(None, op) result = self.netconf_service.get_config(self.ncc, Datastore.running, get_filter) self.assertEqual(is_equal(result, runner), True) runner.two.name += 'modified' op = self.netconf_service.copy_config(self.ncc, Datastore.running, runner) self.assertEqual(None, op) result = self.netconf_service.get_config(self.ncc, Datastore.running, get_filter) self.assertEqual(is_equal(result, runner), True) def test_delete_config(self): pass # startup and candidate cannot be both enabled in ConfD # op = self.netconf_service.delete_config(self.ncc, Datastore.startup) # self.assertEqual(None, op) def test_delete_config_fail(self): self.assertRaises(YPYServiceError, self.netconf_service.delete_config, self.ncc, Datastore.running) self.assertRaises(YPYServiceError, self.netconf_service.delete_config, self.ncc, Datastore.candidate) def test_copy_config_fail(self): self.assertRaises(YPYServiceError, self.netconf_service.copy_config, self.ncc, target=123, source=456) def test_edit_config_fail(self): self.assertRaises(YPYServiceError, self.netconf_service.edit_config, self.ncc, Datastore.startup, Datastore.candidate) def test_get_config_fail(self): runner = ysanity.Runner() self.assertRaises(YPYServiceError, self.netconf_service.get_config, self.ncc, "invalid-input", runner) def test_lock_fail(self): self.assertRaises(YPYServiceError, self.netconf_service.lock, self.ncc, "invalid-input") def test_unlock_fail(self): self.assertRaises(YPYServiceError, self.netconf_service.unlock, self.ncc, "invalid-input")
nbr_ipv6_afsf = nbr_ipv6.afi_safis.AfiSafi() nbr_ipv6_afsf.afi_safi_name = 'ipv6-unicast' nbr_ipv6_afsf.config.peer_as = 65002 nbr_ipv6_afsf.config.afi_safi_name = 'ipv6-unicast' nbr_ipv6_afsf.config.enabled = True nbr_ipv6.afi_safis.afi_safi.append(nbr_ipv6_afsf) netconf_service.edit_config(session, Datastore.candidate, bgp_cfg) nbr_ipv6_filter = bgp.Bgp.Neighbors.Neighbor() nbr_ipv6_filter.neighbor_address = '2001:db8:fff1::1' nbr_ipv6_read = netconf_service.get_config(session, Datastore.candidate, bgp_cfg) print nbr_ipv6_read if __name__ == "__main__": init_logging() provider = NetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) netconf_service = NetconfService() bgp_run(netconf_service, provider) exit()
class SanityNetconf(unittest.TestCase): PROVIDER_TYPE = "non-native" @classmethod def setUpClass(self): if SanityNetconf.PROVIDER_TYPE == "native": self.ncc = NativeNetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) else: self.ncc = NetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) self.netconf_service = NetconfService() @classmethod def tearDownClass(self): self.ncc.close() def setUp(self): from ydk.services import CRUDService crud = CRUDService() runner = ysanity.Runner() crud.delete(self.ncc, runner) def tearDown(self): pass def test_edit_commit_get(self): runner = ysanity.Runner() runner.one.number = 1 runner.one.name = 'runner:one:name' get_filter = ysanity.Runner() op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) self.assertIn('ok', op) result = self.netconf_service.get_config(self.ncc, Datastore.candidate, get_filter) self.assertEqual(is_equal(runner, result), True) op = self.netconf_service.commit(self.ncc) self.assertIn('ok', op) result = self.netconf_service.get(self.ncc, get_filter) self.assertEqual(is_equal(runner, result), True) def test_copy(self): op = self.netconf_service.copy_config(self.ncc, target=Datastore.candidate, source=Datastore.running) self.assertIn('ok', op) def test_lock_unlock(self): op = self.netconf_service.lock(self.ncc, Datastore.running) self.assertIn('ok', op) op = self.netconf_service.unlock(self.ncc, Datastore.running) self.assertIn('ok', op) def test_lock_unlock_fail(self): op = self.netconf_service.lock(self.ncc, Datastore.candidate) self.assertIn('ok', op) try: op = self.netconf_service.unlock(self.ncc, Datastore.running) except Exception as e: self.assertIsInstance(e, YPYError) def test_validate(self): op = self.netconf_service.validate(self.ncc, source=Datastore.candidate) self.assertIn('ok', op) runner = ysanity.Runner() runner.one.number = 1 runner.one.name = 'runner:one:name' op = self.netconf_service.validate(self.ncc, config=runner) self.assertIn('ok', op) def test_validate_fail(self): runner = ysanity.Runner() runner.one.number = 1 runner.one.name = 2 try: self.netconf_service.validate(self.ncc, config=runner) except Exception as e: self.assertIsInstance(e, YPYModelError) def test_commit_discard(self): runner = ysanity.Runner() runner.two.number = 2 runner.two.name = 'runner:two:name' get_filter = ysanity.Runner() op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) self.assertIn('ok', op) op = self.netconf_service.discard_changes(self.ncc) self.assertIn('ok', op) op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) self.assertIn('ok', op) op = self.netconf_service.commit(self.ncc) self.assertIn('ok', op) result = self.netconf_service.get(self.ncc, get_filter) self.assertEqual(is_equal(runner, result), True) def test_copy_config(self): op = self.netconf_service.copy_config(self.ncc, Datastore.candidate, Datastore.running) self.assertIn('ok', op)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(1) try: sock.connect((host, port)) sock.shutdown(socket.SHUT_RDWR) return True except (Exception, socket_error): return False finally: sock.close() json_provider = CodecServiceProvider(type='json') codec = CodecService() nc = NetconfService() class NetConf: def __init__(self, address, port, username, password): self.address = address self.port = port self.username = username self.password = password self.session = self.create_session() def create_session(self): return NetconfServiceProvider(address=self.address, port=self.port, username=self.username, password=self.password)
"description": "\"test desc\"", "unit": [ { "name": "0", "family": { "inet": { "address": [ { "name": "10.0.0.2" } ] } } } ] } ] } } }''' # Invoke the decode method to decode the JSON payload to a YDK python object interface_configurations = codec.decode(json_provider, if_json) # create NETCONF service netconf = NetconfService() netconf.edit_config(nc_provider, Datastore.candidate, interface_configurations) print("exiting...") exit()
class SanityTest(unittest.TestCase): PROVIDER_TYPE = "non-native" @classmethod def setUpClass(self): self.ydk_client = NativeNetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) self.netconf_service = NetconfService() @classmethod def tearDownClass(self): self.ydk_client.close() pass def setUp(self): pass def tearDown(self): pass def test_payload(self): result = self.ydk_client.execute( ''' <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <edit-config> <target><candidate/></target> <config> <runner xmlns="http://cisco.com/ns/yang/ydktest-sanity"><ytypes><built-in-t><number8>12</number8></built-in-t></ytypes></runner> </config> </edit-config> </rpc>''', '') self.assertIn('ok', result) def test_server_error(self): try: result = self.ydk_client.execute( ''' <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <edit-config> <target><candidate/></target> <config> <runner xmlns="http://invalid.com"></runner> </config> </edit-config> </rpc>''', '') self.assertIn('ok', result) except Exception as e: self.assertIsInstance(e, YPYServiceProviderError) msg = str(e) self.assertEqual( msg, 'Server rejected request.\n\terror-type: protocol\n\terror-tag: unknown-namespace\n\t' 'error-severity: error\n\terror-path: /rpc/edit-config/config\n\tbad-element: runner\n' '\tbad-namespace: http://invalid.com') def test_compare_clients(self): ncc = NetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) import time start_time = time.time() native_result = self.netconf_service.get(self.ydk_client, None) native_end_time = time.time() ncc_result = self.netconf_service.get(ncc, None) ncc_end_time = time.time() print('Native client time taken: %s seconds' % (native_end_time - start_time)) print('NCClient time taken: %s seconds' % (ncc_end_time - native_end_time)) self.assertEqual(True, is_equal(native_result, ncc_result))
class SanityNetconf(unittest.TestCase): @classmethod def setUpClass(self): self.ncc = NetconfServiceProvider(address='127.0.0.1', username='******', password='******', protocol='ssh', port=12022) self.netconf_service = NetconfService() @classmethod def tearDownClass(self): self.ncc.close() def setUp(self): from ydk.services import CRUDService crud = CRUDService() runner = ysanity.Runner() crud.delete(self.ncc, runner) print '\nIn method', self._testMethodName + ':' def tearDown(self): pass def test_edit_commit_get(self): runner = ysanity.Runner() runner.one.number = 1 runner.one.name = 'runner:one:name' get_filter = ysanity.Runner() op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) self.assertIn('ok', op) result = self.netconf_service.get_config(self.ncc, Datastore.candidate, get_filter) self.assertEqual(is_equal(runner, result), True) op = self.netconf_service.commit(self.ncc) self.assertIn('ok', op) result = self.netconf_service.get(self.ncc, get_filter) self.assertEqual(is_equal(runner, result), True) def test_copy(self): op = self.netconf_service.copy_config(self.ncc, target=Datastore.candidate, source=Datastore.running) self.assertIn('ok', op) def test_lock_unlock(self): op = self.netconf_service.lock(self.ncc, Datastore.running) self.assertIn('ok', op) op = self.netconf_service.unlock(self.ncc, Datastore.running) self.assertIn('ok', op) def test_lock_unlock_fail(self): op = self.netconf_service.lock(self.ncc, Datastore.candidate) self.assertIn('ok', op) try: op = self.netconf_service.unlock(self.ncc, Datastore.running) except Exception as e: self.assertIsInstance(e, YPYError) def test_validate(self): op = self.netconf_service.validate(self.ncc, source=Datastore.candidate) self.assertIn('ok', op) runner = ysanity.Runner() runner.one.number = 1 runner.one.name = 'runner:one:name' op = self.netconf_service.validate(self.ncc, config=runner) self.assertIn('ok', op) def test_validate_fail(self): runner = ysanity.Runner() runner.one.number = 1 runner.one.name = 2 try: self.netconf_service.validate(self.ncc, config=runner) except Exception as e: self.assertIsInstance(e, YPYDataValidationError) def test_commit_discard(self): runner = ysanity.Runner() runner.two.number = 2 runner.two.name = 'runner:two:name' get_filter = ysanity.Runner() op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) self.assertIn('ok', op) op = self.netconf_service.discard_changes(self.ncc) self.assertIn('ok', op) op = self.netconf_service.edit_config(self.ncc, Datastore.candidate, runner) self.assertIn('ok', op) op = self.netconf_service.commit(self.ncc) self.assertIn('ok', op) result = self.netconf_service.get(self.ncc, get_filter) self.assertEqual(is_equal(runner, result), True) def test_copy_config(self): op = self.netconf_service.copy_config(self.ncc, Datastore.candidate, Datastore.running) self.assertIn('ok', op)
''' from ydk.services import NetconfService, Datastore from ydk.providers import NetconfServiceProvider from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ip_domain_cfg \ as xr_ip_domain_cfg import logging if __name__ == "__main__": # create NETCONF session provider = NetconfServiceProvider(address="10.82.79.35", port=830, username="******", password="******", protocol="ssh") # Create NetConfService instance cfg = NetconfService() # Commit XRv9K prior to retrieving configuration print(cfg.commit(provider)) # create object ip_domain = xr_ip_domain_cfg.IpDomain() # get configuration from netconf device ip_domain = cfg.get_config(provider, Datastore.running, ip_domain) # close NETCONF session and exit provider.close() exit()
crud_update(crud_service, provider, ydk_obj) {% endfor %}{% endif %} {% if service == 'delete' %}{% for i in ydk_obj_names.split %} ydk_obj = {{i}}() crud_delete(crud_service, provider, ydk_obj) {% endfor %}{% endif %} {% if service == 'get' %}{% for i in ydk_obj_names.split %} ydk_obj = {{i}}() crud_get(crud_service, provider, ydk_obj) {% endfor %}{% endif %} {% if service == 'get_config' %}{% for i in ydk_obj_names.split %} ydk_obj = {{i}}() crud_get_config(crud_service, provider, ydk_obj) {% endfor %}{% endif %}{% endif %} {% if service_type == 'netconf' %}netconf_service = NetconfService() {% if service == 'create' %}{% for i in ydk_obj_names.split %} ydk_obj = {{i}}() netconf_create(netconf_service, provider, ydk_obj) {% endfor %}{% endif %} {% if service == 'update' %}{% for i in ydk_obj_names.split %} ydk_obj = {{i}}() netconf_replace(netconf_service, provider, ydk_obj) {% endfor %}{% endif %} {% if service == 'delete' %}{% for i in ydk_obj_names.split %} ydk_obj = {{i}}() netconf_delete(netconf_service, provider, ydk_obj) {% endfor %}{% endif %} {% if service == 'get' %}{% for i in ydk_obj_names.split %} ydk_obj = {{i}}() netconf_get(netconf_service, provider, ydk_obj)
logger = logging.getLogger("ydk") logger.setLevel(logging.INFO) handler = logging.StreamHandler() formatter = logging.Formatter(("%(asctime)s - %(name)s - " "%(levelname)s - %(message)s")) handler.setFormatter(formatter) logger.addHandler(handler) # create NETCONF provider provider = NetconfServiceProvider(address=device.hostname, port=device.port, username=device.username, password=device.password, protocol=device.scheme) # create NETCONF service netconf = NetconfService() # create OC-interfaces object (optical channels) interfaces = oc_interfaces.Interfaces() config_interfaces(interfaces) # create OC-Terminal_device object (mapping between logical ports) terminal_device = oc_terminal_device.TerminalDevice() config_terminal_device(terminal_device) # create OC-platform object (description for optical channels) components = oc_platform.Components() config_components(components) # edit configuration on NETCONF device # netconf.lock(provider, Datastore.candidate)
"""Execute main program.""" # create IOS-XR NETCONF provider xr_provider = NetconfServiceProvider(address=iosxr_host, port=netconf_port, username=username, password=password, timeout=10) # create JunOS NETCONF provider ##junos_provider = NetconfServiceProvider(address=junos_host, ## port=netconf_port, ## username=username, ## password=password, ## timeout=10) #create an instance of the netconf service netconf = NetconfService() # Create an IOSXR instance from the OpenConfig BGP Model xr_bgp = oc_bgp.Bgp() # Create an JunOS instance from the OpenConfig BGP Model #junos_bgp = oc_bgp.Bgp() # Populate the BGP object with xr Configuration config_bgp(xr_bgp,global_as=100,router_id='1.1.1.1',peer_group_name='EBGP',remote_as=101,remote_peer='1.1.1.1') # Populate the BGP object with JunOS Configuration #config_bgp(junos_bgp,global_as=101,router_id='https://www.linkedin.com/redir/invalid-link-page?url=10%2e1%2e3%2e2',peer_group_name='EBGP',remote_as=100,remote_peer='https://www.linkedin.com/redir/invalid-link-page?url=10%2e1%2e3%2e1') # Send the XR bgp object via NETCONF to the Candidate DataStore on the XR router netconf.edit_config(xr_provider,Datastore.candidate,xr_bgp) # commit the configuration netconf.commit(xr_provider) # close the NETCONF session towards the XR router