Split out yang-data-tree-impl
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / 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.tree.impl;
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.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNode;
33 import org.opendaylight.yangtools.yang.data.spi.tree.Version;
34 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
35 import org.opendaylight.yangtools.yang.data.tree.impl.AbstractNodeContainerModificationStrategy.Visible;
36 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40
41 final class ChoiceModificationStrategy extends Visible<ChoiceSchemaNode> {
42     private static final NormalizedNodeContainerSupport<NodeIdentifier, ChoiceNode> SUPPORT =
43             new NormalizedNodeContainerSupport<>(ChoiceNode.class, ImmutableChoiceNodeBuilder::create,
44                     ImmutableChoiceNodeBuilder::create);
45
46     private final ImmutableMap<PathArgument, ModificationApplyOperation> childNodes;
47     // FIXME: enforce leaves not coming from two case statements at the same time
48     private final ImmutableMap<CaseEnforcer, Collection<CaseEnforcer>> exclusions;
49     private final ImmutableMap<PathArgument, CaseEnforcer> caseEnforcers;
50     private final @NonNull ChoiceNode emptyNode;
51
52     ChoiceModificationStrategy(final ChoiceSchemaNode schema, final DataTreeConfiguration treeConfig) {
53         super(SUPPORT, treeConfig, schema);
54
55         final Builder<PathArgument, ModificationApplyOperation> childBuilder = ImmutableMap.builder();
56         final Builder<PathArgument, CaseEnforcer> enforcerBuilder = ImmutableMap.builder();
57         for (final CaseSchemaNode caze : schema.getCases()) {
58             final CaseEnforcer enforcer = CaseEnforcer.forTree(caze, treeConfig);
59             if (enforcer != null) {
60                 for (final Entry<NodeIdentifier, DataSchemaNode> entry : enforcer.getChildEntries()) {
61                     final ModificationApplyOperation childOper;
62                     try {
63                         childOper = SchemaAwareApplyOperation.from(entry.getValue(), treeConfig);
64                     } catch (ExcludedDataSchemaNodeException e) {
65                         // This should never happen as enforcer performs filtering
66                         throw new IllegalStateException("Enforcer references out-of-tree child " + entry, e);
67                     }
68
69                     childBuilder.put(entry.getKey(), childOper);
70                     enforcerBuilder.put(entry.getKey(), enforcer);
71                 }
72                 for (final Entry<AugmentationIdentifier, AugmentationSchemaNode> e
73                         : enforcer.getAugmentationEntries()) {
74                     childBuilder.put(e.getKey(), new AugmentationModificationStrategy(e.getValue(), caze, treeConfig));
75                     enforcerBuilder.put(e.getKey(), enforcer);
76                 }
77             }
78         }
79         childNodes = childBuilder.build();
80         caseEnforcers = enforcerBuilder.build();
81
82         final Map<CaseEnforcer, Collection<CaseEnforcer>> exclusionsBuilder = new HashMap<>();
83         for (final CaseEnforcer e : caseEnforcers.values()) {
84             exclusionsBuilder.put(e, ImmutableList.copyOf(
85                 Collections2.filter(caseEnforcers.values(), Predicates.not(Predicates.equalTo(e)))));
86         }
87         exclusions = ImmutableMap.copyOf(exclusionsBuilder);
88         emptyNode = ImmutableNodes.choiceNode(schema.getQName());
89     }
90
91     @Override
92     Optional<? extends TreeNode> apply(final ModifiedNode modification, final Optional<? extends TreeNode> storeMeta,
93             final Version version) {
94         return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, storeMeta,
95             version);
96     }
97
98     @Override
99     TreeNode defaultTreeNode() {
100         return defaultTreeNode(emptyNode);
101     }
102
103     @Override
104     public ModificationApplyOperation childByArg(final PathArgument arg) {
105         return childNodes.get(arg);
106     }
107
108     @Override
109     void optionalVerifyValueChildren(final NormalizedNode writtenValue) {
110         enforceCases(writtenValue);
111     }
112
113     private void enforceCases(final TreeNode tree) {
114         enforceCases(tree.getData());
115     }
116
117     private void enforceCases(final NormalizedNode normalizedNode) {
118         Verify.verify(normalizedNode instanceof ChoiceNode);
119         final Collection<DataContainerChild> children = ((ChoiceNode) normalizedNode).body();
120         if (!children.isEmpty()) {
121             final DataContainerChild firstChild = children.iterator().next();
122             final CaseEnforcer enforcer = Verify.verifyNotNull(caseEnforcers.get(firstChild.getIdentifier()),
123                 "Case enforcer cannot be null. Most probably, child node %s of choice node %s does not belong "
124                 + "in current tree type.", firstChild.getIdentifier(), normalizedNode.getIdentifier());
125
126             // Make sure no leaves from other cases are present
127             for (final CaseEnforcer other : exclusions.get(enforcer)) {
128                 for (final PathArgument id : other.getAllChildIdentifiers()) {
129                     final Optional<NormalizedNode> maybeChild = NormalizedNodes.getDirectChild(normalizedNode,
130                         id);
131                     Preconditions.checkArgument(!maybeChild.isPresent(),
132                         "Child %s (from case %s) implies non-presence of child %s (from case %s), which is %s",
133                         firstChild.getIdentifier(), enforcer, id, other, maybeChild.orElse(null));
134                 }
135             }
136
137             // Make sure all mandatory children are present
138             enforcer.enforceOnTreeNode(normalizedNode);
139         }
140     }
141
142     @Override
143     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
144         final TreeNode ret = super.applyMerge(modification, currentMeta, version);
145         enforceCases(ret);
146         return ret;
147     }
148
149     @Override
150     protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue,
151             final Optional<? extends TreeNode> currentMeta, final Version version) {
152         final TreeNode ret = super.applyWrite(modification, newValue, currentMeta, version);
153         enforceCases(ret);
154         return ret;
155     }
156
157     @Override
158     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
159         final TreeNode ret = super.applyTouch(modification, currentMeta, version);
160         enforceCases(ret);
161         return ret;
162     }
163 }
164