Add YangError.getErrorTag()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / SchemaOrderedNormalizedNodeWriter.java
index 4cd619b8116767cefbd006c99efb4561ca1ca1d5..6cb35a5dbb61e77094c9c3574ea9771e11ecd164 100644 (file)
@@ -19,26 +19,25 @@ 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;
 
 /**
- * This is an iterator over a {@link NormalizedNode}. Unlike {@link NormalizedNodeWriter},
- * this iterates over elements in order as they are defined in .yang file.
+ * 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;
 
@@ -52,10 +51,9 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
      * @param path
      *            path
      */
-    public SchemaOrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext,
-            final SchemaPath path) {
+    public SchemaOrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer,
+            final EffectiveModelContext schemaContext, final SchemaPath path) {
         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",
@@ -69,12 +67,11 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
     @Override
     public SchemaOrderedNormalizedNodeWriter write(final NormalizedNode<?, ?> node) throws IOException {
         if (Objects.equals(root, schemaContext)) {
-            currentSchemaNode = schemaContext.getDataChildByName(node.getNodeType());
+            currentSchemaNode = schemaContext.dataChildByName(node.getNodeType());
         } else {
             currentSchemaNode = root;
         }
         return write(node, currentSchemaNode);
-
     }
 
     /**
@@ -92,10 +89,10 @@ 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) throws IOException {
+    private SchemaOrderedNormalizedNodeWriter write(final NormalizedNode<?, ?> node, final SchemaNode dataSchemaNode)
+            throws IOException {
 
         //Set current schemaNode
         try (SchemaNodeSetter sns = new SchemaNodeSetter(dataSchemaNode)) {
@@ -126,7 +123,8 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
         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) {
@@ -146,7 +144,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);
@@ -159,7 +157,7 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
             }
         }
         if (endParent) {
-            writer.endNode();
+            getWriter().endNode();
         }
         return true;
     }
@@ -184,26 +182,24 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
         return resolvedAugs;
     }
 
-
-    private class SchemaNodeSetter implements AutoCloseable {
+    private final class SchemaNodeSetter implements AutoCloseable {
 
         private final SchemaNode previousSchemaNode;
 
         /**
-         * Sets current schema node new value and store old value for later restore
+         * Sets current schema node new value and store old value for later restore.
          */
-        public SchemaNodeSetter(final SchemaNode schemaNode) {
+        SchemaNodeSetter(final SchemaNode schemaNode) {
             previousSchemaNode = SchemaOrderedNormalizedNodeWriter.this.currentSchemaNode;
             SchemaOrderedNormalizedNodeWriter.this.currentSchemaNode = schemaNode;
         }
 
         /**
-         * Restore previous schema node
+         * Restore previous schema node.
          */
         @Override
         public void close() {
             SchemaOrderedNormalizedNodeWriter.this.currentSchemaNode = previousSchemaNode;
         }
     }
-
 }
\ No newline at end of file