From a40aa6393b02a8e9424bad3e309940a352bc96a4 Mon Sep 17 00:00:00 2001 From: Jingwen Xie Date: Fri, 1 Apr 2022 06:57:05 +0000 Subject: [PATCH 1/5] fix yang validation when table contains empty value --- src/sonic-yang-mgmt/sonic_yang_ext.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/sonic-yang-mgmt/sonic_yang_ext.py b/src/sonic-yang-mgmt/sonic_yang_ext.py index aa36ee0a1951..2da4adc967f4 100644 --- a/src/sonic-yang-mgmt/sonic_yang_ext.py +++ b/src/sonic-yang-mgmt/sonic_yang_ext.py @@ -593,7 +593,11 @@ def _xlateContainerInContainer(self, model, yang, configC, table): ccontainer = model #print(ccontainer['@name']) yang[ccontainer['@name']] = dict() - if not configC.get(ccontainer['@name']): + if configC.get(ccontainer['@name']) is None: + return + if len(ccontainer['@name']) == 0: + # Empty container, clean config and return + del configC[ccontainer['@name']] return self.sysLog(msg="xlateProcessListOfContainer: {}".format(ccontainer['@name'])) self._xlateContainer(ccontainer, yang[ccontainer['@name']], \ From 3048fd867d37556debe08d215c38d89402bf92de Mon Sep 17 00:00:00 2001 From: Jingwen Xie Date: Sat, 2 Apr 2022 04:11:41 +0000 Subject: [PATCH 2/5] resolve comment --- src/sonic-yang-mgmt/sonic_yang_ext.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/sonic-yang-mgmt/sonic_yang_ext.py b/src/sonic-yang-mgmt/sonic_yang_ext.py index 2da4adc967f4..a1bc49397fe9 100644 --- a/src/sonic-yang-mgmt/sonic_yang_ext.py +++ b/src/sonic-yang-mgmt/sonic_yang_ext.py @@ -591,22 +591,22 @@ def _xlateListInContainer(self, model, yang, configC, table, exceptionList): """ def _xlateContainerInContainer(self, model, yang, configC, table): ccontainer = model - #print(ccontainer['@name']) - yang[ccontainer['@name']] = dict() - if configC.get(ccontainer['@name']) is None: + ccName = ccontainer['@name'] + yang[ccName] = dict() + if ccName not in configC: return - if len(ccontainer['@name']) == 0: + if len(ccName) == 0: # Empty container, clean config and return - del configC[ccontainer['@name']] + del configC[ccName] return - self.sysLog(msg="xlateProcessListOfContainer: {}".format(ccontainer['@name'])) - self._xlateContainer(ccontainer, yang[ccontainer['@name']], \ - configC[ccontainer['@name']], table) + self.sysLog(msg="xlateProcessListOfContainer: {}".format(ccName)) + self._xlateContainer(ccontainer, yang[ccName], \ + configC[ccName], table) # clean empty container - if len(yang[ccontainer['@name']]) == 0: - del yang[ccontainer['@name']] + if len(yang[ccName]) == 0: + del yang[ccName] # remove copy after processing - del configC[ccontainer['@name']] + del configC[ccName] return From acad60ff43fdf416ab937d0f479d1834ae71cba6 Mon Sep 17 00:00:00 2001 From: Jingwen Xie Date: Wed, 6 Apr 2022 02:21:01 +0000 Subject: [PATCH 3/5] resolve comment and add ut test --- src/sonic-yang-mgmt/sonic_yang_ext.py | 3 ++- .../tests/yang_model_tests/tests/tacacs.json | 7 +++++-- .../tests/yang_model_tests/tests_config/tacacs.json | 10 ++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/sonic-yang-mgmt/sonic_yang_ext.py b/src/sonic-yang-mgmt/sonic_yang_ext.py index a1bc49397fe9..d43e52970302 100644 --- a/src/sonic-yang-mgmt/sonic_yang_ext.py +++ b/src/sonic-yang-mgmt/sonic_yang_ext.py @@ -594,8 +594,9 @@ def _xlateContainerInContainer(self, model, yang, configC, table): ccName = ccontainer['@name'] yang[ccName] = dict() if ccName not in configC: + # Inner container doesn't exist in config return - if len(ccName) == 0: + if len(configC[ccName]) == 0: # Empty container, clean config and return del configC[ccName] return diff --git a/src/sonic-yang-models/tests/yang_model_tests/tests/tacacs.json b/src/sonic-yang-models/tests/yang_model_tests/tests/tacacs.json index 5236aab596fe..f5a8bfcd15e7 100644 --- a/src/sonic-yang-models/tests/yang_model_tests/tests/tacacs.json +++ b/src/sonic-yang-models/tests/yang_model_tests/tests/tacacs.json @@ -4,18 +4,21 @@ }, "TACPLUS_INVALID_TIMEOUT_TEST": { "desc": "Tacplus global configuration with invalid timeout value in TACPLUS table.", - "eStr": "TACACS timeout must be 1..60" + "eStr": "TACACS timeout must be 1..60" }, "TACPLUS_NOT_PRESENT_SRC_INTF_TEST": { "desc": "Tacplus global configuration with a non existent port in TACPLUS table.", "eStrKey": "InvalidValue" }, + "TACPLUS_VERIFY_VALID_EMPTY_GLOBAL": { + "desc": "Tacplus valid empty global configuration in TACPLUS table." + }, "TACPLUS_SERVER_TEST" : { "desc": "Tacplus server configuration in TACPLUS_SERVER table." }, "TACPLUS_SERVER_INVALID_PRIORITY_TEST": { "desc": "Tacplus server configuration with invalid priority value in TACPLUS_SERVER table.", - "eStr": "TACACS server priority must be 1..64" + "eStr": "TACACS server priority must be 1..64" }, "TACPLUS_SERVER_INVALID_TIMEOUT_TEST" : { "desc": "Tacplus server configuration with invalid timeout value in TACPLUS_SERVER table.", diff --git a/src/sonic-yang-models/tests/yang_model_tests/tests_config/tacacs.json b/src/sonic-yang-models/tests/yang_model_tests/tests_config/tacacs.json index 290813121597..bbb551ace785 100644 --- a/src/sonic-yang-models/tests/yang_model_tests/tests_config/tacacs.json +++ b/src/sonic-yang-models/tests/yang_model_tests/tests_config/tacacs.json @@ -52,6 +52,16 @@ } }, + "TACPLUS_VERIFY_VALID_EMPTY_GLOBAL": { + "sonic-system-tacacs:sonic-system-tacacs": { + "sonic-system-tacacs:TACPLUS": { + "global": { + } + } + } + + }, + "TACPLUS_SERVER_TEST": { "sonic-system-tacacs:sonic-system-tacacs": { "sonic-system-tacacs:TACPLUS_SERVER": { From 5ea5ed62867068057e9b90778297f98868206304 Mon Sep 17 00:00:00 2001 From: Jingwen Xie Date: Wed, 6 Apr 2022 02:38:29 +0000 Subject: [PATCH 4/5] typo --- src/sonic-yang-models/tests/files/sample_config_db.json | 2 +- src/sonic-yang-models/yang-models/sonic-device_metadata.yang | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sonic-yang-models/tests/files/sample_config_db.json b/src/sonic-yang-models/tests/files/sample_config_db.json index ff99fc7335df..e5da44983390 100644 --- a/src/sonic-yang-models/tests/files/sample_config_db.json +++ b/src/sonic-yang-models/tests/files/sample_config_db.json @@ -304,7 +304,7 @@ "switch_id": "2", "switch_type": "voq", "max_cores": "8", - "sub_role": "FrondEnd", + "sub_role": "FrontEnd", "dhcp_server": "disabled" } }, diff --git a/src/sonic-yang-models/yang-models/sonic-device_metadata.yang b/src/sonic-yang-models/yang-models/sonic-device_metadata.yang index 618ed19cd81f..a349a3eb9366 100644 --- a/src/sonic-yang-models/yang-models/sonic-device_metadata.yang +++ b/src/sonic-yang-models/yang-models/sonic-device_metadata.yang @@ -129,7 +129,7 @@ module sonic-device_metadata { leaf sub_role { type string; - description "sub_role indicates if ASIC is FrondEnd or BackEnd."; + description "sub_role indicates if ASIC is FrontEnd or BackEnd."; } leaf downstream_subrole { From 810f74b01993129514b76264adb4bd5d76f72d7b Mon Sep 17 00:00:00 2001 From: Jingwen Xie Date: Wed, 6 Apr 2022 05:58:56 +0000 Subject: [PATCH 5/5] add sample configDB to verify change works --- .../tests/libyang-python-tests/test_sonic_yang.py | 15 +++++++++++++++ .../tests/files/sample_config_db.json | 6 ++++++ .../tests/yang_model_tests/tests/tacacs.json | 3 --- .../yang_model_tests/tests_config/tacacs.json | 10 ---------- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/sonic-yang-mgmt/tests/libyang-python-tests/test_sonic_yang.py b/src/sonic-yang-mgmt/tests/libyang-python-tests/test_sonic_yang.py index 3eb396594e80..a13d4c02e9a0 100644 --- a/src/sonic-yang-mgmt/tests/libyang-python-tests/test_sonic_yang.py +++ b/src/sonic-yang-mgmt/tests/libyang-python-tests/test_sonic_yang.py @@ -364,5 +364,20 @@ def test_table_with_no_yang(self, sonic_yang_data): return + def test_special_json_with_yang(self, sonic_yang_data): + # in this test, we validate unusual json config and check if + # loadData works successfully + test_file = sonic_yang_data['test_file'] + syc = sonic_yang_data['syc'] + + # read config + jIn = self.readIjsonInput(test_file, 'SAMPLE_CONFIG_DB_SPECIAL_CASE') + jIn = json.loads(jIn) + + # load config and create Data tree + syc.loadData(jIn) + + return + def teardown_class(self): pass diff --git a/src/sonic-yang-models/tests/files/sample_config_db.json b/src/sonic-yang-models/tests/files/sample_config_db.json index e5da44983390..6618ed8232b7 100644 --- a/src/sonic-yang-models/tests/files/sample_config_db.json +++ b/src/sonic-yang-models/tests/files/sample_config_db.json @@ -1694,5 +1694,11 @@ "UNKNOWN_TABLE": { "Error": "This Table is for testing, This Table does not have YANG models." } + }, + "SAMPLE_CONFIG_DB_SPECIAL_CASE": { + "TACPLUS": { + "global": { + } + } } } diff --git a/src/sonic-yang-models/tests/yang_model_tests/tests/tacacs.json b/src/sonic-yang-models/tests/yang_model_tests/tests/tacacs.json index f5a8bfcd15e7..6888a4e3f326 100644 --- a/src/sonic-yang-models/tests/yang_model_tests/tests/tacacs.json +++ b/src/sonic-yang-models/tests/yang_model_tests/tests/tacacs.json @@ -10,9 +10,6 @@ "desc": "Tacplus global configuration with a non existent port in TACPLUS table.", "eStrKey": "InvalidValue" }, - "TACPLUS_VERIFY_VALID_EMPTY_GLOBAL": { - "desc": "Tacplus valid empty global configuration in TACPLUS table." - }, "TACPLUS_SERVER_TEST" : { "desc": "Tacplus server configuration in TACPLUS_SERVER table." }, diff --git a/src/sonic-yang-models/tests/yang_model_tests/tests_config/tacacs.json b/src/sonic-yang-models/tests/yang_model_tests/tests_config/tacacs.json index bbb551ace785..290813121597 100644 --- a/src/sonic-yang-models/tests/yang_model_tests/tests_config/tacacs.json +++ b/src/sonic-yang-models/tests/yang_model_tests/tests_config/tacacs.json @@ -52,16 +52,6 @@ } }, - "TACPLUS_VERIFY_VALID_EMPTY_GLOBAL": { - "sonic-system-tacacs:sonic-system-tacacs": { - "sonic-system-tacacs:TACPLUS": { - "global": { - } - } - } - - }, - "TACPLUS_SERVER_TEST": { "sonic-system-tacacs:sonic-system-tacacs": { "sonic-system-tacacs:TACPLUS_SERVER": {