1 package org.opendaylight.mdsal.dom.broker.test;
3 import static org.mockito.Matchers.any;
4 import static org.mockito.Mockito.doNothing;
5 import static org.mockito.Mockito.doReturn;
6 import static org.mockito.Mockito.verify;
8 import com.google.common.util.concurrent.Futures;
9 import java.util.Collection;
10 import java.util.Collections;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.mockito.Mockito;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
17 import org.opendaylight.mdsal.dom.broker.ShardedDOMDataTree;
18 import org.opendaylight.mdsal.dom.broker.test.util.TestModel;
19 import org.opendaylight.mdsal.dom.store.inmemory.DOMDataTreeShardProducer;
20 import org.opendaylight.mdsal.dom.store.inmemory.DOMDataTreeShardWriteTransaction;
21 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataTreeShard;
22 import org.opendaylight.mdsal.dom.store.inmemory.WriteableDOMDataTreeShard;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
36 public class ShardedDOMDataTreeProducerMultiShardTest {
38 private SchemaContext schemaContext;
40 private static final DOMDataTreeIdentifier ROOT_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
41 YangInstanceIdentifier.EMPTY);
42 private static final DOMDataTreeIdentifier TEST_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
45 private static final DOMDataTreeIdentifier TEST2_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
46 TestModel.TEST2_PATH);
48 private static final DOMDataTreeIdentifier INNER_CONTAINER_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
49 private static final DOMDataTreeIdentifier ANOTHER_SHARD_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.ANOTHER_SHARD_PATH);
51 private InMemoryDOMDataTreeShard rootShard;
53 private InMemoryDOMDataTreeShard anotherInnerShard;
56 private ShardedDOMDataTree dataTreeService;
57 private ListenerRegistration<InMemoryDOMDataTreeShard> rootShardReg;
58 private ListenerRegistration<InMemoryDOMDataTreeShard> innerShardReg;
61 public void setUp() throws Exception {
62 schemaContext = TestModel.createTestContext();
64 rootShard = InMemoryDOMDataTreeShard.create(ROOT_ID);
65 rootShard.onGlobalContextUpdated(schemaContext);
67 ShardedDOMDataTree dataTree = new ShardedDOMDataTree();
68 rootShardReg = dataTree.registerDataTreeShard(ROOT_ID, rootShard);
70 dataTreeService = dataTree;
74 public void testMultipleShards() throws Exception {
75 //FIXME after listeners are implemented add them here and test those
77 final InMemoryDOMDataTreeShard innerShard = InMemoryDOMDataTreeShard.create(INNER_CONTAINER_ID);
78 innerShard.onGlobalContextUpdated(schemaContext);
79 innerShardReg = dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard);
81 final DOMDataTreeShardProducer producer = rootShard.createProducer(Collections.singletonList(TEST_ID));
82 final DOMDataTreeShardWriteTransaction transaction = producer.createTransaction();
83 final DOMDataTreeWriteCursor cursor = transaction.createCursor(ROOT_ID);
84 cursor.enter(TestModel.TEST_PATH.getLastPathArgument());
86 final LeafNode<String> shardedValue1 =
87 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_1)).withValue("sharded value 1").build();
88 final LeafNode<String> shardedValue2 =
89 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_2)).withValue("sharded value 2").build();
91 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerNodeBuilder = ImmutableContainerNodeBuilder.create();
92 final ContainerNode containerNode =
94 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
95 .withChild(shardedValue1)
96 .withChild(shardedValue2)
99 cursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(), containerNode);
100 cursor.enter(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
102 final ContainerNode lowerShardContainer = ImmutableContainerNodeBuilder.create()
103 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_CONTAINER))
104 .withChild(ImmutableLeafNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_VALUE)).build())
107 cursor.write(TestModel.ANOTHER_SHARD_PATH.getLastPathArgument(), lowerShardContainer);
110 transaction.submit().get();
112 //verify listeners have been notified
116 public void testMockedSubshards() throws Exception {
117 final WriteableDOMDataTreeShard mockedInnerShard = Mockito.mock(WriteableDOMDataTreeShard.class);
118 dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, mockedInnerShard);
119 final DOMDataTreeShardProducer mockedProducer = Mockito.mock(DOMDataTreeShardProducer.class);
120 doReturn(mockedProducer).when(mockedInnerShard).createProducer(any(Collection.class));
122 final DOMDataTreeShardProducer producer = rootShard.createProducer(Collections.singletonList(TEST_ID));
124 final DOMDataTreeShardWriteTransaction transaction = producer.createTransaction();
125 final DOMDataTreeWriteCursor cursor = transaction.createCursor(ROOT_ID);
126 cursor.enter(TestModel.TEST_PATH.getLastPathArgument());
128 final LeafNode<String> shardedValue1 =
129 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_1)).withValue("sharded value 1").build();
130 final LeafNode<String> shardedValue2 =
131 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_2)).withValue("sharded value 2").build();
133 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerNodeBuilder = ImmutableContainerNodeBuilder.create();
134 final ContainerNode containerNode =
136 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
137 .withChild(shardedValue1)
138 .withChild(shardedValue2)
141 final DOMDataTreeShardWriteTransaction mockedTx = Mockito.mock(DOMDataTreeShardWriteTransaction.class);
142 doReturn(mockedTx).when(mockedProducer).createTransaction();
144 doNothing().when(mockedTx).ready();
145 doReturn(Futures.immediateFuture(true)).when(mockedTx).validate();
146 doReturn(Futures.immediateFuture(null)).when(mockedTx).prepare();
147 doReturn(Futures.immediateFuture(null)).when(mockedTx).commit();
149 final DOMDataTreeWriteCursor mockedCursor = Mockito.mock(DOMDataTreeWriteCursor.class);
150 doNothing().when(mockedCursor).write(any(PathArgument.class), any(NormalizedNode.class));
151 doNothing().when(mockedCursor).close();
152 doReturn(mockedCursor).when(mockedTx).createCursor(any(DOMDataTreeIdentifier.class));
154 cursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(), containerNode);
155 cursor.enter(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
157 final ContainerNode lowerShardContainer = ImmutableContainerNodeBuilder.create()
158 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_CONTAINER))
159 .withChild(ImmutableLeafNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_VALUE)).build())
162 cursor.write(TestModel.ANOTHER_SHARD_PATH.getLastPathArgument(), lowerShardContainer);
165 transaction.submit().get();
167 verify(mockedTx).ready();
168 verify(mockedTx).validate();
169 verify(mockedTx).prepare();
170 verify(mockedTx).commit();