blob: ba866ca6a1db991d987c7ce2471baccc69bc14a8 [file] [log] [blame]
Radek Krejcia1339602017-11-02 13:52:38 +01001"""
2NETCONF data helper functions
3File: data.py
4Author: Radek Krejci <rkrejci@cesnet.cz>
5"""
6
7import json
8import os
9
10import libyang as ly
11import netconf2 as nc
12
13def dataInfoNode(node, recursion=False):
14 schema = node.schema()
15 casted = node.subtype()
16
17 if node.dflt():
18 return None
19
20 info = {}
21 info["type"] = schema.nodetype()
22 info["module"] = schema.module().name()
23 info["name"] = schema.name()
24 info["dsc"] = schema.dsc()
25 info["config"] = True if schema.flags() & ly.LYS_CONFIG_W else False
26
27 result = {}
28 if info["type"] == ly.LYS_LEAF or info["type"] == ly.LYS_LEAFLIST:
29 result["value"] = casted.value_str()
30 elif recursion:
31 result["children"] = []
32 if node.child():
33 for child in node.child().tree_for():
34 childNode = dataInfoNode(child, True)
35 if not childNode:
36 continue
37 if not child.next():
38 childNode["last"] = True
39 result["children"].append(childNode)
40 result["info"] = info
41 result["path"] = node.path()
42
43 return result
44
45def dataInfoSubtree(data, path, recursion=False):
46 try:
47 node = data.find_path(path).data()[0]
48 except:
49 return(json.dumps({'success': False, 'error-msg': 'Invalid data path.'}))
50
51 result = dataInfoNode(node)
52 if not result:
53 return(json.dumps({'success': False, 'error-msg': 'Path refers to a default node.'}))
54
55 result["children"] = []
56 if node.child():
57 for child in node.child().tree_for():
58 childNode = dataInfoNode(child, recursion)
59 if not childNode:
60 continue
61 if not child.next():
62 childNode["last"] = True
63 result["children"].append(childNode)
64
65 return(json.dumps({'success': True, 'data': result}))
66
67
68def dataInfoRoots(data, recursion=False):
69 result = []
70 for root in data.tree_for():
71 rootNode = dataInfoNode(root, recursion)
72 if not rootNode:
73 continue
74 if not root.next():
75 rootNode["last"] = True
76 result.append(rootNode)
77
78 return(json.dumps({'success': True, 'data': result}))