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