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 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 createDataTreeLargeExampleModule(count, datastore): """ Add data to example-module. """ conn = sr.Connection("load test") sess = sr.Session(conn, datastore) subs = sr.Subscribe(sess) for i in xrange(count): xpath = "/example-module:container/list[key1='key" + str(i) + "'][key2='key" + str(i) +"']/leaf" val = sr.Val("leaf" + str(i), sr.SR_STRING_T) sess.set_item(xpath, val) sess.commit()
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 createDataTreeLargeIETFinterfacesModule(count, datastore): """ Add data to ietf-interfaces. """ conn = sr.Connection(sr.SR_CONN_DEFAULT) sess = sr.Session(conn, datastore) subs = sr.Subscribe(sess) for i in range(count): xpath = "/ietf-interfaces:interfaces/interface[name='eth" + str( i) + "']" xpath_ip = xpath + "/ietf-ip:ipv4/address[ip='192.168.1." + str( i) + "]" x_name = xpath + "/name" x_type = xpath + "/type" x_desc = xpath + "/description" x_enabled = xpath + "/enabled" x_ipv4_enabled = xpath + "/ietf-ip:ipv4/enabled" x_ipv4_mtu = xpath + "/ietf-ip:ipv4/mtu" x_ipv4_forward = xpath + "/ietf-ip:ipv4/forwarding" x_prefix_len = xpath_ip + "/prefix-length" val = sr.Val("Ethernet 0", sr.SR_STRING_T) sess.set_item(x_desc, val) val = sr.Val("iana-if-type:ethernetCsmacd", sr.SR_IDENTITYREF_T) sess.set_item(x_type, val) val = sr.Val(True, sr.SR_BOOL_T) sess.set_item(x_enabled, val) val = sr.Val(True, sr.SR_BOOL_T) sess.set_item(x_ipv4_enabled, val) val = sr.Val(1500, sr.SR_UINT16_T) sess.set_item(x_ipv4_mtu, val) val = sr.Val(False, sr.SR_BOOL_T) sess.set_item(x_ipv4_forward, val) sess.apply_changes()
def clearDataTree(module_name, datastore): """ Clear yang model. """ conn = sr.Connection(sr.SR_CONN_DEFAULT) sess = sr.Session(conn, datastore) subs = sr.Subscribe(sess) xpath = "/" + module_name + ":*//*" values = sess.get_items(xpath) if values == None: return for i in range(values.val_cnt()): sess.delete_item(values.val(i).xpath()) sess.apply_changes()
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()
# 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(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()
time.sleep(15) raise Exception("Could not connect to kafka server") def print_current_config(kafka_session, module): name = "/{}:*//*".format(module) logging.info("Retrieving current config for %s module", name) values = kafka_session.get_items(name) for i in range(values.val_cnt()): logging.info(values.val(i).to_string()) if __name__ == "__main__": try: module_name = sys.argv[1] bootstrap_servers = sys.argv[2] topic = sys.argv[3] connection = sr.Connection("example_application2") session = sr.Session(connection) subscribe = sr.Subscribe(session) subscribe.module_change_subscribe(module_name, module_change_callback) print_current_config(session, module_name) kafka_producer = create_producer(bootstrap_servers) sr.global_loop() except Exception as e: logging.exception("Exception occured") raise e
interfaces_get(holder) else: print("unsupported leaf node") # ================================= INITIALIZATION ============================================ try: yang_module_name = "host-numa-pci" print("\n\n ========== Plugin started ========== \n\n") # create a connection to sysrepod connection = sr.Connection("numa-pci-state-data plugin") # start a session using the newly opened connection session = sr.Session(connection) # subscribe to events in the current session subscription = sr.Subscribe(session) print("\n\n ========== Plugin subscription ========== \n\n") subscription.dp_get_items_subscribe( "/" + yang_module_name + ":numa-topology", host_numa_pci_state_data, None, sr.SR_SUBSCR_DEFAULT) subscription.dp_get_items_subscribe( "/" + yang_module_name + ":host-interfaces", host_numa_pci_state_data, None, sr.SR_SUBSCR_DEFAULT) sr.global_loop() subscription.unsubscribe() print("\n\n ========== Plugin exited ========== \n\n")
def __init__(self): self.logger = log("CassiniDataPlane") self.context = "CassiniDataPlane" self.conn = sr.Connection(self.context) self.sess = sr.Session(self.conn, sr.SR_DS_RUNNING) self.subscribe = sr.Subscribe(self.sess)