2

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.

vorburger
  • 3,439
  • 32
  • 38
Jin
  • 21
  • 1
  • 3
  • 1
    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](https://github.com/mbj4668/pyang) has the ability to generate a skeleton for various XML payloads. – predi Aug 24 '18 at 07:57
  • 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 Aug 24 '18 at 13:22
  • 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 Aug 24 '18 at 17:19
  • 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](https://tools.ietf.org/html/rfc6022) and its `get-schema` operation to see what I mean. – predi Aug 27 '18 at 06:39

2 Answers2

1

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).

vorburger
  • 3,439
  • 32
  • 38
  • vorburger, i want to use ODL, i looked into yang-xml-codec but all i see in the test case is already they have xml generated for yang. What i want is programmatically generate yang serialized xml with data which then can be deserialized back. – Jin Aug 24 '18 at 13:24
  • I've not tried this out myself, but my understanding is that you should be able to use a NormalizedNodeWriter.forStreamWriter() like you can see DAEXIM's ExportTask, and use a XmlCodecFactory instead of a JSONCodecFactory. If this doesn't help, it's perhaps best to post a more specific API usage question to the https://lists.opendaylight.org/mailman/listinfo/yangtools-dev mailing list. – vorburger Aug 27 '18 at 09:16
  • some links are not existed any more – Majid Roustaei May 07 '20 at 07:04
1

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