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 java.util.Objects.requireNonNull;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.tree.api.IncorrectDataStructureException;
18 import org.opendaylight.yangtools.yang.data.tree.api.ModificationType;
19 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
20 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 final class ValueNodeModificationStrategy<T extends DataSchemaNode, V extends NormalizedNode>
24 extends SchemaAwareApplyOperation<T> {
25 private final @NonNull Class<V> nodeClass;
26 private final @NonNull T schema;
28 ValueNodeModificationStrategy(final Class<V> nodeClass, final T schema) {
29 this.nodeClass = requireNonNull(nodeClass);
30 this.schema = requireNonNull(schema);
39 public ModificationApplyOperation childByArg(final PathArgument arg) {
40 throw new UnsupportedOperationException("Node " + schema + " is leaf type node. Child nodes not allowed");
44 protected ChildTrackingPolicy getChildPolicy() {
45 return ChildTrackingPolicy.NONE;
49 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta,
50 final Version version) {
51 throw new UnsupportedOperationException("Node " + schema + " is leaf type node. "
52 + " Subtree change is not allowed.");
56 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
57 final Version version) {
58 // Just overwrite whatever was there, but be sure to run validation
59 final NormalizedNode newValue = modification.getWrittenValue();
60 verifyWrittenValue(newValue);
61 modification.resolveModificationType(ModificationType.WRITE);
62 return applyWrite(modification, newValue, null, version);
66 protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue,
67 final TreeNode currentMeta, final Version version) {
68 return TreeNode.of(newValue, version);
72 protected void checkTouchApplicable(final ModificationPath path, final NodeModification modification,
73 final TreeNode currentMeta, final Version version) throws IncorrectDataStructureException {
74 throw new IncorrectDataStructureException(path.toInstanceIdentifier(), "Subtree modification is not allowed.");
78 void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode value, final Version version) {
79 switch (node.getOperation()) {
80 // Delete performs a data dependency check on existence of the node. Performing a merge on DELETE means we
81 // are really performing a write.
82 case DELETE, WRITE -> node.write(value);
83 default -> node.updateValue(LogicalOperation.MERGE, value);
88 void verifyValue(final NormalizedNode writtenValue) {
89 verifyWrittenValue(writtenValue);
93 void recursivelyVerifyStructure(final NormalizedNode value) {
94 verifyWrittenValue(value);
98 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
99 return helper.add("value", nodeClass.getSimpleName());
102 private void verifyWrittenValue(final NormalizedNode value) {
103 checkArgument(nodeClass.isInstance(value), "Expected an instance of %s, have %s", nodeClass, value);