2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.broker;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Matchers.anyCollection;
15 import static org.mockito.Matchers.anyMap;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.timeout;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.verifyNoMoreInteractions;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.List;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.ArgumentCaptor;
30 import org.mockito.Captor;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
33 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
34 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
35 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
36 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
37 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
38 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
39 import org.opendaylight.mdsal.dom.broker.util.TestModel;
40 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataTreeShard;
41 import org.opendaylight.yangtools.concepts.ListenerRegistration;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 public class ShardedDOMDataTreeTest {
57 private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducerMultiShardTest.class);
59 private static SchemaContext schemaContext = null;
63 schemaContext = TestModel.createTestContext();
64 } catch (final ReactorException e) {
65 LOG.error("Unable to create schema context for TestModel", e);
69 private static final DOMDataTreeIdentifier ROOT_ID =
70 new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.EMPTY);
71 private static final DOMDataTreeIdentifier TEST_ID =
72 new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
74 private static final DOMDataTreeIdentifier INNER_CONTAINER_ID =
75 new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
77 private InMemoryDOMDataTreeShard rootShard;
79 private ShardedDOMDataTree dataTreeService;
80 private ListenerRegistration<InMemoryDOMDataTreeShard> rootShardReg;
82 private final ExecutorService executor = Executors.newSingleThreadExecutor();
85 private ArgumentCaptor<Collection<DataTreeCandidate>> captorForChanges;
87 private ArgumentCaptor<Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>>> captorForSubtrees;
89 private final ContainerNode crossShardContainer = createCrossShardContainer();
92 public void setUp() throws Exception {
93 MockitoAnnotations.initMocks(this);
95 rootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1);
96 rootShard.onGlobalContextUpdated(schemaContext);
98 final ShardedDOMDataTree dataTree = new ShardedDOMDataTree();
99 final DOMDataTreeProducer shardRegProducer = dataTree.createProducer(Collections.singletonList(ROOT_ID));
100 rootShardReg = dataTree.registerDataTreeShard(ROOT_ID, rootShard, shardRegProducer);
101 shardRegProducer.close();
103 dataTreeService = dataTree;
106 @Test(expected = IllegalArgumentException.class)
107 public void testProducerPathContention() throws Exception {
108 dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
109 dataTreeService.createProducer(Collections.singletonList(TEST_ID));
113 public void testShardRegistrationClose() throws Exception {
114 rootShardReg.close();
116 final InMemoryDOMDataTreeShard newRootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1);
117 newRootShard.onGlobalContextUpdated(schemaContext);
118 final DOMDataTreeProducer shardRegProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
120 final ListenerRegistration<InMemoryDOMDataTreeShard> newRootShardReg =
121 dataTreeService.registerDataTreeShard(ROOT_ID, rootShard, shardRegProducer);
122 shardRegProducer.close();
124 final InMemoryDOMDataTreeShard innerShard = InMemoryDOMDataTreeShard.create(INNER_CONTAINER_ID, executor, 1);
125 innerShard.onGlobalContextUpdated(schemaContext);
126 final DOMDataTreeProducer shardRegProducer2 =
127 dataTreeService.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
128 ListenerRegistration<InMemoryDOMDataTreeShard> innerShardReg =
129 dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard, shardRegProducer2);
131 innerShardReg.close();
132 // try to register the shard again
133 innerShardReg = dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard, shardRegProducer2);
134 final DOMDataTreeCursorAwareTransaction tx = shardRegProducer2.createTransaction(false);
135 final DOMDataTreeWriteCursor cursor = tx.createCursor(INNER_CONTAINER_ID);
136 assertNotNull(cursor);
140 shardRegProducer2.close();
142 innerShardReg.close();
143 newRootShardReg.close();
147 public void testSingleShardWrite() throws Exception {
148 final DOMDataTreeListener mockedDataTreeListener = Mockito.mock(DOMDataTreeListener.class);
149 doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
151 dataTreeService.registerListener(mockedDataTreeListener, Collections.singletonList(INNER_CONTAINER_ID),
152 true, Collections.emptyList());
154 final DOMDataTreeProducer producer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
155 DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
156 DOMDataTreeWriteCursor cursor = tx.createCursor(ROOT_ID);
157 assertNotNull(cursor);
159 cursor.write(TEST_ID.getRootIdentifier().getLastPathArgument(), crossShardContainer);
162 tx.submit().checkedGet();
163 fail("There's still an open cursor");
164 } catch (final IllegalStateException e) {
165 assertTrue(e.getMessage().contains("cursor open"));
169 tx.submit().checkedGet();
171 tx = producer.createTransaction(false);
172 cursor = tx.createCursor(TEST_ID);
173 assertNotNull(cursor);
175 cursor.delete(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
177 tx.submit().checkedGet();
179 verify(mockedDataTreeListener, timeout(1000).times(3)).onDataTreeChanged(captorForChanges.capture(),
180 captorForSubtrees.capture());
181 final List<Collection<DataTreeCandidate>> capturedValue = captorForChanges.getAllValues();
182 assertTrue(capturedValue.size() == 3);
184 final ContainerNode capturedChange =
185 (ContainerNode) capturedValue.get(1).iterator().next().getRootNode().getDataAfter().get();
186 final ContainerNode innerContainerVerify = (ContainerNode) crossShardContainer.getChild(
187 TestModel.INNER_CONTAINER_PATH.getLastPathArgument()).get();
188 assertEquals(innerContainerVerify, capturedChange);
190 verifyNoMoreInteractions(mockedDataTreeListener);
194 public void testMultipleProducerCursorCreation() throws Exception {
196 final DOMDataTreeProducer rootProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
197 DOMDataTreeCursorAwareTransaction rootTx = rootProducer.createTransaction(false);
198 //check if we can create cursor where the new producer will be
199 DOMDataTreeWriteCursor rootTxCursor = rootTx.createCursor(INNER_CONTAINER_ID);
200 assertNotNull(rootTxCursor);
201 rootTxCursor.close();
204 rootProducer.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
205 fail("Should've failed there is still a tx open");
206 } catch (final IllegalStateException e) {
207 assertTrue(e.getMessage().contains("open"));
210 assertTrue(rootTx.cancel());
212 final DOMDataTreeProducer innerContainerProducer = rootProducer.createProducer(
213 Collections.singletonList(INNER_CONTAINER_ID));
215 rootTx = rootProducer.createTransaction(false);
217 rootTx.createCursor(INNER_CONTAINER_ID);
218 fail("Subtree should not be available to this producer");
219 } catch (final IllegalArgumentException e) {
220 assertTrue(e.getMessage().contains("delegated to child producer"));
223 rootTxCursor = rootTx.createCursor(TEST_ID);
224 assertNotNull(rootTxCursor);
226 rootTxCursor.enter(INNER_CONTAINER_ID.getRootIdentifier().getLastPathArgument());
227 fail("Cursor should not have this subtree available");
228 } catch (final IllegalArgumentException e) {
229 assertTrue(e.getMessage().contains("not available to this cursor"));
233 rootTxCursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(),
234 ImmutableContainerNodeBuilder.create()
235 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
237 fail("Cursor should not have this subtree available");
238 } catch (final IllegalArgumentException e) {
239 assertTrue(e.getMessage().contains("not available to this cursor"));
242 final DOMDataTreeCursorAwareTransaction innerShardTx = innerContainerProducer.createTransaction(false);
243 final DOMDataTreeWriteCursor innerShardCursor = innerShardTx.createCursor(INNER_CONTAINER_ID);
244 assertNotNull(innerShardCursor);
247 private ContainerNode createCrossShardContainer() {
248 final LeafNode<String> shardedValue1 =
249 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(
250 TestModel.SHARDED_VALUE_1)).withValue("sharded value 1").build();
251 final LeafNode<String> shardedValue2 =
252 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(
253 TestModel.SHARDED_VALUE_2)).withValue("sharded value 2").build();
256 final ContainerNode lowerShardContainer = ImmutableContainerNodeBuilder.create()
257 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_CONTAINER))
258 .withChild(ImmutableLeafNodeBuilder.create()
259 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_VALUE))
260 .withValue("testing-value")
264 final ContainerNode containerNode =
265 ImmutableContainerNodeBuilder.create()
266 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
267 .withChild(shardedValue1)
268 .withChild(shardedValue2)
269 .withChild(lowerShardContainer)
272 final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
273 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
274 .withChild(containerNode)
277 return testContainer;