cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1500
Views
5
Helpful
6
Replies

extend has problem in Cisco_IOS_XR_mpls_vpn_oper

magedahmed
Level 1
Level 1

I am trying to use "extend to add two values under "Cisco_IOS_XR_mpls_vpn_oper.L3vpn.Vrfs.Vrf.Interface()" to add two interface to the list but when I am trying to encode I can see only one value appear

 

I also tried append interface1 then interface2 but I got the same result

 

below the code I am using

 

import time
import datetime

from ydk.models.cisco_ios_xr import Cisco_IOS_XR_mpls_vpn_oper
from ydk.services import CodecService
from argparse import ArgumentParser
from ydk.providers import CodecServiceProvider

#print(dir(Cisco_IOS_XR_mpls_vpn_oper.L3vpn.Vrfs))
start = time.time()
class l3vpn_ydk_oper_object():
def __init__(self):
self.l3vpn = Cisco_IOS_XR_mpls_vpn_oper.L3vpn()
self.vrfs = Cisco_IOS_XR_mpls_vpn_oper.L3vpn.Vrfs()

def l3vpn_fun(self, vrf_name, RD, interfaces):
vrf = Cisco_IOS_XR_mpls_vpn_oper.L3vpn.Vrfs.Vrf()
vrf.vrf_name = vrf_name

af = Cisco_IOS_XR_mpls_vpn_oper.L3vpn.Vrfs.Vrf.Af()
af.af_name = Cisco_IOS_XR_mpls_vpn_oper.MplsVpnAfi.ipv4 #MplsVpnAfiEnum.ipv4
af.export_route_policy = 'y'
af.import_route_policy = 'x'
af.saf_name = Cisco_IOS_XR_mpls_vpn_oper.MplsVpnSafi.unicast #MplsVpnSafiEnum.unicast
vrf.af.append(af)

interface1 = Cisco_IOS_XR_mpls_vpn_oper.L3vpn.Vrfs.Vrf.Interface()
interface1.interface_name = interfaces[0]
interface2 = Cisco_IOS_XR_mpls_vpn_oper.L3vpn.Vrfs.Vrf.Interface()
interface2.interface_name = interfaces[1]
vrf.interface.extend([interface2, interface1])

vrf.route_distinguisher = RD
vrf.vrf_name_xr = vrf_name
self.vrfs.vrf.append(vrf)


def return_l3vpn(self):
self.l3vpn.vrfs = self.vrfs
return self.l3vpn

if __name__ == "__main__":
"""Execute main program."""
parser = ArgumentParser()
parser.add_argument("-v", "--verbose", help="print debugging messages",
action="store_true")
args = parser.parse_args()

# log debug messages if verbose argument specified
if args.verbose:
logger = logging.getLogger("ydk")
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = logging.Formatter(("%(asctime)s - %(name)s - "
"%(levelname)s - %(message)s"))
handler.setFormatter(formatter)
logger.addHandler(handler)

# create codec provider
provider = CodecServiceProvider(type="json")

# create codec service
codec = CodecService()

l3vpn_info = l3vpn_ydk_oper_object() # create object
l3vpn_list = ['vrf_red',"192.168.111.117:41",['gi1/1','gi1/2']]
l3vpn_info.l3vpn_fun(l3vpn_list[0],l3vpn_list[1],l3vpn_list[2])

# encode and print object
print(codec.encode(provider, l3vpn_info.return_l3vpn()))

end = time.time()
print("Total time: {}".format(end - start))
exit()

 below the encoding output

 

 

{
  "Cisco-IOS-XR-mpls-vpn-oper:l3vpn": {
    "vrfs": {
      "vrf": [
        {
          "vrf-name": "vrf_red",
          "vrf-name-xr": "vrf_red",
          "route-distinguisher": "192.168.111.117:41",
          "interface": [
            {
              "interface-name": "gi1/2"
            }
          ],
          "af": [
            {
              "af-name": "ipv4",
              "saf-name": "unicast",
              "import-route-policy": "x",
              "export-route-policy": "y"
            }
          ]
        }
      ]
    }
  }
}

 

1 Accepted Solution

Accepted Solutions

Hi magedahmed
I have resolved the bug for C++ and Python. The fix is too complex, going to the foundation of YDK. Therefore it will be available in the next YDK release (0.8.1).
In the meanwhile I can suggest you to use XmlSubtreeCodec (add parameter 'subtree'), which functions outside of libyang library, which is obviously can be used only for XML encoding. You will need to encode the entity to XML and then convert to JSON. I am sure there are some third party packages that can help to do that.
Your actionable part of the script will look like this:
# create codec provider and service
provider = CodecServiceProvider(type="xml")
codec = CodecService()
xml = codec.encode(provider, l3vpn_info.return_l3vpn(), subtree=True)
print(xml)

The result will look like this:







vrf_red

vrf_red

192.168.111.117:41



ipv4

unicast

x

y





gi1/2





gi1/1









Other alternative, which currently works for both XML and JSON encoding, is to use Path API. Please let me know if you need help here.

View solution in original post

6 Replies 6

ygorelik
Cisco Employee
Cisco Employee

It is actually not 'extend' issue, but YDK CodecService failure to encode keyless lists. Thank you for finding this bug. I have opened the corresponding GitHub issue to track its resolution.

@ygorelik

Could you please help by providing a ydk code fix on github so I can use it.

Hi magedahmed
I have resolved the bug for C++ and Python. The fix is too complex, going to the foundation of YDK. Therefore it will be available in the next YDK release (0.8.1).
In the meanwhile I can suggest you to use XmlSubtreeCodec (add parameter 'subtree'), which functions outside of libyang library, which is obviously can be used only for XML encoding. You will need to encode the entity to XML and then convert to JSON. I am sure there are some third party packages that can help to do that.
Your actionable part of the script will look like this:
# create codec provider and service
provider = CodecServiceProvider(type="xml")
codec = CodecService()
xml = codec.encode(provider, l3vpn_info.return_l3vpn(), subtree=True)
print(xml)

The result will look like this:







vrf_red

vrf_red

192.168.111.117:41



ipv4

unicast

x

y





gi1/2





gi1/1









Other alternative, which currently works for both XML and JSON encoding, is to use Path API. Please let me know if you need help here.

Hi ygorelik,

Many thanks for your support I tried on YDK release (0.8.1) and it is working fine

Hi magedahmed
As of today the issue is completely resolved, meaning the solution was applied across all YDK supported encoding, languages and platforms. The complete solution will be available in new YDK release (0.8.1), which most likely will be in February. But in the meanwhile you can use some workarounds, which were described in my previous post.
Thanks,
Yan

I used YDK 0.8.1 from your github and problem solved even with json.
Many thanks for your support