Add utility wrappers for instantiating builders/nodes
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ChoiceModificationStrategy.java
1 /*
2  * Copyright (c) 2014 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.base.Predicates;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableMap.Builder;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Map.Entry;
21 import java.util.Optional;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.tree.NormalizedNodeContainerSupport.Single;
34 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38
39 final class ChoiceModificationStrategy extends AbstractNodeContainerModificationStrategy {
40     private static final Single<NodeIdentifier, ChoiceNode> SUPPORT = new Single<>(ChoiceNode.class,
41             ImmutableChoiceNodeBuilder::create, ImmutableChoiceNodeBuilder::create);
42
43     private final ImmutableMap<PathArgument, ModificationApplyOperation> childNodes;
44     // FIXME: enforce leaves not coming from two case statements at the same time
45     private final ImmutableMap<CaseEnforcer, Collection<CaseEnforcer>> exclusions;
46     private final ImmutableMap<PathArgument, CaseEnforcer> caseEnforcers;
47
48     ChoiceModificationStrategy(final ChoiceSchemaNode schemaNode, final DataTreeConfiguration treeConfig) {
49         super(SUPPORT, treeConfig);
50
51         final Builder<PathArgument, ModificationApplyOperation> childBuilder = ImmutableMap.builder();
52         final Builder<PathArgument, CaseEnforcer> enforcerBuilder = ImmutableMap.builder();
53         for (final CaseSchemaNode caze : schemaNode.getCases().values()) {
54             final CaseEnforcer enforcer = CaseEnforcer.forTree(caze, treeConfig);
55             if (enforcer != null) {
56                 for (final Entry<NodeIdentifier, DataSchemaNode> e : enforcer.getChildEntries()) {
57                     childBuilder.put(e.getKey(), SchemaAwareApplyOperation.from(e.getValue(), treeConfig));
58                     enforcerBuilder.put(e.getKey(), enforcer);
59                 }
60                 for (final Entry<AugmentationIdentifier, AugmentationSchemaNode> e
61                         : enforcer.getAugmentationEntries()) {
62                     childBuilder.put(e.getKey(), SchemaAwareApplyOperation.from(e.getValue(), caze, treeConfig));
63                     enforcerBuilder.put(e.getKey(), enforcer);
64                 }
65             }
66         }
67         childNodes = childBuilder.build();
68         caseEnforcers = enforcerBuilder.build();
69
70         final Map<CaseEnforcer, Collection<CaseEnforcer>> exclusionsBuilder = new HashMap<>();
71         for (final CaseEnforcer e : caseEnforcers.values()) {
72             exclusionsBuilder.put(e, ImmutableList.copyOf(
73                 Collections2.filter(caseEnforcers.values(), Predicates.not(Predicates.equalTo(e)))));
74         }
75         exclusions = ImmutableMap.copyOf(exclusionsBuilder);
76     }
77
78     @Override
79     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
80         return Optional.ofNullable(childNodes.get(child));
81     }
82
83     @Override
84     void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
85         if (verifyChildrenStructure() && verifyChildren) {
86             enforceCases(writtenValue);
87         }
88         super.verifyStructure(writtenValue, verifyChildren);
89     }
90
91     private void enforceCases(final TreeNode tree) {
92         enforceCases(tree.getData());
93     }
94
95     private void enforceCases(final NormalizedNode<?, ?> normalizedNode) {
96         Verify.verify(normalizedNode instanceof ChoiceNode);
97         final Collection<DataContainerChild<?, ?>> children = ((ChoiceNode) normalizedNode).getValue();
98         if (!children.isEmpty()) {
99             final DataContainerChild<?, ?> firstChild = children.iterator().next();
100             final CaseEnforcer enforcer = Verify.verifyNotNull(caseEnforcers.get(firstChild.getIdentifier()),
101                 "Case enforcer cannot be null. Most probably, child node %s of choice node %s does not belong "
102                 + "in current tree type.", firstChild.getIdentifier(), normalizedNode.getIdentifier());
103
104             // Make sure no leaves from other cases are present
105             for (final CaseEnforcer other : exclusions.get(enforcer)) {
106                 for (final PathArgument id : other.getAllChildIdentifiers()) {
107                     final Optional<NormalizedNode<?, ?>> maybeChild = NormalizedNodes.getDirectChild(normalizedNode,
108                         id);
109                     Preconditions.checkArgument(!maybeChild.isPresent(),
110                         "Child %s (from case %s) implies non-presence of child %s (from case %s), which is %s",
111                         firstChild.getIdentifier(), enforcer, id, other, maybeChild.orElse(null));
112                 }
113             }
114
115             // Make sure all mandatory children are present
116             enforcer.enforceOnTreeNode(normalizedNode);
117         }
118     }
119
120     @Override
121     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
122         final TreeNode ret = super.applyMerge(modification, currentMeta, version);
123         enforceCases(ret);
124         return ret;
125     }
126
127     @Override
128     protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode<?, ?> newValue,
129             final Optional<TreeNode> currentMeta, final Version version) {
130         final TreeNode ret = super.applyWrite(modification, newValue, currentMeta, version);
131         enforceCases(ret);
132         return ret;
133     }
134
135     @Override
136     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
137         final TreeNode ret = super.applyTouch(modification, currentMeta, version);
138         enforceCases(ret);
139         return ret;
140     }
141 }
142