Fix NPE when creating a producer after shard was unregistered
[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 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;
55
56 public class ShardedDOMDataTreeTest {
57
58     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducerMultiShardTest.class);
59
60     private static SchemaContext schemaContext = null;
61
62     static {
63         try {
64             schemaContext = TestModel.createTestContext();
65         } catch (final ReactorException e) {
66             LOG.error("Unable to create schema context for TestModel", e);
67         }
68     }
69
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);
74
75     private static final DOMDataTreeIdentifier INNER_CONTAINER_ID =
76             new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
77
78     private InMemoryDOMDataTreeShard rootShard;
79
80     private ShardedDOMDataTree dataTreeService;
81     private ListenerRegistration<InMemoryDOMDataTreeShard> rootShardReg;
82
83     private final ExecutorService executor = Executors.newSingleThreadExecutor();
84
85     @Captor
86     private ArgumentCaptor<Collection<DataTreeCandidate>> captorForChanges;
87     @Captor
88     private ArgumentCaptor<Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>>> captorForSubtrees;
89
90     private final ContainerNode crossShardContainer = createCrossShardContainer();
91
92     @Before
93     public void setUp() throws Exception {
94         MockitoAnnotations.initMocks(this);
95
96         rootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1, 1);
97         rootShard.onGlobalContextUpdated(schemaContext);
98
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();
103
104         dataTreeService = dataTree;
105     }
106
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));
111     }
112
113     @Test
114     public void testShardRegistrationClose() throws Exception {
115         rootShardReg.close();
116
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));
120
121         ListenerRegistration<InMemoryDOMDataTreeShard> newRootShardReg =
122                 dataTreeService.registerDataTreeShard(ROOT_ID, rootShard, shardRegProducer);
123         shardRegProducer.close();
124
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);
129
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);
136
137         cursor.close();
138         tx.cancel();
139         shardRegProducer2.close();
140
141         innerShardReg.close();
142         newRootShardReg.close();
143     }
144
145     @Test
146     public void testSingleShardWrite() throws Exception {
147         final DOMDataTreeListener mockedDataTreeListener = Mockito.mock(DOMDataTreeListener.class);
148         doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
149
150         dataTreeService.registerListener(mockedDataTreeListener, Collections.singletonList(INNER_CONTAINER_ID), true, Collections.emptyList());
151
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);
156
157         cursor.write(TEST_ID.getRootIdentifier().getLastPathArgument(), crossShardContainer);
158
159         try {
160             tx.submit().checkedGet();
161             fail("There's still an open cursor");
162         } catch (final IllegalStateException e) {
163             assertTrue(e.getMessage().contains("cursor open"));
164         }
165
166         cursor.close();
167         tx.submit().checkedGet();
168
169         tx = producer.createTransaction(false);
170         cursor = tx.createCursor(TEST_ID);
171         assertNotNull(cursor);
172
173         cursor.delete(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
174         cursor.close();
175         tx.submit().checkedGet();
176
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);
180
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);
184
185         verifyNoMoreInteractions(mockedDataTreeListener);
186     }
187
188     @Test
189     public void testMultipleProducerCursorCreation() throws Exception {
190
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();
197
198         try {
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"));
203         }
204
205         assertTrue(rootTx.cancel());
206
207         final DOMDataTreeProducer innerContainerProducer = rootProducer.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
208
209         rootTx = rootProducer.createTransaction(false);
210         try {
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"));
215         }
216
217         rootTxCursor = rootTx.createCursor(TEST_ID);
218         assertNotNull(rootTxCursor);
219         try {
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"));
224         }
225
226         try {
227             rootTxCursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(),
228                     ImmutableContainerNodeBuilder.create()
229                             .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
230                             .build());
231             fail("Cursor should not have this subtree available");
232         } catch (final IllegalArgumentException e) {
233             assertTrue(e.getMessage().contains("not available to this cursor"));
234         }
235
236         final DOMDataTreeCursorAwareTransaction innerShardTx = innerContainerProducer.createTransaction(false);
237         final DOMDataTreeWriteCursor innerShardCursor = innerShardTx.createCursor(INNER_CONTAINER_ID);
238         assertNotNull(innerShardCursor);
239     }
240
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();
246
247
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")
253                         .build())
254                 .build();
255
256         final ContainerNode containerNode =
257                 ImmutableContainerNodeBuilder.create()
258                         .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
259                         .withChild(shardedValue1)
260                         .withChild(shardedValue2)
261                         .withChild(lowerShardContainer)
262                         .build();
263
264         final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
265                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
266                 .withChild(containerNode)
267                 .build();
268
269         return testContainer;
270     }
271 }