Merge "Fix missing format placeholder"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / SchemaUtils.java
index bafeeaeb8a16de93d367f25a99e56fac5f08e4f4..a5c5148548418596d13c1ef26543aa89c527aff4 100644 (file)
@@ -7,27 +7,27 @@
  */
 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.collect.Collections2;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
-
 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.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
-import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 
@@ -36,26 +36,63 @@ public final class SchemaUtils {
     private SchemaUtils() {
     }
 
+    /**
+     * @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 final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
+        DataSchemaNode sNode = null;
         if (dataSchemaNode != null && qname != null) {
             for (DataSchemaNode dsn : dataSchemaNode) {
                 if (qname.isEqualWithoutRevision(dsn.getQName())) {
-                    return Optional.<DataSchemaNode> of(dsn);
-                } else if (dsn instanceof ChoiceNode) {
-                    for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
+                    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<DataSchemaNode> 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<DataSchemaNode> 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<DataSchemaNode> childNodes) {
@@ -72,7 +109,7 @@ public final class SchemaUtils {
         return schemaForAugment.get();
     }
 
-    public static AugmentationSchema findSchemaForAugment(final ChoiceNode schema, final Set<QName> qNames) {
+    public static AugmentationSchema findSchemaForAugment(final ChoiceSchemaNode schema, final Set<QName> qNames) {
         Optional<AugmentationSchema> schemaForAugment = Optional.absent();
 
         for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
@@ -92,7 +129,8 @@ public final class SchemaUtils {
 
             HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(), new Function<DataSchemaNode, QName>() {
                 @Override
-                public QName apply(final DataSchemaNode input) {
+                public QName apply(final @Nonnull DataSchemaNode input) {
+                    Preconditions.checkNotNull(input);
                     return input.getQName();
                 }
             }));
@@ -105,7 +143,7 @@ public final class SchemaUtils {
         return Optional.absent();
     }
 
-    public static DataSchemaNode findSchemaForChild(final ChoiceNode schema, final QName childPartialQName) {
+    public static DataSchemaNode findSchemaForChild(final ChoiceSchemaNode schema, final QName childPartialQName) {
         for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
             Optional<DataSchemaNode> childSchema = findFirstSchema(childPartialQName, choiceCaseNode.getChildNodes());
             if (childSchema.isPresent()) {
@@ -123,24 +161,24 @@ public final class SchemaUtils {
      *
      * @return Map with all child nodes, to their most top augmentation
      */
-    public static Map<QName, ChoiceNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
+    public static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
         return mapChildElementsFromChoices(schema, schema.getChildNodes());
     }
 
-    private static Map<QName, ChoiceNode> mapChildElementsFromChoices(final DataNodeContainer schema, final Iterable<DataSchemaNode> childNodes) {
-        Map<QName, ChoiceNode> mappedChoices = Maps.newLinkedHashMap();
+    private static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema, final Iterable<DataSchemaNode> childNodes) {
+        Map<QName, ChoiceSchemaNode> mappedChoices = Maps.newLinkedHashMap();
 
         for (final DataSchemaNode childSchema : childNodes) {
-            if(childSchema instanceof ChoiceNode) {
+            if (childSchema instanceof ChoiceSchemaNode) {
 
-                if(isFromAugment(schema, childSchema)) {
+                if (isFromAugment(schema, childSchema)) {
                     continue;
                 }
 
-                for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) childSchema).getCases()) {
+                for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
 
                     for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
-                        mappedChoices.put(qName, (ChoiceNode) childSchema);
+                        mappedChoices.put(qName, (ChoiceSchemaNode) childSchema);
                     }
                 }
             }
@@ -150,7 +188,7 @@ public final class SchemaUtils {
     }
 
     private static boolean isFromAugment(final DataNodeContainer schema, final DataSchemaNode childSchema) {
-        if(schema instanceof AugmentationTarget == false) {
+        if (!(schema instanceof AugmentationTarget)) {
             return false;
         }
 
@@ -186,7 +224,7 @@ public final class SchemaUtils {
 
             for (DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
                 // If is not augmented child, continue
-                if (augments.containsKey(child.getQName()) == false) {
+                if (!(augments.containsKey(child.getQName()))) {
                     continue;
                 }
 
@@ -197,8 +235,8 @@ public final class SchemaUtils {
                     for (QName qName : getChildNodesRecursive((DataNodeContainer) child)) {
                         childNodesToAugmentation.put(qName, mostTopAugmentation);
                     }
-                } else if (child instanceof ChoiceNode) {
-                    for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) child).getCases()) {
+                } else if (child instanceof ChoiceSchemaNode) {
+                    for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases()) {
                         for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
                             childNodesToAugmentation.put(qName, mostTopAugmentation);
                         }
@@ -210,9 +248,9 @@ public final class SchemaUtils {
         }
 
         // Choice Node has to map child nodes from all its cases
-        if (schema instanceof ChoiceNode) {
-            for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) schema).getCases()) {
-                if (augments.containsKey(choiceCaseNode.getQName()) == false) {
+        if (schema instanceof ChoiceSchemaNode) {
+            for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases()) {
+                if (!(augments.containsKey(choiceCaseNode.getQName()))) {
                     continue;
                 }
 
@@ -234,8 +272,8 @@ public final class SchemaUtils {
         Set<QName> allChildNodes = Sets.newHashSet();
 
         for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
-            if(childSchema instanceof ChoiceNode) {
-                for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) childSchema).getCases()) {
+            if(childSchema instanceof ChoiceSchemaNode) {
+                for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
                     allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
                 }
             } else if(childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
@@ -253,11 +291,11 @@ public final class SchemaUtils {
      * Retrieves real schemas for augmented child node.
      *
      * 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.
+     * Schema of child node from augment is incomplete, therefore its useless for XML/NormalizedNode translation.
      *
      */
     public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
-        if(targetSchema.getAvailableAugmentations().contains(augmentSchema) == false) {
+        if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) {
             return Collections.emptySet();
         }
 
@@ -265,9 +303,9 @@ public final class SchemaUtils {
 
         if(targetSchema instanceof DataNodeContainer) {
             realChildNodes = getRealSchemasForAugment((DataNodeContainer)targetSchema, augmentSchema);
-        } else if(targetSchema instanceof ChoiceNode) {
+        } else if(targetSchema instanceof ChoiceSchemaNode) {
             for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
-                for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) targetSchema).getCases()) {
+                for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases()) {
                     if(getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
                         realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName()));
                     }
@@ -288,11 +326,11 @@ public final class SchemaUtils {
         return realChildNodes;
     }
 
-    public static Optional<ChoiceCaseNode> detectCase(final ChoiceNode schema, final DataContainerChild<?, ?> child) {
+    public static Optional<ChoiceCaseNode> detectCase(final ChoiceSchemaNode schema, final DataContainerChild<?, ?> child) {
         for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
             if (child instanceof AugmentationNode
                     && belongsToCaseAugment(choiceCaseNode,
-                            (InstanceIdentifier.AugmentationIdentifier) child.getIdentifier())) {
+                            (YangInstanceIdentifier.AugmentationIdentifier) child.getIdentifier())) {
                 return Optional.of(choiceCaseNode);
             } else if (choiceCaseNode.getDataChildByName(child.getNodeType()) != null) {
                 return Optional.of(choiceCaseNode);
@@ -302,7 +340,7 @@ public final class SchemaUtils {
         return Optional.absent();
     }
 
-    public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final InstanceIdentifier.AugmentationIdentifier childToProcess) {
+    public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final YangInstanceIdentifier.AugmentationIdentifier childToProcess) {
         for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
 
             Set<QName> currentAugmentChildNodes = Sets.newHashSet();
@@ -318,8 +356,8 @@ public final class SchemaUtils {
         return false;
     }
 
-    public static InstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
-        return new InstanceIdentifier.AugmentationIdentifier(getChildQNames(schema));
+    public static YangInstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
+        return new YangInstanceIdentifier.AugmentationIdentifier(getChildQNames(schema));
     }
 
     public static Set<QName> getChildQNames(final AugmentationSchema schema) {