Bug 4295: Fixed incorrectly introduced nodes when MERGE was followed by DELETE
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / SchemaUtils.java
index 53bdbd84bc53550a6222e3559ef24c7615b327d9..8de2f5bf483681e699199c78326bc735585524cb 100644 (file)
@@ -11,15 +11,18 @@ 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.ImmutableSet;
 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;
@@ -31,7 +34,15 @@ import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 
 public final class SchemaUtils {
 
+    private static final Function<DataSchemaNode, QName> QNAME_FUNCTION = new Function<DataSchemaNode, QName>() {
+        @Override
+        public QName apply(@Nonnull final DataSchemaNode input) {
+            return input.getQName();
+        }
+    };
+
     private SchemaUtils() {
+        throw new UnsupportedOperationException();
     }
 
     /**
@@ -39,7 +50,7 @@ public final class SchemaUtils {
      * @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) {
+    public static Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
         DataSchemaNode sNode = null;
         if (dataSchemaNode != null && qname != null) {
             for (DataSchemaNode dsn : dataSchemaNode) {
@@ -80,6 +91,19 @@ public final class SchemaUtils {
         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) {
         Optional<DataSchemaNode> childSchema = findFirstSchema(qname, childNodes);
         Preconditions.checkState(childSchema.isPresent(),
@@ -114,7 +138,7 @@ public final class SchemaUtils {
 
             HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(), new Function<DataSchemaNode, QName>() {
                 @Override
-                public QName apply(final @Nonnull DataSchemaNode input) {
+                public QName apply(@Nonnull final DataSchemaNode input) {
                     Preconditions.checkNotNull(input);
                     return input.getQName();
                 }
@@ -144,6 +168,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<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
@@ -189,6 +214,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<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
@@ -252,6 +278,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<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
         Set<QName> allChildNodes = Sets.newHashSet();
@@ -278,6 +307,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<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
         if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) {
@@ -314,8 +346,7 @@ public final class SchemaUtils {
     public static Optional<ChoiceCaseNode> 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);
@@ -325,7 +356,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<QName> currentAugmentChildNodes = Sets.newHashSet();
@@ -341,17 +372,29 @@ public final class SchemaUtils {
         return false;
     }
 
-    public static YangInstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
-        return new YangInstanceIdentifier.AugmentationIdentifier(getChildQNames(schema));
-    }
-
-    public static Set<QName> getChildQNames(final AugmentationSchema schema) {
-        Set<QName> qnames = Sets.newHashSet();
-
-        for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
-            qnames.add(dataSchemaNode.getQName());
+    /**
+     * 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;
+    }
 
-        return qnames;
+    public static AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
+        final Collection<QName> qnames = Collections2.transform(schema.getChildNodes(), QNAME_FUNCTION);
+        return new AugmentationIdentifier(ImmutableSet.copyOf(qnames));
     }
+
 }