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%2FSchemaUtils.java;h=6869b57d619d61f283c5367a3a06a6619454f707;hb=6b5d20f6513bc3e6e5db4a2058ee81308edaa9c8;hp=365148cc609f96b58648357009082e25448c41d6;hpb=2719da7f1a121b999310aa1e075872e1a1d8d196;p=yangtools.git diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/SchemaUtils.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/SchemaUtils.java index 365148cc60..6869b57d61 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/SchemaUtils.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/SchemaUtils.java @@ -10,16 +10,21 @@ package org.opendaylight.yangtools.yang.data.impl.schema; import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Preconditions; +import com.google.common.base.Predicate; import com.google.common.collect.Collections2; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; import com.google.common.collect.Maps; import com.google.common.collect.Sets; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Map; import java.util.Set; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; @@ -28,32 +33,81 @@ import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; 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.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; public final class SchemaUtils { + private static final Function QNAME_FUNCTION = new Function() { + @Override + public QName apply(@Nonnull final DataSchemaNode input) { + return input.getQName(); + } + }; + private SchemaUtils() { + throw new UnsupportedOperationException(); } - public static final Optional findFirstSchema(final QName qname, final Iterable dataSchemaNode) { + /** + * @param qname - schema node to find + * @param dataSchemaNode - iterable of schemaNodes to look through + * @return - schema node with newest revision or absent if no schema node with matching qname is found + */ + public static Optional findFirstSchema(final QName qname, final Iterable dataSchemaNode) { + DataSchemaNode sNode = null; if (dataSchemaNode != null && qname != null) { for (DataSchemaNode dsn : dataSchemaNode) { if (qname.isEqualWithoutRevision(dsn.getQName())) { - return Optional. of(dsn); + if (sNode == null || sNode.getQName().getRevision().compareTo(dsn.getQName().getRevision()) < 0) { + sNode = dsn; + } } else if (dsn instanceof ChoiceSchemaNode) { for (ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) { + + final DataSchemaNode dataChildByName = choiceCase.getDataChildByName(qname); + if (dataChildByName != null) { + return Optional.of(dataChildByName); + } Optional foundDsn = findFirstSchema(qname, choiceCase.getChildNodes()); - if (foundDsn != null && foundDsn.isPresent()) { + if (foundDsn.isPresent()) { return foundDsn; } } } } } - return Optional.absent(); + return Optional.fromNullable(sNode); } + /** + * + * Find child schema node identified by its QName within a provided schema node + * + * @param schema schema for parent node - search root + * @param qname qname(with or without a revision) of a child node to be found in the parent schema + * @return found schema node + * @throws java.lang.IllegalStateException if the child was not found in parent schema node + */ public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) { - return findSchemaForChild(schema, qname, schema.getChildNodes()); + // Try to find child schema node directly, but use a fallback that compares QNames without revisions and auto-expands choices + final DataSchemaNode dataChildByName = schema.getDataChildByName(qname); + return dataChildByName == null ? findSchemaForChild(schema, qname, schema.getChildNodes()) : dataChildByName; + } + + @Nullable + public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final boolean strictMode) { + if (strictMode) { + return findSchemaForChild(schema, qname); + } + + Optional childSchemaOptional = findFirstSchema(qname, schema.getChildNodes()); + if (!childSchemaOptional.isPresent()) { + return null; + } + return childSchemaOptional.get(); } public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final Iterable childNodes) { @@ -90,7 +144,7 @@ public final class SchemaUtils { HashSet qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(), new Function() { @Override - public QName apply(final @Nonnull DataSchemaNode input) { + public QName apply(@Nonnull final DataSchemaNode input) { Preconditions.checkNotNull(input); return input.getQName(); } @@ -120,6 +174,7 @@ public final class SchemaUtils { /** * Recursively find all child nodes that come from choices. * + * @param schema schema * @return Map with all child nodes, to their most top augmentation */ public static Map mapChildElementsFromChoices(final DataNodeContainer schema) { @@ -165,6 +220,7 @@ public final class SchemaUtils { /** * Recursively find all child nodes that come from augmentations. * + * @param schema schema * @return Map with all child nodes, to their most top augmentation */ public static Map mapChildElementsFromAugments(final AugmentationTarget schema) { @@ -228,6 +284,9 @@ public final class SchemaUtils { * Recursively list all child nodes. * * In case of choice, augment and cases, step in. + * + * @param nodeContainer node container + * @return set of QNames */ public static Set getChildNodesRecursive(final DataNodeContainer nodeContainer) { Set allChildNodes = Sets.newHashSet(); @@ -254,6 +313,9 @@ public final class SchemaUtils { * Schema of the same child node from augment, and directly from target is not the same. * Schema of child node from augment is incomplete, therefore its useless for XML/NormalizedNode translation. * + * @param targetSchema target schema + * @param augmentSchema augment schema + * @return set of nodes */ public static Set getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) { if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) { @@ -290,8 +352,7 @@ public final class SchemaUtils { public static Optional detectCase(final ChoiceSchemaNode schema, final DataContainerChild child) { for (ChoiceCaseNode choiceCaseNode : schema.getCases()) { if (child instanceof AugmentationNode - && belongsToCaseAugment(choiceCaseNode, - (YangInstanceIdentifier.AugmentationIdentifier) child.getIdentifier())) { + && belongsToCaseAugment(choiceCaseNode, (AugmentationIdentifier) child.getIdentifier())) { return Optional.of(choiceCaseNode); } else if (choiceCaseNode.getDataChildByName(child.getNodeType()) != null) { return Optional.of(choiceCaseNode); @@ -301,7 +362,7 @@ public final class SchemaUtils { return Optional.absent(); } - public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final YangInstanceIdentifier.AugmentationIdentifier childToProcess) { + public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final AugmentationIdentifier childToProcess) { for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) { Set currentAugmentChildNodes = Sets.newHashSet(); @@ -317,17 +378,97 @@ public final class SchemaUtils { return false; } - public static YangInstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) { - return new YangInstanceIdentifier.AugmentationIdentifier(getChildQNames(schema)); + /** + * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such + * node is found then it is returned, else null. + * + * @param parent parent node + * @param child child node + * @return augmentation schema + */ + public static AugmentationSchema findCorrespondingAugment(final DataSchemaNode parent, final DataSchemaNode child) { + if (parent instanceof AugmentationTarget && !(parent instanceof ChoiceSchemaNode)) { + for (AugmentationSchema augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) { + DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName()); + if (childInAugmentation != null) { + return augmentation; + } + } + } + return null; + } + + public static AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) { + final Collection qnames = Collections2.transform(schema.getChildNodes(), QNAME_FUNCTION); + return new AugmentationIdentifier(ImmutableSet.copyOf(qnames)); + } + + /** + * Finds schema node for given path in schema context. + * @param schemaContext schema context + * @param path path + * @return schema node on path + */ + public static SchemaNode findParentSchemaOnPath(final SchemaContext schemaContext, final SchemaPath path) { + SchemaNode current = Preconditions.checkNotNull(schemaContext); + 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; + } + return current; + } + + private static Optional tryFindGroupings(final SchemaContext ctx, final QName qname) { + return Optional.fromNullable(Iterables.find(ctx.getGroupings(), new SchemaNodePredicate(qname), null)); } - public static Set getChildQNames(final AugmentationSchema schema) { - Set qnames = Sets.newHashSet(); + private static Optional tryFindRpc(final SchemaContext ctx, final QName qname) { + return Optional.fromNullable(Iterables.find(ctx.getOperations(), new SchemaNodePredicate(qname), null)); + } - for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) { - qnames.add(dataSchemaNode.getQName()); + private static Optional tryFindNotification(final SchemaContext ctx, final QName qname) { + return Optional.fromNullable(Iterables.find(ctx.getNotifications(), new SchemaNodePredicate(qname), null)); + } + + private static final class SchemaNodePredicate implements Predicate { + private final QName qname; + + public SchemaNodePredicate(final QName qname) { + this.qname = qname; } - return qnames; + @Override + public boolean apply(final SchemaNode input) { + return input.getQName().equals(qname); + } } + }