Merge "Bug 1475: Correctly handle case of typedef that extends boolean"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / valid / DataNodeContainerValidator.java
index 8e0c1d32b05df344586819ba8a4a0e2ec110a25c..e48f5c2592945231e6ffe0ba830ecbac53a4eb41 100644 (file)
@@ -7,16 +7,20 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeSchemaAwareBuilder;
+import java.util.Set;
+
+import com.google.common.collect.Sets;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
 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.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 
-import java.util.Collection;
-import java.util.Collections;
+import com.google.common.base.Preconditions;
 
 /**
  * General validator for container like statements, e.g. container, list-entry, choice, augment
@@ -24,55 +28,53 @@ import java.util.Collections;
 public class DataNodeContainerValidator {
 
     private final DataNodeContainer schema;
-    private Collection<AugmentationSchema> augmentations;
+    private final Set<QName> childNodes;
+    private final Set<YangInstanceIdentifier.AugmentationIdentifier> augments = Sets.newHashSet();
 
     public DataNodeContainerValidator(DataNodeContainer schema) {
-        this.schema = schema;
-        augmentations = schema instanceof AugmentationTarget ? ((AugmentationTarget) schema)
-                .getAvailableAugmentations() : Collections.<AugmentationSchema> emptyList();
-    }
+        this.schema = Preconditions.checkNotNull(schema, "Schema was null");
 
-    private boolean isKnownChild(InstanceIdentifier.PathArgument child) {
-        // check augmentation by comparing all child nodes
-        if(child instanceof InstanceIdentifier.AugmentationIdentifier) {
-            for (AugmentationSchema augmentationSchema : augmentations) {
-                if(equalAugments(augmentationSchema, (InstanceIdentifier.AugmentationIdentifier) child)) {
-                    return true;
-                }
+        this.childNodes = getChildNodes(schema);
+
+        if(schema instanceof AugmentationTarget) {
+            for (AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
+                augments.add(SchemaUtils.getNodeIdentifierForAugmentation(augmentationSchema));
             }
-            // check regular child node
-        } else {
-            return schema.getDataChildByName(child.getNodeType()) != null;
         }
-
-        return false;
     }
 
-    private Optional<AugmentationSchema> isAugmentChild(InstanceIdentifier.PathArgument child) {
-        for (AugmentationSchema augmentationSchema : augmentations) {
-            if(ImmutableAugmentationNodeSchemaAwareBuilder.getChildQNames(augmentationSchema).contains(child.getNodeType())) {
-                return Optional.of(augmentationSchema);
-            }
+    private boolean isKnownChild(YangInstanceIdentifier.PathArgument child) {
+        if(child instanceof YangInstanceIdentifier.AugmentationIdentifier) {
+            return augments.contains(child);
         }
 
-        return Optional.absent();
+        return childNodes.contains(child.getNodeType());
     }
 
-    // FIXME, need to compare Set of QNames(AugmentationIdentifier) with Set of DataSchemaNodes(AugmentationSchema)
-    // throw away set is created just to compare
-    // Or if augmentationSchemaNode had a QName, we would just compare a QName
-    private boolean equalAugments(AugmentationSchema augmentationSchema, InstanceIdentifier.AugmentationIdentifier identifier) {
-        return identifier.getPossibleChildNames().equals(ImmutableAugmentationNodeSchemaAwareBuilder.getChildQNames(augmentationSchema));
+    public void validateChild(YangInstanceIdentifier.PathArgument child) {
+        DataValidationException.checkLegalChild(isKnownChild(child), child, schema, childNodes, augments);
     }
 
-    public void validateChild(InstanceIdentifier.PathArgument child) {
-        Preconditions.checkArgument(isKnownChild(child), "Unknown child node: %s, does not belong to: %s", child.getNodeType(), schema);
+    public DataContainerChild<?, ?> validateChild(DataContainerChild<?, ?> child) {
+        validateChild(child.getIdentifier());
+        return child;
+    }
+
+    /**
+     * Map all direct child nodes. Skip augments since they have no qname. List cases since cases do not exist in NormalizedNode API.
+     */
+    private static Set<QName> getChildNodes(DataNodeContainer nodeContainer) {
+        Set<QName> allChildNodes = Sets.newHashSet();
+
+        for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
+            if(childSchema instanceof ChoiceCaseNode) {
+                allChildNodes.addAll(getChildNodes((DataNodeContainer) childSchema));
+            } else if (childSchema instanceof AugmentationSchema == false) {
+                allChildNodes.add(childSchema.getQName());
+            }
+        }
 
-        // FIXME make a cache for augmentation child sets in constructor
-        Optional<AugmentationSchema> augmentChild = isAugmentChild(child);
-        Preconditions.checkArgument(
-                    augmentChild.isPresent() == false,
-                    "Illegal node type, child nodes from augmentation are not permitted as direct children, must be wrapped in augmentation node, "
-                            + "node: %s, from augmentation: %s, in parent: %s", child.getNodeType(), augmentChild, schema);
+        return allChildNodes;
     }
+
 }