예제 #1
0
def create_example_module():
    connection = sr.Connection(sr.SR_CONN_DEFAULT)
    try:
        connection.install_module(
            os.environ['TESTS_DIR'] + "/files/example-module.yang",
            os.environ['TESTS_DIR'] + "/files", [])
    except Exception as e:
        print(e)
        connection = None
        return False

    connection = None

    try:
        connection = sr.Connection(sr.SR_CONN_DEFAULT)
        session = sr.Session(connection, sr.SR_DS_STARTUP)

        delete_all_items_example(session)
        v = sr.Val("Leaf value", sr.SR_STRING_T)
        session.set_item(
            "/example-module:container/list[key1='key1'][key2='key2']/leaf", v)
        session.apply_changes()
        session.session_stop()

        session = sr.Session(connection, sr.SR_DS_RUNNING)
        session.copy_config(sr.SR_DS_STARTUP)
        session.session_stop()

        connection = None
        return True
    except Exception as e:
        print(e)
        connection = None
        return False
예제 #2
0
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)
예제 #3
0
 def test_move_last_first(self):
     conn = sr.Connection("move_test4")
     self.session = sr.Session(conn, sr.SR_DS_STARTUP)
     self.session.move_item("/test-module:user[name='nameC']",
                            sr.SR_MOVE_LAST)
     items = self.session.get_items("/test-module:user")
     self.compareListItems(items, ["A", "B", "D", "C"])
예제 #4
0
    def setUp(self):
        if not TestModule.create_referenced_data_module():
            self.remove_modules(self)
            self.skipTest(self, "Test environment is not clean!")
            print("Environment is not clean!")
            return
        if not TestModule.create_test_module():
            self.remove_modules(self)
            self.skipTest(self, "Test environment is not clean!")
            print("Environment is not clean!")
            return
        conn = sr.Connection(sr.SR_CONN_DEFAULT)
        session = sr.Session(conn, sr.SR_DS_STARTUP)
        session.delete_item("/test-module:user[name='A']")
        session.delete_item("/test-module:user[name='B']")
        session.delete_item("/test-module:user[name='C']")
        session.delete_item("/test-module:user[name='D']")
        session.apply_changes()
        session.set_item("/test-module:user[name='A']",
                         sr.Val(None, sr.SR_LIST_T))
        session.set_item("/test-module:user[name='B']",
                         sr.Val(None, sr.SR_LIST_T))
        session.set_item("/test-module:user[name='C']",
                         sr.Val(None, sr.SR_LIST_T))
        session.set_item("/test-module:user[name='D']",
                         sr.Val(None, sr.SR_LIST_T))
        session.apply_changes()

        session.session_stop()

        conn = None
예제 #5
0
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)
예제 #6
0
def perf_set_delete_100_test(state, op_num, items):

    conn = state["connection"]
    assert conn is not None, "Unable to get connection."
    sess = sr.Session(conn, state['datastore'])
    assert sess is not None, "Unable to get session."

    xpath = "/example-module:container/list[key1='set_del'][key2='set_1']/leaf"

    sess.apply_changes()

    for i in range(op_num):
        sess.apply_changes()
        for j in range(100):
            xpath = "/example-module:container/list[key1='set_del'][key2='set_" + str(
                j) + "']/leaf"
            val = sr.Val("Leaf", sr.SR_STRING_T)
            sess.set_item(xpath, val)
        sess.apply_changes()
        for j in range(100):
            xpath = "/example-module:container/list[key1='set_del'][key2='set_" + str(
                j) + "']/leaf"
            sess.delete_item(xpath)
        sess.apply_changes()

    return 100 * 1 * 3 * 2
예제 #7
0
 def test_move_after_unknown(self):
     conn = sr.Connection("move_test3")
     self.session = sr.Session(conn, sr.SR_DS_STARTUP)
     with self.assertRaises(RuntimeError):
         self.session.move_item("/test-module:user[name='nameB']",
                                sr.SR_MOVE_AFTER,
                                "/test-module:user[name='nameXY']")
예제 #8
0
 def test_move_before_first(self):
     conn = sr.Connection("move_test2")
     self.session = sr.Session(conn, sr.SR_DS_STARTUP)
     self.session.move_item("/test-module:user[name='nameC']",
                            sr.SR_MOVE_BEFORE,
                            "/test-module:user[name='nameA']")
     items = self.session.get_items("/test-module:user")
     self.compareListItems(items, ["C", "A", "B", "D"])
예제 #9
0
def create_example_module():
    connection = sr.Connection("test-module")

    session = sr.Session(connection, sr.SR_DS_STARTUP)
    session.delete_item("/example-module:*")
    v = sr.Val("Leaf value", sr.SR_STRING_T)
    session.set_item("/example-module:container/list[key1='key1'][key2='key2']/leaf", v)
    session.commit()
예제 #10
0
    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()
예제 #11
0
 def setUp(self):
     conn = sr.Connection("move_test")
     session = sr.Session(conn, sr.SR_DS_STARTUP)
     session.delete_item("/test-module:*")
     session.set_item("/test-module:user[name='nameA']", None)
     session.set_item("/test-module:user[name='nameB']", None)
     session.set_item("/test-module:user[name='nameC']", None)
     session.set_item("/test-module:user[name='nameD']", None)
     session.commit()
예제 #12
0
    def test_move_last_first(self):
        conn = sr.Connection(sr.SR_CONN_DEFAULT)
        self.session = sr.Session(conn, sr.SR_DS_STARTUP)
        self.session.move_item("/test-module:user[name='C']", sr.SR_MOVE_LAST)
        items = self.session.get_items("/test-module:user")
        self.compareListItems(items, ["A", "B", "D", "C"])
        self.session.session_stop()

        conn = None
예제 #13
0
 def test_move_after_last(self):
     conn = sr.Connection("move_test1")
     self.session = sr.Session(conn, sr.SR_DS_STARTUP)
     print("move")
     self.session.move_item("/test-module:user[name='nameB']",
                            sr.SR_MOVE_AFTER,
                            "/test-module:user[name='nameD']")
     items = self.session.get_items("/test-module:user")
     self.compareListItems(items, ["A", "C", "D", "B"])
예제 #14
0
 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()
예제 #15
0
    def __init__(self, sess=None, default_prompt='> ', prefix=''):
        if sess == None:
            conn = sr.Connection()
            sess = sr.Session(conn)
        self.context = Root(sess)

        self.completer = GoldstoneShellCompleter(self.context)
        self.default_input = ''
        self.default_prompt = default_prompt
        self.prefix = prefix
예제 #16
0
 def restartConnection(self):
     try:
         self.sr = sr.Connection(self.name, self.conn)
     except RuntimeError as r:
         if self.conn == sr.SR_CONN_DAEMON_REQUIRED and r.message == "The peer disconnected":
             sleep(1) #wait for daemon to start
             self.sr = sr.Connection(self.name, self.conn)
         else:
             raise r
     self.session = sr.Session(self.sr, self.ds)
예제 #17
0
    def test_move_after_unknown(self):
        conn = sr.Connection(sr.SR_CONN_DEFAULT)
        self.session = sr.Session(conn, sr.SR_DS_STARTUP)
        with self.assertRaises(RuntimeError):
            self.session.move_item("/test-module:user[name='B']",
                                   sr.SR_MOVE_AFTER, "[name='XY']", "B")
            self.session.apply_changes()
        self.session.session_stop()

        conn = None
예제 #18
0
    def test_move_before_first(self):
        conn = sr.Connection(sr.SR_CONN_DEFAULT)
        self.session = sr.Session(conn, sr.SR_DS_STARTUP)
        self.session.move_item("/test-module:user[name='C']",
                               sr.SR_MOVE_BEFORE, "[name='A']", "C")
        self.session.apply_changes()
        items = self.session.get_items("/test-module:user")
        self.compareListItems(items, ["C", "A", "B", "D"])
        self.session.session_stop()

        conn = None
예제 #19
0
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)
예제 #20
0
def perf_get_subtrees_test(state, op_num, items):

    conn = state["connection"]
    assert conn is not None, "Unable to get connection."
    sess = sr.Session(conn, state['datastore'])
    assert sess is not None, "Unable to get session."

    xpath = "/example-module:container/list[key1='key0'][key2='key0']/leaf"
    for i in range(op_num):
        trees = sess.get_subtree(xpath).tree_for()
        assert trees[0] is not None, "check if empty"
    return len(trees)
예제 #21
0
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)
예제 #22
0
    def test_move_after_last(self):
        conn = sr.Connection(sr.SR_CONN_DEFAULT)
        self.session = sr.Session(conn, sr.SR_DS_STARTUP)
        items = self.session.get_items("/test-module:user")
        self.compareListItems(items, ["A", "B", "C", "D"])
        self.session.move_item("/test-module:user[name='B']", sr.SR_MOVE_AFTER,
                               "[name='D']", "B")
        self.session.apply_changes()
        items = self.session.get_items("/test-module:user")
        self.compareListItems(items, ["A", "C", "D", "B"])
        self.session.session_stop()

        conn = None
예제 #23
0
 def test_commit_empty(self):
     TestModule.create_test_module()
     connection = sr.Connection("name")
     session = sr.Session(self.conn, sr.SR_DS_STARTUP)
     v_old = self.session.get_item("/test-module:main/string")
     self.session.delete_item("/test-module:*")
     self.session.commit()
     #test random leaf that was deleted
     v_none = self.session.get_item("/test-module:main/string")
     self.assertIsNone(v_none)
     self.session.set_item("/test-module:main/string", v_old)
     self.session.commit()
     TestModule.create_test_module()
예제 #24
0
def execute_action(conn, key_id, action):
    sess = sr.Session(conn)
    try:
        cur_state = sess.get_item(xpath_of(
            key_id, 'current-status')).data().get_enum()
        next_state_str = STATE_MACHINE[cur_state]['transitions'].get(
            action, None)
        if next_state_str:
            handle_set_state(conn, key_id, next_state_str)
        sess.delete_item(xpath_of(key_id, 'action'))
        sess.commit()
    finally:
        sess.session_stop()
def perf_get_subtree_with_data_load_test(state, op_num, items):

    conn = state["connection"]
    assert conn is not None, "Unable to get connection."

    xpath = "/example-module:container/list[key1='key0'][key2='key0']/leaf"

    for i in xrange(op_num):
        sess = sr.Session(conn, state['datastore'])
        tree = sess.get_subtree(xpath)
        assert tree is not None, "check if empty"

    return 1
def perf_get_items_test(state, op_num, items):

    conn = state["connection"]
    assert conn is not None, "Unable to get connection."
    sess = sr.Session(conn, state['datastore'])
    assert sess is not None, "Unable to get session."

    xpath = "/example-module:container/list/leaf"

    for i in xrange(op_num):
        val = sess.get_items(xpath)

    return 1
def perf_get_item_first_test(state, op_num, items):

    conn = state["connection"]
    assert conn is not None, "Unable to get connection."
    sess = sr.Session(conn, state['datastore'])
    assert sess is not None, "Unable to get session."

    xpath = "/example-module:container"

    for i in xrange(op_num):
        val = sess.get_item(xpath)
        assert val.type() is sr.SR_CONTAINER_T, "check value type"

    return 1
def perf_get_subtrees_test(state, op_num, items):

    conn = state["connection"]
    assert conn is not None, "Unable to get connection."
    sess = sr.Session(conn, state['datastore'])
    assert sess is not None, "Unable to get session."

    xpath = "/example-module:container/list/leaf"

    for i in xrange(op_num):
        trees = sess.get_subtrees(xpath)
        assert trees.tree(0) is not None, "check if empty"

    return trees.tree_cnt()
def perf_get_item_with_data_load_test(state, op_num, items):

    conn = state["connection"]
    assert conn is not None, "Unable to get connection."

    xpath = "/example-module:container/list[key1='key0'][key2='key0']/leaf"

    for i in xrange(op_num):
        sess = sr.Session(conn, state['datastore'])

        val = sess.get_item(xpath)
        assert val.type() is sr.SR_STRING_T, "check value type"

    return 1
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()