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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
13 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeSchemaAwareBuilder;
14 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
15 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
16 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
18 import java.util.Collection;
19 import java.util.Collections;
22 * General validator for container like statements, e.g. container, list-entry, choice, augment
24 public class DataNodeContainerValidator {
26 private final DataNodeContainer schema;
27 private Collection<AugmentationSchema> augmentations;
29 public DataNodeContainerValidator(DataNodeContainer schema) {
31 augmentations = schema instanceof AugmentationTarget ? ((AugmentationTarget) schema)
32 .getAvailableAugmentations() : Collections.<AugmentationSchema> emptyList();
35 private boolean isKnownChild(InstanceIdentifier.PathArgument child) {
36 // check augmentation by comparing all child nodes
37 if(child instanceof InstanceIdentifier.AugmentationIdentifier) {
38 for (AugmentationSchema augmentationSchema : augmentations) {
39 if(equalAugments(augmentationSchema, (InstanceIdentifier.AugmentationIdentifier) child)) {
43 // check regular child node
45 return schema.getDataChildByName(child.getNodeType()) != null;
51 private Optional<AugmentationSchema> isAugmentChild(InstanceIdentifier.PathArgument child) {
52 for (AugmentationSchema augmentationSchema : augmentations) {
53 if(ImmutableAugmentationNodeSchemaAwareBuilder.getChildQNames(augmentationSchema).contains(child.getNodeType())) {
54 return Optional.of(augmentationSchema);
58 return Optional.absent();
61 // FIXME, need to compare Set of QNames(AugmentationIdentifier) with Set of DataSchemaNodes(AugmentationSchema)
62 // throw away set is created just to compare
63 // Or if augmentationSchemaNode had a QName, we would just compare a QName
64 private boolean equalAugments(AugmentationSchema augmentationSchema, InstanceIdentifier.AugmentationIdentifier identifier) {
65 return identifier.getPossibleChildNames().equals(ImmutableAugmentationNodeSchemaAwareBuilder.getChildQNames(augmentationSchema));
68 public void validateChild(InstanceIdentifier.PathArgument child) {
69 Preconditions.checkArgument(isKnownChild(child), "Unknown child node: %s, does not belong to: %s", child.getNodeType(), schema);
71 // FIXME make a cache for augmentation child sets in constructor
72 Optional<AugmentationSchema> augmentChild = isAugmentChild(child);
73 Preconditions.checkArgument(
74 augmentChild.isPresent() == false,
75 "Illegal node type, child nodes from augmentation are not permitted as direct children, must be wrapped in augmentation node, "
76 + "node: %s, from augmentation: %s, in parent: %s", child.getNodeType(), augmentChild, schema);