8e0c1d32b05df344586819ba8a4a0e2ec110a25c
[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 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;
17
18 import java.util.Collection;
19 import java.util.Collections;
20
21 /**
22  * General validator for container like statements, e.g. container, list-entry, choice, augment
23  */
24 public class DataNodeContainerValidator {
25
26     private final DataNodeContainer schema;
27     private Collection<AugmentationSchema> augmentations;
28
29     public DataNodeContainerValidator(DataNodeContainer schema) {
30         this.schema = schema;
31         augmentations = schema instanceof AugmentationTarget ? ((AugmentationTarget) schema)
32                 .getAvailableAugmentations() : Collections.<AugmentationSchema> emptyList();
33     }
34
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)) {
40                     return true;
41                 }
42             }
43             // check regular child node
44         } else {
45             return schema.getDataChildByName(child.getNodeType()) != null;
46         }
47
48         return false;
49     }
50
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);
55             }
56         }
57
58         return Optional.absent();
59     }
60
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));
66     }
67
68     public void validateChild(InstanceIdentifier.PathArgument child) {
69         Preconditions.checkArgument(isKnownChild(child), "Unknown child node: %s, does not belong to: %s", child.getNodeType(), schema);
70
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);
77     }
78 }