def test_conn_remove_module(self): with sysrepo.SysrepoConnection() as conn: conn.remove_module("sysrepo-example") # reconnect to make sure it is removed with sysrepo.SysrepoConnection(err_on_sched_fail=True) as conn: ctx = conn.get_ly_ctx() with self.assertRaises(libyang.LibyangError): ctx.get_module("sysrepo-example")
def test_conn_install_module(self): with sysrepo.SysrepoConnection() as conn: conn.install_module(YANG_FILE, enabled_features=["turbo"]) # reconnect to make sure it is installed with sysrepo.SysrepoConnection(err_on_sched_fail=True) as conn: ctx = conn.get_ly_ctx() mod = ctx.get_module("sysrepo-example") self.assertTrue(mod.implemented()) self.assertTrue(mod.feature_state("turbo"))
def tearDownClass(cls): cls.sess.stop() cls.conn.remove_module("sysrepo-example") cls.conn.disconnect() # reconnect to make sure module is removed with sysrepo.SysrepoConnection(err_on_sched_fail=True): pass
def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity.") args = parser.parse_args() if args.verbose >= 3: level = logging.DEBUG elif args.verbose >= 2: level = logging.INFO elif args.verbose >= 1: level = logging.WARNING else: level = logging.ERROR logging.basicConfig(level=level, format="[%(levelname)s] application: %(message)s") loop = asyncio.get_event_loop() stop_event = asyncio.Event() loop.add_signal_handler(signal.SIGINT, stop_event.set) loop.add_signal_handler(signal.SIGTERM, stop_event.set) try: with sysrepo.SysrepoConnection() as conn: with conn.start_session() as sess: logging.info("subscribing to module changes: sysrepo-example") sess.subscribe_module_change("sysrepo-example", None, module_change_cb, asyncio_register=True) logging.info( "subscribing to operational data requests: /sysrepo-example:state" ) sess.subscribe_oper_data_request( "sysrepo-example", "/sysrepo-example:state", oper_data_cb, asyncio_register=True, ) logging.info( "subscribing to rpc calls: /sysrepo-example:poweroff") sess.subscribe_rpc_call("/sysrepo-example:poweroff", poweroff, asyncio_register=True) logging.info( "subscribing to action calls: /sysrepo-example:conf/security/alarm/trigger" ) sess.subscribe_rpc_call( "/sysrepo-example:conf/security/alarm/trigger", trigger_alarm, asyncio_register=True, ) loop.run_until_complete(stop_event.wait()) return 0 except sysrepo.SysrepoError as e: logging.error("%s", e) return 1
def __init__(self, sess=None, default_prompt='> ', prefix=''): if sess == None: conn = sr.SysrepoConnection() sess = conn.start_session() self.context = Root(conn) self.completer = GoldstoneShellCompleter(self.context) self.default_input = '' self.default_prompt = default_prompt self.prefix = prefix
def main(): parser = argparse.ArgumentParser(description=__doc__) group = parser.add_mutually_exclusive_group(required=True) group.add_argument("-l", "--list", action="store_true", help="List installed YANG modules.") group.add_argument("-i", "--install", help="Install YANG module.") group.add_argument("-u", "--uninstall", help="Uninstall YANG module.") parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity.") args = parser.parse_args() if args.verbose >= 3: level = logging.DEBUG elif args.verbose >= 2: level = logging.INFO elif args.verbose >= 1: level = logging.WARNING else: level = logging.ERROR logging.basicConfig(level=level, format="[%(levelname)s] sysrepoctl.py: %(message)s") sysrepo.configure_logging(py_logging=True) try: with sysrepo.SysrepoConnection() as conn: if args.list: for module in conn.get_ly_ctx(): if module.implemented(): print(module.name()) elif args.install: conn.install_module(args.install) elif args.uninstall: conn.remove_module(args.uninstall) return 0 except sysrepo.SysrepoError as e: logging.error("%s", e) return 1
def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity.") args = parser.parse_args() if args.verbose >= 3: level = logging.DEBUG elif args.verbose >= 2: level = logging.INFO elif args.verbose >= 1: level = logging.WARNING else: level = logging.ERROR logging.basicConfig(level=level, format="[%(levelname)s] application: %(message)s") try: with sysrepo.SysrepoConnection() as conn: with conn.start_session() as sess: logging.info("subscribing to module changes: sysrepo-example") sess.subscribe_module_change("sysrepo-example", None, module_change_cb) logging.info( "subscribing to operational data requests: /sysrepo-example:state" ) sess.subscribe_oper_data_request("sysrepo-example", "/sysrepo-example:state", oper_data_cb) logging.info( "subscribing to rpc calls: /sysrepo-example:poweroff") sess.subscribe_rpc_call("/sysrepo-example:poweroff", poweroff) signal.sigwait({signal.SIGINT, signal.SIGTERM}) return 0 except sysrepo.SysrepoError as e: logging.error("%s", e) return 1
def setUpClass(cls): with sysrepo.SysrepoConnection() as conn: conn.install_module(YANG_FILE, enabled_features=["turbo"]) cls.conn = sysrepo.SysrepoConnection(err_on_sched_fail=True) cls.sess = cls.conn.start_session()
def test_conn_start_session_operational(self): with sysrepo.SysrepoConnection() as conn: with conn.start_session("operational") as sess: self.assertEqual(sess.get_datastore(), "operational")
def test_conn_start_session_ctxmgr(self): with sysrepo.SysrepoConnection(err_on_sched_fail=True) as conn: with conn.start_session() as sess: self.assertEqual(sess.get_datastore(), "running")
def test_conn_start_session(self): with sysrepo.SysrepoConnection(no_sched_changes=True) as conn: sess = conn.start_session() self.assertEqual(sess.get_datastore(), "running") sess.stop()
def test_conn_connect_ctxmgr(self): with sysrepo.SysrepoConnection(): pass
def test_conn_connect(self): conn = sysrepo.SysrepoConnection() conn.disconnect()
def main(): parser = argparse.ArgumentParser(description=__doc__) group = parser.add_mutually_exclusive_group(required=True) group.add_argument( "-I", "--import", metavar="MODULE", dest="import_", help=""" Replace the configuration of MODULE. Data is read from stdin. """, ) group.add_argument( "-X", "--export", metavar="XPATH", help=""" Export data pointed by XPATH. Printed to stdout. """, ) group.add_argument( "-R", "--rpc", action="store_true", help=""" Send a RPC/action. Read the input parameters from stdin. RPC/action output is printed to stdout. """, ) parser.add_argument( "-v", "--verbose", action="count", default=0, help="Increase verbosity." ) parser.add_argument( "-f", "--format", choices=("xml", "json", "lyb"), default="xml", help=""" Data format to be used. By default based on file extension. """, ) parser.add_argument( "-d", "--datastore", choices=("running", "startup", "operational", "candidate"), default="running", help=""" Datastore to be operated on. """, ) parser.add_argument( "-n", "--not-strict", action="store_true", default=False, help=""" Silently ignore any unknown data. """, ) args = parser.parse_args() if args.verbose >= 3: level = logging.DEBUG elif args.verbose >= 2: level = logging.INFO elif args.verbose >= 1: level = logging.WARNING else: level = logging.ERROR logging.basicConfig( level=level, format="[%(levelname)s] sysrepocfg.py: %(message)s" ) sysrepo.configure_logging(py_logging=True) try: with sysrepo.SysrepoConnection() as conn: ctx = conn.get_ly_ctx() with conn.start_session(args.datastore) as sess: if args.import_: data = ctx.parse_data_mem( sys.stdin.read(), args.format, config=True, strict=not args.not_strict, ) sess.replace_config_ly(data, args.import_) elif args.export: data = sess.get_data_ly(args.export) try: data.print_file( sys.stdout, args.format, pretty=True, with_siblings=True ) finally: data.free() elif args.rpc: rpc_input = ctx.parse_data_mem( sys.stdin.read(), args.format, rpc=True, strict=not args.not_strict, ) try: rpc_output = sess.rpc_send_ly(rpc_input) finally: rpc_input.free() try: rpc_output.print_file(sys.stdout, args.format, pretty=True) finally: rpc_output.free() return 0 except sysrepo.SysrepoError as e: logging.error("%s", e) return 1
def __init__(self, taish_server): self.taish = taish.AsyncClient(*taish_server.split(':')) self.loop = asyncio.get_event_loop() self.conn = sysrepo.SysrepoConnection() self.sess = self.conn.start_session()