Bug 499: Initial draft of in-memory datastore and data broker
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / test / java / org / opendaylight / controller / md / sal / dom / store / impl / ModificationMetadataTreeTest.java
1 package org.opendaylight.controller.md.sal.dom.store.impl;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertSame;
7 import static org.junit.Assert.assertTrue;
8 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.ID_QNAME;
9 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.INNER_LIST_QNAME;
10 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.NAME_QNAME;
11 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.OUTER_LIST_PATH;
12 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.OUTER_LIST_QNAME;
13 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.TEST_PATH;
14 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.TEST_QNAME;
15 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.VALUE_QNAME;
16 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
17 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
18 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
19
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
23 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32
33 import com.google.common.base.Optional;
34 import com.google.common.primitives.UnsignedLong;
35
36 /**
37  *
38  * Schema structure of document is
39  *
40  * <pre>
41  * container root { 
42  *      list list-a {
43  *              key leaf-a;
44  *              leaf leaf-a;
45  *              choice choice-a {
46  *                      case one {
47  *                              leaf one;
48  *                      }
49  *                      case two-three {
50  *                              leaf two;
51  *                              leaf three;
52  *                      }
53  *              }
54  *              list list-b {
55  *                      key leaf-b;
56  *                      leaf leaf-b;
57  *              }
58  *      }
59  * }
60  * </pre>
61  *
62  */
63 public class ModificationMetadataTreeTest {
64
65     private static final Short ONE_ID = 1;
66     private static final Short TWO_ID = 2;
67     private static final String TWO_ONE_NAME = "one";
68     private static final String TWO_TWO_NAME = "two";
69
70     private static final InstanceIdentifier OUTER_LIST_1_PATH = InstanceIdentifier.builder(OUTER_LIST_PATH)
71             .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, ONE_ID) //
72             .build();
73
74     private static final InstanceIdentifier OUTER_LIST_2_PATH = InstanceIdentifier.builder(OUTER_LIST_PATH)
75             .nodeWithKey(OUTER_LIST_QNAME, ID_QNAME, TWO_ID) //
76             .build();
77
78     private static final InstanceIdentifier TWO_TWO_PATH = InstanceIdentifier.builder(OUTER_LIST_2_PATH)
79             .node(INNER_LIST_QNAME) //
80             .nodeWithKey(INNER_LIST_QNAME, NAME_QNAME, TWO_TWO_NAME) //
81             .build();
82
83     private static final InstanceIdentifier TWO_TWO_VALUE_PATH = InstanceIdentifier.builder(TWO_TWO_PATH)
84             .node(VALUE_QNAME) //
85             .build();
86
87     private static final MapEntryNode BAR_NODE = mapEntryBuilder(OUTER_LIST_QNAME, ID_QNAME, TWO_ID) //
88             .withChild(mapNodeBuilder(INNER_LIST_QNAME) //
89                     .withChild(mapEntry(INNER_LIST_QNAME, NAME_QNAME, TWO_ONE_NAME)) //
90                     .withChild(mapEntry(INNER_LIST_QNAME, NAME_QNAME, TWO_TWO_NAME)) //
91                     .build()) //
92             .build();
93
94     private SchemaContext schemaContext;
95
96     @Before
97     public void prepare() {
98         schemaContext = TestModel.createTestContext();
99         assertNotNull("Schema context must not be null.", schemaContext);
100     }
101
102     /**
103      * Returns a test document
104      *
105      * <pre>
106      * test
107      *     outer-list
108      *          id 1
109      *     outer-list
110      *          id 2
111      *          inner-list
112      *                  name "one"
113      *          inner-list
114      *                  name "two"
115      *
116      * </pre>
117      *
118      * @return
119      */
120     public NormalizedNode<?, ?> createDocumentOne() {
121         return ImmutableContainerNodeBuilder
122                 .create()
123                 .withNodeIdentifier(new NodeIdentifier(schemaContext.getQName()))
124                 .withChild(createTestContainer()).build();
125
126     }
127
128     private ContainerNode createTestContainer() {
129         return ImmutableContainerNodeBuilder
130                 .create()
131                 .withNodeIdentifier(new NodeIdentifier(TEST_QNAME))
132                 .withChild(
133                         mapNodeBuilder(OUTER_LIST_QNAME)
134                                 .withChild(mapEntry(OUTER_LIST_QNAME, ID_QNAME, ONE_ID))
135                                 .withChild(BAR_NODE).build()).build();
136     }
137
138     @Test
139     public void basicReadWrites() {
140         MutableDataTree modificationTree = MutableDataTree.from(
141                 DataAndMetadataSnapshot.builder() //
142                         .setMetadataTree(StoreMetadataNode.createRecursivelly(createDocumentOne(), UnsignedLong.valueOf(5))) //
143                         .setSchemaContext(schemaContext) //
144                         .build(), new SchemaAwareApplyOperationRoot(schemaContext));
145         Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.read(OUTER_LIST_2_PATH);
146         assertTrue(originalBarNode.isPresent());
147         assertSame(BAR_NODE, originalBarNode.get());
148
149         // writes node to /outer-list/1/inner_list/two/value
150         modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(VALUE_QNAME, "test"));
151
152         // reads node to /outer-list/1/inner_list/two/value
153         // and checks if node is already present
154         Optional<NormalizedNode<?, ?>> barTwoCModified = modificationTree.read(TWO_TWO_VALUE_PATH);
155         assertTrue(barTwoCModified.isPresent());
156         assertEquals(ImmutableNodes.leafNode(VALUE_QNAME, "test"), barTwoCModified.get());
157
158         // delete node to /outer-list/1/inner_list/two/value
159         modificationTree.delete(TWO_TWO_VALUE_PATH);
160         Optional<NormalizedNode<?, ?>> barTwoCAfterDelete = modificationTree.read(TWO_TWO_VALUE_PATH);
161         assertFalse(barTwoCAfterDelete.isPresent());
162     }
163
164
165     public MutableDataTree createEmptyModificationTree() {
166         /**
167          * Creates empty Snapshot with associated schema context.
168          */
169         DataAndMetadataSnapshot emptySnapshot = DataAndMetadataSnapshot.createEmpty(schemaContext);
170
171         /**
172          *
173          * Creates Mutable Data Tree based on provided snapshot and schema
174          * context.
175          *
176          */
177         MutableDataTree modificationTree = MutableDataTree.from(emptySnapshot, new SchemaAwareApplyOperationRoot(
178                 schemaContext));
179         return modificationTree;
180     }
181
182     @Test
183     public void createFromEmptyState() {
184
185         MutableDataTree modificationTree = createEmptyModificationTree();
186         /**
187          * Writes empty container node to /test
188          *
189          */
190         modificationTree.write(TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
191
192         /**
193          * Writes empty list node to /test/outer-list
194          */
195         modificationTree.write(OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(OUTER_LIST_QNAME).build());
196
197         /**
198          * Reads list node from /test/outer-list
199          */
200         Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.read(OUTER_LIST_PATH);
201         assertTrue(potentialOuterList.isPresent());
202
203         /**
204          * Reads container node from /test and verifies that it contains test
205          * node
206          */
207         Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.read(TEST_PATH);
208         ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
209
210         /**
211          *
212          * Gets list from returned snapshot of /test and verifies it contains
213          * outer-list
214          *
215          */
216         assertPresentAndType(containerTest.getChild(new NodeIdentifier(OUTER_LIST_QNAME)), MapNode.class);
217
218     }
219
220     @Test
221     public void writeSubtreeReadChildren() {
222         MutableDataTree modificationTree = createEmptyModificationTree();
223         modificationTree.write(TEST_PATH, createTestContainer());
224         Optional<NormalizedNode<?, ?>> potential = modificationTree.read(TWO_TWO_PATH);
225         MapEntryNode node = assertPresentAndType(potential, MapEntryNode.class);
226     }
227
228     @Test
229     public void writeSubtreeDeleteChildren() {
230         MutableDataTree modificationTree = createEmptyModificationTree();
231         modificationTree.write(TEST_PATH, createTestContainer());
232
233         // We verify data are present
234         Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.read(TWO_TWO_PATH);
235         MapEntryNode node = assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
236
237         modificationTree.delete(TWO_TWO_PATH);
238         Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.read(TWO_TWO_PATH);
239         assertFalse(potentialAfterDelete.isPresent());
240
241     }
242
243     private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
244         assertNotNull(potential);
245         assertTrue(potential.isPresent());
246         assertTrue(type.isInstance(potential.get()));
247         return type.cast(potential.get());
248     }
249
250 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.