cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
256
Views
0
Helpful
6
Replies
Beginner

How to use python maagic to navigate the YANG model's case syntx

Hi:

 

May someone guide me on how to use maagic to navigate the case syntx.

 

The Yang model is :

choice address-choice {
case device {
leaf address {
tailf:info "IP address or host name for the management interface";
type inet:host;
description
"IP address or host name for the management interface on
the device.";
}
leaf port {
tailf:info "Port for the management interface";
type inet:port-number;
description
"Port for the management interface on the device. If this leaf
is not configured, NCS will use a default value based on the
type of device. For example, a NETCONF device uses port 830,
a CLI device over SSH uses port 22, and a SNMP device uses
port 161.";
}
leaf remote-node {
tailf:info "Name of remote node which connects to device";
type leafref {
path "/cluster/remote-node/name";
}
description
"Setting remote-node indicates that this device is managed by
another cluster node. When set, all other device settings are
read and write through to the remote node, i.e. nothing is
locally stored about this device.";
}
}
case lsa-device {
leaf lsa-remote-node {
when "derived-from-or-self(../device-type/netconf/ned-id, "
+ "'ned:lsa-netconf')";
type leafref {
path "/cluster/remote-node/name";
}
description
"Setting lsa-remote-node indicates that this device is the same
as the cluster node. When set the address and port of the
cluster node is used for this device.";
}
}
}

python is:


In [4]: root.ncs__devices.device['cisco'].address_choice
Out[4]: Case name=device tag=617911018

In [5]: c = root.ncs__devices.device['cisco'].address_choice

In [6]: c
Out[6]: Case name=device tag=617911018

In [7]: c.__getattribute__
Out[7]: <method-wrapper '__getattribute__' of Case object at 0x7f675230da50>

 

my question is : how can i access the "address" leaf info.

Everyone's tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
Beginner

Re: How to use python maagic to navigate the YANG model's case syntx

i sloved this problem. in this case ,we can access address leaf this:


In [22]: root.ncs__devices.device['la-cisco-a9k'].address
Out[22]: '1.1.1.1'
In [25]: root.ncs__devices.device['la-cisco-a9k'].device_type
Out[25]: Container name=device-type tag=1723459864

In [26]: root.ncs__devices.device['la-cisco-a9k'].device_type.ne_type
Out[26]: Case name=cli tag=1771971819

In [27]: root.ncs__devices.device['la-cisco-a9k'].device_type.cli
Out[27]: Container name=cli tag=1771971819

In [29]: root.ncs__devices.device['la-cisco-a9k'].device_type.cli.protocol
Out[29]: Enum string="ssh" value="1"

just think the case syntx no exist

6 REPLIES 6
Beginner

Re: How to use python maagic to navigate the YANG model's case syntx

Hi,

you can acces a keaf using the key service :

in your case you can access adress like this :

service.address_choice.device.address

 

 

Beginner

Re: How to use python maagic to navigate the YANG model's case syntx

i got it somehow, but i do not access this address leaf in cb_create func, i just want to access it by root = ncs.maagic.get_root(trans).
Beginner

Re: How to use python maagic to navigate the YANG model's case syntx

Anyone pls suggest. Thanks
Beginner

Re: How to use python maagic to navigate the YANG model's case syntx

pls the key path info:
In [20]: ncs.maagic.get_node(trans, '/ncs:devices/device{cisco}/address-choice')
Out[20]: Case name=device tag=617911018

In [21]: ncs.maagic.get_node(trans, '/ncs:devices/device{cisco}/address-choice/address')
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-21-963c75f07d70> in <module>()
----> 1 ncs.maagic.get_node(trans, '/ncs:devices/device{cisco}/address-choice/address')

/root/nso-5.1.1/src/ncs/pyapi/ncs/maagic.pyc in get_node(backend, path, shared)
1871 """
1872 root = get_root(backend, shared)
-> 1873 return cd(root, path)
1874
1875

/root/nso-5.1.1/src/ncs/pyapi/ncs/maagic.pyc in cd(node, path)
1771 child = ":".join(child)
1772
-> 1773 node = node[child]
1774
1775 return node

/root/nso-5.1.1/src/ncs/pyapi/ncs/maagic.pyc in __getitem__(self, name)
516 return self
517 else:
--> 518 child = self._children.get_by_yang(name)
519 if hasattr(child, 'get_value'):
520 return child.get_value()

/root/nso-5.1.1/src/ncs/pyapi/ncs/childlist.pyc in get_by_yang(self, name)
167 return self.full_names[name].obj
168 else:
--> 169 return self.short_names[name].obj
170
171 def get_by_py(self, py_name):

KeyError: 'address'

Beginner

Re: How to use python maagic to navigate the YANG model's case syntx

i sloved this problem. in this case ,we can access address leaf this:


In [22]: root.ncs__devices.device['la-cisco-a9k'].address
Out[22]: '1.1.1.1'
In [25]: root.ncs__devices.device['la-cisco-a9k'].device_type
Out[25]: Container name=device-type tag=1723459864

In [26]: root.ncs__devices.device['la-cisco-a9k'].device_type.ne_type
Out[26]: Case name=cli tag=1771971819

In [27]: root.ncs__devices.device['la-cisco-a9k'].device_type.cli
Out[27]: Container name=cli tag=1771971819

In [29]: root.ncs__devices.device['la-cisco-a9k'].device_type.cli.protocol
Out[29]: Enum string="ssh" value="1"

just think the case syntx no exist

Highlighted
Cisco Employee

Re: How to use python maagic to navigate the YANG model's case syntx

That's right, don't specify the choice or the case in the path:

 

From YANG RFC:

The "choice" statement defines a set of alternatives, only one of
   which may exist at any one time.  The argument is an identifier,
   followed by a block of substatements that holds detailed choice
   information.  The identifier is used to identify the choice node in
   the schema tree.  A choice node does not exist in the data tree.
   The "case" statement is used to define branches of the choice.  It
   takes as an argument an identifier, followed by a block of
   substatements that holds detailed case information.

   The identifier is used to identify the case node in the schema tree.
   A case node does not exist in the data tree.

 

      container test {

        choice test-choice {

           case test1 {

             leaf test-str-1 {

               type string;

             }

           }

           case test2 {

             leaf test-str-2 {

               type string;

             }

           }

        }

      }

 

admin@ncs% set services l2vpn BD-109 test test-str-1 Test_String_1
[ok][2019-07-18 12:26:16]

 

admin@ncs% show services l2vpn test

l2vpn BD-109 {

    test {

        test-str-1 Test_String_1;

    }

}

 

In [3]: with ncs.maapi.Maapi() as m:

   ...:     with ncs.maapi.Session(m, 'admin', 'python'):

   ...:         with m.start_write_trans() as t:

   ...:             root = ncs.maagic.get_root(t)

   ...:             l2vpn = root.services.l2vpn['BD-109']

   ...:             teststr = l2vpn.test.test_str_1

   ...:             print (teststr)

   ...:

   ...:

Test_String_1