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.tree.impl;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12 import static com.google.common.base.Verify.verifyNotNull;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import java.util.HashMap;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
27 import org.opendaylight.yangtools.yang.data.tree.impl.AbstractNodeContainerModificationStrategy.Visible;
28 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
29 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
32 final class ChoiceModificationStrategy extends Visible<ChoiceSchemaNode> {
33 private static final NormalizedNodeContainerSupport<NodeIdentifier, ChoiceNode> SUPPORT =
34 new NormalizedNodeContainerSupport<>(ChoiceNode.class, ImmutableChoiceNodeBuilder::create,
35 ImmutableChoiceNodeBuilder::create);
37 private final ImmutableMap<PathArgument, ModificationApplyOperation> childNodes;
38 // FIXME: enforce leaves not coming from two case statements at the same time
39 private final ImmutableMap<CaseEnforcer, ImmutableList<CaseEnforcer>> exclusions;
40 private final ImmutableMap<PathArgument, CaseEnforcer> caseEnforcers;
41 private final @NonNull ChoiceNode emptyNode;
43 ChoiceModificationStrategy(final ChoiceSchemaNode schema, final DataTreeConfiguration treeConfig) {
44 super(SUPPORT, treeConfig, schema);
46 final var childBuilder = ImmutableMap.<PathArgument, ModificationApplyOperation>builder();
47 final var enforcerBuilder = ImmutableMap.<PathArgument, CaseEnforcer>builder();
48 for (var caze : schema.getCases()) {
49 final var enforcer = CaseEnforcer.forTree(caze, treeConfig);
50 if (enforcer != null) {
51 for (var entry : enforcer.getChildEntries()) {
52 final ModificationApplyOperation childOper;
54 childOper = SchemaAwareApplyOperation.from(entry.getValue(), treeConfig);
55 } catch (ExcludedDataSchemaNodeException e) {
56 // This should never happen as enforcer performs filtering
57 throw new IllegalStateException("Enforcer references out-of-tree child " + entry, e);
60 childBuilder.put(entry.getKey(), childOper);
61 enforcerBuilder.put(entry.getKey(), enforcer);
65 childNodes = childBuilder.build();
66 caseEnforcers = enforcerBuilder.build();
68 final var exclusionsBuilder = new HashMap<CaseEnforcer, ImmutableList<CaseEnforcer>>();
69 for (var key : caseEnforcers.values()) {
70 exclusionsBuilder.put(key, caseEnforcers.values().stream()
71 .filter(enforcer -> !key.equals(enforcer))
72 .collect(ImmutableList.toImmutableList()));
74 exclusions = ImmutableMap.copyOf(exclusionsBuilder);
75 emptyNode = ImmutableNodes.choiceNode(schema.getQName());
79 Optional<? extends TreeNode> apply(final ModifiedNode modification, final Optional<? extends TreeNode> storeMeta,
80 final Version version) {
81 return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, storeMeta,
86 TreeNode defaultTreeNode() {
87 return defaultTreeNode(emptyNode);
91 public ModificationApplyOperation childByArg(final PathArgument arg) {
92 return childNodes.get(arg);
96 void optionalVerifyValueChildren(final DistinctNodeContainer<?, ?> writtenValue) {
97 enforceCases(writtenValue);
100 private void enforceCases(final TreeNode tree) {
101 enforceCases(tree.getData());
104 private void enforceCases(final NormalizedNode normalizedNode) {
105 verify(normalizedNode instanceof ChoiceNode);
106 final var choice = (ChoiceNode) normalizedNode;
107 if (!choice.isEmpty()) {
108 final var firstChild = choice.body().iterator().next();
109 final var enforcer = verifyNotNull(caseEnforcers.get(firstChild.name()),
110 "Case enforcer cannot be null. Most probably, child node %s of choice node %s does not belong "
111 + "in current tree type.", firstChild.name(), normalizedNode.name());
113 // Make sure no leaves from other cases are present
114 for (var other : verifyNotNull(exclusions.get(enforcer))) {
115 for (var id : other.getChildIdentifiers()) {
116 final var child = choice.childByArg(id);
117 checkArgument(child == null,
118 "Child %s (from case %s) implies non-presence of child %s (from case %s), which is %s",
119 firstChild.name(), enforcer, id, other, child);
123 // Make sure all mandatory children are present
124 enforcer.enforceOnChoice(choice);
129 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
130 final TreeNode ret = super.applyMerge(modification, currentMeta, version);
136 protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue,
137 final Optional<? extends TreeNode> currentMeta, final Version version) {
138 final TreeNode ret = super.applyWrite(modification, newValue, currentMeta, version);
144 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
145 final TreeNode ret = super.applyTouch(modification, currentMeta, version);