Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ffe7504

Browse files
committedMar 3, 2025
IGNORE THIS COMMIT: merge libyang3 step 3 (PR sonic-net#21716)
1 parent abf8a55 commit ffe7504

File tree

7 files changed

+260
-254
lines changed

7 files changed

+260
-254
lines changed
 

‎rules/sonic-yang-mgmt-py3.mk

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
SONIC_YANG_MGMT_PY3 = sonic_yang_mgmt-1.0-py3-none-any.whl
44
$(SONIC_YANG_MGMT_PY3)_SRC_PATH = $(SRC_PATH)/sonic-yang-mgmt
55
$(SONIC_YANG_MGMT_PY3)_PYTHON_VERSION = 3
6-
$(SONIC_YANG_MGMT_PY3)_DEBS_DEPENDS = $(LIBYANG) $(LIBYANG_CPP) $(LIBYANG_PY3)
6+
$(SONIC_YANG_MGMT_PY3)_DEBS_DEPENDS = $(LIBYANG3) $(LIBYANG3_PY3)
77
$(SONIC_YANG_MGMT_PY3)_DEPENDS = $(SONIC_YANG_MODELS_PY3)
8-
$(SONIC_YANG_MGMT_PY3)_RDEPENDS = $(SONIC_YANG_MODELS_PY3) $(LIBYANG) \
9-
$(LIBYANG_CPP) $(LIBYANG_PY3)
8+
$(SONIC_YANG_MGMT_PY3)_RDEPENDS = $(SONIC_YANG_MODELS_PY3) $(LIBYANG3) \
9+
$(LIBYANG3_PY3)
1010

1111
SONIC_PYTHON_WHEELS += $(SONIC_YANG_MGMT_PY3)

‎src/sonic-yang-mgmt/sonic_yang.py

+165-198
Large diffs are not rendered by default.

‎src/sonic-yang-mgmt/sonic_yang_ext.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# class sonic_yang. A separate file is used to avoid a single large file.
33

44
from __future__ import print_function
5-
import yang as ly
5+
import libyang as ly
66
import syslog
77
from json import dump, dumps, loads
88
from xmltodict import parse
@@ -86,7 +86,7 @@ def _loadJsonYangModel(self):
8686
for f in self.yangFiles:
8787
m = self.ctx.get_module(f)
8888
if m is not None:
89-
xml = m.print_mem(ly.LYD_JSON, ly.LYP_FORMAT)
89+
xml = m.print_mem("yin")
9090
self.yJson.append(parse(xml))
9191
self.sysLog(msg="Parsed Json for {}".format(m.name()))
9292
except Exception as e:
@@ -328,7 +328,7 @@ def _fillLeafDictUses(self, uses_s, table, leafDict):
328328
Parameters:
329329
uses_s (str): uses statement in yang module.
330330
table (str): config DB table, this table is being translated.
331-
leafDict (dict): dict with leaf(s) information for List\Container
331+
leafDict (dict): dict with leaf(s) information for List Container
332332
corresponding to config DB table.
333333
334334
Returns:
@@ -369,7 +369,7 @@ def _createLeafDict(self, model, table):
369369
table (str): config DB table, this table is being translated.
370370
371371
Returns:
372-
leafDict (dict): dict with leaf(s) information for List\Container
372+
leafDict (dict): dict with leaf(s) information for List Container
373373
corresponding to config DB table.
374374
'''
375375
leafDict = dict()
@@ -1174,8 +1174,7 @@ def loadData(self, configdbJson, debug=False):
11741174
self._xlateConfigDB(xlateFile=xlateFile)
11751175
#print(self.xlateJson)
11761176
self.sysLog(msg="Try to load Data in the tree")
1177-
self.root = self.ctx.parse_data_mem(dumps(self.xlateJson), \
1178-
ly.LYD_JSON, ly.LYD_OPT_CONFIG|ly.LYD_OPT_STRICT)
1177+
self.root = self.ctx.parse_data_mem(dumps(self.xlateJson), "json", no_state=True, strict=True, json_string_datatypes=True)
11791178

11801179
except Exception as e:
11811180
self.root = None
@@ -1195,7 +1194,7 @@ def getData(self, debug=False):
11951194
revXlateFile = None
11961195
if debug:
11971196
revXlateFile = "revXlateConfig.json"
1198-
self.xlateJson = loads(self._print_data_mem('JSON'))
1197+
self.xlateJson = loads(self._print_data_mem("JSON"))
11991198
# reset reverse xlate
12001199
self.revXlateJson = dict()
12011200
# result will be stored self.revXlateJson
@@ -1229,13 +1228,13 @@ def deleteNode(self, xpath):
12291228
# try to delete parent
12301229
nodeP = self._find_parent_data_node(xpath)
12311230
xpathP = nodeP.path()
1232-
if self._deleteNode(xpath=xpathP, node=nodeP) == False:
1231+
if self._deleteNode(xpath=xpathP) == False:
12331232
raise Exception('_deleteNode failed')
12341233
else:
12351234
return True
12361235

12371236
# delete non key element
1238-
if self._deleteNode(xpath=xpath, node=node) == False:
1237+
if self._deleteNode(xpath=xpath) == False:
12391238
raise Exception('_deleteNode failed')
12401239
except Exception as e:
12411240
self.sysLog(msg="deleteNode:{}".format(str(e)), \

‎src/sonic-yang-mgmt/tests/libyang-python-tests/config_data.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@
267267
"fc02:2000::2"
268268
],
269269
"container-in-list-test": {
270-
"leaf-1": true,
270+
"leaf-1": "up",
271271
"leaf-2": "test1",
272272
"mc-case-leaf-1": 55,
273273
"mc-case-leaf-3": 1234
@@ -281,7 +281,7 @@
281281
"2002:2001::2"
282282
],
283283
"container-in-list-test": {
284-
"leaf-1": false,
284+
"leaf-1": "down",
285285
"leaf-2": "test2",
286286
"mc-case-leaf-2": 77,
287287
"mc-case-leaf-3": 4321

‎src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/test-yang-structure.yang

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module test-yang-structure {
4545
leaf leaf-1 {
4646
description "test leaf in container";
4747
type string {
48-
pattern "false|true";
48+
pattern "down|up";
4949
}
5050
}
5151

‎src/sonic-yang-mgmt/tests/libyang-python-tests/test_SonicYang.json

+74-28
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,25 @@
2525
],
2626
"data_type" : [
2727
{
28-
"data_type" : "LY_TYPE_STRING",
29-
"xpath" : "/test-port:port/test-port:PORT/test-port:PORT_LIST/test-port:port_name"
28+
"data_type" : "string",
29+
"xpath" : "/test-port:port/PORT/PORT_LIST/port_name"
3030
},
3131
{
32-
"data_type" : "LY_TYPE_LEAFREF",
33-
"xpath" : "/test-vlan:vlan/test-vlan:VLAN_INTERFACE/test-vlan:VLAN_INTERFACE_LIST/test-vlan:vlanid"
32+
"data_type" : "leafref",
33+
"xpath" : "/test-vlan:vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST/vlanid"
3434
}
3535
],
3636
"delete_nodes" : [
3737
{
38-
"valid" : "False",
38+
"valid" : false,
3939
"xpath" : "/test-port:port/PORT/PORT_LIST[port_name='Ethernet10']/speed"
4040
},
4141
{
42-
"valid" : "True",
42+
"valid" : true,
4343
"xpath" : "/test-port:port/PORT/PORT_LIST[port_name='Ethernet9']/mtu"
4444
},
4545
{
46-
"valid" : "False",
46+
"valid" : false,
4747
"xpath" : "/test-port:port/PORT/PORT_LIST[port_name='Ethernet20']/mtu"
4848
}
4949
],
@@ -56,6 +56,52 @@
5656
],
5757
"xpath" : "/test-port:port/PORT/PORT_LIST[port_name='Ethernet8']/port_name"
5858
},
59+
{
60+
"dependencies" : [
61+
"/test-acl:acl/ACL_RULE/ACL_RULE_LIST[ACL_TABLE_NAME='PACL-V6'][RULE_NAME='Rule_20']/ACL_TABLE_NAME",
62+
"/test-acl:acl/ACL_RULE/ACL_RULE_LIST[ACL_TABLE_NAME='PACL-test'][RULE_NAME='rule_20']/ACL_TABLE_NAME",
63+
"/test-acl:acl/ACL_RULE/ACL_RULE_LIST[ACL_TABLE_NAME='PACL-V4'][RULE_NAME='Rule_20']/ACL_TABLE_NAME",
64+
"/test-acl:acl/ACL_RULE/ACL_RULE_LIST[ACL_TABLE_NAME='PACL-V4'][RULE_NAME='Rule_40']/ACL_TABLE_NAME"
65+
],
66+
"xpath" : "/test-acl:acl/ACL_TABLE"
67+
},
68+
{
69+
"dependencies" : [
70+
"/test-acl:acl/ACL_TABLE/ACL_TABLE_LIST[ACL_TABLE_NAME='PACL-V6']/ports[.='Ethernet8']",
71+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet2']/port",
72+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet3']/vlanid",
73+
"/test-vlan:vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='555'][ip-prefix='fe80::/10']/vlanid",
74+
"/test-interface:interface/INTERFACE/INTERFACE_LIST[interface='Ethernet8'][ip-prefix='10.1.1.64/26']/interface",
75+
"/test-vlan:vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='2000:f500:45:6709::/64']/vlanid",
76+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet5']/vlanid",
77+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet1']/port",
78+
"/test-acl:acl/ACL_TABLE/ACL_TABLE_LIST[ACL_TABLE_NAME='PACL-V4']/ports[.='Ethernet0']",
79+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet6']/port",
80+
"/test-acl:acl/ACL_TABLE/ACL_TABLE_LIST[ACL_TABLE_NAME='PACL-V4']/ports[.='Ethernet2']",
81+
"/test-acl:acl/ACL_TABLE/ACL_TABLE_LIST[ACL_TABLE_NAME='PACL-V6']/ports[.='Ethernet7']",
82+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet3']/port",
83+
"/test-interface:interface/INTERFACE/INTERFACE_LIST[interface='Ethernet8'][ip-prefix='2000:f500:40:a749::/126']/interface",
84+
"/test-acl:acl/ACL_RULE/ACL_RULE_LIST[ACL_TABLE_NAME='PACL-test'][RULE_NAME='rule_20']/ACL_TABLE_NAME",
85+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet0']/port",
86+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet5']/port",
87+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet1']/vlanid",
88+
"/test-vlan:vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='fe80::/10']/vlanid",
89+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet4']/port",
90+
"/test-acl:acl/ACL_RULE/ACL_RULE_LIST[ACL_TABLE_NAME='PACL-V4'][RULE_NAME='Rule_20']/ACL_TABLE_NAME",
91+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet2']/vlanid",
92+
"/test-acl:acl/ACL_TABLE/ACL_TABLE_LIST[ACL_TABLE_NAME='PACL-V6']/ports[.='Ethernet9']",
93+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet6']/vlanid",
94+
"/test-acl:acl/ACL_RULE/ACL_RULE_LIST[ACL_TABLE_NAME='PACL-V4'][RULE_NAME='Rule_40']/ACL_TABLE_NAME",
95+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet0']/vlanid",
96+
"/test-acl:acl/ACL_RULE/ACL_RULE_LIST[ACL_TABLE_NAME='PACL-V6'][RULE_NAME='Rule_20']/ACL_TABLE_NAME",
97+
"/test-vlan:vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='10.1.1.64/26']/vlanid",
98+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet4']/vlanid",
99+
"/test-acl:acl/ACL_TABLE/ACL_TABLE_LIST[ACL_TABLE_NAME='PACL-V4']/ports[.='Ethernet1']",
100+
"/test-vlan:vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='555'][ip-prefix='2000:f500:41:4e9::/64']/vlanid",
101+
"/test-vlan:vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='555'][ip-prefix='10.1.5.64/26']/vlanid"
102+
],
103+
"xpath" : "/"
104+
},
59105
{
60106
"dependencies" : [],
61107
"xpath" : "/test-port:port/PORT/PORT_LIST[port_name='Ethernet8']/alias"
@@ -67,11 +113,11 @@
67113
"xpath" : "/test-vlan:vlan/test-vlan:VLAN_INTERFACE/test-vlan:VLAN_INTERFACE_LIST/test-vlan:vlanid"
68114
},
69115
{
70-
"leafref_path" : "/test-port:port/test-port:PORT/test-port:PORT_LIST/test-port:port_name",
116+
"leafref_path" : "/port:port/port:PORT/port:PORT_LIST/port:port_name",
71117
"xpath" : "/test-interface:interface/test-interface:INTERFACE/test-interface:INTERFACE_LIST/test-interface:interface"
72118
},
73119
{
74-
"leafref_path" : "/test-port:port/test-port:PORT/test-port:PORT_LIST/test-port:port_name",
120+
"leafref_path" : "/port:port/port:PORT/port:PORT_LIST/port:port_name",
75121
"xpath" : "/test-vlan:vlan/test-vlan:VLAN_MEMBER/test-vlan:VLAN_MEMBER_LIST/test-vlan:port"
76122
},
77123
{
@@ -81,38 +127,38 @@
81127
],
82128
"leafref_type" : [
83129
{
84-
"data_type" : "LY_TYPE_UINT16",
130+
"data_type" : "uint16",
85131
"xpath" : "/test-vlan:vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='2000:f500:45:6709::/64']/vlanid"
86132
},
87133
{
88-
"data_type" : "LY_TYPE_STRING",
134+
"data_type" : "string",
89135
"xpath" : "/test-interface:interface/INTERFACE/INTERFACE_LIST[interface='Ethernet8'][ip-prefix='2000:f500:40:a749::/126']/interface"
90136
},
91137
{
92-
"data_type" : "LY_TYPE_STRING",
138+
"data_type" : "string",
93139
"xpath" : "/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet0']/port"
94140
},
95141
{
96-
"data_type" : "LY_TYPE_UINT16",
142+
"data_type" : "uint16",
97143
"xpath" : "/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST[vlanid='111'][port='Ethernet0']/vlanid"
98144
}
99145
],
100146
"leafref_type_schema" : [
101147
{
102-
"data_type" : "LY_TYPE_UINT16",
103-
"xpath" : "/test-vlan:vlan/test-vlan:VLAN_INTERFACE/test-vlan:VLAN_INTERFACE_LIST/test-vlan:vlanid"
148+
"data_type" : "uint16",
149+
"xpath" : "/test-vlan:vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST/vlanid"
104150
},
105151
{
106-
"data_type" : "LY_TYPE_STRING",
107-
"xpath" : "/test-interface:interface/test-interface:INTERFACE/test-interface:INTERFACE_LIST/test-interface:interface"
152+
"data_type" : "string",
153+
"xpath" : "/test-interface:interface/INTERFACE/INTERFACE_LIST/interface"
108154
},
109155
{
110-
"data_type" : "LY_TYPE_STRING",
111-
"xpath" : "/test-vlan:vlan/test-vlan:VLAN_MEMBER/test-vlan:VLAN_MEMBER_LIST/test-vlan:port"
156+
"data_type" : "string",
157+
"xpath" : "/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST/port"
112158
},
113159
{
114-
"data_type" : "LY_TYPE_UINT16",
115-
"xpath" : "/test-vlan:vlan/test-vlan:VLAN_MEMBER/test-vlan:VLAN_MEMBER_LIST/test-vlan:vlanid"
160+
"data_type" : "uint16",
161+
"xpath" : "/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST/vlanid"
116162
}
117163
],
118164
"members" : [
@@ -257,21 +303,21 @@
257303
"schema_dependencies" : [
258304
{
259305
"schema_dependencies" : [
260-
"/test-acl:acl/test-acl:ACL_TABLE/test-acl:ACL_TABLE_LIST/test-acl:ports",
261-
"/test-portchannel:portchannel/test-portchannel:PORTCHANNEL/test-portchannel:PORTCHANNEL_LIST/test-portchannel:members",
262-
"/test-interface:interface/test-interface:INTERFACE/test-interface:INTERFACE_LIST/test-interface:interface",
263-
"/test-vlan:vlan/test-vlan:VLAN_MEMBER/test-vlan:VLAN_MEMBER_LIST/test-vlan:port"
306+
"/test-acl:acl/ACL_TABLE/ACL_TABLE_LIST/ports",
307+
"/test-portchannel:portchannel/PORTCHANNEL/PORTCHANNEL_LIST/members",
308+
"/test-interface:interface/INTERFACE/INTERFACE_LIST/interface",
309+
"/test-vlan:vlan/VLAN_MEMBER/VLAN_MEMBER_LIST/port"
264310
],
265-
"xpath" : "/test-port:port/test-port:PORT/test-port:PORT_LIST/test-port:port_name"
311+
"xpath" : "/test-port:port/PORT/PORT_LIST/port_name"
266312
}
267313
],
268314
"schema_nodes" : [
269315
{
270-
"value" : "/test-vlan:vlan/test-vlan:VLAN_INTERFACE/test-vlan:VLAN_INTERFACE_LIST/test-vlan:family",
316+
"value" : "/test-vlan:vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST/family",
271317
"xpath" : "/test-vlan:vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='10.1.1.64/26']/family"
272318
},
273319
{
274-
"value" : "/test-port:port/test-port:PORT/test-port:PORT_LIST/test-port:speed",
320+
"value" : "/test-port:port/PORT/PORT_LIST/speed",
275321
"xpath" : "/test-port:port/PORT/PORT_LIST[port_name='Ethernet9']/speed"
276322
}
277323
],

‎src/sonic-yang-mgmt/tests/libyang-python-tests/test_sonic_yang.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,15 @@ def test_find_data_node_value(self, data, yang_s):
153153
for node in data['node_values']:
154154
xpath = str(node['xpath'])
155155
value = str(node['value'])
156-
print(xpath)
157-
print(value)
158156
val = yang_s._find_data_node_value(xpath)
159157
assert str(val) == str(value)
160158

161159
#test delete data node
162160
def test_delete_node(self, data, yang_s):
163161
for node in data['delete_nodes']:
164162
xpath = str(node['xpath'])
165-
yang_s._deleteNode(xpath)
163+
rv = yang_s._deleteNode(xpath)
164+
assert rv == node['valid']
166165

167166
#test set node's value
168167
def test_set_datanode_value(self, data, yang_s):
@@ -218,8 +217,7 @@ def test_find_schema_dependencies(self, yang_s, data):
218217
def test_merge_data_tree(self, data, yang_s):
219218
data_merge_file = data['data_merge_file']
220219
yang_dir = str(data['yang_dir'])
221-
yang_s._merge_data(data_merge_file, yang_dir)
222-
#yang_s.root.print_mem(ly.LYD_JSON, ly.LYP_FORMAT)
220+
yang_s._merge_data(data_merge_file)
223221

224222
#test get module prefix
225223
def test_get_module_prefix(self, yang_s, data):
@@ -234,17 +232,15 @@ def test_get_data_type(self, yang_s, data):
234232
for node in data['data_type']:
235233
xpath = str(node['xpath'])
236234
expected = node['data_type']
237-
expected_type = yang_s._str_to_type(expected)
238235
data_type = yang_s._get_data_type(xpath)
239-
assert expected_type == data_type
236+
assert expected == data_type
240237

241238
def test_get_leafref_type(self, yang_s, data):
242239
for node in data['leafref_type']:
243240
xpath = str(node['xpath'])
244241
expected = node['data_type']
245-
expected_type = yang_s._str_to_type(expected)
246242
data_type = yang_s._get_leafref_type(xpath)
247-
assert expected_type == data_type
243+
assert expected == data_type
248244

249245
def test_get_leafref_path(self, yang_s, data):
250246
for node in data['leafref_path']:
@@ -257,9 +253,8 @@ def test_get_leafref_type_schema(self, yang_s, data):
257253
for node in data['leafref_type_schema']:
258254
xpath = str(node['xpath'])
259255
expected = node['data_type']
260-
expected_type = yang_s._str_to_type(expected)
261256
data_type = yang_s._get_leafref_type_schema(xpath)
262-
assert expected_type == data_type
257+
assert expected == data_type
263258

264259
"""
265260
This is helper function to load YANG models for tests cases, which works
@@ -345,8 +340,7 @@ def test_xlate_rev_xlate(self, sonic_yang_data):
345340
# print for better debugging, in case of failure.
346341
from jsondiff import diff
347342
print(diff(syc.jIn, syc.revXlateJson, syntax='symmetric'))
348-
# make it fail
349-
assert False == True
343+
raise Exception("Xlate and Rev Xlate failed")
350344

351345
return
352346

0 commit comments

Comments
 (0)
Please sign in to comment.