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