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=a93ff38ebdd7754b1b162aed00d6046112fb284f;hb=bb60da5fe2d1928defb46ed92b290cfff93dcd81;hp=7f203557e21c097eaf2d453e45d9540dda7694b8;hpb=e24054c832e38b877db653fc9bb77041b3b2c0ed;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 7f203557e2..a93ff38ebd 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 @@ -15,7 +15,6 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; 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,13 +22,12 @@ 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.data.util.DataSchemaContextNode; import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer; import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode; import org.opendaylight.yangtools.yang.model.api.AugmentationTarget; @@ -38,14 +36,14 @@ 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 } /** @@ -56,7 +54,7 @@ public final class SchemaUtils { * @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) { + final Iterable dataSchemaNode) { DataSchemaNode schema = null; if (dataSchemaNode != null && qname != null) { for (final DataSchemaNode dsn : dataSchemaNode) { @@ -66,11 +64,10 @@ public final class SchemaUtils { schema = dsn; } } else if (dsn instanceof ChoiceSchemaNode) { - for (final CaseSchemaNode choiceCase : ((ChoiceSchemaNode) dsn).getCases().values()) { - - final DataSchemaNode dataChildByName = choiceCase.getDataChildByName(qname); - if (dataChildByName != null) { - return Optional.of(dataChildByName); + for (final CaseSchemaNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) { + final Optional dataChildByName = choiceCase.findDataChildByName(qname); + if (dataChildByName.isPresent()) { + return dataChildByName; } final Optional foundDsn = findFirstSchema(qname, choiceCase.getChildNodes()); if (foundDsn.isPresent()) { @@ -94,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); @@ -113,7 +110,7 @@ public final class SchemaUtils { } public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, - final Iterable childNodes) { + final Iterable childNodes) { final Optional childSchema = findFirstSchema(qname, childNodes); checkState(childSchema.isPresent(), "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname, schema); @@ -121,7 +118,7 @@ public final class SchemaUtils { } public static DataSchemaNode findSchemaForChild(final ChoiceSchemaNode schema, final QName childPartialQName) { - for (final CaseSchemaNode choiceCaseNode : schema.getCases().values()) { + for (final CaseSchemaNode choiceCaseNode : schema.getCases()) { final Optional childSchema = findFirstSchema(childPartialQName, choiceCaseNode.getChildNodes()); if (childSchema.isPresent()) { @@ -143,7 +140,7 @@ public final class SchemaUtils { } public static AugmentationSchemaNode findSchemaForAugment(final ChoiceSchemaNode schema, final Set qnames) { - for (final CaseSchemaNode choiceCaseNode : schema.getCases().values()) { + for (final CaseSchemaNode choiceCaseNode : schema.getCases()) { final Optional schemaForAugment = findAugment(choiceCaseNode, qnames); if (schemaForAugment.isPresent()) { return schemaForAugment.get(); @@ -158,9 +155,8 @@ public final class SchemaUtils { 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); } } @@ -179,7 +175,7 @@ public final class SchemaUtils { } private static Map mapChildElementsFromChoices(final DataNodeContainer schema, - final Iterable childNodes) { + final Iterable childNodes) { final Map mappedChoices = new LinkedHashMap<>(); for (final DataSchemaNode childSchema : childNodes) { @@ -189,7 +185,7 @@ public final class SchemaUtils { continue; } - for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases().values()) { + for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) { for (final QName qname : getChildNodesRecursive(choiceCaseNode)) { mappedChoices.put(qname, (ChoiceSchemaNode) childSchema); } @@ -206,7 +202,7 @@ public final class SchemaUtils { } for (final AugmentationSchemaNode augmentation : ((AugmentationTarget) schema).getAvailableAugmentations()) { - if (augmentation.getDataChildByName(childSchema.getQName()) != null) { + if (augmentation.findDataChildByName(childSchema.getQName()).isPresent()) { return true; } } @@ -250,7 +246,7 @@ public final class SchemaUtils { childNodesToAugmentation.put(qname, mostTopAugmentation); } } else if (child instanceof ChoiceSchemaNode) { - for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases().values()) { + for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases()) { 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 CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases().values()) { + for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases()) { if (!augments.containsKey(choiceCaseNode.getQName())) { continue; } @@ -288,7 +284,7 @@ public final class SchemaUtils { for (final DataSchemaNode childSchema : nodeContainer.getChildNodes()) { if (childSchema instanceof ChoiceSchemaNode) { - for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases().values()) { + for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) { allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode)); } } else if (childSchema instanceof AugmentationSchemaNode || childSchema instanceof CaseSchemaNode) { @@ -315,7 +311,7 @@ public final class SchemaUtils { public static Set getRealSchemasForAugment(final AugmentationTarget targetSchema, 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 CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases().values()) { + for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases()) { if (getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) { realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName())); } @@ -346,11 +342,11 @@ public final class SchemaUtils { public static Optional detectCase(final ChoiceSchemaNode schema, final DataContainerChild child) { - for (final CaseSchemaNode choiceCaseNode : schema.getCases().values()) { + for (final CaseSchemaNode choiceCaseNode : schema.getCases()) { 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); } } @@ -390,27 +386,14 @@ public final class SchemaUtils { } for (final AugmentationSchemaNode augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) { - final DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName()); - if (childInAugmentation != null) { + final Optional childInAugmentation = augmentation.findDataChildByName(child.getQName()); + if (childInAugmentation.isPresent()) { return augmentation; } } return null; } - /** - * Create AugmentationIdentifier from an AugmentationSchemaNode. - * - * @param schema Augmentation schema - * @return AugmentationIdentifier for the schema - * @throws NullPointerException if {@code schema} is null - * @deprecated Use {@link DataSchemaContextNode#augmentationIdentifierFrom(AugmentationSchemaNode)} instead. - */ - @Deprecated - public static AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchemaNode schema) { - return DataSchemaContextNode.augmentationIdentifierFrom(schema); - } - /** * Finds schema node for given path in schema context. This method performs * lookup in the namespace of all leafs, leaf-lists, lists, containers, @@ -444,10 +427,9 @@ 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) { + public static @Nullable SchemaNode findDataChildSchemaByQName(final SchemaNode node, final QName qname) { if (node instanceof DataNodeContainer) { - SchemaNode child = ((DataNodeContainer) node).getDataChildByName(qname); + SchemaNode child = ((DataNodeContainer) node).dataChildByName(qname); if (child == null && node instanceof SchemaContext) { child = tryFind(((SchemaContext) node).getOperations(), qname).orElse(null); } @@ -461,14 +443,14 @@ public final class SchemaUtils { return child; } if (node instanceof ChoiceSchemaNode) { - return ((ChoiceSchemaNode) node).getCaseNodeByName(qname); + return ((ChoiceSchemaNode) node).findCase(qname).orElse(null); } - if (node instanceof RpcDefinition) { + if (node instanceof OperationDefinition) { switch (qname.getLocalName()) { case "input": - return ((RpcDefinition) node).getInput(); + return ((OperationDefinition) node).getInput(); case "output": - return ((RpcDefinition) node).getOutput(); + return ((OperationDefinition) node).getOutput(); default: return null; } @@ -495,10 +477,29 @@ public final class SchemaUtils { */ public static Collection findParentSchemaNodesOnPath(final SchemaContext schemaContext, final SchemaPath path) { + return findParentSchemaNodesOnPath(schemaContext, path.getPathFromRoot()); + } + + /** + * 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 returns collection of SchemaNodes, because name conflicts can occur between the namespace + * of groupings and namespace of data nodes. This method finds and collects all schema nodes that matches supplied + * SchemaPath and returns them all as collection of schema nodes. + * + * @param schemaContext schema context + * @param path path + * @return collection of schema nodes on path + */ + public static Collection findParentSchemaNodesOnPath(final SchemaContext schemaContext, + final Iterable path) { final Collection currentNodes = new ArrayList<>(); final Collection childNodes = new ArrayList<>(); currentNodes.add(requireNonNull(schemaContext)); - for (final QName qname : path.getPathFromRoot()) { + for (final QName qname : path) { for (final SchemaNode current : currentNodes) { childNodes.addAll(findChildSchemaNodesByQName(current, qname)); }