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