Allow JSON/XML writers to be instantiated with root node
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / SchemaTracker.java
index 60ffd77d0f04a2655076adeddb15675fe4d6b4a2..7a9bd4c9065221bdc8d452a0f0863be27b3fa023 100644 (file)
@@ -7,15 +7,18 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.codec;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.Beta;
-import com.google.common.base.Preconditions;
 import com.google.common.collect.Iterables;
 import java.io.IOException;
 import java.util.ArrayDeque;
 import java.util.Collection;
 import java.util.Deque;
-import java.util.HashSet;
 import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.odlext.model.api.YangModeledAnyXmlSchemaNode;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
@@ -23,9 +26,9 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgum
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
-import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
@@ -38,7 +41,6 @@ import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.api.YangModeledAnyXmlSchemaNode;
 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -53,19 +55,8 @@ public final class SchemaTracker {
     private final Deque<Object> schemaStack = new ArrayDeque<>();
     private final DataNodeContainer root;
 
-    private SchemaTracker(final SchemaContext context, final SchemaPath path) {
-        final Collection<SchemaNode> schemaNodes = SchemaUtils.findParentSchemaNodesOnPath(context, path);
-        Preconditions.checkArgument(!schemaNodes.isEmpty(), "Unable to find schema node for supplied schema path: %s",
-                path);
-        if (schemaNodes.size() > 1) {
-            LOG.warn("More possible schema nodes {} for supplied schema path {}", schemaNodes, path);
-        }
-        final Optional<SchemaNode> current = schemaNodes.stream().filter(node -> node instanceof DataNodeContainer)
-                .findFirst();
-        Preconditions.checkArgument(current.isPresent(),
-                "Schema path must point to container or list or an rpc input/output. Supplied path %s pointed to: %s",
-                path, current);
-        root = (DataNodeContainer) current.get();
+    private SchemaTracker(final DataNodeContainer root) {
+        this.root = requireNonNull(root);
     }
 
     /**
@@ -74,8 +65,19 @@ public final class SchemaTracker {
      * @param context Associated {@link SchemaContext}.
      * @return A new {@link NormalizedNodeStreamWriter}
      */
-    public static SchemaTracker create(final SchemaContext context) {
-        return create(context, SchemaPath.ROOT);
+    // FIXME: 3.0.0: remove this method
+    public static @NonNull SchemaTracker create(final SchemaContext context) {
+        return new SchemaTracker(context);
+    }
+
+    /**
+     * Create a new writer with the specified node as its root.
+     *
+     * @param root Root node
+     * @return A new {@link NormalizedNodeStreamWriter}
+     */
+    public static @NonNull SchemaTracker create(final DataNodeContainer root) {
+        return new SchemaTracker(root);
     }
 
     /**
@@ -85,8 +87,19 @@ public final class SchemaTracker {
      * @param path schema path
      * @return A new {@link NormalizedNodeStreamWriter}
      */
-    public static SchemaTracker create(final SchemaContext context, final SchemaPath path) {
-        return new SchemaTracker(context, path);
+    public static @NonNull SchemaTracker create(final SchemaContext context, final SchemaPath path) {
+        final Collection<SchemaNode> schemaNodes = SchemaUtils.findParentSchemaNodesOnPath(context, path);
+        checkArgument(!schemaNodes.isEmpty(), "Unable to find schema node for supplied schema path: %s", path);
+        if (schemaNodes.size() > 1) {
+            LOG.warn("More possible schema nodes {} for supplied schema path {}", schemaNodes, path);
+        }
+        final Optional<DataNodeContainer> current = schemaNodes.stream()
+                .filter(node -> node instanceof DataNodeContainer).map(DataNodeContainer.class::cast)
+                .findFirst();
+        checkArgument(current.isPresent(),
+                "Schema path must point to container or list or an rpc input/output. Supplied path %s pointed to: %s",
+                path, current);
+        return new SchemaTracker(current.get());
     }
 
     public Object getParent() {
@@ -115,13 +128,13 @@ public final class SchemaTracker {
             throw new IllegalStateException("Unsupported schema type " + parent.getClass() + " on stack.");
         }
 
-        Preconditions.checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
+        checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
         return schema;
     }
 
     private static SchemaNode findChildInCases(final ChoiceSchemaNode parent, final QName qname) {
         DataSchemaNode schema = null;
-        for (final ChoiceCaseNode caze : parent.getCases()) {
+        for (final CaseSchemaNode caze : parent.getCases().values()) {
             final DataSchemaNode potential = caze.getDataChildByName(qname);
             if (potential != null) {
                 schema = potential;
@@ -133,7 +146,7 @@ public final class SchemaTracker {
 
     private static SchemaNode findCaseByChild(final ChoiceSchemaNode parent, final QName qname) {
         DataSchemaNode schema = null;
-        for (final ChoiceCaseNode caze : parent.getCases()) {
+        for (final CaseSchemaNode caze : parent.getCases().values()) {
             final DataSchemaNode potential = caze.getDataChildByName(qname);
             if (potential != null) {
                 schema = caze;
@@ -145,40 +158,31 @@ public final class SchemaTracker {
 
     public void startList(final PathArgument name) {
         final SchemaNode schema = getSchema(name);
-        Preconditions.checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
+        checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
         schemaStack.push(schema);
     }
 
     public void startListItem(final PathArgument name) throws IOException {
         final Object schema = getParent();
-        Preconditions.checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate");
+        checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate");
         schemaStack.push(schema);
     }
 
     public LeafSchemaNode leafNode(final NodeIdentifier name) throws IOException {
         final SchemaNode schema = getSchema(name);
 
-        Preconditions.checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema.getPath());
+        checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema.getPath());
         return (LeafSchemaNode) schema;
     }
 
     public LeafListSchemaNode startLeafSet(final NodeIdentifier name) {
         final SchemaNode schema = getSchema(name);
 
-        Preconditions.checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list",
-            schema.getPath());
+        checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema.getPath());
         schemaStack.push(schema);
         return (LeafListSchemaNode)schema;
     }
 
-    @Deprecated
-    public LeafListSchemaNode leafSetEntryNode() {
-        final Object parent = getParent();
-
-        Preconditions.checkArgument(parent instanceof LeafListSchemaNode, "Not currently in a leaf-list");
-        return (LeafListSchemaNode) parent;
-    }
-
     public LeafListSchemaNode leafSetEntryNode(final QName qname) {
         final Object parent = getParent();
         if (parent instanceof LeafListSchemaNode) {
@@ -186,7 +190,7 @@ public final class SchemaTracker {
         }
 
         final SchemaNode child = SchemaUtils.findDataChildSchemaByQName((SchemaNode) parent, qname);
-        Preconditions.checkArgument(child instanceof LeafListSchemaNode,
+        checkArgument(child instanceof LeafListSchemaNode,
             "Node %s is neither a leaf-list nor currently in a leaf-list", child.getPath());
         return (LeafListSchemaNode) child;
     }
@@ -195,7 +199,7 @@ public final class SchemaTracker {
         LOG.debug("Enter choice {}", name);
         final SchemaNode schema = getSchema(name);
 
-        Preconditions.checkArgument(schema instanceof ChoiceSchemaNode, "Node %s is not a choice", schema.getPath());
+        checkArgument(schema instanceof ChoiceSchemaNode, "Node %s is not a choice", schema.getPath());
         schemaStack.push(schema);
         return (ChoiceSchemaNode)schema;
     }
@@ -207,7 +211,7 @@ public final class SchemaTracker {
         boolean isAllowed = schema instanceof ContainerSchemaNode;
         isAllowed |= schema instanceof NotificationDefinition;
 
-        Preconditions.checkArgument(isAllowed, "Node %s is not a container nor a notification", schema.getPath());
+        checkArgument(isAllowed, "Node %s is not a container nor a notification", schema.getPath());
         schemaStack.push(schema);
 
         return schema;
@@ -217,40 +221,35 @@ public final class SchemaTracker {
         LOG.debug("Enter yang modeled anyXml {}", name);
         final SchemaNode schema = getSchema(name);
 
-        Preconditions.checkArgument(schema instanceof YangModeledAnyXmlSchemaNode,
-                "Node %s is not an yang modeled anyXml.", schema.getPath());
+        checkArgument(schema instanceof YangModeledAnyXmlSchemaNode, "Node %s is not an yang modeled anyXml.",
+            schema.getPath());
 
         schemaStack.push(((YangModeledAnyXmlSchemaNode) schema).getSchemaOfAnyXmlData());
 
         return schema;
     }
 
-    public AugmentationSchema startAugmentationNode(final AugmentationIdentifier identifier) {
+    public AugmentationSchemaNode startAugmentationNode(final AugmentationIdentifier identifier) {
         LOG.debug("Enter augmentation {}", identifier);
         Object parent = getParent();
 
-        Preconditions.checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
+        checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
         if (parent instanceof ChoiceSchemaNode) {
             final QName name = Iterables.get(identifier.getPossibleChildNames(), 0);
             parent = findCaseByChild((ChoiceSchemaNode) parent, name);
         }
-        Preconditions.checkArgument(parent instanceof DataNodeContainer,
-            "Augmentation allowed only in DataNodeContainer", parent);
-        final AugmentationSchema schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent,
+        checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer", parent);
+        final AugmentationSchemaNode schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent,
             identifier.getPossibleChildNames());
-        final HashSet<DataSchemaNode> realChildSchemas = new HashSet<>();
-        for (final DataSchemaNode child : schema.getChildNodes()) {
-            realChildSchemas.add(((DataNodeContainer) parent).getDataChildByName(child.getQName()));
-        }
-        final AugmentationSchema resolvedSchema = new EffectiveAugmentationSchema(schema, realChildSchemas);
+        final AugmentationSchemaNode resolvedSchema = EffectiveAugmentationSchema.create(schema,
+            (DataNodeContainer) parent);
         schemaStack.push(resolvedSchema);
         return resolvedSchema;
     }
 
     public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) {
         final SchemaNode schema = getSchema(name);
-
-        Preconditions.checkArgument(schema instanceof AnyXmlSchemaNode, "Node %s is not anyxml", schema.getPath());
+        checkArgument(schema instanceof AnyXmlSchemaNode, "Node %s is not anyxml", schema.getPath());
         return (AnyXmlSchemaNode)schema;
     }