Make AbstractDOMShardTreeChangePublisher advertise initial data change
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / ShardedDOMDataTreeTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.broker;
9
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;
20
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Map;
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;
54
55 public class ShardedDOMDataTreeTest {
56
57     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducerMultiShardTest.class);
58
59     private static SchemaContext schemaContext = null;
60
61     static {
62         try {
63             schemaContext = TestModel.createTestContext();
64         } catch (final ReactorException e) {
65             LOG.error("Unable to create schema context for TestModel", e);
66         }
67     }
68
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);
73
74     private static final DOMDataTreeIdentifier INNER_CONTAINER_ID =
75             new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
76
77     private InMemoryDOMDataTreeShard rootShard;
78
79     private ShardedDOMDataTree dataTreeService;
80     private ListenerRegistration<InMemoryDOMDataTreeShard> rootShardReg;
81
82     private final ExecutorService executor = Executors.newSingleThreadExecutor();
83
84     @Captor
85     private ArgumentCaptor<Collection<DataTreeCandidate>> captorForChanges;
86     @Captor
87     private ArgumentCaptor<Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>>> captorForSubtrees;
88
89     private final ContainerNode crossShardContainer = createCrossShardContainer();
90
91     @Before
92     public void setUp() throws Exception {
93         MockitoAnnotations.initMocks(this);
94
95         rootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1, 1);
96         rootShard.onGlobalContextUpdated(schemaContext);
97
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();
102
103         dataTreeService = dataTree;
104     }
105
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));
110     }
111
112     @Test
113     public void testSingleShardWrite() throws Exception {
114         final DOMDataTreeListener mockedDataTreeListener = Mockito.mock(DOMDataTreeListener.class);
115         doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
116
117         dataTreeService.registerListener(mockedDataTreeListener, Collections.singletonList(INNER_CONTAINER_ID), true, Collections.emptyList());
118
119         final DOMDataTreeProducer producer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
120         DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
121         DOMDataTreeWriteCursor cursor = tx.createCursor(ROOT_ID);
122         assertNotNull(cursor);
123
124         cursor.write(TEST_ID.getRootIdentifier().getLastPathArgument(), crossShardContainer);
125
126         try {
127             tx.submit().checkedGet();
128             fail("There's still an open cursor");
129         } catch (final IllegalStateException e) {
130             assertTrue(e.getMessage().contains("cursor open"));
131         }
132
133         cursor.close();
134         tx.submit().checkedGet();
135
136         tx = producer.createTransaction(false);
137         cursor = tx.createCursor(TEST_ID);
138         assertNotNull(cursor);
139
140         cursor.delete(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
141         cursor.close();
142         tx.submit().checkedGet();
143
144         verify(mockedDataTreeListener, timeout(1000).times(3)).onDataTreeChanged(captorForChanges.capture(), captorForSubtrees.capture());
145         final List<Collection<DataTreeCandidate>> capturedValue = captorForChanges.getAllValues();
146         assertTrue(capturedValue.size() == 3);
147
148         final ContainerNode capturedChange = (ContainerNode) capturedValue.get(1).iterator().next().getRootNode().getDataAfter().get();
149         final ContainerNode innerContainerVerify = (ContainerNode) crossShardContainer.getChild(TestModel.INNER_CONTAINER_PATH.getLastPathArgument()).get();
150         assertEquals(innerContainerVerify, capturedChange);
151
152         verifyNoMoreInteractions(mockedDataTreeListener);
153     }
154
155     @Test
156     public void testMultipleProducerCursorCreation() throws Exception {
157
158         final DOMDataTreeProducer rootProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
159         DOMDataTreeCursorAwareTransaction rootTx = rootProducer.createTransaction(false);
160         //check if we can create cursor where the new producer will be
161         DOMDataTreeWriteCursor rootTxCursor = rootTx.createCursor(INNER_CONTAINER_ID);
162         assertNotNull(rootTxCursor);
163         rootTxCursor.close();
164
165         try {
166             rootProducer.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
167             fail("Should've failed there is still a tx open");
168         } catch (final IllegalStateException e) {
169             assertTrue(e.getMessage().contains("open"));
170         }
171
172         assertTrue(rootTx.cancel());
173
174         final DOMDataTreeProducer innerContainerProducer = rootProducer.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
175
176         rootTx = rootProducer.createTransaction(false);
177         try {
178             rootTx.createCursor(INNER_CONTAINER_ID);
179             fail("Subtree should not be available to this producer");
180         } catch (final IllegalArgumentException e) {
181             assertTrue(e.getMessage().contains("delegated to child producer"));
182         }
183
184         rootTxCursor = rootTx.createCursor(TEST_ID);
185         assertNotNull(rootTxCursor);
186         try {
187             rootTxCursor.enter(INNER_CONTAINER_ID.getRootIdentifier().getLastPathArgument());
188             fail("Cursor should not have this subtree available");
189         } catch (final IllegalArgumentException e) {
190             assertTrue(e.getMessage().contains("not available to this cursor"));
191         }
192
193         try {
194             rootTxCursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(),
195                     ImmutableContainerNodeBuilder.create()
196                             .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
197                             .build());
198             fail("Cursor should not have this subtree available");
199         } catch (final IllegalArgumentException e) {
200             assertTrue(e.getMessage().contains("not available to this cursor"));
201         }
202
203         final DOMDataTreeCursorAwareTransaction innerShardTx = innerContainerProducer.createTransaction(false);
204         final DOMDataTreeWriteCursor innerShardCursor = innerShardTx.createCursor(INNER_CONTAINER_ID);
205         assertNotNull(innerShardCursor);
206     }
207
208     private ContainerNode createCrossShardContainer() {
209         final LeafNode<String> shardedValue1 =
210                 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_1)).withValue("sharded value 1").build();
211         final LeafNode<String> shardedValue2 =
212                 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_2)).withValue("sharded value 2").build();
213
214
215         final ContainerNode lowerShardContainer = ImmutableContainerNodeBuilder.create()
216                 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_CONTAINER))
217                 .withChild(ImmutableLeafNodeBuilder.create()
218                         .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_VALUE))
219                         .withValue("testing-value")
220                         .build())
221                 .build();
222
223         final ContainerNode containerNode =
224                 ImmutableContainerNodeBuilder.create()
225                         .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
226                         .withChild(shardedValue1)
227                         .withChild(shardedValue2)
228                         .withChild(lowerShardContainer)
229                         .build();
230
231         final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
232                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
233                 .withChild(containerNode)
234                 .build();
235
236         return testContainer;
237     }
238 }