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 org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
26 import org.opendaylight.yangtools.yang.data.tree.impl.AbstractNodeContainerModificationStrategy.Visible;
27 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
28 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
31 final class ChoiceModificationStrategy extends Visible<ChoiceSchemaNode> {
32 private static final NormalizedNodeContainerSupport<NodeIdentifier, ChoiceNode> SUPPORT =
33 new NormalizedNodeContainerSupport<>(ChoiceNode.class, ImmutableChoiceNodeBuilder::create,
34 ImmutableChoiceNodeBuilder::new);
36 private final ImmutableMap<PathArgument, ModificationApplyOperation> childNodes;
37 // FIXME: enforce leaves not coming from two case statements at the same time
38 private final ImmutableMap<CaseEnforcer, ImmutableList<CaseEnforcer>> exclusions;
39 private final ImmutableMap<PathArgument, CaseEnforcer> caseEnforcers;
40 private final @NonNull ChoiceNode emptyNode;
42 ChoiceModificationStrategy(final ChoiceSchemaNode schema, final DataTreeConfiguration treeConfig) {
43 super(SUPPORT, treeConfig, schema);
45 final var childBuilder = ImmutableMap.<PathArgument, ModificationApplyOperation>builder();
46 final var enforcerBuilder = ImmutableMap.<PathArgument, CaseEnforcer>builder();
47 for (var caze : schema.getCases()) {
48 final var enforcer = CaseEnforcer.forTree(caze, treeConfig);
49 if (enforcer != null) {
50 for (var entry : enforcer.getChildEntries()) {
51 final ModificationApplyOperation childOper;
53 childOper = SchemaAwareApplyOperation.from(entry.getValue(), treeConfig);
54 } catch (ExcludedDataSchemaNodeException e) {
55 // This should never happen as enforcer performs filtering
56 throw new IllegalStateException("Enforcer references out-of-tree child " + entry, e);
59 childBuilder.put(entry.getKey(), childOper);
60 enforcerBuilder.put(entry.getKey(), enforcer);
64 childNodes = childBuilder.build();
65 caseEnforcers = enforcerBuilder.build();
67 final var exclusionsBuilder = new HashMap<CaseEnforcer, ImmutableList<CaseEnforcer>>();
68 for (var key : caseEnforcers.values()) {
69 exclusionsBuilder.put(key, caseEnforcers.values().stream()
70 .filter(enforcer -> !key.equals(enforcer))
71 .collect(ImmutableList.toImmutableList()));
73 exclusions = ImmutableMap.copyOf(exclusionsBuilder);
74 emptyNode = ImmutableNodes.choiceNode(schema.getQName());
78 TreeNode apply(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
79 return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, currentMeta,
84 TreeNode defaultTreeNode() {
85 return defaultTreeNode(emptyNode);
89 public ModificationApplyOperation childByArg(final PathArgument arg) {
90 return childNodes.get(arg);
94 void optionalVerifyValueChildren(final DistinctNodeContainer<?, ?> writtenValue) {
95 enforceCases(writtenValue);
98 private void enforceCases(final TreeNode tree) {
99 enforceCases(tree.getData());
102 private void enforceCases(final NormalizedNode normalizedNode) {
103 verify(normalizedNode instanceof ChoiceNode);
104 final var choice = (ChoiceNode) normalizedNode;
105 if (!choice.isEmpty()) {
106 final var firstChild = choice.body().iterator().next();
107 final var enforcer = verifyNotNull(caseEnforcers.get(firstChild.name()),
108 "Case enforcer cannot be null. Most probably, child node %s of choice node %s does not belong "
109 + "in current tree type.", firstChild.name(), normalizedNode.name());
111 // Make sure no leaves from other cases are present
112 for (var other : verifyNotNull(exclusions.get(enforcer))) {
113 for (var id : other.getChildIdentifiers()) {
114 final var child = choice.childByArg(id);
115 checkArgument(child == null,
116 "Child %s (from case %s) implies non-presence of child %s (from case %s), which is %s",
117 firstChild.name(), enforcer, id, other, child);
121 // Make sure all mandatory children are present
122 enforcer.enforceOnChoice(choice);
127 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
128 final var ret = super.applyMerge(modification, currentMeta, version);
134 protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue,
135 final TreeNode currentMeta, final Version version) {
136 final var ret = super.applyWrite(modification, newValue, currentMeta, version);
142 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
143 final var ret = super.applyTouch(modification, currentMeta, version);