From 72e7f8889ee56fcf76ffe970a0108b3b184b04cf Mon Sep 17 00:00:00 2001 From: Praveen Chaudhary Date: Fri, 13 Dec 2019 22:20:51 -0800 Subject: [PATCH] [Sonic Yang Tree]: Updating Tree as per modification in YANG models. (#19) * [Sonic Yang Tree]: Updating Tree as per modification in YANG models. * [sonic-port.yang]: Making lanes and speed mandatory and admin_status not mandatory. Including index of port. * [yangTest.json]: Fix tests for YANG models. * [_sonic_yang_ext.py]: Exception handling for delete_node and find_dependencies. --- src/sonic-yang-mgmt/_sonic_yang_ext.py | 41 +++++++++++-------- src/sonic-yang-mgmt/sonic_yang.py | 28 +++++++------ .../tests/yang-model-tests/yangTest.json | 4 +- .../yang-models/Sonic Yang Tree | 14 ++++--- .../yang-models/sonic-port.yang | 9 +++- 5 files changed, 59 insertions(+), 37 deletions(-) diff --git a/src/sonic-yang-mgmt/_sonic_yang_ext.py b/src/sonic-yang-mgmt/_sonic_yang_ext.py index ddfc6010171c..4c57f09c1944 100644 --- a/src/sonic-yang-mgmt/_sonic_yang_ext.py +++ b/src/sonic-yang-mgmt/_sonic_yang_ext.py @@ -606,22 +606,31 @@ def get_data(self): def delete_node(self, xpath): # These MACROS used only here, can we get it from Libyang Header ? - LYS_LEAF = 4 - node = self.find_data_node(xpath) - if node is None: - raise('Node {} not found'.format(xpath)) - - snode = node.schema() - # check for a leaf if it is a key. If yes delete the parent - if (snode.nodetype() == LYS_LEAF): - leaf = ly.Schema_Node_Leaf(snode) - if leaf.is_key(): - # try to delete parent - nodeP = self.find_parent_node(xpath) - xpathP = nodeP.path() - return self._delete_node(xpath=xpathP, node=nodeP) - else: - return self._delete_node(xpath=xpath, node=node) + try: + LYS_LEAF = 4 + node = self.find_data_node(xpath) + if node is None: + raise('Node {} not found'.format(xpath)) + + snode = node.schema() + # check for a leaf if it is a key. If yes delete the parent + if (snode.nodetype() == LYS_LEAF): + leaf = ly.Schema_Node_Leaf(snode) + if leaf.is_key(): + # try to delete parent + nodeP = self.find_parent_node(xpath) + xpathP = nodeP.path() + if self._delete_node(xpath=xpathP, node=nodeP) == False: + raise('_delete_node failed') + else: + return True + + # delete non key element + if self._delete_node(xpath=xpath, node=node) == False: + raise('_delete_node failed') + except Exception as e: + print(e) + raise('Failed to delete node {}'.format(xpath)) return True diff --git a/src/sonic-yang-mgmt/sonic_yang.py b/src/sonic-yang-mgmt/sonic_yang.py index 5e52a16c2be1..c1b5d10446a2 100644 --- a/src/sonic-yang-mgmt/sonic_yang.py +++ b/src/sonic-yang-mgmt/sonic_yang.py @@ -497,17 +497,21 @@ def find_data_dependencies (self, data_xpath): self.fail(e) return ref_list - value = str(self.find_node_value(data_xpath)) - - schema_node = ly.Schema_Node_Leaf(data_node.schema()) - backlinks = schema_node.backlinks() - if backlinks.number() > 0: - for link in backlinks.schema(): - node_set = node.find_path(link.path()) - for data_set in node_set.data(): - schema = data_set.schema() - casted = data_set.subtype() - if value == casted.value_str(): - ref_list.append(data_set.path()) + try: + value = str(self.find_node_value(data_xpath)) + + schema_node = ly.Schema_Node_Leaf(data_node.schema()) + backlinks = schema_node.backlinks() + if backlinks.number() > 0: + for link in backlinks.schema(): + node_set = node.find_path(link.path()) + for data_set in node_set.data(): + schema = data_set.schema() + casted = data_set.subtype() + if value == casted.value_str(): + ref_list.append(data_set.path()) + except Exception as e: + print('Failed to find node or dependencies for {}'.format(data_xpath)) + self.fail(e) return ref_list diff --git a/src/sonic-yang-mgmt/tests/yang-model-tests/yangTest.json b/src/sonic-yang-mgmt/tests/yang-model-tests/yangTest.json index 0e79baf09b57..c6551e198bca 100644 --- a/src/sonic-yang-mgmt/tests/yang-model-tests/yangTest.json +++ b/src/sonic-yang-mgmt/tests/yang-model-tests/yangTest.json @@ -254,7 +254,7 @@ "DST_IP": "10.186.72.0/26", "SRC_IP": "10.176.0.0/15", "PRIORITY": 999980, - "IP_TYPE": "IPV4ANY" + "IP_TYPE": "IPv4ANY" }] }, "sonic-acl:ACL_TABLE": { @@ -299,7 +299,7 @@ "PACKET_ACTION": "FORWARD", "SRC_IPV6": "2001::1/64", "PRIORITY": 999980, - "IP_TYPE": "IPV4ANY" + "IP_TYPE": "IPv4ANY" }] }, "sonic-acl:ACL_TABLE": { diff --git a/src/sonic-yang-mgmt/yang-models/Sonic Yang Tree b/src/sonic-yang-mgmt/yang-models/Sonic Yang Tree index 6dc84d264054..35ccb0bad2df 100644 --- a/src/sonic-yang-mgmt/yang-models/Sonic Yang Tree +++ b/src/sonic-yang-mgmt/yang-models/Sonic Yang Tree @@ -68,6 +68,7 @@ module: sonic-port +--rw speed? uint32 +--rw mtu? uint16 +--rw admin_status head:admin_status + +--rw fec? string module: sonic-portchannel +--rw sonic-portchannel +--rw PORTCHANNEL @@ -81,21 +82,22 @@ module: sonic-portchannel module: sonic-vlan +--rw sonic-vlan +--rw VLAN_INTERFACE - | +--rw VLAN_INTERFACE_LIST* [vlanid ip-prefix] - | +--rw vlanid -> ../../../VLAN/VLAN_LIST/vlanid + | +--rw VLAN_INTERFACE_LIST* [vlan_name ip-prefix] + | +--rw vlan_name -> /sonic-vlan/VLAN/VLAN_LIST/vlan_name | +--rw ip-prefix inet:ip-prefix | +--rw scope? enumeration | +--rw family? head:ip-family +--rw VLAN - | +--rw VLAN_LIST* [vlanid] - | +--rw vlanid uint16 + | +--rw VLAN_LIST* [vlan_name] + | +--rw vlan_name string + | +--rw vlanid? uint16 | +--rw description? string | +--rw dhcp_servers* inet:ip-address | +--rw mtu? uint16 | +--rw admin_status head:admin_status | +--rw members* -> /port:sonic-port/PORT/PORT_LIST/port_name +--rw VLAN_MEMBER - +--rw VLAN_MEMBER_LIST* [vlanid port] - +--rw vlanid -> ../../../VLAN/VLAN_LIST/vlanid + +--rw VLAN_MEMBER_LIST* [vlan_name port] + +--rw vlan_name -> /sonic-vlan/VLAN/VLAN_LIST/vlan_name +--rw port -> /port:sonic-port/PORT/PORT_LIST/port_name +--rw tagging_mode head:vlan_tagging_mode diff --git a/src/sonic-yang-mgmt/yang-models/sonic-port.yang b/src/sonic-yang-mgmt/yang-models/sonic-port.yang index 07e6aa73ab50..c56064e44aab 100644 --- a/src/sonic-yang-mgmt/yang-models/sonic-port.yang +++ b/src/sonic-yang-mgmt/yang-models/sonic-port.yang @@ -48,6 +48,7 @@ module sonic-port{ } leaf lanes { + mandatory true; type string { length 1..128; } @@ -60,6 +61,7 @@ module sonic-port{ } leaf speed { + mandatory true; type uint32 { range 1..100000; } @@ -71,8 +73,13 @@ module sonic-port{ } } + leaf index { + type uint8 { + range 1..128; + } + } + leaf admin_status { - mandatory true; type head:admin_status; }