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%2Fcodec%2FSchemaTracker.java;h=aec921d926382504084850531d694bd0a0b01519;hb=45d29b94e9f05a6067a4c1e3531ae4fa9f6a1eed;hp=40e86f3ee8e7a5d2adc2e0b9c1f01c0e17cd331d;hpb=19efe56f8f20f5692a100e765a581fbc8f0b4aca;p=yangtools.git diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/SchemaTracker.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/SchemaTracker.java index 40e86f3ee8..aec921d926 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/SchemaTracker.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/SchemaTracker.java @@ -16,6 +16,7 @@ import java.io.IOException; import java.util.ArrayDeque; import java.util.Collection; import java.util.Deque; +import java.util.List; import java.util.Optional; import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.yangtools.odlext.model.api.YangModeledAnyxmlSchemaNode; @@ -32,18 +33,19 @@ import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode; import org.opendaylight.yangtools.yang.model.api.AugmentationTarget; 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.ContainerLike; import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; 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.stmt.SchemaNodeIdentifier.Absolute; import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -75,11 +77,26 @@ public final class SchemaTracker { /** * Create a new writer with the specified context and rooted in the specified schema path. * - * @param context Associated {@link SchemaContext} + * @param context Associated {@link EffectiveModelContext} * @param path schema path * @return A new {@link NormalizedNodeStreamWriter} */ - public static @NonNull SchemaTracker create(final SchemaContext context, final SchemaPath path) { + public static @NonNull SchemaTracker create(final EffectiveModelContext context, final Absolute path) { + return create(context, path.getNodeIdentifiers()); + } + + /** + * Create a new writer with the specified context and rooted in the specified schema path. + * + * @param context Associated {@link EffectiveModelContext} + * @param path schema path + * @return A new {@link NormalizedNodeStreamWriter} + */ + public static @NonNull SchemaTracker create(final EffectiveModelContext context, final SchemaPath path) { + return create(context, path.getPathFromRoot()); + } + + private static @NonNull SchemaTracker create(final EffectiveModelContext context, final Iterable path) { final Collection schemaNodes = SchemaUtils.findParentSchemaNodesOnPath(context, path); checkArgument(!schemaNodes.isEmpty(), "Unable to find schema node for supplied schema path: %s", path); if (schemaNodes.size() > 1) { @@ -94,6 +111,19 @@ public final class SchemaTracker { return new SchemaTracker(current.get()); } + /** + * Create a new writer with the specified context and rooted in the specified schema path. + * + * @param context Associated {@link EffectiveModelContext} + * @param operation Operation schema path + * @param qname Input/Output container QName + * @return A new {@link NormalizedNodeStreamWriter} + */ + public static @NonNull SchemaTracker forOperation(final EffectiveModelContext context, final Absolute operation, + final QName qname) { + return create(context, Iterables.concat(operation.getNodeIdentifiers(), List.of(qname))); + } + public Object getParent() { if (schemaStack.isEmpty()) { return root; @@ -125,7 +155,7 @@ public final class SchemaTracker { } private static SchemaNode findChildInCases(final ChoiceSchemaNode parent, final QName qname) { - for (final CaseSchemaNode caze : parent.getCases().values()) { + for (final CaseSchemaNode caze : parent.getCases()) { final Optional potential = caze.findDataChildByName(qname); if (potential.isPresent()) { return potential.get(); @@ -135,7 +165,7 @@ public final class SchemaTracker { } private static SchemaNode findCaseByChild(final ChoiceSchemaNode parent, final QName qname) { - for (final CaseSchemaNode caze : parent.getCases().values()) { + for (final CaseSchemaNode caze : parent.getCases()) { final Optional potential = caze.findDataChildByName(qname); if (potential.isPresent()) { return caze; @@ -146,7 +176,7 @@ public final class SchemaTracker { public void startList(final PathArgument name) { final SchemaNode schema = getSchema(name); - checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath()); + checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema); schemaStack.push(schema); } @@ -158,8 +188,7 @@ public final class SchemaTracker { public LeafSchemaNode leafNode(final NodeIdentifier name) throws IOException { final SchemaNode schema = getSchema(name); - - checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema.getPath()); + checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema); return (LeafSchemaNode) schema; } @@ -169,10 +198,9 @@ public final class SchemaTracker { public LeafListSchemaNode startLeafSet(final NodeIdentifier name) { final SchemaNode schema = getSchema(name); - - 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); schemaStack.push(schema); - return (LeafListSchemaNode)schema; + return (LeafListSchemaNode) schema; } public LeafListSchemaNode leafSetEntryNode(final QName qname) { @@ -183,7 +211,7 @@ public final class SchemaTracker { final SchemaNode child = SchemaUtils.findDataChildSchemaByQName((SchemaNode) parent, qname); checkArgument(child instanceof LeafListSchemaNode, - "Node %s is neither a leaf-list nor currently in a leaf-list", child.getPath()); + "Node %s is neither a leaf-list nor currently in a leaf-list", child); return (LeafListSchemaNode) child; } @@ -195,7 +223,7 @@ public final class SchemaTracker { LOG.debug("Enter choice {}", name); final SchemaNode schema = getSchema(name); - checkArgument(schema instanceof ChoiceSchemaNode, "Node %s is not a choice", schema.getPath()); + checkArgument(schema instanceof ChoiceSchemaNode, "Node %s is not a choice", schema); schemaStack.push(schema); return (ChoiceSchemaNode)schema; } @@ -203,13 +231,10 @@ public final class SchemaTracker { public SchemaNode startContainerNode(final NodeIdentifier name) { LOG.debug("Enter container {}", name); final SchemaNode schema = getSchema(name); + final boolean isAllowed = schema instanceof ContainerLike || schema instanceof NotificationDefinition; - boolean isAllowed = schema instanceof ContainerSchemaNode; - isAllowed |= schema instanceof NotificationDefinition; - - 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); schemaStack.push(schema); - return schema; } @@ -217,11 +242,8 @@ public final class SchemaTracker { LOG.debug("Enter yang modeled anyXml {}", name); final SchemaNode schema = getSchema(name); - 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); schemaStack.push(((YangModeledAnyxmlSchemaNode) schema).getSchemaOfAnyXmlData()); - return schema; } @@ -245,7 +267,7 @@ public final class SchemaTracker { public AnyxmlSchemaNode anyxmlNode(final NodeIdentifier name) { final SchemaNode schema = getSchema(name); - checkArgument(schema instanceof AnyxmlSchemaNode, "Node %s is not anyxml", schema.getPath()); + checkArgument(schema instanceof AnyxmlSchemaNode, "Node %s is not anyxml", schema); return (AnyxmlSchemaNode)schema; } @@ -255,7 +277,7 @@ public final class SchemaTracker { public AnydataSchemaNode anydataNode(final NodeIdentifier name) { final SchemaNode schema = getSchema(name); - checkArgument(schema instanceof AnydataSchemaNode, "Node %s is not anydata", schema.getPath()); + checkArgument(schema instanceof AnydataSchemaNode, "Node %s is not anydata", schema); return (AnydataSchemaNode)schema; }