Bug 7945: Fix schema validation for augmentation of case
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / CaseEnforcer.java
1 /*
2  * Copyright (c) 2015 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.tree;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableMap.Builder;
13 import com.google.common.collect.Sets;
14
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18
19 import org.opendaylight.yangtools.concepts.Immutable;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
26 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30
31 final class CaseEnforcer implements Immutable {
32     private final Map<NodeIdentifier, DataSchemaNode> children;
33     private final Map<AugmentationIdentifier, AugmentationSchema> augmentations;
34     private final MandatoryLeafEnforcer enforcer;
35
36     private CaseEnforcer(final Map<NodeIdentifier, DataSchemaNode> children,
37                          final Map<AugmentationIdentifier, AugmentationSchema> augmentations,
38                          final MandatoryLeafEnforcer enforcer) {
39         this.children = Preconditions.checkNotNull(children);
40         this.augmentations = Preconditions.checkNotNull(augmentations);
41         this.enforcer = Preconditions.checkNotNull(enforcer);
42     }
43
44     static CaseEnforcer forTree(final ChoiceCaseNode schema, final DataTreeConfiguration treeConfig) {
45         final TreeType type = treeConfig.getTreeType();
46         final Builder<NodeIdentifier, DataSchemaNode> childrenBuilder = ImmutableMap.builder();
47         final Builder<AugmentationIdentifier, AugmentationSchema> augmentationsBuilder = ImmutableMap.builder();
48         if (SchemaAwareApplyOperation.belongsToTree(type, schema)) {
49             for (final DataSchemaNode child : schema.getChildNodes()) {
50                 if (SchemaAwareApplyOperation.belongsToTree(type, child)) {
51                     childrenBuilder.put(NodeIdentifier.create(child.getQName()), child);
52                 }
53             }
54             for (final AugmentationSchema augment : schema.getAvailableAugmentations()) {
55                 if (augment.getChildNodes().stream()
56                         .anyMatch(child -> SchemaAwareApplyOperation.belongsToTree(type, child))) {
57                     augmentationsBuilder.put(SchemaUtils.getNodeIdentifierForAugmentation(augment), augment);
58                 }
59             }
60         }
61
62         final Map<NodeIdentifier, DataSchemaNode> children = childrenBuilder.build();
63         final Map<AugmentationIdentifier, AugmentationSchema> augmentations = augmentationsBuilder.build();
64         return children.isEmpty() ? null
65                 : new CaseEnforcer(children, augmentations, MandatoryLeafEnforcer.forContainer(schema, treeConfig));
66     }
67
68     Set<Entry<NodeIdentifier, DataSchemaNode>> getChildEntries() {
69         return children.entrySet();
70     }
71
72     Set<NodeIdentifier> getChildIdentifiers() {
73         return children.keySet();
74     }
75
76     Set<Entry<AugmentationIdentifier, AugmentationSchema>> getAugmentationEntries() {
77         return augmentations.entrySet();
78     }
79
80     Set<AugmentationIdentifier> getAugmentationIdentifiers() {
81         return augmentations.keySet();
82     }
83
84     Set<PathArgument> getAllChildIdentifiers() {
85         return Sets.union(children.keySet(), augmentations.keySet());
86     }
87
88     void enforceOnTreeNode(final NormalizedNode<?, ?> normalizedNode) {
89         enforcer.enforceOnData(normalizedNode);
90     }
91 }