Add MultipleEntryDataWithSchema interface 03/90303/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 5 Jun 2020 13:54:51 +0000 (15:54 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 5 Jun 2020 14:30:50 +0000 (16:30 +0200)
We are sharing essentially the same code in both JSON and XML
parsers, where we really to make this common code exposed from
appropriate NodeDataWithSchema.

Centralizing the interface contract and implementations allows us
to ditch external users of CompositeNodeDataWithSchema.addChild(),
which is now deprecated.

Change-Id: I5aca3d3eecba3f9e6ffee4b9ee15e70afa47aed6
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 94336a79d974543e50cd5e9047d6bd148e4ddbda)

yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JsonParserStream.java
yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlParserStream.java
yang/yang-data-util/src/main/java/org/opendaylight/yangtools/yang/data/util/CompositeNodeDataWithSchema.java
yang/yang-data-util/src/main/java/org/opendaylight/yangtools/yang/data/util/LeafListNodeDataWithSchema.java
yang/yang-data-util/src/main/java/org/opendaylight/yangtools/yang/data/util/ListEntryNodeDataWithSchema.java
yang/yang-data-util/src/main/java/org/opendaylight/yangtools/yang/data/util/ListNodeDataWithSchema.java
yang/yang-data-util/src/main/java/org/opendaylight/yangtools/yang/data/util/MultipleEntryDataWithSchema.java [new file with mode: 0644]

index acfc3afa4e8106b2a2e818bd3aca91d27c271723..a6160c3cb73b87cafe96d4dd8cd007b6f3971f84 100644 (file)
@@ -37,11 +37,10 @@ import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStre
 import org.opendaylight.yangtools.yang.data.util.AbstractNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.AnyXmlNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema;
-import org.opendaylight.yangtools.yang.data.util.LeafListEntryNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.LeafListNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.LeafNodeDataWithSchema;
-import org.opendaylight.yangtools.yang.data.util.ListEntryNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.ListNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.data.util.MultipleEntryDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.OperationAsContainer;
 import org.opendaylight.yangtools.yang.data.util.ParserStreamUtils;
 import org.opendaylight.yangtools.yang.data.util.SimpleNodeDataWithSchema;
@@ -327,16 +326,10 @@ public final class JsonParserStream implements Closeable, Flushable {
     }
 
     private static AbstractNodeDataWithSchema<?> newArrayEntry(final AbstractNodeDataWithSchema<?> parent) {
-        AbstractNodeDataWithSchema<?> newChild;
-        if (parent instanceof ListNodeDataWithSchema) {
-            newChild = ListEntryNodeDataWithSchema.forSchema(((ListNodeDataWithSchema) parent).getSchema());
-        } else if (parent instanceof LeafListNodeDataWithSchema) {
-            newChild = new LeafListEntryNodeDataWithSchema(((LeafListNodeDataWithSchema) parent).getSchema());
-        } else {
+        if (!(parent instanceof MultipleEntryDataWithSchema)) {
             throw new IllegalStateException("Found an unexpected array nested under " + parent.getSchema().getQName());
         }
-        ((CompositeNodeDataWithSchema<?>) parent).addChild(newChild);
-        return newChild;
+        return ((MultipleEntryDataWithSchema<?>) parent).newChildEntry();
     }
 
     private void setValue(final AbstractNodeDataWithSchema<?> parent, final String value) {
index 93dc036dd79a3d50840c46774d0371241499ee97..4c81dc75624b19dd40c143a40a7dce93659ad4da 100644 (file)
@@ -66,6 +66,7 @@ import org.opendaylight.yangtools.yang.data.util.LeafNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.ListEntryNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.ListNodeDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.MountPointData;
+import org.opendaylight.yangtools.yang.data.util.MultipleEntryDataWithSchema;
 import org.opendaylight.yangtools.yang.data.util.OperationAsContainer;
 import org.opendaylight.yangtools.yang.data.util.ParserStreamUtils;
 import org.opendaylight.yangtools.yang.data.util.SimpleNodeDataWithSchema;
@@ -628,15 +629,8 @@ public final class XmlParserStream implements Closeable, Flushable {
     }
 
     private static AbstractNodeDataWithSchema<?> newEntryNode(final AbstractNodeDataWithSchema<?> parent) {
-        final AbstractNodeDataWithSchema<?> newChild;
-        if (parent instanceof ListNodeDataWithSchema) {
-            newChild = ListEntryNodeDataWithSchema.forSchema(((ListNodeDataWithSchema) parent).getSchema());
-        } else {
-            verify(parent instanceof LeafListNodeDataWithSchema, "Unexpected parent %s", parent);
-            newChild = new LeafListEntryNodeDataWithSchema(((LeafListNodeDataWithSchema) parent).getSchema());
-        }
-        ((CompositeNodeDataWithSchema<?>) parent).addChild(newChild);
-        return newChild;
+        verify(parent instanceof MultipleEntryDataWithSchema, "Unexpected parent %s", parent);
+        return ((MultipleEntryDataWithSchema<?>) parent).newChildEntry();
     }
 
     @Override
index 4894aa5f0c1974607cad891df19d7082c08aac8f..076d6527c906fec4197a2474dce00a4f2773e86f 100644 (file)
@@ -60,6 +60,7 @@ public class CompositeNodeDataWithSchema<T extends DataSchemaNode> extends Abstr
         return newChild == null ? addCompositeChild(schema) : newChild;
     }
 
+    @Deprecated
     public void addChild(final AbstractNodeDataWithSchema<?> newChild) {
         children.add(newChild);
     }
index cb924b3bfdb24ebd3158b6683993cc531ce04dfe..b33bf47dfd35e578b6615da7474a39a2f2de29f0 100644 (file)
@@ -19,7 +19,8 @@ import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
  * <p>
  * Represents a YANG leaf-list node.
  */
-public class LeafListNodeDataWithSchema extends CompositeNodeDataWithSchema<LeafListSchemaNode> {
+public class LeafListNodeDataWithSchema extends CompositeNodeDataWithSchema<LeafListSchemaNode>
+        implements MultipleEntryDataWithSchema<LeafListEntryNodeDataWithSchema> {
     public LeafListNodeDataWithSchema(final LeafListSchemaNode schema) {
         super(schema);
     }
@@ -37,4 +38,11 @@ public class LeafListNodeDataWithSchema extends CompositeNodeDataWithSchema<Leaf
         super.write(writer, metaWriter);
         writer.endNode();
     }
+
+    @Override
+    public final LeafListEntryNodeDataWithSchema newChildEntry() {
+        final LeafListEntryNodeDataWithSchema child = new LeafListEntryNodeDataWithSchema(getSchema());
+        addChild(child);
+        return child;
+    }
 }
index 483315f286c37f5c1ec19c77d8c5af3865c6e11f..c01cb1f435dbc24fb95ec4e8829cd92248159da1 100644 (file)
@@ -13,6 +13,7 @@ import java.io.IOException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadataStreamWriter;
 import org.opendaylight.yangtools.util.ImmutableMapTemplate;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -86,7 +87,7 @@ public abstract class ListEntryNodeDataWithSchema extends AbstractMountPointData
         super(schema);
     }
 
-    public static ListEntryNodeDataWithSchema forSchema(final ListSchemaNode schema) {
+    public static @NonNull ListEntryNodeDataWithSchema forSchema(final ListSchemaNode schema) {
         final List<QName> keyDef = schema.getKeyDefinition();
         return keyDef.isEmpty() ? new Unkeyed(schema) :  new Keyed(schema, keyDef);
     }
index 2cfd2470751a1227b70aba97d9c4f6e578bb1ed9..4bc24b60edad52aea2017ed3d48ba31913396a2a 100644 (file)
@@ -19,8 +19,8 @@ import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
  * <p>
  * Represents a YANG list node.
  */
-public class ListNodeDataWithSchema extends CompositeNodeDataWithSchema<ListSchemaNode> {
-
+public class ListNodeDataWithSchema extends CompositeNodeDataWithSchema<ListSchemaNode>
+        implements MultipleEntryDataWithSchema<ListEntryNodeDataWithSchema> {
     public ListNodeDataWithSchema(final ListSchemaNode schema) {
         super(schema);
     }
@@ -41,4 +41,10 @@ public class ListNodeDataWithSchema extends CompositeNodeDataWithSchema<ListSche
         writer.endNode();
     }
 
+    @Override
+    public final ListEntryNodeDataWithSchema newChildEntry() {
+        final ListEntryNodeDataWithSchema child = ListEntryNodeDataWithSchema.forSchema(getSchema());
+        addChild(child);
+        return child;
+    }
 }
diff --git a/yang/yang-data-util/src/main/java/org/opendaylight/yangtools/yang/data/util/MultipleEntryDataWithSchema.java b/yang/yang-data-util/src/main/java/org/opendaylight/yangtools/yang/data/util/MultipleEntryDataWithSchema.java
new file mode 100644 (file)
index 0000000..e9b7ddd
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.util;
+
+import com.google.common.annotations.Beta;
+import org.eclipse.jdt.annotation.NonNull;
+
+/**
+ * Marker interface for {@link AbstractNodeDataWithSchema} specializations which are composed of multiple entries.
+ *
+ * @param <T> Entry AbstractNodeDataWithSchema type
+ */
+@Beta
+public interface MultipleEntryDataWithSchema<T extends AbstractNodeDataWithSchema<?>> {
+    /**
+     * Create a new child entry.
+     *
+     * @return A new child entry.
+     */
+    @NonNull T newChildEntry();
+}