Add yang.model.api.EffectiveStatementInference
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / SchemaOrderedNormalizedNodeWriter.java
index 186d88c55dcbb9b1f5cff5840b4e364e057b152b..fa28b02717487d27fc6cf72098472ab47bccbe97 100644 (file)
@@ -7,69 +7,79 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema;
 
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.collect.ArrayListMultimap;
 import java.io.IOException;
 import java.util.Collection;
 import java.util.List;
-import java.util.Objects;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
-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.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
-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.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
 
 /**
  * This is an iterator over a {@link NormalizedNode}. Unlike {@link NormalizedNodeWriter}, this iterates over elements
  * in the order as they are defined in YANG file.
  */
 public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
-    private static final Logger LOG = LoggerFactory.getLogger(SchemaOrderedNormalizedNodeWriter.class);
-    private final SchemaContext schemaContext;
+    private final EffectiveModelContext schemaContext;
     private final SchemaNode root;
-    private final NormalizedNodeStreamWriter writer;
 
     private SchemaNode currentSchemaNode;
 
     /**
      * Create a new writer backed by a {@link NormalizedNodeStreamWriter}.
      *
-     * @param writer
-     *            Back-end writer
-     * @param schemaContext
-     *            Schema context
-     * @param path
-     *            path
+     * @param writer Back-end writer
+     * @param schemaContext Associated {@link EffectiveModelContext}
      */
-    public SchemaOrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext,
-            final SchemaPath path) {
+    public SchemaOrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer,
+            final EffectiveModelContext schemaContext) {
         super(writer);
-        this.writer = writer;
-        this.schemaContext = schemaContext;
-        final Collection<SchemaNode> schemaNodes = SchemaUtils.findParentSchemaNodesOnPath(schemaContext, 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);
+        this.root = this.schemaContext = requireNonNull(schemaContext);
+    }
+
+    /**
+     * Create a new writer backed by a {@link NormalizedNodeStreamWriter}.
+     *
+     * @param writer Back-end writer
+     * @param schemaContext Associated {@link EffectiveModelContext}
+     * @param path root path
+     */
+    public SchemaOrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer,
+            final EffectiveModelContext schemaContext, final SchemaPath path) {
+        super(writer);
+        this.schemaContext = requireNonNull(schemaContext);
+
+        final SchemaInferenceStack stack = SchemaInferenceStack.ofInstantiatedPath(schemaContext, path);
+        if (!stack.isEmpty()) {
+            final EffectiveStatement<?, ?> current = stack.currentStatement();
+            // FIXME: this should be one of NormalizedNodeContainer/NotificationDefinition/OperationDefinition
+            checkArgument(current instanceof SchemaNode, "Instantiating at %s is not supported", current);
+            root = (SchemaNode) current;
+        } else {
+            root = schemaContext;
         }
-        this.root = schemaNodes.iterator().next();
     }
 
     @Override
-    public SchemaOrderedNormalizedNodeWriter write(final NormalizedNode<?, ?> node) throws IOException {
-        if (Objects.equals(root, schemaContext)) {
-            currentSchemaNode = schemaContext.getDataChildByName(node.getNodeType());
+    public SchemaOrderedNormalizedNodeWriter write(final NormalizedNode node) throws IOException {
+        if (schemaContext.equals(root)) {
+            currentSchemaNode = schemaContext.dataChildByName(node.getNodeType());
         } else {
             currentSchemaNode = root;
         }
@@ -84,7 +94,7 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
      * @return NormalizedNodeWriter this
      * @throws IOException when thrown from the backing writer.
      */
-    public SchemaOrderedNormalizedNodeWriter write(final Collection<DataContainerChild<?,?>> nodes) throws IOException {
+    public SchemaOrderedNormalizedNodeWriter write(final Collection<DataContainerChild> nodes) throws IOException {
         currentSchemaNode = root;
         if (writeChildren(nodes, currentSchemaNode, false)) {
             return this;
@@ -93,7 +103,7 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
         throw new IllegalStateException("It wasn't possible to serialize nodes " + nodes);
     }
 
-    private SchemaOrderedNormalizedNodeWriter write(final NormalizedNode<?, ?> node, final SchemaNode dataSchemaNode)
+    private SchemaOrderedNormalizedNodeWriter write(final NormalizedNode node, final SchemaNode dataSchemaNode)
             throws IOException {
 
         //Set current schemaNode
@@ -114,22 +124,22 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
         throw new IllegalStateException("It wasn't possible to serialize node " + node);
     }
 
-    private void write(final List<NormalizedNode<?, ?>> nodes, final SchemaNode dataSchemaNode) throws IOException {
-        for (final NormalizedNode<?, ?> node : nodes) {
+    private void write(final List<NormalizedNode> nodes, final SchemaNode dataSchemaNode) throws IOException {
+        for (final NormalizedNode node : nodes) {
             write(node, dataSchemaNode);
         }
     }
 
     @Override
-    protected boolean writeChildren(final Iterable<? extends NormalizedNode<?, ?>> children) throws IOException {
+    protected boolean writeChildren(final Iterable<? extends NormalizedNode> children) throws IOException {
         return writeChildren(children, currentSchemaNode, true);
     }
 
-    private boolean writeChildren(final Iterable<? extends NormalizedNode<?, ?>> children,
-            final SchemaNode parentSchemaNode, final boolean endParent) throws IOException {
+    private boolean writeChildren(final Iterable<? extends NormalizedNode> children, final SchemaNode parentSchemaNode,
+            final boolean endParent) throws IOException {
         //Augmentations cannot be gotten with node.getChild so create our own structure with augmentations resolved
-        final ArrayListMultimap<QName, NormalizedNode<?, ?>> qNameToNodes = ArrayListMultimap.create();
-        for (final NormalizedNode<?, ?> child : children) {
+        final ArrayListMultimap<QName, NormalizedNode> qNameToNodes = ArrayListMultimap.create();
+        for (final NormalizedNode child : children) {
             if (child instanceof AugmentationNode) {
                 qNameToNodes.putAll(resolveAugmentations(child));
             } else {
@@ -146,7 +156,7 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
                 }
             }
         } else if (parentSchemaNode instanceof ChoiceSchemaNode) {
-            for (final ChoiceCaseNode ccNode : ((ChoiceSchemaNode) parentSchemaNode).getCases()) {
+            for (final CaseSchemaNode ccNode : ((ChoiceSchemaNode) parentSchemaNode).getCases()) {
                 for (final DataSchemaNode dsn : ccNode.getChildNodes()) {
                     if (qNameToNodes.containsKey(dsn.getQName())) {
                         write(qNameToNodes.get(dsn.getQName()), dsn);
@@ -154,17 +164,17 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
                 }
             }
         } else {
-            for (final NormalizedNode<?, ?> child : children) {
+            for (final NormalizedNode child : children) {
                 writeLeaf(child);
             }
         }
         if (endParent) {
-            writer.endNode();
+            getWriter().endNode();
         }
         return true;
     }
 
-    private SchemaOrderedNormalizedNodeWriter writeLeaf(final NormalizedNode<?, ?> node) throws IOException {
+    private SchemaOrderedNormalizedNodeWriter writeLeaf(final NormalizedNode node) throws IOException {
         if (wasProcessAsSimpleNode(node)) {
             return this;
         }
@@ -172,9 +182,9 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
         throw new IllegalStateException("It wasn't possible to serialize node " + node);
     }
 
-    private ArrayListMultimap<QName, NormalizedNode<?, ?>> resolveAugmentations(final NormalizedNode<?, ?> child) {
-        final ArrayListMultimap<QName, NormalizedNode<?, ?>> resolvedAugs = ArrayListMultimap.create();
-        for (final NormalizedNode<?, ?> node : ((AugmentationNode) child).getValue()) {
+    private ArrayListMultimap<QName, NormalizedNode> resolveAugmentations(final NormalizedNode child) {
+        final ArrayListMultimap<QName, NormalizedNode> resolvedAugs = ArrayListMultimap.create();
+        for (final NormalizedNode node : ((AugmentationNode) child).body()) {
             if (node instanceof AugmentationNode) {
                 resolvedAugs.putAll(resolveAugmentations(node));
             } else {