900fa320a167838a8d04155becb2096385dfaaed
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / data / 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.controller.md.sal.dom.store.impl.tree.data;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import org.opendaylight.controller.md.sal.dom.store.impl.tree.IncorrectDataStructureException;
13 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNode;
14 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNodeFactory;
15 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.Version;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24
25 import com.google.common.base.Optional;
26
27 abstract class ValueNodeModificationStrategy<T extends DataSchemaNode> extends SchemaAwareApplyOperation {
28
29     private final T schema;
30     private final Class<? extends NormalizedNode<?, ?>> nodeClass;
31
32     protected ValueNodeModificationStrategy(final T schema, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
33         super();
34         this.schema = schema;
35         this.nodeClass = nodeClass;
36     }
37
38     @Override
39     protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
40         checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
41     }
42
43     @Override
44     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
45         throw new UnsupportedOperationException("Node " + schema.getPath()
46                 + "is leaf type node. Child nodes not allowed");
47     }
48
49     @Override
50     protected TreeNode applySubtreeChange(final ModifiedNode modification,
51             final TreeNode currentMeta, final Version version) {
52         throw new UnsupportedOperationException("Node " + schema.getPath()
53                 + "is leaf type node. Subtree change is not allowed.");
54     }
55
56     @Override
57     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
58             final Version version) {
59         // Just overwrite whatever was there
60         return applyWrite(modification, null, version);
61     }
62
63     @Override
64     protected TreeNode applyWrite(final ModifiedNode modification,
65             final Optional<TreeNode> currentMeta, final Version version) {
66         return TreeNodeFactory.createTreeNode(modification.getWrittenValue(), version);
67     }
68
69     @Override
70     protected void checkSubtreeModificationApplicable(final InstanceIdentifier path, final NodeModification modification,
71             final Optional<TreeNode> current) throws IncorrectDataStructureException {
72         throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
73     }
74
75     public static class LeafSetEntryModificationStrategy extends ValueNodeModificationStrategy<LeafListSchemaNode> {
76         @SuppressWarnings({ "unchecked", "rawtypes" })
77         protected LeafSetEntryModificationStrategy(final LeafListSchemaNode schema) {
78             super(schema, (Class) LeafSetEntryNode.class);
79         }
80     }
81
82     public static class LeafModificationStrategy extends ValueNodeModificationStrategy<LeafSchemaNode> {
83         @SuppressWarnings({ "unchecked", "rawtypes" })
84         protected LeafModificationStrategy(final LeafSchemaNode schema) {
85             super(schema, (Class) LeafNode.class);
86         }
87     }
88 }