Migrate from YangInstanceIdentifier.EMPTY
[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.ArgumentMatchers.anyCollection;
15 import static org.mockito.ArgumentMatchers.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.collect.Lists;
33 import com.google.common.util.concurrent.FluentFuture;
34 import com.google.common.util.concurrent.ListenableFuture;
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.Optional;
42 import java.util.Set;
43 import java.util.concurrent.CompletionStage;
44 import java.util.concurrent.TimeUnit;
45 import org.junit.After;
46 import org.junit.Assert;
47 import org.junit.Before;
48 import org.junit.Ignore;
49 import org.junit.Test;
50 import org.mockito.ArgumentCaptor;
51 import org.mockito.Captor;
52 import org.mockito.Mockito;
53 import org.mockito.MockitoAnnotations;
54 import org.opendaylight.controller.cluster.ActorSystemProvider;
55 import org.opendaylight.controller.cluster.access.concepts.MemberName;
56 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory;
57 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
58 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
59 import org.opendaylight.controller.cluster.databroker.actors.dds.SimpleDataStoreClientActor;
60 import org.opendaylight.controller.cluster.datastore.AbstractTest;
61 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
62 import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder;
63 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
64 import org.opendaylight.controller.cluster.datastore.IntegrationTestKit;
65 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
66 import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
67 import org.opendaylight.controller.cluster.dom.api.CDSDataTreeProducer;
68 import org.opendaylight.controller.cluster.dom.api.CDSShardAccess;
69 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
70 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
71 import org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration;
72 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
73 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
74 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
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() {
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.commit().get();
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.getActorUtils(),
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.commit().get();
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 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 ActorUtils actorUtils = leaderDistributedDataStore.getActorUtils();
262         final Props distributedDataStoreClientProps =
263                 SimpleDataStoreClientActor.props(actorUtils.getCurrentMemberName(), "Shard-" + shardName, actorUtils,
264                     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 FluentFuture<Optional<NormalizedNode<?, ?>>> read = tx2.read(YangInstanceIdentifier.empty());
273
274         final Optional<NormalizedNode<?, ?>> optional = read.get();
275         tx2.abort();
276         localHistory.close();
277
278         shardRegistration.close().toCompletableFuture().get();
279
280     }
281
282     @Test
283     public void testMultipleWritesIntoSingleMapEntry() throws Exception {
284         initEmptyDatastores();
285
286         final DistributedShardRegistration shardRegistration = waitOnAsyncTask(
287                 leaderShardFactory.createDistributedShard(TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME)),
288                 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
289
290         leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorUtils(),
291                 ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
292
293         LOG.warn("Got after waiting for nonleader");
294         final ActorRef leaderShardManager = leaderDistributedDataStore.getActorUtils().getShardManager();
295
296         leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorUtils(),
297                 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
298
299         final YangInstanceIdentifier oid1 = getOuterListIdFor(0);
300         final DOMDataTreeIdentifier outerListPath = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1);
301
302         final DistributedShardRegistration outerListShardReg = waitOnAsyncTask(
303                 leaderShardFactory.createDistributedShard(outerListPath, Lists.newArrayList(AbstractTest.MEMBER_NAME)),
304                 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
305
306         leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorUtils(),
307                 ClusterUtils.getCleanShardName(outerListPath.getRootIdentifier()));
308
309         final DOMDataTreeProducer shardProducer = leaderShardFactory.createProducer(
310                 Collections.singletonList(outerListPath));
311
312         final DOMDataTreeCursorAwareTransaction tx = shardProducer.createTransaction(false);
313         final DOMDataTreeWriteCursor cursor =
314                 tx.createCursor(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1));
315         assertNotNull(cursor);
316
317         MapNode innerList = ImmutableMapNodeBuilder
318                 .create()
319                 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
320                 .build();
321
322         cursor.write(new NodeIdentifier(TestModel.INNER_LIST_QNAME), innerList);
323         cursor.close();
324         tx.commit().get();
325
326         final ArrayList<ListenableFuture<?>> futures = new ArrayList<>();
327         for (int i = 0; i < 1000; i++) {
328             final Collection<MapEntryNode> innerListMapEntries = createInnerListMapEntries(1000, "run-" + i);
329             for (final MapEntryNode innerListMapEntry : innerListMapEntries) {
330                 final DOMDataTreeCursorAwareTransaction tx1 = shardProducer.createTransaction(false);
331                 final DOMDataTreeWriteCursor cursor1 = tx1.createCursor(
332                         new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
333                                 oid1.node(new NodeIdentifier(TestModel.INNER_LIST_QNAME))));
334                 cursor1.write(innerListMapEntry.getIdentifier(), innerListMapEntry);
335                 cursor1.close();
336                 futures.add(tx1.commit());
337             }
338         }
339
340         futures.get(futures.size() - 1).get();
341
342         final DOMDataTreeListener mockedDataTreeListener = mock(DOMDataTreeListener.class);
343         doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
344
345         leaderShardFactory.registerListener(mockedDataTreeListener, Collections.singletonList(INNER_LIST_ID),
346                 true, Collections.emptyList());
347
348         verify(mockedDataTreeListener, timeout(1000).times(1)).onDataTreeChanged(captorForChanges.capture(),
349                 captorForSubtrees.capture());
350         verifyNoMoreInteractions(mockedDataTreeListener);
351         final List<Collection<DataTreeCandidate>> capturedValue = captorForChanges.getAllValues();
352
353         final NormalizedNode<?,?> expected =
354                 ImmutableMapNodeBuilder
355                         .create()
356                         .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
357                                 // only the values from the last run should be present
358                         .withValue(createInnerListMapEntries(1000, "run-999"))
359                         .build();
360
361         assertEquals("List values dont match the expected values from the last run",
362                 expected, capturedValue.get(0).iterator().next().getRootNode().getDataAfter().get());
363
364     }
365
366     // top level shard at TEST element, with subshards on each outer-list map entry
367     @Test
368     @Ignore
369     public void testMultipleShardLevels() throws Exception {
370         initEmptyDatastores();
371
372         final DistributedShardRegistration testShardReg = waitOnAsyncTask(
373                 leaderShardFactory.createDistributedShard(TEST_ID, SINGLE_MEMBER),
374                 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
375
376         final ArrayList<DistributedShardRegistration> registrations = new ArrayList<>();
377         final int listSize = 5;
378         for (int i = 0; i < listSize; i++) {
379             final YangInstanceIdentifier entryYID = getOuterListIdFor(i);
380             final CompletionStage<DistributedShardRegistration> future = leaderShardFactory.createDistributedShard(
381                     new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, entryYID), SINGLE_MEMBER);
382
383             registrations.add(waitOnAsyncTask(future, DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION));
384         }
385
386         final DOMDataTreeIdentifier rootId =
387                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty());
388         final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singletonList(
389                 rootId));
390
391         DOMDataTreeCursorAwareTransaction transaction = producer.createTransaction(false);
392
393         DOMDataTreeWriteCursor cursor = transaction.createCursor(rootId);
394         assertNotNull(cursor);
395
396         final MapNode outerList =
397                 ImmutableMapNodeBuilder.create()
398                         .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME)).build();
399
400         final ContainerNode testNode =
401                 ImmutableContainerNodeBuilder.create()
402                         .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
403                         .withChild(outerList)
404                         .build();
405
406         cursor.write(testNode.getIdentifier(), testNode);
407
408         cursor.close();
409         transaction.commit().get();
410
411         final DOMDataTreeListener mockedDataTreeListener = mock(DOMDataTreeListener.class);
412         doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
413
414         final MapNode wholeList = ImmutableMapNodeBuilder.create(outerList)
415                 .withValue(createOuterEntries(listSize, "testing-values")).build();
416
417         transaction = producer.createTransaction(false);
418         cursor = transaction.createCursor(TEST_ID);
419         assertNotNull(cursor);
420
421         cursor.write(wholeList.getIdentifier(), wholeList);
422         cursor.close();
423
424         transaction.commit().get();
425
426         leaderShardFactory.registerListener(mockedDataTreeListener, Collections.singletonList(TEST_ID),
427                 true, Collections.emptyList());
428
429         verify(mockedDataTreeListener, timeout(35000).atLeast(2)).onDataTreeChanged(captorForChanges.capture(),
430                 captorForSubtrees.capture());
431         verifyNoMoreInteractions(mockedDataTreeListener);
432         final List<Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>>> allSubtrees = captorForSubtrees.getAllValues();
433
434         final Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> lastSubtree = allSubtrees.get(allSubtrees.size() - 1);
435
436         final NormalizedNode<?, ?> actual = lastSubtree.get(TEST_ID);
437         assertNotNull(actual);
438
439         final NormalizedNode<?, ?> expected =
440                 ImmutableContainerNodeBuilder.create()
441                         .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
442                         .withChild(ImmutableMapNodeBuilder.create(outerList)
443                                 .withValue(createOuterEntries(listSize, "testing-values")).build())
444                         .build();
445
446
447         for (final DistributedShardRegistration registration : registrations) {
448             waitOnAsyncTask(registration.close(), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
449         }
450
451         waitOnAsyncTask(testShardReg.close(), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
452
453         assertEquals(expected, actual);
454     }
455
456     @Test
457     public void testMultipleRegistrationsAtOnePrefix() throws Exception {
458         initEmptyDatastores();
459
460         for (int i = 0; i < 10; i++) {
461             LOG.debug("Round {}", i);
462             final DistributedShardRegistration reg1 = waitOnAsyncTask(leaderShardFactory.createDistributedShard(
463                     TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME)),
464                     DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
465
466             leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorUtils(),
467                     ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
468
469             assertNotNull(findLocalShard(leaderDistributedDataStore.getActorUtils(),
470                     ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
471
472             waitOnAsyncTask(reg1.close(), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
473
474             waitUntilShardIsDown(leaderDistributedDataStore.getActorUtils(),
475                     ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
476         }
477     }
478
479     @Test
480     public void testCDSDataTreeProducer() throws Exception {
481         initEmptyDatastores();
482
483         final DistributedShardRegistration reg1 = waitOnAsyncTask(leaderShardFactory.createDistributedShard(
484                 TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME)),
485                 DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
486
487         leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorUtils(),
488                 ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
489
490         assertNotNull(findLocalShard(leaderDistributedDataStore.getActorUtils(),
491                 ClusterUtils.getCleanShardName(TestModel.TEST_PATH)));
492
493
494         final DOMDataTreeIdentifier configRoot =
495                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty());
496         final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singleton(configRoot));
497
498         assertTrue(producer instanceof CDSDataTreeProducer);
499
500         final CDSDataTreeProducer cdsProducer = (CDSDataTreeProducer) producer;
501         CDSShardAccess shardAccess = cdsProducer.getShardAccess(TEST_ID);
502         assertEquals(shardAccess.getShardIdentifier(), TEST_ID);
503
504         shardAccess = cdsProducer.getShardAccess(INNER_LIST_ID);
505         assertEquals(TEST_ID, shardAccess.getShardIdentifier());
506
507         shardAccess = cdsProducer.getShardAccess(configRoot);
508         assertEquals(configRoot, shardAccess.getShardIdentifier());
509
510         waitOnAsyncTask(reg1.close(), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
511     }
512
513     private static Collection<MapEntryNode> createOuterEntries(final int amount, final String valuePrefix) {
514         final Collection<MapEntryNode> ret = new ArrayList<>();
515         for (int i = 0; i < amount; i++) {
516             ret.add(ImmutableNodes.mapEntryBuilder()
517                     .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME,
518                             QName.create(TestModel.OUTER_LIST_QNAME, "id"), i))
519                     .withChild(ImmutableNodes
520                             .leafNode(QName.create(TestModel.OUTER_LIST_QNAME, "id"), i))
521                     .withChild(createWholeInnerList(amount, "outer id: " + i + " " + valuePrefix))
522                     .build());
523         }
524
525         return ret;
526     }
527
528     private static MapNode createWholeInnerList(final int amount, final String valuePrefix) {
529         return ImmutableMapNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
530                 .withValue(createInnerListMapEntries(amount, valuePrefix)).build();
531     }
532
533     private static Collection<MapEntryNode> createInnerListMapEntries(final int amount, final String valuePrefix) {
534         final Collection<MapEntryNode> ret = new ArrayList<>();
535         for (int i = 0; i < amount; i++) {
536             ret.add(ImmutableNodes.mapEntryBuilder()
537                     .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.INNER_LIST_QNAME,
538                             QName.create(TestModel.INNER_LIST_QNAME, "name"), Integer.toString(i)))
539                     .withChild(ImmutableNodes
540                             .leafNode(QName.create(TestModel.INNER_LIST_QNAME, "value"), valuePrefix + "-" + i))
541                     .build());
542         }
543
544         return ret;
545     }
546
547     private static YangInstanceIdentifier getOuterListIdFor(final int id) {
548         return TestModel.OUTER_LIST_PATH.node(NodeIdentifierWithPredicates.of(
549                 TestModel.OUTER_LIST_QNAME, QName.create(TestModel.OUTER_LIST_QNAME, "id"), id));
550     }
551 }