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=472be45cbafb0a62845cc58147121a757eb0e15d;hpb=2c1d8cbaeacc2a688db2034329bd972a3ef69e48;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 472be45cba..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 @@ -7,39 +7,45 @@ */ 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.Optional; -import com.google.common.base.Preconditions; -import com.google.common.base.Predicate; 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.List; +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; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; 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.AnydataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode; +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.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.RpcDefinition; -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.api.stmt.SchemaNodeIdentifier.Absolute; import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -51,79 +57,71 @@ import org.slf4j.LoggerFactory; @Beta public final class SchemaTracker { private static final Logger LOG = LoggerFactory.getLogger(SchemaTracker.class); - private final Deque schemaStack = new ArrayDeque<>(); + private final Deque schemaStack = new ArrayDeque<>(); private final DataNodeContainer root; - private SchemaTracker(final SchemaContext context, final SchemaPath path) { - SchemaNode current = Preconditions.checkNotNull(context); - for (final QName qname : path.getPathFromRoot()) { - SchemaNode child; - if(current instanceof DataNodeContainer) { - child = ((DataNodeContainer) current).getDataChildByName(qname); - - if (child == null && current instanceof SchemaContext) { - child = tryFindGroupings((SchemaContext) current, qname).orNull(); - } - - if(child == null && current instanceof SchemaContext) { - child = tryFindNotification((SchemaContext) current, qname) - .or(tryFindRpc(((SchemaContext) current), qname)).orNull(); - } - } else if (current instanceof ChoiceSchemaNode) { - child = ((ChoiceSchemaNode) current).getCaseNodeByName(qname); - } else if (current instanceof RpcDefinition) { - switch (qname.getLocalName()) { - case "input": - child = ((RpcDefinition) current).getInput(); - break; - case "output": - child = ((RpcDefinition) current).getOutput(); - break; - default: - child = null; - break; - } - } else { - throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", current)); - } - current = child; - } - Preconditions.checkArgument(current instanceof DataNodeContainer,"Schema path must point to container or list or an rpc input/output. Supplied path %s pointed to: %s",path,current); - root = (DataNodeContainer) current; - } - - private static Optional tryFindGroupings(final SchemaContext ctx, final QName qname) { - return Optional. fromNullable(Iterables.find(ctx.getGroupings(), new SchemaNodePredicate(qname), null)); + private SchemaTracker(final DataNodeContainer root) { + this.root = requireNonNull(root); } - private static Optional tryFindRpc(final SchemaContext ctx, final QName qname) { - return Optional.fromNullable(Iterables.find(ctx.getOperations(), new SchemaNodePredicate(qname), null)); - } - - private static Optional tryFindNotification(final SchemaContext ctx, final QName qname) { - return Optional.fromNullable(Iterables.find(ctx.getNotifications(), new SchemaNodePredicate(qname), null)); + /** + * 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); } /** - * Create a new writer with the specified context as its root. + * 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 SchemaTracker create(final SchemaContext context) { - return create(context, SchemaPath.ROOT); + 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 + * 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 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) { + LOG.warn("More possible schema nodes {} for supplied schema path {}", schemaNodes, path); + } + final Optional 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()); + } + + /** + * 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 SchemaTracker create(final SchemaContext context, final SchemaPath path) { - return new SchemaTracker(context, path); + 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() { @@ -137,88 +135,95 @@ public final class SchemaTracker { final Object parent = getParent(); SchemaNode schema = null; final QName qname = name.getNodeType(); - if(parent instanceof DataNodeContainer) { + if (parent instanceof DataNodeContainer) { schema = ((DataNodeContainer)parent).getDataChildByName(qname); - - if(schema == null && parent instanceof GroupingDefinition) { - schema = ((GroupingDefinition) parent); - } - - if(schema == null && parent instanceof NotificationDefinition) { - schema = ((NotificationDefinition) parent); + if (schema == null) { + if (parent instanceof GroupingDefinition) { + schema = (GroupingDefinition) parent; + } else if (parent instanceof NotificationDefinition) { + schema = (NotificationDefinition) parent; + } } - } else if(parent instanceof ChoiceSchemaNode) { + } else if (parent instanceof ChoiceSchemaNode) { schema = findChildInCases((ChoiceSchemaNode) parent, qname); } else { - throw new IllegalStateException("Unsupported schema type "+ parent.getClass() +" on stack."); + 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()) { - final DataSchemaNode potential = caze.getDataChildByName(qname); - if(potential != null) { - schema = potential; - break; + for (final CaseSchemaNode caze : parent.getCases()) { + final Optional potential = caze.findDataChildByName(qname); + if (potential.isPresent()) { + return potential.get(); } } - return schema; + return null; } private static SchemaNode findCaseByChild(final ChoiceSchemaNode parent, final QName qname) { - DataSchemaNode schema = null; - for(final ChoiceCaseNode caze : parent.getCases()) { - final DataSchemaNode potential = caze.getDataChildByName(qname); - if(potential != null) { - schema = caze; - break; + for (final CaseSchemaNode caze : parent.getCases()) { + final Optional potential = caze.findDataChildByName(qname); + if (potential.isPresent()) { + return caze; } } - return schema; + return null; } 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); 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"); - schemaStack.push(schema); + checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate"); + schemaStack.push((ListSchemaNode) 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); return (LeafSchemaNode) schema; } + public void startLeafNode(final NodeIdentifier name) throws IOException { + schemaStack.push(leafNode(name)); + } + 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); schemaStack.push(schema); - return (LeafListSchemaNode)schema; + return (LeafListSchemaNode) schema; } - public LeafListSchemaNode leafSetEntryNode() { + public LeafListSchemaNode leafSetEntryNode(final QName qname) { final Object parent = getParent(); + if (parent instanceof LeafListSchemaNode) { + return (LeafListSchemaNode) parent; + } - Preconditions.checkArgument(parent instanceof LeafListSchemaNode, "Not currently in a leaf-list"); - return (LeafListSchemaNode) parent; + 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); + return (LeafListSchemaNode) child; + } + + public void startLeafSetEntryNode(final NodeWithValue name) { + schemaStack.push(leafSetEntryNode(name.getNodeType())); } public ChoiceSchemaNode startChoiceNode(final NodeIdentifier name) { 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); schemaStack.push(schema); return (ChoiceSchemaNode)schema; } @@ -226,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; - - 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); schemaStack.push(schema); - return schema; } @@ -240,55 +242,50 @@ 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()); - - schemaStack.push(((YangModeledAnyXmlSchemaNode) schema).getSchemaOfAnyXmlData()); - + checkArgument(schema instanceof YangModeledAnyxmlSchemaNode, "Node %s is not an yang modeled anyXml.", schema); + 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); - if(parent instanceof ChoiceSchemaNode) { + 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, identifier.getPossibleChildNames()); - final HashSet realChildSchemas = new HashSet<>(); - for(final DataSchemaNode child : schema.getChildNodes()) { - realChildSchemas.add(((DataNodeContainer) parent).getDataChildByName(child.getQName())); - } - final AugmentationSchema resolvedSchema = new EffectiveAugmentationSchema(schema, realChildSchemas); + checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer", parent); + final AugmentationSchemaNode schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent, + identifier.getPossibleChildNames()); + final AugmentationSchemaNode resolvedSchema = EffectiveAugmentationSchema.create(schema, + (DataNodeContainer) parent); schemaStack.push(resolvedSchema); return resolvedSchema; } - public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) { + public AnyxmlSchemaNode anyxmlNode(final NodeIdentifier name) { final SchemaNode schema = getSchema(name); - - Preconditions.checkArgument(schema instanceof AnyXmlSchemaNode, "Node %s is not anyxml", schema.getPath()); - return (AnyXmlSchemaNode)schema; + checkArgument(schema instanceof AnyxmlSchemaNode, "Node %s is not anyxml", schema); + return (AnyxmlSchemaNode)schema; } - public Object endNode() { - return schemaStack.pop(); + public void startAnyxmlNode(final NodeIdentifier name) { + schemaStack.push(anyxmlNode(name)); } - private static final class SchemaNodePredicate implements Predicate { - private final QName qname; + public AnydataSchemaNode anydataNode(final NodeIdentifier name) { + final SchemaNode schema = getSchema(name); + checkArgument(schema instanceof AnydataSchemaNode, "Node %s is not anydata", schema); + return (AnydataSchemaNode)schema; + } - public SchemaNodePredicate(final QName qname) { - this.qname = qname; - } + public void startAnydataNode(final NodeIdentifier name) { + schemaStack.push(anydataNode(name)); + } - @Override - public boolean apply(final SchemaNode input) { - return input.getQName().equals(qname); - } + public Object endNode() { + return schemaStack.pop(); } }