X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fimpl%2Fschema%2Fbuilder%2Fimpl%2Fvalid%2FDataNodeContainerValidator.java;h=e48f5c2592945231e6ffe0ba830ecbac53a4eb41;hb=30b223a20b7a10ddab42115eee15d7552477f752;hp=f3d8f31478cfba2e46c624e8527d098ae71afb1e;hpb=4a5d26c9de475a4cff151ee4255e027211c2eeb4;p=yangtools.git diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/valid/DataNodeContainerValidator.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/valid/DataNodeContainerValidator.java index f3d8f31478..e48f5c2592 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/valid/DataNodeContainerValidator.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/valid/DataNodeContainerValidator.java @@ -7,14 +7,19 @@ */ package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid; -import java.util.Collection; -import java.util.Collections; +import java.util.Set; -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.*; +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 com.google.common.base.Optional; import com.google.common.base.Preconditions; /** @@ -23,63 +28,53 @@ import com.google.common.base.Preconditions; public class DataNodeContainerValidator { private final DataNodeContainer schema; - private Collection augmentations; + private final Set childNodes; + private final Set augments = Sets.newHashSet(); public DataNodeContainerValidator(DataNodeContainer schema) { - this.schema = schema; - augmentations = schema instanceof AugmentationTarget ? ((AugmentationTarget) schema) - .getAvailableAugmentations() : Collections. 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; - } - } - // check regular child node (also in child cases) - } else if(schema.getDataChildByName(child.getNodeType()) == null) { - for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) { - if(dataSchemaNode instanceof ChoiceCaseNode) { - if(((ChoiceCaseNode) dataSchemaNode).getDataChildByName(child.getNodeType()) != null) { - return true; - } - } + this.childNodes = getChildNodes(schema); + + if(schema instanceof AugmentationTarget) { + for (AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) { + augments.add(SchemaUtils.getNodeIdentifierForAugmentation(augmentationSchema)); } - } else { - return true; } - - return false; } - private Optional 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 - public static 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, schema); + public DataContainerChild validateChild(DataContainerChild child) { + validateChild(child.getIdentifier()); + return child; + } - // FIXME make a cache for augmentation child sets in constructor - Optional 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); + /** + * 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 getChildNodes(DataNodeContainer nodeContainer) { + Set 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()); + } + } + + return allChildNodes; } + }