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;
35 public class ShardedDOMDataTreeProducerMultiShardTest {
37 private final SchemaContext schemaContext = TestModel.createTestContext();
39 private static final DOMDataTreeIdentifier ROOT_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
40 YangInstanceIdentifier.EMPTY);
41 private static final DOMDataTreeIdentifier TEST_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
44 private static final DOMDataTreeIdentifier TEST2_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
45 TestModel.TEST2_PATH);
47 private static final DOMDataTreeIdentifier INNER_CONTAINER_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
48 private static final DOMDataTreeIdentifier ANOTHER_SHARD_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.ANOTHER_SHARD_PATH);
50 private InMemoryDOMDataTreeShard rootShard;
52 private InMemoryDOMDataTreeShard anotherInnerShard;
55 private ShardedDOMDataTree dataTreeService;
56 private ListenerRegistration<InMemoryDOMDataTreeShard> rootShardReg;
57 private ListenerRegistration<InMemoryDOMDataTreeShard> innerShardReg;
60 public void setUp() throws Exception {
62 rootShard = InMemoryDOMDataTreeShard.create(ROOT_ID);
63 rootShard.onGlobalContextUpdated(schemaContext);
65 ShardedDOMDataTree dataTree = new ShardedDOMDataTree();
66 rootShardReg = dataTree.registerDataTreeShard(ROOT_ID, rootShard);
68 dataTreeService = dataTree;
72 public void testMultipleShards() throws Exception {
73 //FIXME after listeners are implemented add them here and test those
75 final InMemoryDOMDataTreeShard innerShard = InMemoryDOMDataTreeShard.create(INNER_CONTAINER_ID);
76 innerShard.onGlobalContextUpdated(schemaContext);
77 innerShardReg = dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard);
79 final DOMDataTreeShardProducer producer = rootShard.createProducer(Collections.singletonList(TEST_ID));
80 final DOMDataTreeShardWriteTransaction transaction = producer.createTransaction();
81 final DOMDataTreeWriteCursor cursor = transaction.createCursor(ROOT_ID);
82 cursor.enter(TestModel.TEST_PATH.getLastPathArgument());
84 final LeafNode<String> shardedValue1 =
85 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_1)).withValue("sharded value 1").build();
86 final LeafNode<String> shardedValue2 =
87 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_2)).withValue("sharded value 2").build();
89 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerNodeBuilder = ImmutableContainerNodeBuilder.create();
90 final ContainerNode containerNode =
92 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
93 .withChild(shardedValue1)
94 .withChild(shardedValue2)
97 cursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(), containerNode);
98 cursor.enter(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
100 final ContainerNode lowerShardContainer = ImmutableContainerNodeBuilder.create()
101 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_CONTAINER))
102 .withChild(ImmutableLeafNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_VALUE)).build())
105 cursor.write(TestModel.ANOTHER_SHARD_PATH.getLastPathArgument(), lowerShardContainer);
108 transaction.submit().get();
110 //verify listeners have been notified
114 public void testMockedSubshards() throws Exception {
115 final WriteableDOMDataTreeShard mockedInnerShard = Mockito.mock(WriteableDOMDataTreeShard.class);
116 dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, mockedInnerShard);
117 final DOMDataTreeShardProducer mockedProducer = Mockito.mock(DOMDataTreeShardProducer.class);
118 doReturn(mockedProducer).when(mockedInnerShard).createProducer(any(Collection.class));
120 final DOMDataTreeShardProducer producer = rootShard.createProducer(Collections.singletonList(TEST_ID));
122 final DOMDataTreeShardWriteTransaction transaction = producer.createTransaction();
123 final DOMDataTreeWriteCursor cursor = transaction.createCursor(ROOT_ID);
124 cursor.enter(TestModel.TEST_PATH.getLastPathArgument());
126 final LeafNode<String> shardedValue1 =
127 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_1)).withValue("sharded value 1").build();
128 final LeafNode<String> shardedValue2 =
129 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_2)).withValue("sharded value 2").build();
131 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerNodeBuilder = ImmutableContainerNodeBuilder.create();
132 final ContainerNode containerNode =
134 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
135 .withChild(shardedValue1)
136 .withChild(shardedValue2)
139 final DOMDataTreeShardWriteTransaction mockedTx = Mockito.mock(DOMDataTreeShardWriteTransaction.class);
140 doReturn(mockedTx).when(mockedProducer).createTransaction();
142 doNothing().when(mockedTx).ready();
143 doReturn(Futures.immediateFuture(true)).when(mockedTx).validate();
144 doReturn(Futures.immediateFuture(null)).when(mockedTx).prepare();
145 doReturn(Futures.immediateFuture(null)).when(mockedTx).commit();
147 final DOMDataTreeWriteCursor mockedCursor = Mockito.mock(DOMDataTreeWriteCursor.class);
148 doNothing().when(mockedCursor).write(any(PathArgument.class), any(NormalizedNode.class));
149 doNothing().when(mockedCursor).close();
150 doReturn(mockedCursor).when(mockedTx).createCursor(any(DOMDataTreeIdentifier.class));
152 cursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(), containerNode);
153 cursor.enter(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
155 final ContainerNode lowerShardContainer = ImmutableContainerNodeBuilder.create()
156 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_CONTAINER))
157 .withChild(ImmutableLeafNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_VALUE)).build())
160 cursor.write(TestModel.ANOTHER_SHARD_PATH.getLastPathArgument(), lowerShardContainer);
163 transaction.submit().get();
165 verify(mockedTx).ready();
166 verify(mockedTx).validate();
167 verify(mockedTx).prepare();
168 verify(mockedTx).commit();