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=7a514484e5f5ffa17ab637b92a0aa132f9d5bde5;hb=f429749a59a10820b1bad0e9c3502ceac2ec1d07;hp=37c563709765f0f2a669bf3f31f811ed6d7e1ae4;hpb=b212baa59f859732bd3a799425bb420035fe6154;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 37c5637097..7a514484e5 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 @@ -7,15 +7,14 @@ */ package org.opendaylight.yangtools.yang.data.impl.schema; -import com.google.common.base.Preconditions; -import com.google.common.base.Predicate; +import static com.google.common.base.Preconditions.checkState; +import static java.util.Objects.requireNonNull; + import com.google.common.collect.Collections2; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Iterables; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; @@ -23,28 +22,28 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; -import javax.annotation.Nullable; +import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.Revision; 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.ActionNodeContainer; -import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; +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.DataNodeContainer; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer; -import org.opendaylight.yangtools.yang.model.api.RpcDefinition; +import org.opendaylight.yangtools.yang.model.api.OperationDefinition; 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 SchemaUtils() { - throw new UnsupportedOperationException(); + // Hidden on purpose } /** @@ -65,11 +64,10 @@ public final class SchemaUtils { schema = dsn; } } else if (dsn instanceof ChoiceSchemaNode) { - for (final ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) { - - final DataSchemaNode dataChildByName = choiceCase.getDataChildByName(qname); - if (dataChildByName != null) { - return Optional.of(dataChildByName); + for (final CaseSchemaNode choiceCase : ((ChoiceSchemaNode) dsn).getCases().values()) { + final Optional dataChildByName = choiceCase.findDataChildByName(qname); + if (dataChildByName.isPresent()) { + return dataChildByName; } final Optional foundDsn = findFirstSchema(qname, choiceCase.getChildNodes()); if (foundDsn.isPresent()) { @@ -93,12 +91,12 @@ public final class SchemaUtils { public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) { // 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; + final Optional dataChildByName = schema.findDataChildByName(qname); + return dataChildByName.isPresent() ? dataChildByName.get() + : findSchemaForChild(schema, qname, schema.getChildNodes()); } - @Nullable - public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, + public static @Nullable DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final boolean strictMode) { if (strictMode) { return findSchemaForChild(schema, qname); @@ -114,13 +112,13 @@ public final class SchemaUtils { public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final Iterable childNodes) { final Optional childSchema = findFirstSchema(qname, childNodes); - Preconditions.checkState(childSchema.isPresent(), - "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname, schema); + checkState(childSchema.isPresent(), "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname, + schema); return childSchema.get(); } public static DataSchemaNode findSchemaForChild(final ChoiceSchemaNode schema, final QName childPartialQName) { - for (final ChoiceCaseNode choiceCaseNode : schema.getCases()) { + for (final CaseSchemaNode choiceCaseNode : schema.getCases().values()) { final Optional childSchema = findFirstSchema(childPartialQName, choiceCaseNode.getChildNodes()); if (childSchema.isPresent()) { @@ -133,34 +131,32 @@ public final class SchemaUtils { childPartialQName, schema)); } - public static AugmentationSchema findSchemaForAugment(final AugmentationTarget schema, final Set qnames) { - final Optional schemaForAugment = findAugment(schema, qnames); - Preconditions.checkState(schemaForAugment.isPresent(), - "Unknown augmentation node detected, identified by: %s, in: %s", qnames, schema); + public static AugmentationSchemaNode findSchemaForAugment(final AugmentationTarget schema, + final Set qnames) { + final Optional schemaForAugment = findAugment(schema, qnames); + checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s", + qnames, schema); return schemaForAugment.get(); } - public static AugmentationSchema findSchemaForAugment(final ChoiceSchemaNode schema, final Set qnames) { - Optional schemaForAugment = Optional.empty(); - - for (final ChoiceCaseNode choiceCaseNode : schema.getCases()) { - schemaForAugment = findAugment(choiceCaseNode, qnames); + public static AugmentationSchemaNode findSchemaForAugment(final ChoiceSchemaNode schema, final Set qnames) { + for (final CaseSchemaNode choiceCaseNode : schema.getCases().values()) { + final Optional schemaForAugment = findAugment(choiceCaseNode, qnames); if (schemaForAugment.isPresent()) { - break; + return schemaForAugment.get(); } } - Preconditions.checkState(schemaForAugment.isPresent(), - "Unknown augmentation node detected, identified by: %s, in: %s", qnames, schema); - return schemaForAugment.get(); + throw new IllegalStateException(String.format("Unknown augmentation node detected, identified by: %s, in: %s", + qnames, schema)); } - private static Optional findAugment(final AugmentationTarget schema, final Set qnames) { - for (final AugmentationSchema augment : schema.getAvailableAugmentations()) { + private static Optional findAugment(final AugmentationTarget schema, + final Set qnames) { + for (final AugmentationSchemaNode augment : schema.getAvailableAugmentations()) { final Set qNamesFromAugment = ImmutableSet.copyOf(Collections2.transform(augment.getChildNodes(), - DataSchemaNode::getQName)); - - if (qNamesFromAugment.equals(qnames)) { + DataSchemaNode::getQName)); + if (qnames.equals(qNamesFromAugment)) { return Optional.of(augment); } } @@ -189,7 +185,7 @@ public final class SchemaUtils { continue; } - for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) { + for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases().values()) { for (final QName qname : getChildNodesRecursive(choiceCaseNode)) { mappedChoices.put(qname, (ChoiceSchemaNode) childSchema); } @@ -205,8 +201,8 @@ public final class SchemaUtils { return false; } - for (final AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) { - if (augmentationSchema.getDataChildByName(childSchema.getQName()) != null) { + for (final AugmentationSchemaNode augmentation : ((AugmentationTarget) schema).getAvailableAugmentations()) { + if (augmentation.findDataChildByName(childSchema.getQName()).isPresent()) { return true; } } @@ -220,13 +216,13 @@ public final class SchemaUtils { * @param schema schema * @return Map with all child nodes, to their most top augmentation */ - public static Map mapChildElementsFromAugments(final AugmentationTarget schema) { + public static Map mapChildElementsFromAugments(final AugmentationTarget schema) { - final Map childNodesToAugmentation = new LinkedHashMap<>(); + final Map childNodesToAugmentation = new LinkedHashMap<>(); // Find QNames of augmented child nodes - final Map augments = new HashMap<>(); - for (final AugmentationSchema augmentationSchema : schema.getAvailableAugmentations()) { + final Map augments = new HashMap<>(); + for (final AugmentationSchemaNode augmentationSchema : schema.getAvailableAugmentations()) { for (final DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) { augments.put(dataSchemaNode.getQName(), augmentationSchema); } @@ -242,15 +238,15 @@ public final class SchemaUtils { continue; } - final AugmentationSchema mostTopAugmentation = augments.get(child.getQName()); + final AugmentationSchemaNode mostTopAugmentation = augments.get(child.getQName()); // recursively add all child nodes in case of augment, case and choice - if (child instanceof AugmentationSchema || child instanceof ChoiceCaseNode) { + if (child instanceof AugmentationSchemaNode || child instanceof CaseSchemaNode) { for (final QName qname : getChildNodesRecursive((DataNodeContainer) child)) { childNodesToAugmentation.put(qname, mostTopAugmentation); } } else if (child instanceof ChoiceSchemaNode) { - for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases()) { + for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases().values()) { for (final QName qname : getChildNodesRecursive(choiceCaseNode)) { childNodesToAugmentation.put(qname, mostTopAugmentation); } @@ -263,7 +259,7 @@ public final class SchemaUtils { // Choice Node has to map child nodes from all its cases if (schema instanceof ChoiceSchemaNode) { - for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases()) { + for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases().values()) { if (!augments.containsKey(choiceCaseNode.getQName())) { continue; } @@ -288,10 +284,10 @@ public final class SchemaUtils { for (final DataSchemaNode childSchema : nodeContainer.getChildNodes()) { if (childSchema instanceof ChoiceSchemaNode) { - for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) { + for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases().values()) { allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode)); } - } else if (childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) { + } else if (childSchema instanceof AugmentationSchemaNode || childSchema instanceof CaseSchemaNode) { allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema)); } else { allChildNodes.add(childSchema.getQName()); @@ -313,9 +309,9 @@ public final class SchemaUtils { * @return set of nodes */ public static Set getRealSchemasForAugment(final AugmentationTarget targetSchema, - final AugmentationSchema augmentSchema) { + final AugmentationSchemaNode augmentSchema) { if (!targetSchema.getAvailableAugmentations().contains(augmentSchema)) { - return Collections.emptySet(); + return ImmutableSet.of(); } if (targetSchema instanceof DataNodeContainer) { return getRealSchemasForAugment((DataNodeContainer)targetSchema, augmentSchema); @@ -323,7 +319,7 @@ public final class SchemaUtils { final Set realChildNodes = new HashSet<>(); if (targetSchema instanceof ChoiceSchemaNode) { for (final DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) { - for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases()) { + for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases().values()) { if (getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) { realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName())); } @@ -335,7 +331,7 @@ public final class SchemaUtils { } public static Set getRealSchemasForAugment(final DataNodeContainer targetSchema, - final AugmentationSchema augmentSchema) { + final AugmentationSchemaNode augmentSchema) { final Set realChildNodes = new HashSet<>(); for (final DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) { final DataSchemaNode realChild = targetSchema.getDataChildByName(dataSchemaNode.getQName()); @@ -344,13 +340,13 @@ public final class SchemaUtils { return realChildNodes; } - public static Optional detectCase(final ChoiceSchemaNode schema, + public static Optional detectCase(final ChoiceSchemaNode schema, final DataContainerChild child) { - for (final ChoiceCaseNode choiceCaseNode : schema.getCases()) { + for (final CaseSchemaNode choiceCaseNode : schema.getCases().values()) { if (child instanceof AugmentationNode && belongsToCaseAugment(choiceCaseNode, (AugmentationIdentifier) child.getIdentifier())) { return Optional.of(choiceCaseNode); - } else if (choiceCaseNode.getDataChildByName(child.getNodeType()) != null) { + } else if (choiceCaseNode.findDataChildByName(child.getNodeType()).isPresent()) { return Optional.of(choiceCaseNode); } } @@ -358,9 +354,9 @@ public final class SchemaUtils { return Optional.empty(); } - public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, + public static boolean belongsToCaseAugment(final CaseSchemaNode caseNode, final AugmentationIdentifier childToProcess) { - for (final AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) { + for (final AugmentationSchemaNode augmentationSchema : caseNode.getAvailableAugmentations()) { final Set currentAugmentChildNodes = new HashSet<>(); for (final DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) { @@ -383,23 +379,21 @@ public final class SchemaUtils { * @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 (final AugmentationSchema augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) { - final DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName()); - if (childInAugmentation != null) { - return augmentation; - } + public static AugmentationSchemaNode findCorrespondingAugment(final DataSchemaNode parent, + final DataSchemaNode child) { + if (!(parent instanceof AugmentationTarget) || parent instanceof ChoiceSchemaNode) { + return null; + } + + for (final AugmentationSchemaNode augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) { + final Optional childInAugmentation = augmentation.findDataChildByName(child.getQName()); + if (childInAugmentation.isPresent()) { + return augmentation; } } return null; } - public static AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) { - final Collection qnames = Collections2.transform(schema.getChildNodes(), DataSchemaNode::getQName); - return new AugmentationIdentifier(ImmutableSet.copyOf(qnames)); - } - /** * Finds schema node for given path in schema context. This method performs * lookup in the namespace of all leafs, leaf-lists, lists, containers, @@ -413,43 +407,17 @@ public final class SchemaUtils { * @return schema node on path */ public static SchemaNode findDataParentSchemaOnPath(final SchemaContext schemaContext, final SchemaPath path) { - SchemaNode current = Preconditions.checkNotNull(schemaContext); + SchemaNode current = requireNonNull(schemaContext); for (final QName qname : path.getPathFromRoot()) { current = findDataChildSchemaByQName(current, qname); } return current; } - /** - * Finds schema node for given path in schema context. This method performs lookup in both the namespace - * of groupings and the namespace of all leafs, leaf-lists, lists, containers, choices, rpcs, actions, - * notifications, anydatas and anyxmls according to Rfc6050/Rfc7950 section 6.2.1. - * - *

- * This method is deprecated, because name conflicts can occur between the namespace of groupings and namespace - * of data nodes and in consequence lookup could be ambiguous. - * - * @param schemaContext - * schema context - * @param path - * path - * @return schema node on path - * - * @deprecated Use {@link #findParentSchemaNodesOnPath(SchemaContext, SchemaPath)} instead. - */ - @Deprecated - public static SchemaNode findParentSchemaOnPath(final SchemaContext schemaContext, final SchemaPath path) { - SchemaNode current = Preconditions.checkNotNull(schemaContext); - for (final QName qname : path.getPathFromRoot()) { - current = findChildSchemaByQName(current, qname); - } - return current; - } - /** * Find child data schema node identified by its QName within a provided schema node. This method performs lookup * in the namespace of all leafs, leaf-lists, lists, containers, choices, rpcs, actions, notifications, anydatas - * and anyxmls according to Rfc6050/Rfc7950 section 6.2.1. + * and anyxmls according to RFC6050/RFC7950 section 6.2.1. * * @param node * schema node @@ -459,68 +427,36 @@ public final class SchemaUtils { * @throws IllegalArgumentException * if the schema node does not allow children */ - @Nullable - public static SchemaNode findDataChildSchemaByQName(final SchemaNode node, final QName qname) { - SchemaNode child = null; + public static @Nullable SchemaNode findDataChildSchemaByQName(final SchemaNode node, final QName qname) { if (node instanceof DataNodeContainer) { - child = ((DataNodeContainer) node).getDataChildByName(qname); + SchemaNode child = ((DataNodeContainer) node).getDataChildByName(qname); if (child == null && node instanceof SchemaContext) { - child = tryFindRpc((SchemaContext) node, qname).orElse(null); + child = tryFind(((SchemaContext) node).getOperations(), qname).orElse(null); } if (child == null && node instanceof NotificationNodeContainer) { - child = tryFindNotification((NotificationNodeContainer) node, qname).orElse(null); + child = tryFind(((NotificationNodeContainer) node).getNotifications(), qname).orElse(null); } if (child == null && node instanceof ActionNodeContainer) { - child = tryFindAction((ActionNodeContainer) node, qname).orElse(null); + child = tryFind(((ActionNodeContainer) node).getActions(), qname).orElse(null); } - } else if (node instanceof ChoiceSchemaNode) { - child = ((ChoiceSchemaNode) node).getCaseNodeByName(qname); - } else if (node instanceof RpcDefinition) { + + return child; + } + if (node instanceof ChoiceSchemaNode) { + return ((ChoiceSchemaNode) node).getCases().get(qname); + } + if (node instanceof OperationDefinition) { switch (qname.getLocalName()) { case "input": - child = ((RpcDefinition) node).getInput(); - break; + return ((OperationDefinition) node).getInput(); case "output": - child = ((RpcDefinition) node).getOutput(); - break; + return ((OperationDefinition) node).getOutput(); default: - child = null; - break; + return null; } - } else { - throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", node)); } - return child; - } - - /** - * Find child schema node identified by its QName within a provided schema node. This method performs lookup - * in both the namespace of groupings and the namespace of all leafs, leaf-lists, lists, containers, choices, rpcs, - * actions, notifications, anydatas and anyxmls according to RFC6050/RFC7950 section 6.2.1. - * - *

- * This method is deprecated, because name conflicts can occur between the namespace of groupings and namespace - * of data nodes and in consequence lookup could be ambiguous. - * - * @param node - * schema node - * @param qname - * QName - * @return child schema node - * @throws IllegalArgumentException - * if the schema node does not allow children - * - * @deprecated Use {@link #findChildSchemaNodesByQName(SchemaNode, QName)} instead. - */ - @Deprecated - public static SchemaNode findChildSchemaByQName(final SchemaNode node, final QName qname) { - SchemaNode child = findDataChildSchemaByQName(node, qname); - if (child == null && node instanceof DataNodeContainer) { - child = tryFindGroupings((DataNodeContainer) node, qname).orElse(null); - } - - return child; + throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", node)); } /** @@ -543,7 +479,7 @@ public final class SchemaUtils { final SchemaPath path) { final Collection currentNodes = new ArrayList<>(); final Collection childNodes = new ArrayList<>(); - currentNodes.add(Preconditions.checkNotNull(schemaContext)); + currentNodes.add(requireNonNull(schemaContext)); for (final QName qname : path.getPathFromRoot()) { for (final SchemaNode current : currentNodes) { childNodes.addAll(findChildSchemaNodesByQName(current, qname)); @@ -581,40 +517,12 @@ public final class SchemaUtils { childNodes.add(dataNode); } if (node instanceof DataNodeContainer) { - tryFindGroupings((DataNodeContainer) node, qname).ifPresent(childNodes::add); + tryFind(((DataNodeContainer) node).getGroupings(), qname).ifPresent(childNodes::add); } - return childNodes.isEmpty() ? Collections.emptyList() : ImmutableList.copyOf(childNodes); - } - - private static Optional tryFindGroupings(final DataNodeContainer dataNodeContainer, final QName qname) { - return Optional - .ofNullable(Iterables.find(dataNodeContainer.getGroupings(), new SchemaNodePredicate(qname), null)); - } - - private static Optional tryFindRpc(final SchemaContext ctx, final QName qname) { - return Optional.ofNullable(Iterables.find(ctx.getOperations(), new SchemaNodePredicate(qname), null)); + return childNodes.isEmpty() ? ImmutableList.of() : ImmutableList.copyOf(childNodes); } - private static Optional tryFindNotification(final NotificationNodeContainer notificationContanier, - final QName qname) { - return Optional.ofNullable( - Iterables.find(notificationContanier.getNotifications(), new SchemaNodePredicate(qname), null)); - } - - private static Optional tryFindAction(final ActionNodeContainer actionContanier, final QName qname) { - return Optional.ofNullable(Iterables.find(actionContanier.getActions(), new SchemaNodePredicate(qname), null)); - } - - private static final class SchemaNodePredicate implements Predicate { - private final QName qname; - - SchemaNodePredicate(final QName qname) { - this.qname = qname; - } - - @Override - public boolean apply(final SchemaNode input) { - return input.getQName().equals(qname); - } + private static Optional tryFind(final Collection nodes, final QName qname) { + return nodes.stream().filter(node -> qname.equals(node.getQName())).findFirst(); } }