Merge "Leafref and identityref types to Json"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / JsonMapper.java
index 217ef14c647813aa8595f42fda26a2207091751a..a42c468c2af27ea5f2cb84887757ed8057a4466f 100644 (file)
@@ -8,6 +8,7 @@ import java.util.*;
 import javax.activation.UnsupportedDataTypeException;
 
 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.*;
 import org.opendaylight.yangtools.yang.model.api.*;
 import org.opendaylight.yangtools.yang.model.api.type.*;
@@ -49,6 +50,7 @@ class JsonMapper {
 
         for (Node<?> child : parent.getChildren()) {
             DataSchemaNode childSchema = findFirstSchemaForNode(child, parentSchema.getChildNodes());
+
             if (childSchema == null) {
                 throw new UnsupportedDataTypeException("Probably the data node \"" + child.getNodeType().getLocalName()
                         + "\" is not conform to schema");
@@ -96,6 +98,13 @@ class JsonMapper {
         for (DataSchemaNode dsn : dataSchemaNode) {
             if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
                 return dsn;
+            } else if (dsn instanceof ChoiceNode) {
+                for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
+                    DataSchemaNode foundDsn = findFirstSchemaForNode(node, choiceCase.getChildNodes());
+                    if (foundDsn != null) {
+                        return foundDsn;
+                    }
+                }
             }
         }
         return null;
@@ -136,28 +145,43 @@ class JsonMapper {
 
         List<SimpleNode<?>> nodeLeafLists = nodeParent.getSimpleNodesByName(node.getNodeType());
         for (SimpleNode<?> nodeLeafList : nodeLeafLists) {
-            writeValueOfNodeByType(writer, nodeLeafList, schema.getType());
+            writeValueOfNodeByType(writer, nodeLeafList, schema.getType(), schema);
         }
-
         writer.endArray();
     }
 
     private void writeLeaf(JsonWriter writer, SimpleNode<?> node, LeafSchemaNode schema) throws IOException {
         writeName(node, schema, writer);
-        writeValueOfNodeByType(writer, node, schema.getType());
+        writeValueOfNodeByType(writer, node, schema.getType(), schema);
     }
 
-    private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type)
-            throws IOException {
-        if (!(node.getValue() instanceof String)) {
-            throw new IllegalStateException("Value in SimpleNode should be type String");
-        }
+    private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type,
+            DataSchemaNode schema) throws IOException {
 
-        String value = (String) node.getValue();
-        // TODO check Leafref, InstanceIdentifierTypeDefinition,
-        // IdentityrefTypeDefinition, UnionTypeDefinition
+        String value = String.valueOf(node.getValue());
         TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
-        if (baseType instanceof InstanceIdentifierTypeDefinition) {
+
+        // TODO check InstanceIdentifierTypeDefinition,
+        // IdentityrefTypeDefinition
+        if (baseType instanceof IdentityrefTypeDefinition) {
+            if (node.getValue() instanceof QName) {
+                QName qName = (QName) node.getValue();
+
+                ControllerContext contContext = ControllerContext.getInstance();
+                String moduleName = contContext.findModuleByNamespace(qName.getNamespace());
+
+                writer.value(moduleName + ":" + qName.getLocalName());
+            }
+
+        } else if (baseType instanceof LeafrefTypeDefinition) {
+            ControllerContext contContext = ControllerContext.getInstance();
+            LeafSchemaNode lfSchemaNode = contContext.resolveTypeFromLeafref((LeafrefTypeDefinition) baseType, schema);
+            if (lfSchemaNode != null) {
+                writeValueOfNodeByType(writer, node, lfSchemaNode.getType(), lfSchemaNode);
+            } else {
+                writer.value(value);
+            }
+        } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
             writer.value(((InstanceIdentifierTypeDefinition) baseType).getPathStatement().toString());
         } else if (baseType instanceof UnionTypeDefinition) {
             processTypeIsUnionType(writer, (UnionTypeDefinition) baseType, value);
@@ -169,7 +193,7 @@ class JsonMapper {
         } else if (baseType instanceof EmptyTypeDefinition) {
             writeEmptyDataTypeToJson(writer);
         } else {
-            writer.value(value != null ? value : "");
+            writer.value(value.equals("null") ? "" : value);
         }
     }