Migrate getDataChildByName() users
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / SchemaUtils.java
index 9747829b393cfc7c777546a486d0714e2137fb0c..a93ff38ebdd7754b1b162aed00d6046112fb284f 100644 (file)
@@ -43,7 +43,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 public final class SchemaUtils {
     private SchemaUtils() {
-        throw new UnsupportedOperationException();
+        // Hidden on purpose
     }
 
     /**
@@ -54,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<DataSchemaNode> findFirstSchema(final QName qname,
-            final Iterable<DataSchemaNode> dataSchemaNode) {
+            final Iterable<? extends DataSchemaNode> dataSchemaNode) {
         DataSchemaNode schema = null;
         if (dataSchemaNode != null && qname != null) {
             for (final DataSchemaNode dsn : dataSchemaNode) {
@@ -64,7 +64,7 @@ public final class SchemaUtils {
                         schema = dsn;
                     }
                 } else if (dsn instanceof ChoiceSchemaNode) {
-                    for (final CaseSchemaNode choiceCase : ((ChoiceSchemaNode) dsn).getCases().values()) {
+                    for (final CaseSchemaNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
                         final Optional<DataSchemaNode> dataChildByName = choiceCase.findDataChildByName(qname);
                         if (dataChildByName.isPresent()) {
                             return dataChildByName;
@@ -110,7 +110,7 @@ public final class SchemaUtils {
     }
 
     public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname,
-            final Iterable<DataSchemaNode> childNodes) {
+            final Iterable<? extends DataSchemaNode> childNodes) {
         final Optional<DataSchemaNode> childSchema = findFirstSchema(qname, childNodes);
         checkState(childSchema.isPresent(), "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname,
             schema);
@@ -118,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<DataSchemaNode> childSchema = findFirstSchema(childPartialQName,
                 choiceCaseNode.getChildNodes());
             if (childSchema.isPresent()) {
@@ -140,7 +140,7 @@ public final class SchemaUtils {
     }
 
     public static AugmentationSchemaNode findSchemaForAugment(final ChoiceSchemaNode schema, final Set<QName> qnames) {
-        for (final CaseSchemaNode choiceCaseNode : schema.getCases().values()) {
+        for (final CaseSchemaNode choiceCaseNode : schema.getCases()) {
             final Optional<AugmentationSchemaNode> schemaForAugment = findAugment(choiceCaseNode, qnames);
             if (schemaForAugment.isPresent()) {
                 return schemaForAugment.get();
@@ -175,7 +175,7 @@ public final class SchemaUtils {
     }
 
     private static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema,
-            final Iterable<DataSchemaNode> childNodes) {
+            final Iterable<? extends DataSchemaNode> childNodes) {
         final Map<QName, ChoiceSchemaNode> mappedChoices = new LinkedHashMap<>();
 
         for (final DataSchemaNode childSchema : childNodes) {
@@ -185,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);
                     }
@@ -246,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);
                         }
@@ -259,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;
                 }
@@ -284,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) {
@@ -319,7 +319,7 @@ public final class SchemaUtils {
         final Set<DataSchemaNode> 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()));
                     }
@@ -342,7 +342,7 @@ public final class SchemaUtils {
 
     public static Optional<CaseSchemaNode> 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);
@@ -429,7 +429,7 @@ public final class SchemaUtils {
      */
     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);
             }
@@ -443,7 +443,7 @@ public final class SchemaUtils {
             return child;
         }
         if (node instanceof ChoiceSchemaNode) {
-            return ((ChoiceSchemaNode) node).getCases().get(qname);
+            return ((ChoiceSchemaNode) node).findCase(qname).orElse(null);
         }
         if (node instanceof OperationDefinition) {
             switch (qname.getLocalName()) {
@@ -477,10 +477,29 @@ public final class SchemaUtils {
      */
     public static Collection<SchemaNode> 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.
+     *
+     * <p>
+     * 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<SchemaNode> findParentSchemaNodesOnPath(final SchemaContext schemaContext,
+            final Iterable<QName> path) {
         final Collection<SchemaNode> currentNodes = new ArrayList<>();
         final Collection<SchemaNode> 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));
             }