Revert "BUG 2282 - JSON top level element without module name" 94/12694/1
authorColin Dixon <colin@colindixon.com>
Mon, 10 Nov 2014 20:32:04 +0000 (14:32 -0600)
committerColin Dixon <colin@colindixon.com>
Mon, 10 Nov 2014 20:35:06 +0000 (14:35 -0600)
To roll back to the same code as in the git hash
8c0ae36b12e57323dea7fbd9fa7a79da84dab980 before applying the Helium.1
release patch.

This reverts commit 6eee2a1ce9e69a7b653a7b4427573eff67588d1f.

Change-Id: Id14020c2ee1dcf9869d602bfbd1815d65ced0755
Signed-off-by: Colin Dixon <colin@colindixon.com>
yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JsonParserStream.java
yang/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/JsonStreamToNormalizedNodeTest.java
yang/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/TestingNormalizedNodeStructuresCreator.java
yang/yang-data-codec-gson/src/test/resources/complexjson/missing-module-in-top-level.json [deleted file]
yang/yang-data-codec-gson/src/test/resources/complexjson/namesakes.json [deleted file]
yang/yang-data-codec-gson/src/test/resources/complexjson/not-existing-element.json [deleted file]
yang/yang-data-codec-gson/src/test/resources/complexjson/unkeyed-node-in-container.json
yang/yang-data-codec-gson/src/test/resources/complexjson/yang/complexjson-augmentation-namesake.yang [deleted file]
yang/yang-data-codec-gson/src/test/resources/complexjson/yang/complexjson-augmentation.yang

index 724c85cff1dcbda40d3fbec38f5392edbc1db8de..93946567dcb9080e98d131c9922afc3acdd220a5 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.yang.data.codec.gson;
 
 import com.google.common.annotations.Beta;
+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.gson.JsonIOException;
 import com.google.gson.JsonParseException;
@@ -23,7 +24,6 @@ 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;
 import java.util.List;
@@ -146,7 +146,7 @@ public final class JsonParserStream implements Closeable, Flushable {
             in.beginObject();
             while (in.hasNext()) {
                 final String jsonElementName = in.nextName();
-                final NamespaceAndName namespaceAndName = resolveNamespace(jsonElementName, parent.getSchema());
+                final NamespaceAndName namespaceAndName = resolveNamespace(jsonElementName);
                 final String localName = namespaceAndName.getName();
                 addNamespace(namespaceAndName.getUri());
                 if (namesakes.contains(jsonElementName)) {
@@ -213,11 +213,19 @@ public final class JsonParserStream implements Closeable, Flushable {
         namespaces.pop();
     }
 
-    private void addNamespace(final URI namespace) {
-        namespaces.push(namespace);
+    private void addNamespace(final Optional<URI> namespace) {
+        if (!namespace.isPresent()) {
+            if (namespaces.isEmpty()) {
+                throw new IllegalStateException("Namespace has to be specified at top level.");
+            } else {
+                namespaces.push(namespaces.peek());
+            }
+        } else {
+            namespaces.push(namespace.get());
+        }
     }
 
-    private NamespaceAndName resolveNamespace(final String childName, final DataSchemaNode dataSchemaNode) {
+    private NamespaceAndName resolveNamespace(final String childName) {
         int lastIndexOfColon = childName.lastIndexOf(':');
         String moduleNamePart = null;
         String nodeNamePart = null;
@@ -232,52 +240,8 @@ public final class JsonParserStream implements Closeable, Flushable {
             nodeNamePart = childName;
         }
 
-        if (namespace == null) {
-            Set<URI> potentialUris = Collections.emptySet();
-            potentialUris = resolveAllPotentialNamespaces(nodeNamePart, dataSchemaNode);
-            if (potentialUris.contains(getCurrentNamespace())) {
-                namespace = getCurrentNamespace();
-            } else if (potentialUris.size() == 1) {
-                namespace = potentialUris.iterator().next();
-            } 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.");
-            }
-        }
-
-        return new NamespaceAndName(nodeNamePart, namespace);
-    }
-
-    private String toModuleNames(Set<URI> potentialUris) {
-        final StringBuilder builder = new StringBuilder();
-        for (URI potentialUri : potentialUris) {
-            builder.append("\n");
-            //FIXME how to get information about revision from JSON input? currently first available is used.
-            builder.append(schema.findModuleByNamespace(potentialUri).iterator().next().getName());
-        }
-        return builder.toString();
-    }
-
-    private Set<URI> resolveAllPotentialNamespaces(final String elementName, final DataSchemaNode dataSchemaNode) {
-        final Set<URI> potentialUris = new HashSet<>();
-        final Set<ChoiceNode> choices = new HashSet<>();
-        if (dataSchemaNode instanceof DataNodeContainer) {
-            for (DataSchemaNode childSchemaNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
-                if (childSchemaNode instanceof ChoiceNode) {
-                    choices.add((ChoiceNode)childSchemaNode);
-                } else if (childSchemaNode.getQName().getLocalName().equals(elementName)) {
-                    potentialUris.add(childSchemaNode.getQName().getNamespace());
-                }
-            }
-
-            for (ChoiceNode choiceNode : choices) {
-                for (ChoiceCaseNode concreteCase : choiceNode.getCases()) {
-                    potentialUris.addAll(resolveAllPotentialNamespaces(elementName, concreteCase));
-                }
-            }
-        }
-        return potentialUris;
+        Optional<URI> namespaceOpt = namespace == null ? Optional.<URI> absent() : Optional.of(namespace);
+        return new NamespaceAndName(nodeNamePart, namespaceOpt);
     }
 
     private URI getCurrentNamespace() {
@@ -328,10 +292,10 @@ public final class JsonParserStream implements Closeable, Flushable {
     }
 
     private static class NamespaceAndName {
-        private final URI uri;
+        private final Optional<URI> uri;
         private final String name;
 
-        public NamespaceAndName(final String name, final URI uri) {
+        public NamespaceAndName(final String name, final Optional<URI> uri) {
             this.name = name;
             this.uri = uri;
         }
@@ -340,7 +304,7 @@ public final class JsonParserStream implements Closeable, Flushable {
             return name;
         }
 
-        public URI getUri() {
+        public Optional<URI> getUri() {
             return uri;
         }
     }
index 1a71142be5553ecee451b42edb5fce7d02bf2612..56d12104bd8b86e27888762767bca7f4b5b88d81 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.yangtools.yang.data.codec.gson;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
 
@@ -134,57 +133,6 @@ public class JsonStreamToNormalizedNodeTest {
         verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
     }
 
-    /**
-     * Top level JSON element contains no information about module name.
-     *
-     * It should be possible to find out potential module name from available schema context.
-     *
-     */
-    @Test
-    public void missingModuleInfoInTopLevelElement() throws IOException, URISyntaxException {
-        String inputJson = loadTextFile("/complexjson/missing-module-in-top-level.json");
-        verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.topLevelContainer());
-    }
-
-    /**
-     *
-     * Exception expected.
-     *
-     * It tests case when several elements with the same name and various namespaces exists and are in JSON specified
-     * without module name prefix.
-     */
-    @Test
-    public void leafNamesakes() throws IOException, URISyntaxException {
-        String inputJson = loadTextFile("/complexjson/namesakes.json");
-        try {
-            //second parameter isn't necessary because error will be raised before it is used.
-            verifyTransformationToNormalizedNode(inputJson, null);
-        } catch (IllegalStateException e) {
-            final String errorMessage = e.getMessage();
-            assertTrue(errorMessage.contains("Choose suitable module name for element lf11-namesake:"));
-            assertTrue(errorMessage.contains("complexjson-augmentation"));
-            assertTrue(errorMessage.contains("complexjson-augmentation-namesake"));
-        }
-    }
-
-    /**
-     *
-     * Exception expected.
-     *
-     * Json input contains element which doesn't exist in YANG schema
-     */
-    @Test
-    public void parsingNotExistingElement() throws IOException, URISyntaxException {
-        String inputJson = loadTextFile("/complexjson/not-existing-element.json");
-        try {
-            //second parameter isn't necessary because error will be raised before it is used.
-            verifyTransformationToNormalizedNode(inputJson, null);
-        } catch (IllegalStateException e) {
-            assertTrue(e.getMessage().contains("Schema node with name dummy-element wasn't found."));
-        }
-    }
-
-
     private void verifyTransformationToNormalizedNode(final String inputJson,
             final NormalizedNode<?, ?> awaitedStructure) {
         NormalizedNodeResult result = new NormalizedNodeResult();
index 8898cee98158d17df767e405a14d43ef1ea7818a..1038ce883db6d8389f6ed15a77cc243b05011e0a 100644 (file)
@@ -314,7 +314,4 @@ public class TestingNormalizedNodeStructuresCreator {
         return cont1Node(lst12Node());
     }
 
-    public static NormalizedNode<?, ?> topLevelContainer() {
-        return cont1Node();
-    }
 }
diff --git a/yang/yang-data-codec-gson/src/test/resources/complexjson/missing-module-in-top-level.json b/yang/yang-data-codec-gson/src/test/resources/complexjson/missing-module-in-top-level.json
deleted file mode 100644 (file)
index 1070c36..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-{
-    "cont1": {
-    }
-}
diff --git a/yang/yang-data-codec-gson/src/test/resources/complexjson/namesakes.json b/yang/yang-data-codec-gson/src/test/resources/complexjson/namesakes.json
deleted file mode 100644 (file)
index 9c0fab4..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-{
-    "cont1": {
-        "lf11-namesake":"value lf11"
-    }
-}
\ No newline at end of file
diff --git a/yang/yang-data-codec-gson/src/test/resources/complexjson/not-existing-element.json b/yang/yang-data-codec-gson/src/test/resources/complexjson/not-existing-element.json
deleted file mode 100644 (file)
index ebc44bb..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-{
-    "cont1": {
-        "dummy-element":"value lf11"
-    }
-}
\ No newline at end of file
diff --git a/yang/yang-data-codec-gson/src/test/resources/complexjson/yang/complexjson-augmentation-namesake.yang b/yang/yang-data-codec-gson/src/test/resources/complexjson/yang/complexjson-augmentation-namesake.yang
deleted file mode 100644 (file)
index e94acb2..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-module complexjson-augmentation-namesake {
-    namespace "ns:complex:json:augmentation:namesake";
-    prefix cjaugnmsk;
-
-  import complexjson {
-    prefix cj;
-  }
-
-    revision "2014-08-14" {
-    }
-
-    augment "/cj:cont1" {
-        leaf lf11-namesake {
-            type string;
-        }
-    }
-
-}
index a4cf5adeb52738569597e4aa6614ea2ff8e5a12b..d4ea623b19098c9d073b2d00012839ad99f16113 100644 (file)
@@ -1,30 +1,24 @@
 module complexjson-augmentation {
     namespace "ns:complex:json:augmentation";
     prefix cjaug;
-
+    
   import complexjson {
     prefix cj;
-  }
+  }    
 
-    revision "2014-08-14" {
+    revision "2014-08-14" {        
     }
-
-    augment "/cj:cont1" {
-        leaf lf11-namesake {
-            type string;
-        }
-    }
-
+    
     augment "/cj:cont1/cj:choc11/cj:c11A" {
-        leaf lf15_11 {
+        leaf lf15_11  {
                     type string;
                 }
-        leaf lf15_12 {
+        leaf lf15_12  {
                     type string;
                 }
-
-    }
-
+                
+    }    
+    
     augment "/cj:cont1" {
         leaf lf12_1aug {
                     type string;
@@ -32,12 +26,12 @@ module complexjson-augmentation {
         leaf lf12_2aug {
             type string;
         }
-    }
-
+    }    
+    
     augment "/cj:cont1/cj:choc11/cj:c11A" {
         leaf lf15_21aug {
                     type string;
                 }
-    }
+    }    
 
 }