I've been trying to find something that can generate sample xml/json data from the yang model in java, For eg for xsd there are tools where you can generate the Sample xml.
I tried Pyang: 1. Its in Python. 2. After conversion it gives me yin format which is xml equivalent of yang specs. for Eg. for following yang code if I convert it to YIN using pyang:
leaf templateSendPeriod {
when "../exportProtocol!='netflow-v5'";
type uint16;
default 60;
units seconds;
}
This is what I got-
<leaf name="templateSendPeriod">
<when condition="../exportProtocol!='netflow-v5'"/>
<type name="uint16"/>
<default value="60"/>
<units name="seconds"/>
</leaf>
Instead what I wanted was
<templateSendPeriod></templateSendPeriod>
So that I could get the xml, enter the details and validate against the same yang.
You can do this way, First declare your model
// module name
module napalm-star-wars {
// boilerplate
yang-version "1";
namespace "https://napalm-yang.readthedocs.io/napalm-star-wars";
prefix "napalm-star-wars";
// identity to unequivocally identify the faction an individual belongs to
identity AFFILIATION {
description "To which group someone belongs to";
}
identity EMPIRE {
base AFFILIATION;
description "Affiliated to the empire";
}
identity REBEL_ALLIANCE {
base AFFILIATION;
description "Affiliated to the rebel alliance";
}
// new type to enforce correctness of the data
typedef age {
type uint16 {
range 1..2000;
}
}
// this grouping will all the personal data we will assign to individuals
grouping personal-data {
leaf name {
type string;
}
leaf age {
type age;
}
leaf affiliation {
type identityref {
base napalm-star-wars:AFFILIATION;
}
}
}
// this is the root object defined by the model
container universe {
list individual {
// identify each individual by using the name as key
key "name";
// each individual will have the elements defined in the grouping
uses personal-data;
}
}
}
Do tree representation of your model
$ pyang -f tree napalm-star-wars.yang
module: napalm-star-wars
+--rw roster
+--rw individual* [name]
+--rw name string
+--rw age? age
+--rw affiliation? identityref
later use this in your python code:
>>> import napalm_star_wars
>>>
>>> sw = napalm_star_wars.napalm_star_wars()
>>>
>>> obi = sw.universe.individual.add("Obi-Wan Kenobi")
>>> obi.affiliation = "REBEL_ALLIANCE"
>>> obi.age = 57
>>>
>>> luke = sw.universe.individual.add("Luke Skywalker")
>>> luke.affiliation = "REBEL_ALLIANCE"
>>> luke.age = 19
Here you get the answer for getting json or xml as per your choice..
import json
>>> print(json.dumps(sw.get(), indent=4))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With