Unify ModificationApplyOperation.toString()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ValueNodeModificationStrategy.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24
25 final class ValueNodeModificationStrategy<T extends DataSchemaNode, V extends NormalizedNode<?, ?>>
26         extends SchemaAwareApplyOperation<T> {
27     private final @NonNull Class<V> nodeClass;
28     private final @NonNull T schema;
29
30     ValueNodeModificationStrategy(final Class<V> nodeClass, final T schema) {
31         this.nodeClass = requireNonNull(nodeClass);
32         this.schema = requireNonNull(schema);
33     }
34
35     @Override
36     T getSchema() {
37         return schema;
38     }
39
40     @Override
41     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
42         throw new UnsupportedOperationException("Node " + schema + " is leaf type node. Child nodes not allowed");
43     }
44
45     @Override
46     protected ChildTrackingPolicy getChildPolicy() {
47         return ChildTrackingPolicy.NONE;
48     }
49
50     @Override
51     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta,
52             final Version version) {
53         throw new UnsupportedOperationException("Node " + schema + " is leaf type node. "
54             + " Subtree change is not allowed.");
55     }
56
57     @Override
58     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
59             final Version version) {
60         // Just overwrite whatever was there, but be sure to run validation
61         final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
62         verifyWrittenValue(newValue);
63         modification.resolveModificationType(ModificationType.WRITE);
64         return applyWrite(modification, newValue, null, version);
65     }
66
67     @Override
68     protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode<?, ?> newValue,
69             final Optional<? extends TreeNode> currentMeta, final Version version) {
70         return TreeNodeFactory.createTreeNode(newValue, version);
71     }
72
73     @Override
74     protected void checkTouchApplicable(final ModificationPath path, final NodeModification modification,
75             final Optional<? extends TreeNode> current, final Version version) throws IncorrectDataStructureException {
76         throw new IncorrectDataStructureException(path.toInstanceIdentifier(), "Subtree modification is not allowed.");
77     }
78
79     @Override
80     void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
81         switch (node.getOperation()) {
82             // Delete performs a data dependency check on existence of the node. Performing a merge
83             // on DELETE means we
84             // are really performing a write.
85             case DELETE:
86             case WRITE:
87                 node.write(value);
88                 break;
89             default:
90                 node.updateValue(LogicalOperation.MERGE, value);
91         }
92     }
93
94     @Override
95     void verifyValue(final NormalizedNode<?, ?> writtenValue) {
96         verifyWrittenValue(writtenValue);
97     }
98
99     @Override
100     void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
101         verifyWrittenValue(value);
102     }
103
104     @Override
105     ToStringHelper addToStringAttributes(final ToStringHelper helper) {
106         return helper.add("value", nodeClass.getSimpleName());
107     }
108
109     private void verifyWrittenValue(final NormalizedNode<?, ?> value) {
110         checkArgument(nodeClass.isInstance(value), "Expected an instance of %s, have %s", nodeClass, value);
111     }
112 }