*/
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 org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import static java.util.Objects.requireNonNull;
+
+import java.util.HashSet;
+import java.util.Set;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
+import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-
-import java.util.Collection;
-import java.util.Collections;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
/**
* General validator for container like statements, e.g. container, list-entry, choice, augment
*/
public class DataNodeContainerValidator {
-
+ private final Set<AugmentationIdentifier> augments = new HashSet<>();
private final DataNodeContainer schema;
- private Collection<AugmentationSchema> augmentations;
+ private final Set<QName> childNodes;
- public DataNodeContainerValidator(DataNodeContainer schema) {
- this.schema = schema;
- augmentations = schema instanceof AugmentationTarget ? ((AugmentationTarget) schema)
- .getAvailableAugmentations() : Collections.<AugmentationSchema> emptyList();
- }
+ public DataNodeContainerValidator(final DataNodeContainer schema) {
+ this.schema = requireNonNull(schema, "Schema was null");
+ this.childNodes = getChildNodes(schema);
- 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;
- }
+ if (schema instanceof AugmentationTarget) {
+ for (AugmentationSchemaNode augmentation : ((AugmentationTarget) schema).getAvailableAugmentations()) {
+ augments.add(DataSchemaContextNode.augmentationIdentifierFrom(augmentation));
}
- // 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(final PathArgument child) {
+ if (child instanceof 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(final 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(final 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(final DataNodeContainer nodeContainer) {
+ Set<QName> allChildNodes = new HashSet<>();
+
+ for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
+ if (childSchema instanceof CaseSchemaNode) {
+ allChildNodes.addAll(getChildNodes((DataNodeContainer) childSchema));
+ } else if (!(childSchema instanceof AugmentationSchemaNode)) {
+ 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;
}
}