cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1072
Views
0
Helpful
11
Replies
Highlighted
Contributor

Using PYANG to help with JSON format

All,

 

 

I’m struggling to get a rest call working for a service.

 

I consistently get a 400 status code that I’m guessing means my json payload is structured incorrectly.

 

 

I initially deployed the service via ncs_cli and used a “Show services …” | display JSON as a template for use in my python script.

 

That didn’t change the error.

 

 

Can pyang convert directly from a source .yang file to a JSON format?

 

I’m unable to find it documented and don’t find the option using pyang –help.

 

11 REPLIES 11
Highlighted
Contributor

Unfortunately, pyang cannot directly convert to JSON format for now.

 

 

Pyangbind might help you. It takes a little time, but It's very useful.

 

https://github.com/robshakir/pyangbind

 

 

Please refer to the below chapter:

 

Serialising a Data Instance

 

---

 

import pyangbind.lib.pybindJSON as pybindJSON

 

# Dump the entire instance as JSON in PyangBind format

 

print(pybindJSON.dumps(oclr))

 

---

 

This code snippet output JSON format data stratucure.

 

 

I am also using the library for automatically documentation from yang.

Highlighted

Use PYANG to create REST Doc

 

    mkdir -p ../doc/rest
     pyang -f rest-doc --rest-doc-output=docbook --rest-doc-path=/sample --rest-doc-show-description -p $NCS_DIR/src/ncs/yang/:./yang/ yang/*.yang -o ../doc/rest/sample-doc.txt

 

Use XSLTPROC to create REST XTML GUIDE

 

xsltproc -o ../doc/rest/sample-doc.html -v docbook.xsl ../doc/rest/sample-doc.txt

Highlighted

You can use pyang to create xml payload and then use one of the readily available xml->json tools found on-line.

 

 

Or perhaps we can help you determine the problem in the rest call/json payload that you currently have, if you provide the info here.

 

The show services… approach should result in accurate json payload.

Highlighted

Another approach would be to create a service instance using the CLI, then read the instance using a rest call to see the format.

 

 

Commas and curlies will kill you…. 

Highlighted

How can I create xml from yang. can you please give some inputs.

I tried with pyang sampl-xml-skeleton, but I am not able to produce the output.

Thanks in advance.

Highlighted

Hello Sujani,

Please try "-f yin" option as follows.

cisco@debian:~/NSO-4.5/examples.ncs/service-provider/mpls-vpn/packages/l3vpn/src/yang$ pyang -f yin l3vpn.yang

/home/cisco/NSO-4.5/src/ncs/yang/tailf-ncs-devices.yang:22: warning: imported module tailf-ncs-monitoring not used

<?xml version="1.0" encoding="UTF-8"?>

<module name="l3vpn"

        xmlns="urn:ietf:params:xml:ns:yang:yin:1"

        xmlns:l3vpn="http://com/example/l3vpn"

        xmlns:inet="urn:ietf:params:xml:ns:yang:ietf-inet-types"

        xmlns:tailf="http://tail-f.com/yang/common"

        xmlns:ncs="http://tail-f.com/ns/ncs">

  <namespace uri="http://com/example/l3vpn"/>

  <prefix value="l3vpn"/>

  <import module="ietf-inet-types">

    <prefix value="inet"/>

  </import>

  <import module="tailf-common">

    <prefix value="tailf"/>

  </import>

(snip)

Best regards,

Hiro

Highlighted

Hi Hirotsugu,

Thanks for the response.

I need xml format same as service instance xml format for the given yang. I tried using yin and format is different.

Ex:

module l3vpn {

   container switch {

       leaf access {

       }

  }

}

Xml fomat:

<switch>

  <access>true</access>

</switch>

Regards,

Sujani.

Highlighted

Hi Sujani,

I don't think that's possible.

If we generate XML from YANG, that means you'll get a data format without any value.

On the other hand, XML in a service instance has an actual value.

There is no way to complement the value from YANG file.

Let's see your example.

module l3vpn {

   container switch {

       leaf access {

       }

  }

}

What you can get here is a container named "switch" and a leaf named "access".

There is no way to find out the leaf "access" has a value "true" as you mentioned.

I think the only way to achieve your goal is to use dry-run outformat=xml via CLI, REST or any NSO APIs.

Best regards,

Hiro

Highlighted

yeah, I just need xml (with out any values is also fine) as you mentioned. Here I don't have an option to create service instance and take out format xml from cli or any other approaches.

I tried with sample-xml-skeleton-defaults using pyang. But not working.

Highlighted

How about using -f rest-doc

MMALYSZ-M-60YE:yang mmalysz$ pyang -f rest-doc --rest-doc-output=patch sample-cfs.yang

# container /samplecfs:cfs-sample

PATCH /api/running/cfs-sample

Content-Type: application/vnd.yang.data+json

{

  "cfs-sample" : {

    "sample-service-list" : {

      "sample-service-id" : "<<SAMPLE-SERVICE-ID>>",

      "sample-service-state" : "...",

      "param-1" : "param-1 value"

    }

  }

}

Then you can use json2xml

https://github.com/mbj4668/pyang/wiki/XmlJson

Highlighted

Yeah, I was able to create JSON using rest doc. From this thread, some one replied saying, we can also generate xml from yang. Tying to find if I can directly generate xml.

But had issues with json2xml. Trying to figure it out.

root@vm--003:~# pip install json2xml

Requirement already satisfied: json2xml in /usr/local/lib/python2.7/dist-packages

Requirement already satisfied: xmltodict==0.11.0 in /usr/local/lib/python2.7/dist-packages (from json2xml)

Requirement already satisfied: six==1.11.0 in /usr/local/lib/python2.7/dist-packages (from json2xml)

Requirement already satisfied: dict2xml==1.5 in /usr/local/lib/python2.7/dist-packages (from json2xml)

Requirement already satisfied: requests==2.18.4 in /usr/local/lib/python2.7/dist-packages (from json2xml)

Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/local/lib/python2.7/dist-packages (from requests==2.18.4->json2xml)

Requirement already satisfied: idna<2.7,>=2.5 in /usr/local/lib/python2.7/dist-packages (from requests==2.18.4->json2xml)

Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python2.7/dist-packages (from requests==2.18.4->json2xml)

Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python2.7/dist-packages (from requests==2.18.4->json2xml)

root@vm-schalaga-003:~# json2xml

json2xml: command not found