Abstract infrastructure for normalized nodes translation.
[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.*;
16
17 import com.google.common.base.Optional;
18 import com.google.common.base.Preconditions;
19
20 /**
21  * General validator for container like statements, e.g. container, list-entry, choice, augment
22  */
23 public class DataNodeContainerValidator {
24
25     private final DataNodeContainer schema;
26     private Collection<AugmentationSchema> augmentations;
27
28     public DataNodeContainerValidator(DataNodeContainer schema) {
29         this.schema = schema;
30         augmentations = schema instanceof AugmentationTarget ? ((AugmentationTarget) schema)
31                 .getAvailableAugmentations() : Collections.<AugmentationSchema> emptyList();
32     }
33
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)) {
39                     return true;
40                 }
41             }
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) {
47                         return true;
48                     }
49                 }
50             }
51         } else {
52             return true;
53         }
54
55         return false;
56     }
57
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);
62             }
63         }
64
65         return Optional.absent();
66     }
67
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));
73     }
74
75     public void validateChild(InstanceIdentifier.PathArgument child) {
76         Preconditions.checkArgument(isKnownChild(child), "Unknown child node: %s, does not belong to: %s", child, schema);
77
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);
84     }
85 }