Hi,
As suggested, I looked at the TTPUtils for an example on how to serialize and
deserialize the DTOs between machines.
My attempt was to serialize an ElanInstance from the elan.yang model.
This is the model:
container elan-instances {
list elan-instance {
....
First of all, serializing the ElanInstance itself resulted in:
Exception in thread "main" java.lang.IllegalArgumentException: List item is not
appropriate
at
com.google.common.base.Preconditions.checkArgument(Preconditions.java:122)
at
org.opendaylight.yangtools.yang.data.impl.codec.SchemaTracker.startListItem(SchemaTracker.java:144)
at
org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter.startMapEntryNode(JSONNormalizedNodeStreamWriter.java:156)
at
org.opendaylight.yangtools.binding.data.codec.impl.BindingToNormalizedStreamWriter.startMapEntryNode(BindingToNormalizedStreamWriter.java:175)
at
org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance$StreamWriter.serialize(DataObjectSerializerPrototype.java)
at
org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry$DataObjectSerializerProxy.serialize(BindingNormalizedNodeCodecRegistry.java:295)
at
org.opendaylight.newfederation.transform.TTPUtils.jsonStringFromDataObject(TTPUtils.java:146)
at
org.opendaylight.newfederation.tests.JsonParsing.main(JsonParsing.java:73)
Then, I tried to serialize the ElanInstances container initialized with the
ElanInstance, and it succeeded, but with a lot of verbose code, that I guess
could be reduced somehow.
This is the code:
SERIALIZER CODE:
TTPUtils ttp = new
TTPUtils(Collections.singleton(BindingReflections.getModuleInfo(ElanInstances.class)));
ElanInstance newInstance = new
ElanInstanceBuilder().setElanInstanceName("GUYELAN")
.setDescription("ElanDescription").build();
ArrayList<ElanInstance> list = new ArrayList<>();
list.add(newInstance);
ElanInstances instances = new
ElanInstancesBuilder().setElanInstance(list).build();
InstanceIdentifier<ElanInstances> id =
InstanceIdentifier.create(ElanInstances.class);
String jsonStringFromDataObject = ttp.jsonStringFromDataObject(id,
instances, true);
System.out.println(jsonStringFromDataObject);
DESERIALIZER CODE:
NormalizedNode<?, ?> normalized =
ttp.normalizedNodeFromJsonString(jsonStringFromDataObject);
DataObject finalDataObject =
ttp.dataObjectFromNormalizedNode("urn:opendaylight:netvirt:elan", "2015-06-02",
"elan-instances", normalized);
ElanInstances finalInstances = (ElanInstances) finalDataObject;
System.out.println("RESULT: " +
finalInstances.getElanInstance().get(0).getElanInstanceName());
I implemented the ttp.dataObjectFromNormalizedNode myself, like this:
public DataObject dataObjectFromNormalizedNode(String namespace, String
revision, String localName, NormalizedNode<?, ?> nn) {
QName qname = QName.create(namespace, revision, localName);
return
codecRegistry.fromNormalizedNode(YangInstanceIdentifier.of(qname),
nn).getValue();
}
Now to my questions:
1) Is it possible to do what I'm trying to do with less "knowledge" in the
deserializer side? The deserializer had to know that the NormalizedNode it got
was specifically belongs to this model: ("urn:opendaylight:netvirt:elan",
"2015-06-02", "elan-instances"), in order to do the deserialization.
2) How will this work with augmentations that are declared in different
models?
3) Can I use it with a specific ElanInstance? (I got an exception as I
illustrated).
4) I have a feeling I am not doing this in the right way, is there any
example of how to implement what I'm trying to do? If not, can someone help me
with a code snippet ?
Thanks,
Guy Sela
_______________________________________________
controller-dev mailing list
[email protected]
https://lists.opendaylight.org/mailman/listinfo/controller-dev