def subscribe(): conn = sr.Connection("subscriber") sess = sr.Session(conn, sr.SR_DS_RUNNING) subscribe1 = sr.Subscribe(sess) subscribe1.module_change_subscribe('nemea-test-1', change_cb) subscribe2 = sr.Subscribe(sess) subscribe2.module_change_subscribe('link-traffic-test-1', change_cb) sr.global_loop()
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 range(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 createDataTreeLargeIETFinterfacesModule(count, datastore): """ Add data to ietf-interfaces. """ conn = sr.Connection("load test") 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.commit()
def clearDataTree(module_name, datastore): """ Clear yang model. """ conn = sr.Connection("clear") 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.commit()
def module_change_cb(sess, module_name, event, private_ctx): print( "\n\n ========== CONFIG HAS CHANGED, CURRENT RUNNING CONFIG: ==========\n" ) try: print("\n\n ========== Notification " + ev_to_str(event) + " =============================================\n") if (sr.SR_EV_APPLY == event): print( "\n ========== CONFIG HAS CHANGED, CURRENT RUNNING CONFIG: ==========\n" ) print_current_config(sess, module_name) print( "\n ========== CHANGES: =============================================\n" ) change_path = "/", module_name + ":*" subscribe = sr.Subscribe(sess) it = subscribe.get_changes_iter(change_path) while True: change = subscribe.get_change_next(it) if change == None: break print_change(change.oper(), change.old_val(), change.new_val()) print( "\n\n ========== END OF CHANGES =======================================\n" ) except Exception as e: print(e) return sr.SR_ERR_OK
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()