X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frest%2Fimpl%2FJsonMapper.java;h=5a1b42fd8009d074613407088c94359bac5dc6d9;hb=7007ef0c2b418b9b49e56e6c3525e8906fefa522;hp=04556bbe547a8d22acd9d8584be1ff4386c1ed5f;hpb=7612a3b8684d2f8d4d42404bed8a688a440cb486;p=controller.git diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java index 04556bbe54..5a1b42fd80 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java @@ -26,6 +26,7 @@ import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefi import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; +import com.google.common.base.Preconditions; import com.google.gson.stream.JsonWriter; class JsonMapper { @@ -34,12 +35,16 @@ class JsonMapper { private final Set foundLists = new HashSet<>(); public void write(JsonWriter writer, CompositeNode data, DataNodeContainer schema) throws IOException { + Preconditions.checkNotNull(writer); + Preconditions.checkNotNull(data); + Preconditions.checkNotNull(schema); + writer.beginObject(); if (schema instanceof ContainerSchemaNode) { - writeContainer(writer, (CompositeNode) data, (ContainerSchemaNode) schema); + writeContainer(writer, data, (ContainerSchemaNode) schema); } else if (schema instanceof ListSchemaNode) { - writeList(writer, (CompositeNode) data, (ListSchemaNode) schema); + writeList(writer, data, (ListSchemaNode) schema); } else { throw new UnsupportedDataTypeException( "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet."); @@ -56,25 +61,33 @@ class JsonMapper { checkNotNull(parentSchema); for (Node child : parent.getChildren()) { - DataSchemaNode childSchema = findSchemaForNode(child, parentSchema.getChildNodes()); + DataSchemaNode childSchema = findFirstSchemaForNode(child, parentSchema.getChildNodes()); if (childSchema == null) { throw new UnsupportedDataTypeException("Probably the data node \"" + child.getNodeType().getLocalName() + "\" is not conform to schema"); } if (childSchema instanceof ContainerSchemaNode) { + Preconditions.checkState(child instanceof CompositeNode, + "Data representation of Container should be CompositeNode - " + child.getNodeType()); writeContainer(writer, (CompositeNode) child, (ContainerSchemaNode) childSchema); } else if (childSchema instanceof ListSchemaNode) { if (!foundLists.contains(childSchema)) { + Preconditions.checkState(child instanceof CompositeNode, + "Data representation of List should be CompositeNode - " + child.getNodeType()); foundLists.add((ListSchemaNode) childSchema); writeList(writer, (CompositeNode) child, (ListSchemaNode) childSchema); } } else if (childSchema instanceof LeafListSchemaNode) { if (!foundLeafLists.contains(childSchema)) { + Preconditions.checkState(child instanceof SimpleNode, + "Data representation of LeafList should be SimpleNode - " + child.getNodeType()); foundLeafLists.add((LeafListSchemaNode) childSchema); writeLeafList(writer, (SimpleNode) child, (LeafListSchemaNode) childSchema); } } else if (childSchema instanceof LeafSchemaNode) { + Preconditions.checkState(child instanceof SimpleNode, + "Data representation of LeafList should be SimpleNode - " + child.getNodeType()); writeLeaf(writer, (SimpleNode) child, (LeafSchemaNode) childSchema); } else { throw new UnsupportedDataTypeException("Schema can be ContainerSchemaNode, ListSchemaNode, " @@ -83,7 +96,7 @@ class JsonMapper { } for (Node child : parent.getChildren()) { - DataSchemaNode childSchema = findSchemaForNode(child, parentSchema.getChildNodes()); + DataSchemaNode childSchema = findFirstSchemaForNode(child, parentSchema.getChildNodes()); if (childSchema instanceof LeafListSchemaNode) { foundLeafLists.remove((LeafListSchemaNode) childSchema); } else if (childSchema instanceof ListSchemaNode) { @@ -92,7 +105,7 @@ class JsonMapper { } } - private DataSchemaNode findSchemaForNode(Node node, Set dataSchemaNode) { + private DataSchemaNode findFirstSchemaForNode(Node node, Set dataSchemaNode) { for (DataSchemaNode dsn : dataSchemaNode) { if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) { return dsn; @@ -169,7 +182,7 @@ class JsonMapper { writer.nullValue(); writer.endArray(); } else { - writer.value(value); + writer.value(value != null ? value : ""); } }