Bug-6195: Fix issue with leafSetEntryNode in SchemaTracker 75/41875/3
authorAjay <ajayl.bro@gmail.com>
Fri, 15 Jul 2016 06:37:23 +0000 (06:37 +0000)
committerRyan Goulding <ryandgoulding@gmail.com>
Wed, 20 Jul 2016 13:30:35 +0000 (09:30 -0400)
Changed SchemaTracker:leafSetEntryNode so that if LeafSetEntryNode
is present without it's parent LeafEntryNode in data-change notification,
it's serialization is handled properly.  The existing leafSetEntryNode
functionality is kept around for API compatability reasons, but should
be removed in Boron as stated in the FIXME.

Change-Id: I6cf936752588c671268d11a0c277ccccd806e086
Signed-off-by: Ajay <ajayl.bro@gmail.com>
Signed-off-by: Ryan Goulding <ryandgoulding@gmail.com>
yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONNormalizedNodeStreamWriter.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/SchemaTracker.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XMLStreamNormalizedNodeStreamWriter.java

index e5dc228b334a3dd88802f2a74d56cbb5c916a652..e3b105e1f9c5d5c559a7c1dc7ba6d5ea32a2c933 100644 (file)
@@ -106,7 +106,7 @@ public final class JSONNormalizedNodeStreamWriter implements NormalizedNodeStrea
 
     @Override
     public void leafSetEntryNode(final QName name, final Object value) throws IOException {
-        final LeafListSchemaNode schema = tracker.leafSetEntryNode();
+        final LeafListSchemaNode schema = tracker.leafSetEntryNode(name);
         final JSONCodec<Object> codec = codecs.codecFor(schema);
         context.emittingChild(codecs.getSchemaContext(), writer);
         writeValue(value, codec);
index 472be45cbafb0a62845cc58147121a757eb0e15d..a19c23b07bdb14a8064bb78a93bd783af9d39bc2 100644 (file)
@@ -57,41 +57,47 @@ public final class SchemaTracker {
     private SchemaTracker(final SchemaContext context, final SchemaPath path) {
         SchemaNode current = Preconditions.checkNotNull(context);
         for (final QName qname : path.getPathFromRoot()) {
-            SchemaNode child;
-            if(current instanceof DataNodeContainer) {
-                child = ((DataNodeContainer) current).getDataChildByName(qname);
-
-                if (child == null && current instanceof SchemaContext) {
-                    child = tryFindGroupings((SchemaContext) current, qname).orNull();
-                }
-
-                if(child == null && current instanceof SchemaContext) {
-                    child = tryFindNotification((SchemaContext) current, qname)
-                            .or(tryFindRpc(((SchemaContext) current), qname)).orNull();
-                }
-            } else if (current instanceof ChoiceSchemaNode) {
-                child = ((ChoiceSchemaNode) current).getCaseNodeByName(qname);
-            } else if (current instanceof RpcDefinition) {
-                switch (qname.getLocalName()) {
-                    case "input":
-                        child = ((RpcDefinition) current).getInput();
-                        break;
-                    case "output":
-                        child = ((RpcDefinition) current).getOutput();
-                        break;
-                    default:
-                        child = null;
-                        break;
-                }
-            } else {
-                throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", current));
-            }
-            current = child;
+            current = getChild(current, qname);
         }
         Preconditions.checkArgument(current instanceof DataNodeContainer,"Schema path must point to container or list or an rpc input/output. Supplied path %s pointed to: %s",path,current);
         root = (DataNodeContainer) current;
     }
 
+    private SchemaNode getChild(final SchemaNode node, final QName qname) {
+        SchemaNode child = null;
+
+        if (node instanceof DataNodeContainer) {
+            child = ((DataNodeContainer) node).getDataChildByName(qname);
+
+            if (child == null && node instanceof SchemaContext) {
+                child = tryFindGroupings((SchemaContext) node, qname).orNull();
+            }
+
+            if(child == null && node instanceof SchemaContext) {
+                child = tryFindNotification((SchemaContext) node, qname)
+                        .or(tryFindRpc(((SchemaContext) node), qname)).orNull();
+            }
+        } else if (node instanceof ChoiceSchemaNode) {
+            child = ((ChoiceSchemaNode) node).getCaseNodeByName(qname);
+        } else if (node instanceof RpcDefinition) {
+            switch (qname.getLocalName()) {
+                case "input":
+                    child = ((RpcDefinition) node).getInput();
+                    break;
+                case "output":
+                    child = ((RpcDefinition) node).getOutput();
+                    break;
+                default:
+                    child = null;
+                    break;
+            }
+        } else {
+            throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", node));
+        }
+
+        return child;
+    }
+
     private static Optional<SchemaNode> tryFindGroupings(final SchemaContext ctx, final QName qname) {
         return Optional.<SchemaNode> fromNullable(Iterables.find(ctx.getGroupings(), new SchemaNodePredicate(qname), null));
     }
@@ -207,6 +213,11 @@ public final class SchemaTracker {
         return (LeafListSchemaNode)schema;
     }
 
+    /*
+     * FIXME: This method is kept for API compatability in stable/beryllium and should be removed in boron.
+     * It has been marked Deprecated to dissuade continued use in the stable/beryllium branch.
+     */
+    @Deprecated
     public LeafListSchemaNode leafSetEntryNode() {
         final Object parent = getParent();
 
@@ -214,6 +225,18 @@ public final class SchemaTracker {
         return (LeafListSchemaNode) parent;
     }
 
+    public LeafListSchemaNode leafSetEntryNode(final QName qname) {
+        final Object parent = getParent();
+        if (parent instanceof LeafListSchemaNode) {
+            return (LeafListSchemaNode) parent;
+        } else {
+            final SchemaNode child = getChild((SchemaNode) parent, qname);
+            Preconditions.checkArgument(child instanceof LeafListSchemaNode,
+                    "Node %s is neither a leaf-list nor currently in a leaf-list", child.getPath());
+            return (LeafListSchemaNode) child;
+        }
+    }
+
     public ChoiceSchemaNode startChoiceNode(final NodeIdentifier name) {
         LOG.debug("Enter choice {}", name);
         final SchemaNode schema = getSchema(name);
index 028374965f32a82bd3c4c29a95e6024cd494e6f9..ad56e2f9e0f7b0eb7d55fb89ee11c90226b7ac03 100644 (file)
@@ -154,7 +154,7 @@ public final class XMLStreamNormalizedNodeStreamWriter implements NormalizedNode
 
     @Override
     public void leafSetEntryNode(final QName name, final Object value, final Map<QName, String> attributes) throws IOException {
-        final LeafListSchemaNode schema = tracker.leafSetEntryNode();
+        final LeafListSchemaNode schema = tracker.leafSetEntryNode(name);
         writeElement(schema.getQName(), schema, value, attributes);
     }
 
@@ -206,7 +206,7 @@ public final class XMLStreamNormalizedNodeStreamWriter implements NormalizedNode
 
     @Override
     public void leafSetEntryNode(final QName name, final Object value) throws IOException {
-        final LeafListSchemaNode schema = tracker.leafSetEntryNode();
+        final LeafListSchemaNode schema = tracker.leafSetEntryNode(name);
         writeElement(schema.getQName(), schema, value);
     }