JSON from yang object builder


M. Ranganathan
 


I am writing a java opendaylight application.

I have a YANG-tool generated java class which I populate with appropriate values and  that I would like to POST to another service. I believe JSON-RPC seems to provide me the facility that I need to do this.  However, I need to do something more :

From the generated Java source, I can use a builder to populate a java object corresponding to the YANG model.  I now want to convert this to JSON String so I can log it.

 I know I need create a NormalizedNode<? ?> from the builder of my YANG model to achieve this but I am not sure how to do that.

I would greatly appreciate some pointers / code examples on how to proceed.

Thank you in advance for your help.

Regards, Ranga


--
M. Ranganathan


Richard Kosegi
 

Hi Ranganathan,

 I know I need create a NormalizedNode<? ?> from the builder of my YANG model to achieve this but I am not sure how to do that.

You need to inject  org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer into your bean via blueprint.
Then you can use something like this:

YourClass yourClassInstance = new YourClassBuilder().build(); // This is an instance of your generated class
InstanceIdentifier<YourClass> ii = ... //this is instance identifier of your generated class.

BindingNormalizedNodeSerializer codec = ... //get this from OSGi registry via Blueprint

Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> e = codec = toNormalizedNode(ii, yourClassInstance);

Now you have NormalizedNode and YangInstanceIdentifier in Entry.

To create JSON, you can use this:

    private static final JSONCodecFactorySupplier CODEC_SUPPLIER =
            JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02;

        JSONCodecFactory jsonCodecFactory = JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02
                .createLazy(schemaContext);

        try (NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(
                JSONNormalizedNodeStreamWriter.createNestedWriter(codecFactory, yii, null, jsonWriter),
                true)) {
            nnWriter.write(node);
            nnWriter.flush();
            jsonWriter.endObject();
        }
 
You can obtain SchemaContext from org.opendaylight.mdsal.dom.api.DOMSchemaService (inject via blueprint)

Codec itself is imported via

        <dependency>
            <groupId>org.opendaylight.yangtools</groupId>
            <artifactId>yang-data-codec-gson</artifactId>
        </dependency>

in your pom.xml



Regards,
Richard.

I would greatly appreciate some pointers / code examples on how to proceed.

Thank you in advance for your help.

Regards, Ranga


--
M. Ranganathan

_______________________________________________
jsonrpc-dev mailing list
jsonrpc-dev@...
https://lists.opendaylight.org/mailman/listinfo/jsonrpc-dev