Mass migrate fields to use Immutable collections
[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.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 ImmutableMap<NodeIdentifier, DataSchemaNode> children;
31     private final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations;
32     private final MandatoryLeafEnforcer enforcer;
33
34     private CaseEnforcer(final ImmutableMap<NodeIdentifier, DataSchemaNode> children,
35                          final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations,
36                          final MandatoryLeafEnforcer enforcer) {
37         this.children = requireNonNull(children);
38         this.augmentations = requireNonNull(augmentations);
39         this.enforcer = requireNonNull(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 ImmutableMap<NodeIdentifier, DataSchemaNode> children = childrenBuilder.build();
61         return children.isEmpty() ? null : new CaseEnforcer(children, augmentationsBuilder.build(),
62             MandatoryLeafEnforcer.forContainer(schema, treeConfig));
63     }
64
65     Set<Entry<NodeIdentifier, DataSchemaNode>> getChildEntries() {
66         return children.entrySet();
67     }
68
69     Set<NodeIdentifier> getChildIdentifiers() {
70         return children.keySet();
71     }
72
73     Set<Entry<AugmentationIdentifier, AugmentationSchemaNode>> getAugmentationEntries() {
74         return augmentations.entrySet();
75     }
76
77     Set<AugmentationIdentifier> getAugmentationIdentifiers() {
78         return augmentations.keySet();
79     }
80
81     Set<PathArgument> getAllChildIdentifiers() {
82         return Sets.union(children.keySet(), augmentations.keySet());
83     }
84
85     void enforceOnTreeNode(final NormalizedNode<?, ?> normalizedNode) {
86         enforcer.enforceOnData(normalizedNode);
87     }
88 }