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.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.api.schema.tree.DataTreeConfiguration;
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.data.impl.schema.tree.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;
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);
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;
52 ChoiceModificationStrategy(final ChoiceSchemaNode schema, final DataTreeConfiguration treeConfig) {
53 super(SUPPORT, treeConfig, schema);
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;
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);
69 childBuilder.put(entry.getKey(), childOper);
70 enforcerBuilder.put(entry.getKey(), enforcer);
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);
79 childNodes = childBuilder.build();
80 caseEnforcers = enforcerBuilder.build();
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)))));
87 exclusions = ImmutableMap.copyOf(exclusionsBuilder);
88 emptyNode = ImmutableNodes.choiceNode(schema.getQName());
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,
99 TreeNode defaultTreeNode() {
100 return defaultTreeNode(emptyNode);
104 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
105 return Optional.ofNullable(childNodes.get(child));
109 void optionalVerifyValueChildren(final NormalizedNode<?, ?> writtenValue) {
110 enforceCases(writtenValue);
113 private void enforceCases(final TreeNode tree) {
114 enforceCases(tree.getData());
117 private void enforceCases(final NormalizedNode<?, ?> normalizedNode) {
118 Verify.verify(normalizedNode instanceof ChoiceNode);
119 final Collection<DataContainerChild<?, ?>> children = ((ChoiceNode) normalizedNode).getValue();
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());
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,
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));
137 // Make sure all mandatory children are present
138 enforcer.enforceOnTreeNode(normalizedNode);
143 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
144 final TreeNode ret = super.applyMerge(modification, currentMeta, version);
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);
158 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
159 final TreeNode ret = super.applyTouch(modification, currentMeta, version);