Provide better error messages when parsing JSON.
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JsonParserStream.java
index aad07b35665f9c8c15390a659cb0af448cd45983..69cfb6fea44f7a908467203b78fed2b19858c025 100644 (file)
@@ -21,7 +21,6 @@ import java.io.IOException;
 import java.net.URI;
 import java.util.ArrayDeque;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.Deque;
 import java.util.HashSet;
@@ -34,13 +33,10 @@ import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 
 /**
  * This class parses JSON elements from a GSON JsonReader. It disallows multiple elements of the same name unlike the
@@ -109,13 +105,13 @@ public final class JsonParserStream implements Closeable, Flushable {
     }
 
     private final void setValue(final AbstractNodeDataWithSchema parent, final String value) {
-        Preconditions.checkArgument(parent instanceof SimpleNodeDataWithSchema, "Node %s is not a simple type", parent);
+        Preconditions.checkArgument(parent instanceof SimpleNodeDataWithSchema, "Node %s is not a simple type", parent.getSchema().getQName());
 
         final Object translatedValue = translateValueByType(value, parent.getSchema());
         ((SimpleNodeDataWithSchema) parent).setValue(translatedValue);
     }
 
-    public void read(final JsonReader in, final AbstractNodeDataWithSchema parent) throws IOException {
+    public void read(final JsonReader in, AbstractNodeDataWithSchema parent) throws IOException {
         switch (in.peek()) {
         case STRING:
         case NUMBER:
@@ -131,21 +127,28 @@ public final class JsonParserStream implements Closeable, Flushable {
         case BEGIN_ARRAY:
             in.beginArray();
             while (in.hasNext()) {
-                AbstractNodeDataWithSchema newChild = null;
-                if (parent instanceof ListNodeDataWithSchema) {
-                    newChild = new ListEntryNodeDataWithSchema(parent.getSchema());
-                    ((CompositeNodeDataWithSchema) parent).addChild(newChild);
-                } else if (parent instanceof LeafListNodeDataWithSchema) {
-                    newChild = new LeafListEntryNodeDataWithSchema(parent.getSchema());
-                    ((CompositeNodeDataWithSchema) parent).addChild(newChild);
+                if (parent instanceof LeafNodeDataWithSchema) {
+                    read(in, parent);
+                } else {
+                    final AbstractNodeDataWithSchema newChild = newArrayEntry(parent);
+                    read(in, newChild);
                 }
-                read(in, newChild);
             }
             in.endArray();
             return;
         case BEGIN_OBJECT:
             final Set<String> namesakes = new HashSet<>();
             in.beginObject();
+            /*
+             * This allows parsing of incorrectly /as showcased/
+             * in testconf nesting of list items - eg.
+             * lists with one value are sometimes serialized
+             * without wrapping array.
+             *
+             */
+            if(isArray(parent)) {
+                parent = newArrayEntry(parent);
+            }
             while (in.hasNext()) {
                 final String jsonElementName = in.nextName();
                 final NamespaceAndName namespaceAndName = resolveNamespace(jsonElementName, parent.getSchema());
@@ -162,9 +165,12 @@ public final class JsonParserStream implements Closeable, Flushable {
                             + getCurrentNamespace() + " doesn't exist.");
                 }
 
-                AbstractNodeDataWithSchema newChild;
-                newChild = ((CompositeNodeDataWithSchema) parent).addChild(childDataSchemaNodes);
-//                FIXME:anyxml data shouldn't be skipped but should be loaded somehow. will be specified after 17AUG2014
+                final AbstractNodeDataWithSchema newChild = ((CompositeNodeDataWithSchema) parent).addChild(childDataSchemaNodes);
+                /*
+                 * FIXME:anyxml data shouldn't be skipped but should be loaded somehow.
+                 * will be able to load anyxml which conforms to YANG data using these
+                 * parser, for other anyxml will be harder.
+                 */
                 if (newChild instanceof AnyXmlNodeDataWithSchema) {
                     in.skipValue();
                 } else {
@@ -182,33 +188,33 @@ public final class JsonParserStream implements Closeable, Flushable {
         }
     }
 
-    private Object translateValueByType(final String value, final DataSchemaNode node) {
-        final TypeDefinition<? extends Object> typeDefinition = typeDefinition(node);
-        if (typeDefinition == null) {
-            return value;
-        }
-
-        return codecs.codecFor(typeDefinition).deserialize(value);
+    private static boolean isArray(final AbstractNodeDataWithSchema parent) {
+        return parent instanceof ListNodeDataWithSchema || parent instanceof LeafListNodeDataWithSchema;
     }
 
-    private static TypeDefinition<? extends Object> typeDefinition(final DataSchemaNode node) {
-        TypeDefinition<?> baseType = null;
-        if (node instanceof LeafListSchemaNode) {
-            baseType = ((LeafListSchemaNode) node).getType();
-        } else if (node instanceof LeafSchemaNode) {
-            baseType = ((LeafSchemaNode) node).getType();
-        } else if (node instanceof AnyXmlSchemaNode) {
-            return null;
+    private AbstractNodeDataWithSchema newArrayEntry(final AbstractNodeDataWithSchema parent) {
+        AbstractNodeDataWithSchema newChild;
+        if (parent instanceof ListNodeDataWithSchema) {
+            newChild = new ListEntryNodeDataWithSchema(parent.getSchema());
+        } else if (parent instanceof LeafListNodeDataWithSchema) {
+            newChild = new LeafListEntryNodeDataWithSchema(parent.getSchema());
         } else {
-            throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object> asList(node).toString());
+            throw new IllegalStateException("Found an unexpected array nested under "+ parent.getSchema().getQName());
         }
+        ((CompositeNodeDataWithSchema) parent).addChild(newChild);
+        return newChild;
+    }
 
-        if (baseType != null) {
-            while (baseType.getBaseType() != null) {
-                baseType = baseType.getBaseType();
-            }
+    private Object translateValueByType(final String value, final DataSchemaNode node) {
+        if (node instanceof AnyXmlSchemaNode) {
+            /*
+             *  FIXME: Figure out some YANG extension dispatch, which will
+             *  reuse JSON parsing or XML parsing - anyxml is not well-defined in
+             * JSON.
+             */
+            return value;
         }
-        return baseType;
+        return codecs.codecFor(node).deserialize(value);
     }
 
     private void removeNamespace() {
@@ -244,7 +250,7 @@ public final class JsonParserStream implements Closeable, Flushable {
             } else if (potentialUris.size() > 1) {
                 throw new IllegalStateException("Choose suitable module name for element "+nodeNamePart+":"+toModuleNames(potentialUris));
             } else if (potentialUris.isEmpty()) {
-                throw new IllegalStateException("Schema node with name "+nodeNamePart+" wasn't found.");
+                throw new IllegalStateException("Schema node with name "+nodeNamePart+" wasn't found under "+dataSchemaNode.getQName()+".");
             }
         }
 
@@ -301,19 +307,28 @@ public final class JsonParserStream implements Closeable, Flushable {
             final String childName, final URI namespace) {
         final Deque<DataSchemaNode> result = new ArrayDeque<>();
         final List<ChoiceSchemaNode> childChoices = new ArrayList<>();
+        DataSchemaNode potentialChildNode = null;
         if (dataSchemaNode instanceof DataNodeContainer) {
             for (final DataSchemaNode childNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
                 if (childNode instanceof ChoiceSchemaNode) {
                     childChoices.add((ChoiceSchemaNode) childNode);
                 } else {
                     final QName childQName = childNode.getQName();
+
                     if (childQName.getLocalName().equals(childName) && childQName.getNamespace().equals(namespace)) {
-                        result.push(childNode);
-                        return result;
+                        if (potentialChildNode == null ||
+                                childQName.getRevision().after(potentialChildNode.getQName().getRevision())) {
+                            potentialChildNode = childNode;
+                        }
                     }
                 }
             }
         }
+        if (potentialChildNode != null) {
+            result.push(potentialChildNode);
+            return result;
+        }
+
         // try to find data schema node in choice (looking for first match)
         for (final ChoiceSchemaNode choiceNode : childChoices) {
             for (final ChoiceCaseNode concreteCase : choiceNode.getCases()) {