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 junit.framework.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Captor;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
34 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
35 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
36 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
37 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
38 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
39 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
40 import org.opendaylight.mdsal.dom.broker.util.TestModel;
41 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataTreeShard;
42 import org.opendaylight.yangtools.concepts.ListenerRegistration;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
51 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
52 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class ShardedDOMDataTreeTest {
58 private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducerMultiShardTest.class);
60 private static SchemaContext schemaContext = null;
64 schemaContext = TestModel.createTestContext();
65 } catch (final ReactorException e) {
66 LOG.error("Unable to create schema context for TestModel", e);
70 private static final DOMDataTreeIdentifier ROOT_ID =
71 new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.EMPTY);
72 private static final DOMDataTreeIdentifier TEST_ID =
73 new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
75 private static final DOMDataTreeIdentifier INNER_CONTAINER_ID =
76 new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
78 private InMemoryDOMDataTreeShard rootShard;
80 private ShardedDOMDataTree dataTreeService;
81 private ListenerRegistration<InMemoryDOMDataTreeShard> rootShardReg;
83 private final ExecutorService executor = Executors.newSingleThreadExecutor();
86 private ArgumentCaptor<Collection<DataTreeCandidate>> captorForChanges;
88 private ArgumentCaptor<Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>>> captorForSubtrees;
90 private final ContainerNode crossShardContainer = createCrossShardContainer();
93 public void setUp() throws Exception {
94 MockitoAnnotations.initMocks(this);
96 rootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1, 1);
97 rootShard.onGlobalContextUpdated(schemaContext);
99 final ShardedDOMDataTree dataTree = new ShardedDOMDataTree();
100 final DOMDataTreeProducer shardRegProducer = dataTree.createProducer(Collections.singletonList(ROOT_ID));
101 rootShardReg = dataTree.registerDataTreeShard(ROOT_ID, rootShard, shardRegProducer);
102 shardRegProducer.close();
104 dataTreeService = dataTree;
107 @Test(expected = IllegalArgumentException.class)
108 public void testProducerPathContention() throws Exception {
109 dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
110 dataTreeService.createProducer(Collections.singletonList(TEST_ID));
114 public void testShardRegistrationClose() throws Exception {
115 rootShardReg.close();
117 final InMemoryDOMDataTreeShard newRootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1, 1);
118 newRootShard.onGlobalContextUpdated(schemaContext);
119 final DOMDataTreeProducer shardRegProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
121 ListenerRegistration<InMemoryDOMDataTreeShard> newRootShardReg =
122 dataTreeService.registerDataTreeShard(ROOT_ID, rootShard, shardRegProducer);
123 shardRegProducer.close();
125 final InMemoryDOMDataTreeShard innerShard = InMemoryDOMDataTreeShard.create(INNER_CONTAINER_ID, executor, 1, 1);
126 innerShard.onGlobalContextUpdated(schemaContext);
127 final DOMDataTreeProducer shardRegProducer2 = dataTreeService.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
128 ListenerRegistration<InMemoryDOMDataTreeShard> innerShardReg = dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard, shardRegProducer2);
130 innerShardReg.close();
131 // try to register the shard again
132 innerShardReg = dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard, shardRegProducer2);
133 final DOMDataTreeCursorAwareTransaction tx = shardRegProducer2.createTransaction(false);
134 final DOMDataTreeWriteCursor cursor = tx.createCursor(INNER_CONTAINER_ID);
135 assertNotNull(cursor);
139 shardRegProducer2.close();
141 innerShardReg.close();
142 newRootShardReg.close();
146 public void testSingleShardWrite() throws Exception {
147 final DOMDataTreeListener mockedDataTreeListener = Mockito.mock(DOMDataTreeListener.class);
148 doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
150 dataTreeService.registerListener(mockedDataTreeListener, Collections.singletonList(INNER_CONTAINER_ID), true, Collections.emptyList());
152 final DOMDataTreeProducer producer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
153 DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
154 DOMDataTreeWriteCursor cursor = tx.createCursor(ROOT_ID);
155 assertNotNull(cursor);
157 cursor.write(TEST_ID.getRootIdentifier().getLastPathArgument(), crossShardContainer);
160 tx.submit().checkedGet();
161 fail("There's still an open cursor");
162 } catch (final IllegalStateException e) {
163 assertTrue(e.getMessage().contains("cursor open"));
167 tx.submit().checkedGet();
169 tx = producer.createTransaction(false);
170 cursor = tx.createCursor(TEST_ID);
171 assertNotNull(cursor);
173 cursor.delete(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
175 tx.submit().checkedGet();
177 verify(mockedDataTreeListener, timeout(1000).times(3)).onDataTreeChanged(captorForChanges.capture(), captorForSubtrees.capture());
178 final List<Collection<DataTreeCandidate>> capturedValue = captorForChanges.getAllValues();
179 assertTrue(capturedValue.size() == 3);
181 final ContainerNode capturedChange = (ContainerNode) capturedValue.get(1).iterator().next().getRootNode().getDataAfter().get();
182 final ContainerNode innerContainerVerify = (ContainerNode) crossShardContainer.getChild(TestModel.INNER_CONTAINER_PATH.getLastPathArgument()).get();
183 assertEquals(innerContainerVerify, capturedChange);
185 verifyNoMoreInteractions(mockedDataTreeListener);
189 public void testMultipleProducerCursorCreation() throws Exception {
191 final DOMDataTreeProducer rootProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
192 DOMDataTreeCursorAwareTransaction rootTx = rootProducer.createTransaction(false);
193 //check if we can create cursor where the new producer will be
194 DOMDataTreeWriteCursor rootTxCursor = rootTx.createCursor(INNER_CONTAINER_ID);
195 assertNotNull(rootTxCursor);
196 rootTxCursor.close();
199 rootProducer.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
200 fail("Should've failed there is still a tx open");
201 } catch (final IllegalStateException e) {
202 assertTrue(e.getMessage().contains("open"));
205 assertTrue(rootTx.cancel());
207 final DOMDataTreeProducer innerContainerProducer = rootProducer.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
209 rootTx = rootProducer.createTransaction(false);
211 rootTx.createCursor(INNER_CONTAINER_ID);
212 fail("Subtree should not be available to this producer");
213 } catch (final IllegalArgumentException e) {
214 assertTrue(e.getMessage().contains("delegated to child producer"));
217 rootTxCursor = rootTx.createCursor(TEST_ID);
218 assertNotNull(rootTxCursor);
220 rootTxCursor.enter(INNER_CONTAINER_ID.getRootIdentifier().getLastPathArgument());
221 fail("Cursor should not have this subtree available");
222 } catch (final IllegalArgumentException e) {
223 assertTrue(e.getMessage().contains("not available to this cursor"));
227 rootTxCursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(),
228 ImmutableContainerNodeBuilder.create()
229 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
231 fail("Cursor should not have this subtree available");
232 } catch (final IllegalArgumentException e) {
233 assertTrue(e.getMessage().contains("not available to this cursor"));
236 final DOMDataTreeCursorAwareTransaction innerShardTx = innerContainerProducer.createTransaction(false);
237 final DOMDataTreeWriteCursor innerShardCursor = innerShardTx.createCursor(INNER_CONTAINER_ID);
238 assertNotNull(innerShardCursor);
241 private ContainerNode createCrossShardContainer() {
242 final LeafNode<String> shardedValue1 =
243 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_1)).withValue("sharded value 1").build();
244 final LeafNode<String> shardedValue2 =
245 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_2)).withValue("sharded value 2").build();
248 final ContainerNode lowerShardContainer = ImmutableContainerNodeBuilder.create()
249 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_CONTAINER))
250 .withChild(ImmutableLeafNodeBuilder.create()
251 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_VALUE))
252 .withValue("testing-value")
256 final ContainerNode containerNode =
257 ImmutableContainerNodeBuilder.create()
258 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
259 .withChild(shardedValue1)
260 .withChild(shardedValue2)
261 .withChild(lowerShardContainer)
264 final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
265 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
266 .withChild(containerNode)
269 return testContainer;