c12b67745204d77e4540183193e9d96be8ace755
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableMap.Builder;
14 import com.google.common.collect.Sets;
15 import java.util.Map.Entry;
16 import java.util.Optional;
17 import java.util.Set;
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
25 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29
30 class CaseEnforcer implements Immutable {
31     private static final class EnforcingMandatory extends CaseEnforcer {
32         private final MandatoryLeafEnforcer enforcer;
33
34         EnforcingMandatory(final ImmutableMap<NodeIdentifier, DataSchemaNode> children,
35                 final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations,
36                 final MandatoryLeafEnforcer enforcer) {
37             super(children, augmentations);
38             this.enforcer = requireNonNull(enforcer);
39         }
40
41         @Override
42         void enforceOnTreeNode(final NormalizedNode<?, ?> normalizedNode) {
43             enforcer.enforceOnData(normalizedNode);
44         }
45     }
46
47     private final ImmutableMap<NodeIdentifier, DataSchemaNode> children;
48     private final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations;
49
50     CaseEnforcer(final ImmutableMap<NodeIdentifier, DataSchemaNode> children,
51             final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations) {
52         this.children = requireNonNull(children);
53         this.augmentations = requireNonNull(augmentations);
54     }
55
56     static CaseEnforcer forTree(final CaseSchemaNode schema, final DataTreeConfiguration treeConfig) {
57         final TreeType type = treeConfig.getTreeType();
58         final Builder<NodeIdentifier, DataSchemaNode> childrenBuilder = ImmutableMap.builder();
59         final Builder<AugmentationIdentifier, AugmentationSchemaNode> augmentationsBuilder = ImmutableMap.builder();
60         if (SchemaAwareApplyOperation.belongsToTree(type, schema)) {
61             for (final DataSchemaNode child : schema.getChildNodes()) {
62                 if (SchemaAwareApplyOperation.belongsToTree(type, child)) {
63                     childrenBuilder.put(NodeIdentifier.create(child.getQName()), child);
64                 }
65             }
66             for (final AugmentationSchemaNode augment : schema.getAvailableAugmentations()) {
67                 if (augment.getChildNodes().stream()
68                         .anyMatch(child -> SchemaAwareApplyOperation.belongsToTree(type, child))) {
69                     augmentationsBuilder.put(DataSchemaContextNode.augmentationIdentifierFrom(augment), augment);
70                 }
71             }
72         }
73
74         final ImmutableMap<NodeIdentifier, DataSchemaNode> children = childrenBuilder.build();
75         if (children.isEmpty()) {
76             return null;
77         }
78         final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations = augmentationsBuilder.build();
79         final Optional<MandatoryLeafEnforcer> enforcer = MandatoryLeafEnforcer.forContainer(schema, treeConfig);
80         return enforcer.isPresent() ? new EnforcingMandatory(children, augmentations, enforcer.get())
81                 : new CaseEnforcer(children, augmentations);
82     }
83
84     final Set<Entry<NodeIdentifier, DataSchemaNode>> getChildEntries() {
85         return children.entrySet();
86     }
87
88     final Set<NodeIdentifier> getChildIdentifiers() {
89         return children.keySet();
90     }
91
92     final Set<Entry<AugmentationIdentifier, AugmentationSchemaNode>> getAugmentationEntries() {
93         return augmentations.entrySet();
94     }
95
96     final Set<AugmentationIdentifier> getAugmentationIdentifiers() {
97         return augmentations.keySet();
98     }
99
100     final Set<PathArgument> getAllChildIdentifiers() {
101         return Sets.union(children.keySet(), augmentations.keySet());
102     }
103
104     void enforceOnTreeNode(final NormalizedNode<?, ?> normalizedNode) {
105         // Default is no-op
106     }
107 }