コード例 #1
0
ファイル: test_tree_data.py プロジェクト: rjarry/libyang
    def test_ly_data_node_insert_after(self):
        yang_folder = config.TESTS_DIR + "/api/files"
        config_file = config.TESTS_DIR + "/api/files/a.xml"

        try:
            # Setup
            ctx = ly.Context(yang_folder)
            self.assertIsNotNone(ctx)
            ctx.parse_module_mem(lys_module_a, ly.LYS_IN_YIN)
            root = ctx.parse_data_path(config_file, ly.LYD_XML,
                                       ly.LYD_OPT_CONFIG | ly.LYD_OPT_STRICT)
            self.assertIsNotNone(root)

            # Tests
            last = root.next()
            new_node = ly.Data_Node(None, root.schema().module(), "y", "test")
            self.assertIsNotNone(new_node)
            rc = root.insert_after(new_node)
            self.assertEqual(0, rc)
            self.assertNotEqual(last.schema().name(),
                                root.next().schema().name())
            self.assertEqual("y", root.next().schema().name())

        except Exception as e:
            self.fail(e)
コード例 #2
0
def _create_child(ctx, parent, child_def):
    at = child_def['info']['module'].find('@')
    if at == -1:
        module = ctx.get_module(child_def['info']['module'])
    else:
        module = ctx.get_module(child_def['info']['module'][:at],
                                child_def['info']['module'][at + 1:])
    if child_def['info']['type'] == 4:
        yang.Data_Node(parent, module, child_def['info']['name'],
                       child_def['value'])
    elif child_def['info']['type'] == 8:
        yang.Data_Node(parent, module, child_def['info']['name'],
                       child_def['value'][0])
    else:
        child = yang.Data_Node(parent, module, child_def['info']['name'])
        if 'children' in child_def:
            for grandchild in child_def['children']:
                _create_child(ctx, child, grandchild)
コード例 #3
0
ファイル: test_tree_data.py プロジェクト: rjarry/libyang
    def test_ly_data_node_schema_sort(self):
        yang_folder = config.TESTS_DIR + "/api/files"
        config_file = config.TESTS_DIR + "/api/files/a.xml"

        try:
            # Setup
            ctx = ly.Context(yang_folder)
            self.assertIsNotNone(ctx)
            ctx.parse_module_mem(lys_module_a, ly.LYS_IN_YIN)
            ctx.parse_data_path(config_file, ly.LYD_XML,
                                ly.LYD_OPT_CONFIG | ly.LYD_OPT_STRICT)
            mod = ctx.get_module("a", None, 1)
            self.assertIsNotNone(mod)

            # Tests
            root = ly.Data_Node(None, mod, "l")
            self.assertIsNotNone(root)
            node = ly.Data_Node(root, mod, "key1", "1")
            self.assertIsNotNone(node)
            node = ly.Data_Node(root, mod, "key2", "2")
            self.assertIsNotNone(node)

            node = ly.Data_Node(None, mod, "x")
            self.assertIsNotNone(node)
            rc = root.insert_after(node)
            self.assertEqual(0, rc)
            node = root.next()

            node2 = ly.Data_Node(node, mod, "bubba", "a")
            self.assertIsNotNone(node2)
            node2 = ly.Data_Node(node, mod, "bar-gggg", "b")
            self.assertIsNotNone(node2)
            node2 = ly.Data_Node(node, mod, "number64", "64")
            self.assertIsNotNone(node2)
            node2 = ly.Data_Node(node, mod, "number32", "32")
            self.assertIsNotNone(node2)

            rc = root.schema_sort(1)
            self.assertEqual(0, rc)

            root = node
            self.assertEqual("x", root.schema().name())
            self.assertEqual("l", root.next().schema().name())

            self.assertEqual("bar-gggg", root.child().schema().name())
            self.assertEqual("bubba", root.child().next().schema().name())
            self.assertEqual("number32",
                             root.child().next().next().schema().name())
            self.assertEqual("number64",
                             root.child().next().next().next().schema().name())

        except Exception as e:
            self.fail(e)
コード例 #4
0
ファイル: connections.py プロジェクト: jeson1981/Netopeer2GUI
def _create_child(ctx, parent, child_def):
	at = child_def['info']['module'].find('@')
	if at == -1:
		module = ctx.get_module(child_def['info']['module'])
	else:
		module = ctx.get_module(child_def['info']['module'][:at], child_def['info']['module'][at + 1:])
	# print('child: ' + json.dumps(child_def))
	# print('parent: ' + parent.schema().name())
	# print('module: ' + module.name())
	# print('name: ' + child_def['info']['name'])
	if child_def['info']['type'] == 4 :
		# print('value: ' + str(child_def['value']))
		yang.Data_Node(parent, module, child_def['info']['name'], child_def['value'])
	elif child_def['info']['type'] == 8:
		# print('value: ' + child_def['value'][0])
		yang.Data_Node(parent, module, child_def['info']['name'], child_def['value'][0])
	else:
		child = yang.Data_Node(parent, module, child_def['info']['name'])
		if 'children' in child_def:
			for grandchild in child_def['children']:
				_create_child(ctx, child, grandchild)
コード例 #5
0
ファイル: test_tree_data.py プロジェクト: rjarry/libyang
    def test_ly_data_node_new_path(self):
        yang_folder = config.TESTS_DIR + "/api/files"
        config_file = config.TESTS_DIR + "/api/files/a.xml"
        try:
            # Setup
            ctx = ly.Context(yang_folder)
            self.assertIsNotNone(ctx)
            ctx.parse_module_mem(lys_module_a, ly.LYS_IN_YIN)
            ctx.parse_data_path(config_file, ly.LYD_XML,
                                ly.LYD_OPT_CONFIG | ly.LYD_OPT_STRICT)
            mod = ctx.get_module("a", None, 1)
            self.assertIsNotNone(mod)

            # Tests
            root = ly.Data_Node(ctx, "/a:x/bar-gggg", "a", 0, 0)
            self.assertIsNotNone(root)
            self.assertEqual("x", root.schema().name())
            self.assertEqual("bar-gggg", root.child().schema().name())

            node = root.new_path(ctx, "def-leaf", "def", 0,
                                 ly.LYD_PATH_OPT_DFLT)
            self.assertIsNotNone(node)
            self.assertEqual("def-leaf", node.schema().name())
            self.assertEqual(1, node.dflt())

            node = root.new_path(ctx, "def-leaf", "def", 0, 0)
            self.assertIsNotNone(node)
            self.assertEqual("def-leaf", node.schema().name())
            self.assertEqual(0, node.dflt())

            node = root.new_path(ctx, "bubba", "b", 0, 0)
            self.assertIsNotNone(node)
            self.assertEqual("bubba", node.schema().name())

            node = root.new_path(ctx, "/a:x/number32", "3", 0, 0)
            self.assertIsNotNone(node)
            self.assertEqual("number32", node.schema().name())

            node = root.new_path(ctx, "/a:l[key1='1'][key2='2']/value", None,
                                 0, 0)
            self.assertIsNotNone(node)
            self.assertEqual("l", node.schema().name())
            self.assertEqual("key1", node.child().schema().name())
            self.assertEqual("key2", node.child().next().schema().name())
            self.assertEqual("value",
                             node.child().next().next().schema().name())

        except Exception as e:
            self.fail(e)
コード例 #6
0
ファイル: test_tree_data.py プロジェクト: rjarry/libyang
    def test_ly_data_node_leaf(self):
        yang_folder = config.TESTS_DIR + "/api/files"
        config_file = config.TESTS_DIR + "/api/files/a.xml"

        try:
            ctx = ly.Context(yang_folder)
            self.assertIsNotNone(ctx)
            ctx.parse_module_mem(lys_module_a, ly.LYS_IN_YIN)
            root = ctx.parse_data_path(config_file, ly.LYD_XML,
                                       ly.LYD_OPT_CONFIG | ly.LYD_OPT_STRICT)
            self.assertIsNotNone(root)

            new_node = ly.Data_Node(root,
                                    root.schema().module(), "number32", "100")
            self.assertIsNotNone(new_node)

        except Exception as e:
            self.fail(e)
コード例 #7
0
ファイル: test_tree_data.py プロジェクト: rjarry/libyang
    def test_ly_data_node_anydata(self):
        yang_folder = config.TESTS_DIR + "/api/files"
        config_file = config.TESTS_DIR + "/api/files/a.xml"

        try:
            ctx = ly.Context(yang_folder)
            self.assertIsNotNone(ctx)
            ctx.parse_module_mem(lys_module_a, ly.LYS_IN_YIN)
            root = ctx.parse_data_path(config_file, ly.LYD_XML,
                                       ly.LYD_OPT_CONFIG | ly.LYD_OPT_STRICT)
            self.assertIsNotNone(root)
            mod = ctx.get_module("a", None, 1)

            new_node = ly.Data_Node(root, mod, "any-data", "100",
                                    ly.LYD_ANYDATA_CONSTSTRING)
            self.assertIsNotNone(new_node)

        except Exception as e:
            self.fail(e)
コード例 #8
0
ファイル: test_tree_data.py プロジェクト: rjarry/libyang
    def test_ly_data_node_unlink(self):
        yang_folder = config.TESTS_DIR + "/api/files"
        config_file = config.TESTS_DIR + "/api/files/a.xml"
        try:
            # Setup
            ctx = ly.Context(yang_folder)
            self.assertIsNotNone(ctx)
            ctx.parse_module_mem(lys_module_a, ly.LYS_IN_YIN)
            root = ctx.parse_data_path(config_file, ly.LYD_XML,
                                       ly.LYD_OPT_CONFIG | ly.LYD_OPT_STRICT)
            self.assertIsNotNone(root)

            # Tests
            node = root.child()
            new = ly.Data_Node(root, node.schema().module(), "number32", "1")
            self.assertIsNotNone(new)
            rc = root.insert(new)
            self.assertEqual(0, rc)

            schema = node.prev().schema()
            if (ly.LYS_LEAF == schema.nodetype()
                    or ly.LYS_LEAFLIST == schema.nodetype()):
                casted = node.prev().subtype()
                self.assertEqual("1", casted.value_str())
            else:
                self.fail()

            rc = node.prev().unlink()
            self.assertEqual(0, rc)
            schema = node.prev().schema()
            if (ly.LYS_LEAF == schema.nodetype()
                    or ly.LYS_LEAFLIST == schema.nodetype()):
                self.fail()
            else:
                return

        except Exception as e:
            self.fail(e)
コード例 #9
0
ファイル: test_tree_data.py プロジェクト: rjarry/libyang
    def test_ly_data_node_validate(self):
        yang_folder = config.TESTS_DIR + "/api/files"
        config_file = config.TESTS_DIR + "/api/files/a.xml"
        try:
            # Setup
            ctx = ly.Context(yang_folder)
            self.assertIsNotNone(ctx)
            ctx.parse_module_mem(lys_module_a, ly.LYS_IN_YIN)
            root = ctx.parse_data_path(config_file, ly.LYD_XML,
                                       ly.LYD_OPT_CONFIG | ly.LYD_OPT_STRICT)
            self.assertIsNotNone(root)

            # Tests
            rc = root.validate(ly.LYD_OPT_CONFIG, ctx)
            self.assertEqual(0, rc)
            new = ly.Data_Node(root, root.schema().module(), "number32", "1")
            self.assertIsNotNone(new)
            rc = root.insert(new)
            self.assertEqual(0, rc)
            rc = root.validate(ly.LYD_OPT_CONFIG, ctx)
            self.assertEqual(0, rc)

        except Exception as e:
            self.fail(e)
コード例 #10
0
def session_commit():
    session = auth.lookup(request.headers.get('lgui-Authorization', None))
    user = session['user']

    req = request.get_json(keep_order=True)
    if 'key' not in req:
        return json.dumps({
            'success': False,
            'code': 500,
            'message': 'Missing session key.'
        })
    if 'modifications' not in req:
        return json.dumps({
            'success': False,
            'code': 500,
            'message': 'Missing modifications.'
        })

    mods = req['modifications']
    ctx = sessions[user.username][req['key']]['session'].context
    root = None
    reorders = []
    for key in mods:
        recursion = False
        # get correct path and value if needed
        path = key
        value = None
        if mods[key]['type'] == 'change':
            value = mods[key]['value']
        elif mods[key]['type'] == 'create' or mods[key]['type'] == 'replace':
            if mods[key]['data']['info']['type'] == 1:
                # creating/replacing container
                recursion = True
            elif mods[key]['data']['info']['type'] == 4:
                # creating/replacing leaf
                value = mods[key]['data']['value']
            elif mods[key]['data']['info']['type'] == 8:
                # creating/replacing leaf-list
                value = mods[key]['data']['value'][0]
                path = mods[key]['data']['path']
            elif mods[key]['data']['info']['type'] == 16:
                recursion = True
                path = mods[key]['data']['path']
        elif mods[key]['type'] == 'reorder':
            # postpone reorders
            reorders.extend(mods[key]['transactions'])
            continue

        # create node
        # print("creating " + path)
        # print("value " + str(value))
        if root:
            root.new_path(ctx, path, value, 0, 0)
        else:
            try:
                root = yang.Data_Node(ctx, path, value, 0, 0)
            except Exception as e:
                print(e)
                return json.dumps({
                    'success':
                    False,
                    'code':
                    404,
                    'message':
                    'Path not found in session context.'
                })
        node = root.find_path(path).data()[0]

        # set operation attribute and add additional data if any
        if mods[key]['type'] == 'change':
            node.insert_attr(None, 'ietf-netconf:operation', 'merge')
        elif mods[key]['type'] == 'delete':
            node.insert_attr(None, 'ietf-netconf:operation', 'delete')
        elif mods[key]['type'] == 'create':
            node.insert_attr(None, 'ietf-netconf:operation', 'create')
        elif mods[key]['type'] == 'replace':
            node.insert_attr(None, 'ietf-netconf:operation', 'replace')
        else:
            return json.dumps({
                'success': False,
                'error-msg': 'Invalid modification ' + key
            })

        if recursion and 'children' in mods[key]['data']:
            for child in mods[key]['data']['children']:
                if 'key' in child['info'] and child['info']['key']:
                    continue
                _create_child(ctx, node, child)

    # finally process reorders which must be last since they may refer newly created nodes
    # and they do not reflect removed nodes
    for move in reorders:
        try:
            node = root.find_path(move['node']).data()[0]
            parent = node.parent()
            node.unlink()
            if parent:
                parent.insert(node)
            else:
                root.insert_sibling(node)
        except Exception as e:
            if root:
                root.new_path(ctx, move['node'], None, 0, 0)
            else:
                root = yang.Data_Node(ctx, move['node'], None, 0, 0)
            node = root.find_path(move['node']).data()[0]
        node.insert_attr(None, 'yang:insert', move['insert'])
        if move['insert'] == 'after' or move['insert'] == 'before':
            if 'key' in move:
                node.insert_attr(None, 'yang:key', move['key'])
            elif 'value' in move:
                node.insert_attr(None, 'yang:value', move['value'])

    # print(root.print_mem(yang.LYD_XML, yang.LYP_FORMAT))
    try:
        sessions[user.username][req['key']]['session'].rpcEditConfig(
            nc.DATASTORE_RUNNING, root)
    except nc.ReplyError as e:
        reply = {'success': False, 'code': 500, 'message': '[]'}
        for err in e.args[0]:
            reply['message'] += json.loads(str(err)) + '; '
        return json.dumps(reply)

    return json.dumps({'success': True})
コード例 #11
0
ファイル: editconfig.py プロジェクト: ahoopy/libnetconf2
ssh.setAuthHostkeyCheckClb(hostkey_check)

#
# create NETCONF session to the server
#
try:
    session = nc.Session(host, port, ssh)
except Exception as e:
    print(e)
    sys.exit(1)

# prepare config content as string or data tree
tm = session.context.get_module("turing-machine")
# config = "<turing-machine xmlns=\"http://example.net/turing-machine\"><transition-function><delta><label>left summand</label><input><state>0</state></input></delta></transition-function></turing-machine>"
config = yang.Data_Node(
    session.context,
    "/turing-machine:turing-machine/transition-function/delta[label='left summand']/input/state",
    "5", 0, 0)

# perform <edit-config> and print result
try:
    session.rpcEditConfig(nc.DATASTORE_RUNNING, config)
except nc.ReplyError as e:
    reply = {'success': False, 'error': []}
    for err in e.args[0]:
        reply['error'].append(json.loads(str(err)))
    print(json.dumps(reply))
    sys.exit(1)

# print(data.print_mem(ly.LYD_XML, ly.LYP_FORMAT | ly.LYP_WITHSIBLINGS))