Skip to content

[YANG] Statement"choice" does not work in YANG file except sonic-acl.yang #17518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wen587 opened this issue Dec 15, 2023 · 1 comment
Open
Assignees
Labels
Triaged this issue has been triaged YANG YANG model related changes

Comments

@wen587
Copy link
Contributor

wen587 commented Dec 15, 2023

Description

Tried use choice to identify different case. But it failed YANG validation if we add choice statement in YANG mode except for sonic-acl.yang.

Steps to reproduce the issue:

  1. Run YANG validation for current runnning config and it pass
admin@bjw-can-3800-1:~$ cat verify_configDB_yang.py
import subprocess
import sonic_yang
import json
def get_config_db_json():
    cmd = "show runningconfiguration all"
    result = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    text, err = result.communicate()
    return json.loads(text)
configDbJson = get_config_db_json()
sy = sonic_yang.SonicYang("/usr/local/yang-models")
sy.loadYangModel()
sy.loadData(configDbJson )
sy.validate_data_tree()

admin@bjw-can-3800-1:~$ python verify_configDB_yang.py
sonic_yang(6):Note: Below table(s) have no YANG models: CONSOLE_SWITCH, DHCP_SERVER, RESTAPI, bgpraw
admin@bjw-can-3800-1:~$ echo $?
0
admin@bjw-can-3800-1:~$
  1. Take a snapshot of YANG file and partial config under this YANG model.
admin@bjw-can-3800-1:~$ show run all | grep BGP_DEVICE_GLOBAL -A5
    "BGP_DEVICE_GLOBAL": {
        "STATE": {
            "tsa_enabled": "false"
        }
    },
    "BGP_NEIGHBOR": {
admin@bjw-can-3800-1:~$
admin@bjw-can-3800-1:~$
admin@bjw-can-3800-1:~$ cat /usr/local/yang-models/sonic-bgp-device-global.yang
module sonic-bgp-device-global {
    namespace "http://github.com/Azure/sonic-bgp-device-global";
    prefix bgp_device_global;
    yang-version 1.1;

    organization
        "SONiC";

    contact
        "SONiC";

    description
        "SONIC Device-specific BGP global data";

    revision 2022-06-26 {
        description
            "Initial revision";
    }

    container sonic-bgp-device-global {
        container BGP_DEVICE_GLOBAL {
            container STATE {
                description "BGP device-specific global data";
                leaf tsa_enabled {
                    type boolean;
                    default "false";
                    description
                    "When set to true, Traffic is shifted away (TSA), i.e, BGP routes are not advertised to neighboring routers";
                }

            } /* end of STATE container */
        } /* end of BGP_DEVICE_GLOBAL container */

    } /* end of top level container */

} /* end of module sonic-bgp-device-global */
  1. Add below choice statement to YANG model. This is a sample by mimicking sonic-acl.yang: https://github.com/sonic-net/sonic-buildimage/blob/master/src/sonic-yang-models/yang-templates/sonic-acl.yang.j2#L141-L155

             choice test_choice {
                 case test_case_one {
                     leaf test_num {
                         type uint16;
                     }
                 }
    
                 case test_case_two {
                     leaf test_range {
                         type string {
                             pattern '[0-9]{1,4}';
                         }
                     }
                 }
             }
    
  2. Take a snapshot of YANG model after modification then rerun YANG validation.

admin@bjw-can-3800-1:~$ cat /usr/local/yang-models/sonic-bgp-device-global.yang
module sonic-bgp-device-global {
    namespace "http://github.com/Azure/sonic-bgp-device-global";
    prefix bgp_device_global;
    yang-version 1.1;

    organization
        "SONiC";

    contact
        "SONiC";

    description
        "SONIC Device-specific BGP global data";

    revision 2022-06-26 {
        description
            "Initial revision";
    }

    container sonic-bgp-device-global {
        container BGP_DEVICE_GLOBAL {
            container STATE {
                description "BGP device-specific global data";
                leaf tsa_enabled {
                    type boolean;
                    default "false";
                    description
                    "When set to true, Traffic is shifted away (TSA), i.e, BGP routes are not advertised to neighboring routers";
                }
                choice test_choice {
                    case test_case_one {
                        leaf test_num {
                            type uint16;
                        }
                    }

                    case test_case_two {
                        leaf test_range {
                            type string {
                                pattern '[0-9]{1,4}';
                            }
                        }
                    }
                }
            } /* end of STATE container */
        } /* end of BGP_DEVICE_GLOBAL container */

    } /* end of top level container */

} /* end of module sonic-bgp-device-global */

admin@bjw-can-3800-1:~$ show run all | grep BGP_DEVICE_GLOBAL -A5
    "BGP_DEVICE_GLOBAL": {
        "STATE": {
            "tsa_enabled": "false"
        }
    },
    "BGP_NEIGHBOR": {
admin@bjw-can-3800-1:~$ python verify_configDB_yang.py
sonic_yang(6):Note: Below table(s) have no YANG models: CONSOLE_SWITCH, DHCP_SERVER, RESTAPI, bgpraw
sonic_yang(3):Data Loading Failed:string indices must be integers
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 1112, in loadData
    self._xlateConfigDB(xlateFile=xlateFile)
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 730, in _xlateConfigDB
    self._xlateConfigDBtoYang(jIn, yangJ)
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 716, in _xlateConfigDBtoYang
    self._xlateContainer(cmap['container'], yangJ[key][subkey], \
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 670, in _xlateContainer
    self._xlateContainerInContainer(ccontainer, yang, configC, table)
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 631, in _xlateContainerInContainer
    self._xlateContainer(ccontainer, yang[ccName], \
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 677, in _xlateContainer
    leafDict = self._createLeafDict(model, table)
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 381, in _createLeafDict
    cases = choice['case']
TypeError: string indices must be integers

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/admin/verify_configDB_yang.py", line 12, in <module>
    sy.loadData(configDbJson )
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 1122, in loadData
    raise SonicYangException("Data Loading Failed\n{}".format(str(e)))
sonic_yang_ext.SonicYangException: Data Loading Failed
string indices must be integers

Describe the results you received:

admin@bjw-can-3800-1:~$ python verify_configDB_yang.py
sonic_yang(6):Note: Below table(s) have no YANG models: CONSOLE_SWITCH, DHCP_SERVER, RESTAPI, bgpraw
sonic_yang(3):Data Loading Failed:string indices must be integers
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 1112, in loadData
    self._xlateConfigDB(xlateFile=xlateFile)
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 730, in _xlateConfigDB
    self._xlateConfigDBtoYang(jIn, yangJ)
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 716, in _xlateConfigDBtoYang
    self._xlateContainer(cmap['container'], yangJ[key][subkey], \
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 670, in _xlateContainer
    self._xlateContainerInContainer(ccontainer, yang, configC, table)
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 631, in _xlateContainerInContainer
    self._xlateContainer(ccontainer, yang[ccName], \
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 677, in _xlateContainer
    leafDict = self._createLeafDict(model, table)
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 381, in _createLeafDict
    cases = choice['case']
TypeError: string indices must be integers

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/admin/verify_configDB_yang.py", line 12, in <module>
    sy.loadData(configDbJson )
  File "/usr/local/lib/python3.9/dist-packages/sonic_yang_ext.py", line 1122, in loadData
    raise SonicYangException("Data Loading Failed\n{}".format(str(e)))
sonic_yang_ext.SonicYangException: Data Loading Failed
string indices must be integers

Describe the results you expected:

choice statement should not break yang validation.

admin@bjw-can-3800-1:~$ python verify_configDB_yang.py
sonic_yang(6):Note: Below table(s) have no YANG models: CONSOLE_SWITCH, DHCP_SERVER, RESTAPI, bgpraw
admin@bjw-can-3800-1:~$ echo $?
0

Output of show version:

admin@bjw-can-3800-1:~$ show ver

SONiC Software Version: SONiC.20220531.45
SONiC OS Version: 11
Distribution: Debian 11.8
Kernel: 5.10.0-18-2-amd64
Build commit: 1a8426fd17
Build date: Thu Nov  9 20:37:58 UTC 2023
Built by: cloudtest@1b5bef75c000002

Platform: x86_64-mlnx_msn3800-r0
HwSKU: ACS-MSN3800
ASIC: mellanox
ASIC Count: 1
Serial Number: MT2228X04010
Model Number: MSN3800-CS2ROS
Hardware Revision: A3
Uptime: 05:38:49 up 2 days,  1:07,  2 users,  load average: 1.13, 1.22, 1.13
Date: Fri 15 Dec 2023 05:38:49

Output of show techsupport:

(paste your output here or download and attach the file here )

Additional information you deem important (e.g. issue happens only occasionally):

@wen587 wen587 added the YANG YANG model related changes label Dec 15, 2023
@prabhataravind prabhataravind added the Triaged this issue has been triaged label Dec 20, 2023
@prabhataravind
Copy link
Contributor

@qiluo-msft to discuss the issue with @wen587 and assign.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Triaged this issue has been triaged YANG YANG model related changes
Projects
None yet
Development

No branches or pull requests

4 participants