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