cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
10436
Views
25
Helpful
9
Replies

Using pyang trying to first convert yang file into xml and then converting xml to json

mitshett
Cisco Employee
Cisco Employee

We are hitting an error when we try to convert yang file to xml  in one of NSO setup but works fine in another setup.

Can anyone share more information about pyang,  Is there any packages we need to install for it to work.

We are kind of stuck in this step

Traceback (most recent call last):
  File "/root/nso-4.4.1.3/lib/pyang/bin/pyang", line 411, in <module>
    run()
  File "/root/nso-4.4.1.3/lib/pyang/bin/pyang", line 383, in run
    emit_obj.emit(ctx, modules, fd)
  File "/root/nso-4.4.1.3/lib/pyang/pyang/plugins/sample-xml-skeleton.py", line 118, in emit
    self.process_children(yam, self.top, None)
  File "/root/nso-4.4.1.3/lib/pyang/pyang/plugins/sample-xml-skeleton.py", line 133, in process_children
    self.node_handler[ch.keyword](ch, elem, module)
  File "/root/nso-4.4.1.3/lib/pyang/pyang/plugins/sample-xml-skeleton.py", line 163, in list
    self.process_children(node, nel, newm)
  File "/root/nso-4.4.1.3/lib/pyang/pyang/plugins/sample-xml-skeleton.py", line 133, in process_children
    self.node_handler[ch.keyword](ch, elem, module)
KeyError: ('tailf-common', 'action')

1 Accepted Solution

Accepted Solutions

Jan Lindblad
Cisco Employee
Cisco Employee

Hard to say exactly what you are doing based on the above. But I can tell you are not using the latest pyang version. First step I'd recommend is to upgrade: https://github.com/mbj4668/pyang

View solution in original post

9 Replies 9

Jan Lindblad
Cisco Employee
Cisco Employee

Hard to say exactly what you are doing based on the above. But I can tell you are not using the latest pyang version. First step I'd recommend is to upgrade: https://github.com/mbj4668/pyang

I am running the below command to convert yang to XML file

pyang -f sample-xml-skeleton -o router.xml router.yang   (this command should get me router.xml file  out of router.yang file)

Above command should give me XML file of that yang model.

Reason, why I am converting yang to XML and to JSON, is to prepare a request body using that JSON to activate service in NSO by  making REST call.

Thank you Jan  Lindbald, it worked but we are facing one more issue

sample-xml-skeleton plugin needs a valid module

Any information you have about this.

schalaga
Cisco Employee
Cisco Employee

Hi Mithun,

May I know for which version of pyang, yang to xml conversion is working. I have 1.5, I see issues while using pyang.

Were you able to generate xml out of yang?

Please share the information if you are able to convert,

Thanks,

Sujani.

When you say "generate xml out of yang", what is it that you're trying to accomplish exactly?

Hi,

I  would like to create service instance xml by traversing through the yang. I do not want to create service instance from cli and then take out format as xml.

Thanks.

Maybe not terribly useful, but if you want to generate something like that, you can use the pyang -f sample-xml-skeleton command:

pyang -f sample-xml-skeleton --sample-xml-skeleton-doctype=config ~/nso/4.5.1/examples.ncs/getting-started/developing-with-ncs/17-mpls-vpn-python/packages/l3vpn/src/yang/l3vpn.yang

Hi Jan,

Thanks for the response.

I was trying with sample-xml-skeleton option with data and config. But its just giving as below. Were you able to generate?

I have nso-4.4.2.1.

- pyang -f sample-xml-skeleton --sample-xml-skeleton-doctype=config l3vpn/src/yang/l3vpn.yang

<?xml version='1.0' encoding='utf-8'?>

<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" />

amaugust
Cisco Employee
Cisco Employee

Was searching for a document on how to convert yang model to xml, came across this document.

Following is what I have done hope others find it useful.

  • To view/convert to a tree format use the following
pyang -f tree ietf-interfaces.yang

this is documented in `pyang --help` 

  -f FORMAT, --format=FORMAT
                        Convert to FORMAT.  Supported formats are: yang, yin,
                        dsdl, jsonxsl, capability, depend, identifiers, tree,
                        jstree, sample-xml-skeleton, omni, uml, jtox, name

The sample-xml-skeleton require few more options, infact the error log doenst give you what exactly is missing, use the following code to convert to xml 

pyang -f sample-xml-skeleton --sample-xml-skeleton-doctype=config  ietf-interfaces.yang
<?xml version='1.0' encoding='UTF-8'?>
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
    <interface>
      <name/>
      <description/>
      <type/>
      <link-up-down-trap-enable/>
    </interface>
  </interfaces>
</config>

we can also use --sample-xml-skeleton-defaults or --sample-xml-skeleton-annotations arguments, they do what they specifies

These options are also specified in pyang --help

  Sample-xml-skeleton output specific options:
    --sample-xml-skeleton-doctype=DOCTYPE
                        Type of sample XML document (data or config).
    --sample-xml-skeleton-defaults
                        Insert leafs with defaults values.
    --sample-xml-skeleton-annotations
                        Add annotations as XML comments.
    --sample-xml-skeleton-path=SAMPLE_PATH
                        Subtree to print
Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the NSO Developer community: