2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
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;
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;
40 final class ChoiceModificationStrategy extends AbstractNodeContainerModificationStrategy {
41 private static final NormalizedNodeContainerSupport<NodeIdentifier, ChoiceNode> SUPPORT =
42 new NormalizedNodeContainerSupport<>(ChoiceNode.class, ImmutableChoiceNodeBuilder::create,
43 ImmutableChoiceNodeBuilder::create);
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;
51 ChoiceModificationStrategy(final ChoiceSchemaNode schema, final DataTreeConfiguration treeConfig) {
52 super(SUPPORT, treeConfig);
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);
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);
70 childNodes = childBuilder.build();
71 caseEnforcers = enforcerBuilder.build();
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)))));
78 exclusions = ImmutableMap.copyOf(exclusionsBuilder);
79 emptyNode = ImmutableNodes.choiceNode(schema.getQName());
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,
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,
97 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
98 return Optional.ofNullable(childNodes.get(child));
102 void additionalVerifyValueChildren(final NormalizedNode<?, ?> writtenValue) {
103 enforceCases(writtenValue);
106 private void enforceCases(final TreeNode tree) {
107 enforceCases(tree.getData());
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());
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,
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));
130 // Make sure all mandatory children are present
131 enforcer.enforceOnTreeNode(normalizedNode);
136 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
137 final TreeNode ret = super.applyMerge(modification, currentMeta, version);
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);
151 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
152 final TreeNode ret = super.applyTouch(modification, currentMeta, version);