Convert CDS implementation to use msdal APIs
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / sharding / DistributedShardedDOMDataTreeTest.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
9 package org.opendaylight.controller.cluster.sharding;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
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.doReturn;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.timeout;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.verifyNoMoreInteractions;
22 import static org.opendaylight.controller.cluster.datastore.IntegrationTestKit.findLocalShard;
23 import static org.opendaylight.controller.cluster.datastore.IntegrationTestKit.waitUntilShardIsDown;
24
25 import akka.actor.ActorRef;
26 import akka.actor.ActorSystem;
27 import akka.actor.Address;
28 import akka.actor.AddressFromURIString;
29 import akka.actor.Props;
30 import akka.cluster.Cluster;
31 import akka.testkit.javadsl.TestKit;
32 import com.google.common.base.Optional;
33 import com.google.common.collect.Lists;
34 import com.google.common.util.concurrent.CheckedFuture;
35 import com.typesafe.config.ConfigFactory;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Set;
42 import java.util.concurrent.CompletionStage;
43 import java.util.concurrent.TimeUnit;
44 import org.junit.After;
45 import org.junit.Assert;
46 import org.junit.Before;
47 import org.junit.Ignore;
48 import org.junit.Test;
49 import org.mockito.ArgumentCaptor;
50 import org.mockito.Captor;
51 import org.mockito.Mockito;
52 import org.mockito.MockitoAnnotations;
53 import org.opendaylight.controller.cluster.ActorSystemProvider;
54 import org.opendaylight.controller.cluster.access.concepts.MemberName;
55 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory;
56 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
57 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
58 import org.opendaylight.controller.cluster.databroker.actors.dds.SimpleDataStoreClientActor;
59 import org.opendaylight.controller.cluster.datastore.AbstractTest;
60 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
61 import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder;
62 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
63 import org.opendaylight.controller.cluster.datastore.IntegrationTestKit;
64 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
65 import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
66 import org.opendaylight.controller.cluster.dom.api.CDSDataTreeProducer;
67 import org.opendaylight.controller.cluster.dom.api.CDSShardAccess;
68 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
69 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
70 import org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration;
71 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
72 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
73 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
74 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
75 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
76 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
77 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
78 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
79 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
80 import org.opendaylight.yangtools.yang.common.QName;
81 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
82 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
83 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
84 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
85 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
86 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
87 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
88 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
89 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
90 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
91 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
92 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
93 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
94 import org.slf4j.Logger;
95 import org.slf4j.LoggerFactory;
96
97 public class DistributedShardedDOMDataTreeTest extends AbstractTest {
98
99     private static final Logger LOG = LoggerFactory.getLogger(DistributedShardedDOMDataTreeRemotingTest.class);
100
101     private static final Address MEMBER_1_ADDRESS =
102             AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2558");
103
104     private static final DOMDataTreeIdentifier TEST_ID =
105             new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
106
107     private static final DOMDataTreeIdentifier INNER_LIST_ID =
108             new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
109                     YangInstanceIdentifier.create(getOuterListIdFor(0).getPathArguments())
110                             .node(TestModel.INNER_LIST_QNAME));
111     private static final Set<MemberName> SINGLE_MEMBER = Collections.singleton(AbstractTest.MEMBER_NAME);
112
113     private static final String MODULE_SHARDS_CONFIG = "module-shards-default-member-1.conf";
114
115     private ActorSystem leaderSystem;
116
117     private final Builder leaderDatastoreContextBuilder =
118             DatastoreContext.newBuilder()
119                     .shardHeartbeatIntervalInMillis(100)
120                     .shardElectionTimeoutFactor(2)
121                     .logicalStoreType(LogicalDatastoreType.CONFIGURATION);
122
123     private DistributedDataStore leaderDistributedDataStore;
124     private DistributedDataStore operDistributedDatastore;
125     private IntegrationTestKit leaderTestKit;
126
127     private DistributedShardedDOMDataTree leaderShardFactory;
128
129     @Captor
130     private ArgumentCaptor<Collection<DataTreeCandidate>> captorForChanges;
131     @Captor
132     private ArgumentCaptor<Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>>> captorForSubtrees;
133
134     private ActorSystemProvider leaderSystemProvider;
135
136     @Before
137     public void setUp() {
138         MockitoAnnotations.initMocks(this);
139
140         InMemoryJournal.clear();
141         InMemorySnapshotStore.clear();
142
143         leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
144         Cluster.get(leaderSystem).join(MEMBER_1_ADDRESS);
145
146         leaderSystemProvider = Mockito.mock(ActorSystemProvider.class);
147         doReturn(leaderSystem).when(leaderSystemProvider).getActorSystem();
148     }
149
150     @After
151     public void tearDown() throws Exception {
152         if (leaderDistributedDataStore != null) {
153             leaderDistributedDataStore.close();
154         }
155
156         if (operDistributedDatastore != null) {
157             operDistributedDatastore.close();
158         }
159
160         TestKit.shutdownActorSystem(leaderSystem);
161
162         InMemoryJournal.clear();
163         InMemorySnapshotStore.clear();
164     }
165
166     private void initEmptyDatastores() throws Exception {
167         leaderTestKit = new IntegrationTestKit(leaderSystem, leaderDatastoreContextBuilder);
168
169         leaderDistributedDataStore = leaderTestKit.setupDistributedDataStore(
170                 "config", MODULE_SHARDS_CONFIG, "empty-modules.conf", true,
171                 SchemaContextHelper.distributedShardedDOMDataTreeSchemaContext());
172
173         operDistributedDatastore = leaderTestKit.setupDistributedDataStore(
174                 "operational", MODULE_SHARDS_CONFIG, "empty-modules.conf",true,
175                 SchemaContextHelper.distributedShardedDOMDataTreeSchemaContext());
176
177         leaderShardFactory = new DistributedShardedDOMDataTree(leaderSystemProvider,
178                 operDistributedDatastore,
179                 leaderDistributedDataStore);
180
181         leaderShardFactory.init();
182     }
183
184
185     @Test
186     public void testWritesIntoDefaultShard() throws Exception {
187         initEmptyDatastores();
188
189         final DOMDataTreeIdentifier configRoot =
190                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
191
192         final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singleton(configRoot));
193
194         final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(true);
195         final DOMDataTreeWriteCursor cursor =
196                 tx.createCursor(new DOMDataTreeIdentifier(
197                         LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY));
198         Assert.assertNotNull(cursor);
199
200         final ContainerNode test =
201                 ImmutableContainerNodeBuilder.create()
202                         .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
203
204         cursor.write(test.getIdentifier(), test);
205         cursor.close();
206
207         tx.submit().checkedGet();
208     }
209
210     @Test
211     public void testSingleNodeWritesAndRead() throws Exception {
212         initEmptyDatastores();
213
214         final DistributedShardRegistration shardRegistration = waitOnAsyncTask(
215                 leaderShardFactory.createDistributedShard(TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME)),
216                 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
217
218         leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(),
219                 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
220
221         final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singleton(TEST_ID));
222
223         final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(true);
224         final DOMDataTreeWriteCursor cursor = tx.createCursor(TEST_ID);
225         Assert.assertNotNull(cursor);
226         final YangInstanceIdentifier nameId =
227                 YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(TestModel.NAME_QNAME).build();
228         final LeafNode<String> valueToCheck = ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(
229                 new NodeIdentifier(TestModel.NAME_QNAME)).withValue("Test Value").build();
230         LOG.debug("Writing data {} at {}, cursor {}", nameId.getLastPathArgument(), valueToCheck, cursor);
231         cursor.write(nameId.getLastPathArgument(),
232                 valueToCheck);
233
234         cursor.close();
235         LOG.debug("Got to pre submit");
236
237         tx.submit().checkedGet();
238
239         final DOMDataTreeListener mockedDataTreeListener = mock(DOMDataTreeListener.class);
240         doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
241
242         leaderShardFactory.registerListener(mockedDataTreeListener, Collections.singletonList(TEST_ID),
243                 true, Collections.emptyList());
244
245         verify(mockedDataTreeListener, timeout(1000).times(1)).onDataTreeChanged(captorForChanges.capture(),
246                 captorForSubtrees.capture());
247         final List<Collection<DataTreeCandidate>> capturedValue = captorForChanges.getAllValues();
248
249         final java.util.Optional<NormalizedNode<?, ?>> dataAfter =
250                 capturedValue.get(0).iterator().next().getRootNode().getDataAfter();
251
252         final NormalizedNode<?,?> expected = ImmutableContainerNodeBuilder.create()
253                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).withChild(valueToCheck).build();
254         assertEquals(expected, dataAfter.get());
255
256         verifyNoMoreInteractions(mockedDataTreeListener);
257
258         final String shardName = ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier());
259         LOG.debug("Creating distributed datastore client for shard {}", shardName);
260
261         final ActorContext actorContext = leaderDistributedDataStore.getActorContext();
262         final Props distributedDataStoreClientProps =
263                 SimpleDataStoreClientActor.props(actorContext.getCurrentMemberName(),
264                         "Shard-" + shardName, actorContext, shardName);
265
266         final ActorRef clientActor = leaderSystem.actorOf(distributedDataStoreClientProps);
267         final DataStoreClient distributedDataStoreClient = SimpleDataStoreClientActor
268                     .getDistributedDataStoreClient(clientActor, 30, TimeUnit.SECONDS);
269
270         final ClientLocalHistory localHistory = distributedDataStoreClient.createLocalHistory();
271         final ClientTransaction tx2 = localHistory.createTransaction();
272         final CheckedFuture<Optional<NormalizedNode<?, ?>>,
273                 org.opendaylight.mdsal.common.api.ReadFailedException> read =
274                 tx2.read(YangInstanceIdentifier.EMPTY);
275
276         final Optional<NormalizedNode<?, ?>> optional = read.checkedGet();
277         tx2.abort();
278         localHistory.close();
279
280         shardRegistration.close().toCompletableFuture().get();
281
282     }
283
284     @Test
285     public void testMultipleWritesIntoSingleMapEntry() throws Exception {
286         initEmptyDatastores();
287
288         final DistributedShardRegistration shardRegistration = waitOnAsyncTask(
289                 leaderShardFactory.createDistributedShard(TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME)),
290                 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
291
292         leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(),
293                 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
294
295         LOG.warn("Got after waiting for nonleader");
296         final ActorRef leaderShardManager = leaderDistributedDataStore.getActorContext().getShardManager();
297
298         leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(),
299                 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
300
301         final YangInstanceIdentifier oid1 = getOuterListIdFor(0);
302         final DOMDataTreeIdentifier outerListPath = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1);
303
304         final DistributedShardRegistration outerListShardReg = waitOnAsyncTask(
305                 leaderShardFactory.createDistributedShard(outerListPath, Lists.newArrayList(AbstractTest.MEMBER_NAME)),
306                 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
307
308         leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(),
309                 ClusterUtils.getCleanShardName(outerListPath.getRootIdentifier()));
310
311         final DOMDataTreeProducer shardProducer = leaderShardFactory.createProducer(
312                 Collections.singletonList(outerListPath));
313
314         final DOMDataTreeCursorAwareTransaction tx = shardProducer.createTransaction(false);
315         final DOMDataTreeWriteCursor cursor =
316                 tx.createCursor(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1));
317         assertNotNull(cursor);
318
319         MapNode innerList = ImmutableMapNodeBuilder
320                 .create()
321                 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
322                 .build();
323
324         cursor.write(new NodeIdentifier(TestModel.INNER_LIST_QNAME), innerList);
325         cursor.close();
326         tx.submit().checkedGet();
327
328         final ArrayList<CheckedFuture<Void, TransactionCommitFailedException>> futures = new ArrayList<>();
329         for (int i = 0; i < 1000; i++) {
330             final Collection<MapEntryNode> innerListMapEntries = createInnerListMapEntries(1000, "run-" + i);
331             for (final MapEntryNode innerListMapEntry : innerListMapEntries) {
332                 final DOMDataTreeCursorAwareTransaction tx1 = shardProducer.createTransaction(false);
333                 final DOMDataTreeWriteCursor cursor1 = tx1.createCursor(
334                         new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
335                                 oid1.node(new NodeIdentifier(TestModel.INNER_LIST_QNAME))));
336                 cursor1.write(innerListMapEntry.getIdentifier(), innerListMapEntry);
337                 cursor1.close();
338                 futures.add(tx1.submit());
339             }
340         }
341
342         futures.get(futures.size() - 1).checkedGet();
343
344         final DOMDataTreeListener mockedDataTreeListener = mock(DOMDataTreeListener.class);
345         doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
346
347         leaderShardFactory.registerListener(mockedDataTreeListener, Collections.singletonList(INNER_LIST_ID),
348                 true, Collections.emptyList());
349
350         verify(mockedDataTreeListener, timeout(1000).times(1)).onDataTreeChanged(captorForChanges.capture(),
351                 captorForSubtrees.capture());
352         verifyNoMoreInteractions(mockedDataTreeListener);
353         final List<Collection<DataTreeCandidate>> capturedValue = captorForChanges.getAllValues();
354
355         final NormalizedNode<?,?> expected =
356                 ImmutableMapNodeBuilder
357                         .create()
358                         .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
359                                 // only the values from the last run should be present
360                         .withValue(createInnerListMapEntries(1000, "run-999"))
361                         .build();
362
363         assertEquals("List values dont match the expected values from the last run",
364                 expected, capturedValue.get(0).iterator().next().getRootNode().getDataAfter().get());
365
366     }
367
368     // top level shard at TEST element, with subshards on each outer-list map entry
369     @Test
370     @Ignore
371     public void testMultipleShardLevels() throws Exception {
372         initEmptyDatastores();
373
374         final DistributedShardRegistration testShardReg = waitOnAsyncTask(
375                 leaderShardFactory.createDistributedShard(TEST_ID, SINGLE_MEMBER),
376                 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
377
378         final ArrayList<DistributedShardRegistration> registrations = new ArrayList<>();
379         final int listSize = 5;
380         for (int i = 0; i < listSize; i++) {
381             final YangInstanceIdentifier entryYID = getOuterListIdFor(i);
382             final CompletionStage<DistributedShardRegistration> future = leaderShardFactory.createDistributedShard(
383                     new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, entryYID), SINGLE_MEMBER);
384
385             registrations.add(waitOnAsyncTask(future, DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION));
386         }
387
388         final DOMDataTreeIdentifier rootId =
389                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
390         final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singletonList(
391                 rootId));
392
393         DOMDataTreeCursorAwareTransaction transaction = producer.createTransaction(false);
394
395         DOMDataTreeWriteCursor cursor = transaction.createCursor(rootId);
396         assertNotNull(cursor);
397
398         final MapNode outerList =
399                 ImmutableMapNodeBuilder.create()
400                         .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME)).build();
401
402         final ContainerNode testNode =
403                 ImmutableContainerNodeBuilder.create()
404                         .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
405                         .withChild(outerList)
406                         .build();
407
408         cursor.write(testNode.getIdentifier(), testNode);
409
410         cursor.close();
411         transaction.submit().checkedGet();
412
413         final DOMDataTreeListener mockedDataTreeListener = mock(DOMDataTreeListener.class);
414         doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
415
416         final MapNode wholeList = ImmutableMapNodeBuilder.create(outerList)
417                 .withValue(createOuterEntries(listSize, "testing-values")).build();
418
419         transaction = producer.createTransaction(false);
420         cursor = transaction.createCursor(TEST_ID);
421         assertNotNull(cursor);
422
423         cursor.write(wholeList.getIdentifier(), wholeList);
424         cursor.close();
425
426         transaction.submit().checkedGet();
427
428         leaderShardFactory.registerListener(mockedDataTreeListener, Collections.singletonList(TEST_ID),
429                 true, Collections.emptyList());
430
431         verify(mockedDataTreeListener, timeout(35000).atLeast(2)).onDataTreeChanged(captorForChanges.capture(),
432                 captorForSubtrees.capture());
433         verifyNoMoreInteractions(mockedDataTreeListener);
434         final List<Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>>> allSubtrees = captorForSubtrees.getAllValues();
435
436         final Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> lastSubtree = allSubtrees.get(allSubtrees.size() - 1);
437
438         final NormalizedNode<?, ?> actual = lastSubtree.get(TEST_ID);
439         assertNotNull(actual);
440
441         final NormalizedNode<?, ?> expected =
442                 ImmutableContainerNodeBuilder.create()
443                         .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
444                         .withChild(ImmutableMapNodeBuilder.create(outerList)
445                                 .withValue(createOuterEntries(listSize, "testing-values")).build())
446                         .build();
447
448
449         for (final DistributedShardRegistration registration : registrations) {
450             waitOnAsyncTask(registration.close(), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
451         }
452
453         waitOnAsyncTask(testShardReg.close(), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
454
455         assertEquals(expected, actual);
456     }
457
458     @Test
459     public void testMultipleRegistrationsAtOnePrefix() throws Exception {
460         initEmptyDatastores();
461
462         for (int i = 0; i < 10; i++) {
463             LOG.debug("Round {}", i);
464             final DistributedShardRegistration reg1 = waitOnAsyncTask(leaderShardFactory.createDistributedShard(
465                     TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME)),
466                     DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
467
468             leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(),
469                     ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
470
471             assertNotNull(findLocalShard(leaderDistributedDataStore.getActorContext(),
472                     ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
473
474             waitOnAsyncTask(reg1.close(), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
475
476             waitUntilShardIsDown(leaderDistributedDataStore.getActorContext(),
477                     ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
478         }
479     }
480
481     @Test
482     public void testCDSDataTreeProducer() throws Exception {
483         initEmptyDatastores();
484
485         final DistributedShardRegistration reg1 = waitOnAsyncTask(leaderShardFactory.createDistributedShard(
486                 TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME)),
487                 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
488
489         leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(),
490                 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
491
492         assertNotNull(findLocalShard(leaderDistributedDataStore.getActorContext(),
493                 ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
494
495
496         final DOMDataTreeIdentifier configRoot =
497                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
498         final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singleton(configRoot));
499
500         assertTrue(producer instanceof CDSDataTreeProducer);
501
502         final CDSDataTreeProducer cdsProducer = (CDSDataTreeProducer) producer;
503         CDSShardAccess shardAccess = cdsProducer.getShardAccess(TEST_ID);
504         assertEquals(shardAccess.getShardIdentifier(), TEST_ID);
505
506         shardAccess = cdsProducer.getShardAccess(INNER_LIST_ID);
507         assertEquals(TEST_ID, shardAccess.getShardIdentifier());
508
509         shardAccess = cdsProducer.getShardAccess(configRoot);
510         assertEquals(configRoot, shardAccess.getShardIdentifier());
511
512         waitOnAsyncTask(reg1.close(), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
513     }
514
515     private static Collection<MapEntryNode> createOuterEntries(final int amount, final String valuePrefix) {
516         final Collection<MapEntryNode> ret = new ArrayList<>();
517         for (int i = 0; i < amount; i++) {
518             ret.add(ImmutableNodes.mapEntryBuilder()
519                     .withNodeIdentifier(new NodeIdentifierWithPredicates(TestModel.OUTER_LIST_QNAME,
520                             QName.create(TestModel.OUTER_LIST_QNAME, "id"), i))
521                     .withChild(ImmutableNodes
522                             .leafNode(QName.create(TestModel.OUTER_LIST_QNAME, "id"), i))
523                     .withChild(createWholeInnerList(amount, "outer id: " + i + " " + valuePrefix))
524                     .build());
525         }
526
527         return ret;
528     }
529
530     private static MapNode createWholeInnerList(final int amount, final String valuePrefix) {
531         return ImmutableMapNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
532                 .withValue(createInnerListMapEntries(amount, valuePrefix)).build();
533     }
534
535     private static Collection<MapEntryNode> createInnerListMapEntries(final int amount, final String valuePrefix) {
536         final Collection<MapEntryNode> ret = new ArrayList<>();
537         for (int i = 0; i < amount; i++) {
538             ret.add(ImmutableNodes.mapEntryBuilder()
539                     .withNodeIdentifier(new NodeIdentifierWithPredicates(TestModel.INNER_LIST_QNAME,
540                             QName.create(TestModel.INNER_LIST_QNAME, "name"), Integer.toString(i)))
541                     .withChild(ImmutableNodes
542                             .leafNode(QName.create(TestModel.INNER_LIST_QNAME, "value"), valuePrefix + "-" + i))
543                     .build());
544         }
545
546         return ret;
547     }
548
549     private static YangInstanceIdentifier getOuterListIdFor(final int id) {
550         return TestModel.OUTER_LIST_PATH.node(new NodeIdentifierWithPredicates(
551                 TestModel.OUTER_LIST_QNAME, QName.create(TestModel.OUTER_LIST_QNAME, "id"), id));
552     }
553 }