X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fimpl%2Fschema%2FSchemaOrderedNormalizedNodeWriter.java;h=9a1b9bd1f09b25b40a97b83a3c97d3e4b935cac2;hb=02b4c77462a4fafd2e3fd0a67dd563e984566913;hp=037265f94f00e4764e5d082bf4df4cb2541638f3;hpb=82af449e4ef07d80490e79484d0402b81009541e;p=yangtools.git diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/SchemaOrderedNormalizedNodeWriter.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/SchemaOrderedNormalizedNodeWriter.java index 037265f94f..9a1b9bd1f0 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/SchemaOrderedNormalizedNodeWriter.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/SchemaOrderedNormalizedNodeWriter.java @@ -7,6 +7,7 @@ */ package org.opendaylight.yangtools.yang.data.impl.schema; +import com.google.common.base.Preconditions; import com.google.common.collect.ArrayListMultimap; import java.io.IOException; import java.util.Collection; @@ -18,7 +19,7 @@ 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; @@ -26,13 +27,15 @@ 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 SchemaNode root; private final NormalizedNodeStreamWriter writer; @@ -41,15 +44,26 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter { /** * 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 + * Schema context + * @param path + * path */ - public SchemaOrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext, final SchemaPath path) { + public SchemaOrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext, + final SchemaPath path) { super(writer); this.writer = writer; this.schemaContext = schemaContext; - this.root = SchemaUtils.findParentSchemaOnPath(schemaContext, path); + final Collection 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 = schemaNodes.iterator().next(); } @Override @@ -60,7 +74,6 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter { currentSchemaNode = root; } return write(node, currentSchemaNode); - } /** @@ -78,10 +91,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)) { @@ -102,19 +115,21 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter { } private void write(final List> nodes, final SchemaNode dataSchemaNode) throws IOException { - for (NormalizedNode node : nodes) { + for (final NormalizedNode node : nodes) { write(node, dataSchemaNode); } } + @Override protected boolean writeChildren(final Iterable> children) throws IOException { return writeChildren(children, currentSchemaNode, true); } - private boolean writeChildren(final Iterable> children, final SchemaNode parentSchemaNode, boolean endParent) throws IOException { + private boolean writeChildren(final Iterable> children, + final SchemaNode parentSchemaNode, final boolean endParent) throws IOException { //Augmentations cannot be gotten with node.getChild so create our own structure with augmentations resolved - ArrayListMultimap> qNameToNodes = ArrayListMultimap.create(); - for (NormalizedNode child : children) { + final ArrayListMultimap> qNameToNodes = ArrayListMultimap.create(); + for (final NormalizedNode child : children) { if (child instanceof AugmentationNode) { qNameToNodes.putAll(resolveAugmentations(child)); } else { @@ -126,20 +141,20 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter { if (parentSchemaNode instanceof ListSchemaNode && qNameToNodes.containsKey(parentSchemaNode.getQName())) { write(qNameToNodes.get(parentSchemaNode.getQName()), parentSchemaNode); } else { - for (DataSchemaNode schemaNode : ((DataNodeContainer) parentSchemaNode).getChildNodes()) { + for (final DataSchemaNode schemaNode : ((DataNodeContainer) parentSchemaNode).getChildNodes()) { write(qNameToNodes.get(schemaNode.getQName()), schemaNode); } } } else if (parentSchemaNode instanceof ChoiceSchemaNode) { - for (ChoiceCaseNode ccNode : ((ChoiceSchemaNode) parentSchemaNode).getCases()) { - for (DataSchemaNode dsn : ccNode.getChildNodes()) { + for (final CaseSchemaNode ccNode : ((ChoiceSchemaNode) parentSchemaNode).getCases().values()) { + for (final DataSchemaNode dsn : ccNode.getChildNodes()) { if (qNameToNodes.containsKey(dsn.getQName())) { write(qNameToNodes.get(dsn.getQName()), dsn); } } } } else { - for (NormalizedNode child : children) { + for (final NormalizedNode child : children) { writeLeaf(child); } } @@ -159,7 +174,7 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter { private ArrayListMultimap> resolveAugmentations(final NormalizedNode child) { final ArrayListMultimap> resolvedAugs = ArrayListMultimap.create(); - for (NormalizedNode node : ((AugmentationNode) child).getValue()) { + for (final NormalizedNode node : ((AugmentationNode) child).getValue()) { if (node instanceof AugmentationNode) { resolvedAugs.putAll(resolveAugmentations(node)); } else { @@ -169,26 +184,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