Add attributes to nodes that can take attributes
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / valid / DataNodeContainerValidator.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid;
9
10 import java.util.Collection;
11 import java.util.Collections;
12
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;
20
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
23
24 /**
25  * General validator for container like statements, e.g. container, list-entry, choice, augment
26  */
27 public class DataNodeContainerValidator {
28
29     private final DataNodeContainer schema;
30     private final Collection<AugmentationSchema> augmentations;
31
32     public DataNodeContainerValidator(DataNodeContainer schema) {
33         this.schema = Preconditions.checkNotNull(schema);
34         augmentations = schema instanceof AugmentationTarget ? ((AugmentationTarget) schema)
35                 .getAvailableAugmentations() : Collections.<AugmentationSchema> emptyList();
36     }
37
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)) {
43                     return true;
44                 }
45             }
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) {
51                         return true;
52                     }
53                 }
54             }
55         } else {
56             return true;
57         }
58
59         return false;
60     }
61
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);
66             }
67         }
68
69         return Optional.absent();
70     }
71
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));
77     }
78
79     public void validateChild(InstanceIdentifier.PathArgument child) {
80         Preconditions.checkArgument(isKnownChild(child), "Unknown child node: %s, does not belong to: %s", child, schema);
81
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);
88     }
89 }