def main(): # Notable difference between c implementation is using exception mechanism for open handling unexpected events. # Here it is useful because `Connection`, `Session` and `Subscribe` could throw an exception. try: module_name = sys.argv[1] logger.info(f"Application will watch for changes in {module_name}") # connect to sysrepo conn = sr.Connection(module_name) # start session sess = sr.Session(conn) # subscribe for changes in running config */ subscribe = sr.Subscribe(sess) subscribe.module_change_subscribe(module_name, module_change_cb) try: print_current_config(sess, module_name) except Exception as e: logger.error(e) logger.info("========== STARTUP CONFIG APPLIED AS RUNNING ==========") sr.global_loop() logger.info("Application exit requested, exiting.") except Exception as e: logger.error(e)
def main(): # Notable difference between c implementation is using exception mechanism for open handling unexpected events. # Here it is useful because `Conenction`, `Session` and `Subscribe` could throw an exception. try: module_name = "ietf-interfaces" if len(sys.argv) > 1: module_name = sys.argv[1] else: print("\nYou can pass the module name to be subscribed as the first argument") # connect to sysrepo conn = sr.Connection("example_application") # start session sess = sr.Session(conn) # subscribe for changes in running config */ subscribe = sr.Subscribe(sess) subscribe.module_change_subscribe(module_name, module_change_cb, None, 0, sr.SR_SUBSCR_DEFAULT | sr.SR_SUBSCR_APPLY_ONLY) print("\n\n ========== READING STARTUP CONFIG: ==========\n") try: print_current_config(sess, module_name) except Exception as e: print(e) print("\n\n ========== STARTUP CONFIG APPLIED AS RUNNING ==========\n") sr.global_loop() print("Application exit requested, exiting.\n") except Exception as e: print(e)
def init(self): self.print_banner() self.logger.info("Initializing cassini dataplane") self.logger.info("Discovering Sysrepo repository") s = self.sess.list_schemas() if s is None: self.logger.error("Sysrepo not found") self.logger.info("The dataplane application is existing") exit(2) else: self.logger.info("Sysrepo was found") self.logger.info("Retrieving data of repository") self.add_phy_interfaces() self.add_logical_interfaces() self.logger.info("Registering events") try: self.subscribe.module_change_subscribe(self.MOD_PLATAFORM, self.module_cb) self.subscribe.module_change_subscribe(self.MOD_TERM_DEV, self.module_cb) self.logger.info("Waiting events") sr.global_loop() self.logger.warning("Application exit requested, exiting.\n") finally: self.delete_phy_interfaces()
def connect(self): self.conn = sr.Connection("provider_%s" % (self.MODULE_NAME)) self.session = sr.Session(self.conn) self.subscribe = sr.Subscribe(self.session) self.subscribe.module_change_subscribe(self.MODULE_NAME, self.callback) sr.global_loop()
def connect_oper(self, path): self.conn = sr.Connection("oper_%s" % (self.MODULE_NAME)) self.session = sr.Session(self.conn) self.subscribe = sr.Subscribe(self.session) self.last_oper_refresh = 0 self.oper_val_dict = {} self.oper_module_path = path self.subscribe.dp_get_items_subscribe(path, self.callback_for_oper) sr.global_loop()
def start(): """ main function to create connection based on moudule name. """ try: module_name = "pnf-subscriptions" conn = sr.Connection(module_name) sess = sr.Session(conn) subscribe = sr.Subscribe(sess) subscribe.module_change_subscribe(module_name, module_change_cb) sr.global_loop() print("Application exit requested, exiting.") except Exception as error: print(error)
def start(): """ main function to create connection based on module name. """ try: module_name = 'pnf-subscriptions' conn = sr.Connection(module_name) sess = sr.Session(conn) subscribe = sr.Subscribe(sess) subscribe.module_change_subscribe(module_name, module_change_cb) sr.global_loop() logger.info('Application exit requested, exiting.') except Exception as error: logger.error(error, exc_info=True)
def main(): try: conn = sr.Connection(YANG_MODULE_NAME) sess = sr.Session(conn) subscribe = sr.Subscribe(sess) subscribe.module_change_subscribe(YANG_MODULE_NAME, module_change_cb, conn) try: print_current_config(sess, YANG_MODULE_NAME) except Exception as e: logger.error(e) sr.global_loop() logger.info("Application exit requested, exiting.") except Exception as e: logger.error(e)
def main(): module_name = 'dns-server-amended-with-zone' log.debug("connecting to sysrepo") # connect to sysrepo conn = sr.Connection("pdns") log.info("connected to sysrepo") log.debug("starting session") # start session session = sr.Session(conn) log.info("session started") sub = sr.Subscribe(session) # module_change_subscribe API docs: # https://www.sysrepo.org/static/doc/html/group__cl.html#ga35341cf4bf9584127f7c5a79405a878f sub.module_change_subscribe(module_name, change_cb, None, sr.SR_SUBSCR_DEFAULT | sr.SR_SUBSCR_APPLY_ONLY) sr.global_loop()
else: print ("\nYou can pass the module name to be subscribed as the first argument") print ("Application will watch for changes in " + module_name + "\n") # connect to sysrepo conn = sr.Connection("example_application") # start session sess = sr.Session(conn) # subscribe for changes in running config */ subscribe = sr.Subscribe(sess) subscribe.module_change_subscribe(module_name, module_change_cb) print ("\n\n ========== READING STARTUP CONFIG: ==========\n") try: print_current_config(sess, module_name); except Exception as e: print (e) print ("\n\n ========== STARTUP CONFIG APPLIED AS RUNNING ==========\n") sr.global_loop() print ("Application exit requested, exiting.\n") except Exception as e: print (e)
# connect to sysrepo conn = sr.Connection(sr.SR_CONN_DEFAULT) # start session sess = sr.Session(conn) # subscribe for changes in running config */ subscribe = sr.Subscribe(sess) subscribe.module_change_subscribe(module_name, module_change_cb, None, None, 0, sr.SR_SUBSCR_DONE_ONLY) print ("\n\n ========== READING RUNNING CONFIG: ==========\n") try: print_current_config(sess, module_name) except Exception as e: print (e) sr.global_loop() subscribe.unsubscribe() sess.session_stop() conn=None print ("Application exit requested, exiting.\n") except Exception as e: print (e)