1
votes

I have a Yang file, i want to send data using yang schema in an xml format how do i do that.

Suppose i have yang file like below

module jtest {
    namespace "jtest";
    prefix jtest;

    container jtest {
        container mycontainer1 {
            leaf myleaf1 {
                type string;
            }
        }

        container mycontainer2 {
            container innercontainer {
                leaf myleaf2 {
                    type string;
                }
            }
            leaf myleaf3 {
                type string;
            }
        }
        container mycontainer3 {
            leaf myleaf1 {
                type string;
            }
        }

    }
}

I want to send data in xml format as per yang schema, how do serialize or convert yang to xml.

2
You probably meant to ask how one could generate a sample XML instance document based on your YANG module. You could use a tool for that. I believe pyang has the ability to generate a skeleton for various XML payloads. - predi
Predi, i did use pyang with sample-xml-skeleton it does create xml. But what exactly i want is to serialize yang to xml and send it across and at the receiver side they deserialize it . - Jin
What do you mean by "serialize yang to xml"? yang is a schema or template - xml is one possible representation of actual payload data that conforms to the schema. - Tom Pantelis
If so, then you can simply send the content of a YANG module as a text value in a RPC reply. No need to serialize the statements into YIN. Perhaps take a look at ietf-netconf-monitoring and its get-schema operation to see what I mean. - predi

2 Answers

1
votes

Assuming (as per @predi comment) that you are probably asking about how to create an XML instance conforming to a YANG schema:

I'm not 100% sure if you mean programmatically (by code) or as an end-users, and if you use OpenDaylight (ODL) or not, but just in case, the DAEXIM project in ODL imports YANG from the ODL datastore to and from JSON, so you may be interested in that? If you are an end-user, then perhaps the Data Export/Import User Guide is of use to you. If you are a developer, then have a look at the ExportTask class to learn how it writes JSON - and you should then be able to use ODL yangtools' XmlCodecFactory similarly to how DAEXIM is using the JSONCodecFactory to write XML instead of JSON.

If you want to to tranform a YANG schema itself to XML, that's what YIN is for (but I don't think that's what you are asking).

0
votes

You can use the pyang to generate xml file from yang file:

$ pyang -h
Usage: pyang [options] [<filename>...]
 
  -f FORMAT, --format=FORMAT
                        Convert to FORMAT.  Supported formats are: yang, yin,
                        dsdl, capability, depend, jsonxsl, jstree, jtox, name,
                        omni, sample-xml-skeleton, tree, uml
 
  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

like this:

pyang -f sample-xml-skeleton --sample-xml-skeleton-defaults -o output.xml input.yang