04-03-2022 11:40 PM
Hello,
YDK is returning IndexError: basic_string::at error when converting a file from YML to XML which was originally converted successfully from XML -> YML.
Error returned is the following
Traceback (most recent call last): File "convert.py", line 166, in <module> print(yang_yaml_to_xml(ARGS['yaml_file'])) File "convert.py", line 134, in yang_yaml_to_xml decoded_json_yang = CODEC.decode(JSON_PROVIDER, config_json_yang) File "/opt/neteng/lib/python3.6/site-packages/ydk/errors/error_handler.py", line 112, in helper return func(self, provider, entity, *args, **kwargs) File "/opt/neteng/lib/python3.6/site-packages/ydk/services/codec_service.py", line 140, in decode return self._decode(provider, payload_holder, subtree) File "/opt/neteng/lib/python3.6/site-packages/ydk/services/codec_service.py", line 174, in _decode root_data_node = codec_service.decode(root_schema, payload, provider.encoding) IndexError: basic_string::at
Offending code in YAML is, specifically the empty choice-value string: choice-value: ''. The element was generated by YDK when converting from XML to YML
junos-conf-policy-options:policy-options: policy-statement: - name: TEST term: - from: protocol: - ospf route-filter: - address: 1.1.1.0/24 choice-ident: orlonger choice-value: '' name: OSPF then: accept: - null
XML code used for the conversion to YML - omitted the full policy. Issue can be easily reproducible on my system when <choice-value/> has no value which is the case for route filters such as exact, orlonger.
<name>OSPF-ACCEPT</name> <from> <protocol>ospf</protocol> <route-filter> <address>1.1.1.0/24</address> <choice-ident>orlonger</choice-ident> <choice-value/> </route-filter>
Thank you,
Luca
Solved! Go to Solution.
04-06-2022 08:36 AM
Hi Luca
I have resolved the issue. The fix will be available in the next patch release (0.8.6.3). If this is critical for your project, you can get the fix in my fork master branch.
Thank you,
Yan
04-04-2022 08:52 AM
Hi Luca
The YDK does not offer service to convert from/to YAML. It is completely on the user side. If you see YDK codec issue in conversion of YANG model compliant XML or JSON payload, then we can have the discussion.
Regards,
Yan
04-04-2022 06:59 PM
Hi Yan,
Thank you for the reply.
I am aware YDK does not convert from/to YAML which I am doing via json.dumps methods. I omitted the explanation for the sake of space and assumed the json method would return the correct payload, apologies for that. In any case, the same error is reproducible via JSON payload which was converted by YDK from XML code downloaded from a Juniper device. Here the json payload is converted from XML (I omit the XML payload for the sake of space)
{ "junos-conf-root:configuration": { "junos-conf-policy-options:policy-options": { "policy-statement": [ { "name": "IPV4", "term": [ { "name": "OSPF-ACCEPT", "from": { "protocol": [ "ospf" ], "route-filter": [ { "address": "1.1.1.0/24", "choice-ident": "orlonger", "choice-value": "" } ] }, "then": { "accept": [null] } } ] } ] } } }
The error returned is the same as in my previous post
{"junos-conf-root:configuration": {"junos-conf-policy-options:policy-options": {"policy-statement": [{"name": "IPV4", "term": [{"name": "OSPF-ACCEPT", "from": {"protocol": ["ospf"], "route-filter": [{"address": "1.1.1.0/24", "choice-ident": "orlonger", "choice-value": ""}]}, "then": {"accept": [null]}}]}]}}} Traceback (most recent call last): File "convert1.py", line 249, in <module> print(yang_yaml_to_xml(ARGS['yaml_file'])) File "convert1.py", line 218, in yang_yaml_to_xml decoded_json_yang = CODEC.decode(JSON_PROVIDER, config_json_yang) File "/opt/neteng/lib/python3.6/site-packages/ydk/errors/error_handler.py", line 112, in helper return func(self, provider, entity, *args, **kwargs) File "/opt/neteng/lib/python3.6/site-packages/ydk/services/codec_service.py", line 140, in decode return self._decode(provider, payload_holder, subtree) File "/opt/neteng/lib/python3.6/site-packages/ydk/services/codec_service.py", line 174, in _decode root_data_node = codec_service.decode(root_schema, payload, provider.encoding) IndexError: basic_string::at
Any string on choice-value <> from "" (which was correctly returned by the XML -> JSON conversion) would return a successful conversion to XML
{"junos-conf-root:configuration": {"junos-conf-policy-options:policy-options": {"policy-statement": [{"name": "IPV4", "term": [{"name": "OSPF-ACCEPT", "from": {"protocol": ["ospf"], "route-filter": [{"address": "1.1.1.0/24", "choice-ident": "orlonger", "choice-value": "orlonger"}]}, "then": {"accept": [null]}}]}]}}} <configuration xmlns="http://yang.juniper.net/junos/conf/root"> <policy-options xmlns="http://yang.juniper.net/junos/conf/policy-options"> <policy-statement> <name>IPV4</name> <term> <name>OSPF-ACCEPT</name> <from> <protocol>ospf</protocol> <route-filter> <address>1.1.1.0/24</address> <choice-ident>orlonger</choice-ident> <choice-value>orlonger</choice-value> </route-filter> </from> <then> <accept/> </then> </term> </policy-statement> </policy-options> </configuration>
The issue in the conversion JSON -> XML seems to be the empty string for choice-value, which for the above payload would be a legitimate value.
Code for the conversion
def yang_yaml_to_xml(json_file): config_json_yang = json.dumps(json.load(open(json_file))) print (config_json_yang) decoded_json_yang = CODEC.decode(JSON_PROVIDER, config_json_yang) yang_xml = CODEC.encode(XML_PROVIDER, decoded_json_yang) return yang_xml
Thank you in advance for the comment and for always providing quick replies,
Luca
04-05-2022 09:53 AM
Hi Luca
I reproduced the issue in test environment. It is a bug! Could you please open new issue here.
Thanks,
Yan
04-05-2022 07:53 PM
04-06-2022 08:36 AM
Hi Luca
I have resolved the issue. The fix will be available in the next patch release (0.8.6.3). If this is critical for your project, you can get the fix in my fork master branch.
Thank you,
Yan
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide