def sr_plugin_init_cb(session): ctx = {} ctx["yang_model"] = yang_model ctx["sess"] = session ctx["sub"] = sr.Subscribe(session) ctx["config_file"] = "network" ctx["uci_sections"] = ["interface"] ctx["map"] = table_sr_uci rc = load_startup_datastore(ctx) if sr.SR_ERR_OK != rc: return sr.SR_ERR_INTERNAL rc = sync_datastores(ctx) if sr.SR_ERR_OK != rc: return sr.SR_ERR_INTERNAL # subscribe for changes in running config */ ctx["sub"].module_change_subscribe(ctx["yang_model"], module_change_cb, ctx) print "\n\n ========== READING STARTUP CONFIG: ==========\n" try: print_current_config(session, yang_model); except Exception as e: print e print "\n\n ========== STARTUP CONFIG APPLIED AS RUNNING ==========\n" sr.global_loop() return rc
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
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 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 xrange(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("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 xrange(values.val_cnt()): sess.delete_item(values.val(i).xpath()) sess.commit()
except Exception as e: six.print_(e) try: module_name = "test" xpath = "/test:hello" # connect to sysrepo conn = sr.Connection("example_application") # start session sess = sr.Session(conn) # subscribe for changes in running config */ subscribe = sr.Subscribe(sess) six.print_("\n ========== SUBSCRIBE TO RPC CALL ==========") subscribe.rpc_subscribe(xpath, test_rpc_cb) in_vals = sr.Vals(1) in_vals.val(0).set(xpath + "/name", "Laura", sr.SR_STRING_T) six.print_("\n ========== START RPC CALL ==========") out_vals = sess.rpc_send(xpath, in_vals) six.print_("\n ========== PRINT RETURN VALUE ==========") for n in range(out_vals.val_cnt()): print_value(out_vals.val(n)) six.print_("\n ========== END PROGRAM ==========\n")