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.*;
17 import com.google.common.base.Optional;
18 import com.google.common.base.Preconditions;
21 * General validator for container like statements, e.g. container, list-entry, choice, augment
23 public class DataNodeContainerValidator {
25 private final DataNodeContainer schema;
26 private Collection<AugmentationSchema> augmentations;
28 public DataNodeContainerValidator(DataNodeContainer schema) {
30 augmentations = schema instanceof AugmentationTarget ? ((AugmentationTarget) schema)
31 .getAvailableAugmentations() : Collections.<AugmentationSchema> emptyList();
34 private boolean isKnownChild(InstanceIdentifier.PathArgument child) {
35 // check augmentation by comparing all child nodes
36 if(child instanceof InstanceIdentifier.AugmentationIdentifier) {
37 for (AugmentationSchema augmentationSchema : augmentations) {
38 if(equalAugments(augmentationSchema, (InstanceIdentifier.AugmentationIdentifier) child)) {
42 // check regular child node (also in child cases)
43 } else if(schema.getDataChildByName(child.getNodeType()) == null) {
44 for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
45 if(dataSchemaNode instanceof ChoiceCaseNode) {
46 if(((ChoiceCaseNode) dataSchemaNode).getDataChildByName(child.getNodeType()) != null) {
58 private Optional<AugmentationSchema> isAugmentChild(InstanceIdentifier.PathArgument child) {
59 for (AugmentationSchema augmentationSchema : augmentations) {
60 if(ImmutableAugmentationNodeSchemaAwareBuilder.getChildQNames(augmentationSchema).contains(child.getNodeType())) {
61 return Optional.of(augmentationSchema);
65 return Optional.absent();
68 // FIXME, need to compare Set of QNames(AugmentationIdentifier) with Set of DataSchemaNodes(AugmentationSchema)
69 // throw away set is created just to compare
70 // Or if augmentationSchemaNode had a QName, we would just compare a QName
71 public static boolean equalAugments(AugmentationSchema augmentationSchema, InstanceIdentifier.AugmentationIdentifier identifier) {
72 return identifier.getPossibleChildNames().equals(ImmutableAugmentationNodeSchemaAwareBuilder.getChildQNames(augmentationSchema));
75 public void validateChild(InstanceIdentifier.PathArgument child) {
76 Preconditions.checkArgument(isKnownChild(child), "Unknown child node: %s, does not belong to: %s", child, schema);
78 // FIXME make a cache for augmentation child sets in constructor
79 Optional<AugmentationSchema> augmentChild = isAugmentChild(child);
80 Preconditions.checkArgument(
81 augmentChild.isPresent() == false,
82 "Illegal node type, child nodes from augmentation are not permitted as direct children, must be wrapped in augmentation node, "
83 + "node: %s, from augmentation: %s, in parent: %s", child.getNodeType(), augmentChild, schema);