2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid;
10 import java.util.Collection;
11 import java.util.Collections;
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeSchemaAwareBuilder;
15 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
16 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
17 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
18 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
25 * General validator for container like statements, e.g. container, list-entry, choice, augment
27 public class DataNodeContainerValidator {
29 private final DataNodeContainer schema;
30 private final Collection<AugmentationSchema> augmentations;
32 public DataNodeContainerValidator(DataNodeContainer schema) {
33 this.schema = Preconditions.checkNotNull(schema);
34 augmentations = schema instanceof AugmentationTarget ? ((AugmentationTarget) schema)
35 .getAvailableAugmentations() : Collections.<AugmentationSchema> emptyList();
38 private boolean isKnownChild(InstanceIdentifier.PathArgument child) {
39 // check augmentation by comparing all child nodes
40 if(child instanceof InstanceIdentifier.AugmentationIdentifier) {
41 for (AugmentationSchema augmentationSchema : augmentations) {
42 if(equalAugments(augmentationSchema, (InstanceIdentifier.AugmentationIdentifier) child)) {
46 // check regular child node (also in child cases)
47 } else if(schema.getDataChildByName(child.getNodeType()) == null) {
48 for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
49 if(dataSchemaNode instanceof ChoiceCaseNode) {
50 if(((ChoiceCaseNode) dataSchemaNode).getDataChildByName(child.getNodeType()) != null) {
62 private Optional<AugmentationSchema> isAugmentChild(InstanceIdentifier.PathArgument child) {
63 for (AugmentationSchema augmentationSchema : augmentations) {
64 if(ImmutableAugmentationNodeSchemaAwareBuilder.getChildQNames(augmentationSchema).contains(child.getNodeType())) {
65 return Optional.of(augmentationSchema);
69 return Optional.absent();
72 // FIXME, need to compare Set of QNames(AugmentationIdentifier) with Set of DataSchemaNodes(AugmentationSchema)
73 // throw away set is created just to compare
74 // Or if augmentationSchemaNode had a QName, we would just compare a QName
75 public static boolean equalAugments(AugmentationSchema augmentationSchema, InstanceIdentifier.AugmentationIdentifier identifier) {
76 return identifier.getPossibleChildNames().equals(ImmutableAugmentationNodeSchemaAwareBuilder.getChildQNames(augmentationSchema));
79 public void validateChild(InstanceIdentifier.PathArgument child) {
80 Preconditions.checkArgument(isKnownChild(child), "Unknown child node: %s, does not belong to: %s", child, schema);
82 // FIXME make a cache for augmentation child sets in constructor
83 Optional<AugmentationSchema> augmentChild = isAugmentChild(child);
84 Preconditions.checkArgument(
85 augmentChild.isPresent() == false,
86 "Illegal node type, child nodes from augmentation are not permitted as direct children, must be wrapped in augmentation node, "
87 + "node: %s, from augmentation: %s, in parent: %s", child.getNodeType(), augmentChild, schema);