Rename ValueNodeModificationStrategy
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractValueNodeModificationStrategy.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 com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22
23 abstract class AbstractValueNodeModificationStrategy<T extends DataSchemaNode> extends SchemaAwareApplyOperation {
24     private final Class<? extends NormalizedNode<?, ?>> nodeClass;
25     private final T schema;
26
27     protected AbstractValueNodeModificationStrategy(final T schema, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
28         this.nodeClass = Preconditions.checkNotNull(nodeClass);
29         this.schema = schema;
30     }
31
32     @Override
33     protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
34         checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
35     }
36
37     @Override
38     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
39         throw new UnsupportedOperationException("Node " + schema.getPath()
40                 + "is leaf type node. Child nodes not allowed");
41     }
42
43     @Override
44     protected TreeNode applySubtreeChange(final ModifiedNode modification,
45             final TreeNode currentMeta, final Version version) {
46         throw new UnsupportedOperationException("Node " + schema.getPath()
47                 + "is leaf type node. Subtree change is not allowed.");
48     }
49
50     @Override
51     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
52             final Version version) {
53         // Just overwrite whatever was there
54         modification.resolveModificationType(ModificationType.WRITE);
55         return applyWrite(modification, null, version);
56     }
57
58     @Override
59     protected TreeNode applyWrite(final ModifiedNode modification,
60             final Optional<TreeNode> currentMeta, final Version version) {
61         return TreeNodeFactory.createTreeNodeRecursively(modification.getWrittenValue(), version);
62     }
63
64     @Override
65     protected void checkSubtreeModificationApplicable(final YangInstanceIdentifier path, final NodeModification modification,
66             final Optional<TreeNode> current) throws IncorrectDataStructureException {
67         throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
68     }
69 }