# 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
# 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) netconf.edit_config(provider, Datastore.candidate, interfaces) netconf.edit_config(provider, Datastore.candidate, terminal_device) netconf.edit_config(provider, Datastore.candidate, components) netconf.commit(provider) # netconf.unlock(provider, Datastore.candidate) exit() # End of script ##### helpfull commands to check the status on the NCS1002: ### 'sh hw-module slice 0' to find the provisioning progress ### 'sh terminal-device layout' to check accepted configuration \ ### layout with a clear picture of logical channels and their mappings ### 'sh terminal-device logical-channel <all|number>' to check details \ ### either for all logical channels or for a specific one
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)
# 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 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")
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)
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')
## 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 xr_provider.close() # Wait for 2 second time.sleep(2) # Send the JUnOS bgp object via NETCONF to the Candidate DataStore on the JUnOS router netconf.edit_config(junos_provider,Datastore.candidate,junos_bgp) # commit the configuration netconf.commit(junos_provider) # close the NETCONF session towards the JUnOS router #junos_provider.close() exit()
"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()