Bump versions to 4.0.0-SNAPSHOT
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / sharding / DistributedShardFrontendTest.java
1 /*
2  * Copyright (c) 2016, 2017 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.controller.cluster.sharding;
9
10 import static org.hamcrest.CoreMatchers.hasItems;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
20
21 import com.google.common.util.concurrent.Futures;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import java.util.Collections;
24 import java.util.List;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Captor;
29 import org.mockito.MockitoAnnotations;
30 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory;
31 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
32 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
33 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
34 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
35 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
36 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
37 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
38 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
39 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
40 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
41 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
42 import org.opendaylight.mdsal.dom.broker.ShardedDOMDataTree;
43 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
48 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
52 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
53 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
54
55 @Deprecated(forRemoval = true)
56 public class DistributedShardFrontendTest {
57
58     private static final DOMDataTreeIdentifier ROOT =
59             new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty());
60     private static final ListenableFuture<Object> SUCCESS_FUTURE = Futures.immediateFuture(null);
61
62     private ShardedDOMDataTree shardedDOMDataTree;
63
64     private DataStoreClient client;
65     private ClientLocalHistory clientHistory;
66     private ClientTransaction clientTransaction;
67     private DOMDataTreeWriteCursor cursor;
68
69     private static final YangInstanceIdentifier OUTER_LIST_YID = TestModel.OUTER_LIST_PATH.node(
70             NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
71     private static final DOMDataTreeIdentifier OUTER_LIST_ID =
72             new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, OUTER_LIST_YID);
73
74     @Captor
75     private ArgumentCaptor<PathArgument> pathArgumentCaptor;
76     @Captor
77     private ArgumentCaptor<NormalizedNode<?, ?>> nodeCaptor;
78
79     private DOMStoreThreePhaseCommitCohort commitCohort;
80
81     @Before
82     public void setUp() {
83         MockitoAnnotations.initMocks(this);
84         shardedDOMDataTree = new ShardedDOMDataTree();
85         client = mock(DataStoreClient.class);
86         cursor = mock(DOMDataTreeWriteCursor.class);
87         clientTransaction = mock(ClientTransaction.class);
88         clientHistory = mock(ClientLocalHistory.class);
89         commitCohort = mock(DOMStoreThreePhaseCommitCohort.class);
90
91         doReturn(SUCCESS_FUTURE).when(commitCohort).canCommit();
92         doReturn(SUCCESS_FUTURE).when(commitCohort).preCommit();
93         doReturn(SUCCESS_FUTURE).when(commitCohort).commit();
94         doReturn(SUCCESS_FUTURE).when(commitCohort).abort();
95
96         doReturn(clientTransaction).when(client).createTransaction();
97         doReturn(clientTransaction).when(clientHistory).createTransaction();
98         doNothing().when(clientHistory).close();
99
100         doNothing().when(client).close();
101         doReturn(clientHistory).when(client).createLocalHistory();
102
103         doReturn(cursor).when(clientTransaction).openCursor();
104         doNothing().when(cursor).close();
105         doNothing().when(cursor).write(any(), any());
106         doNothing().when(cursor).merge(any(), any());
107         doNothing().when(cursor).delete(any());
108
109         doReturn(commitCohort).when(clientTransaction).ready();
110     }
111
112     @Test
113     public void testClientTransaction() throws Exception {
114         final DistributedDataStore distributedDataStore = mock(DistributedDataStore.class);
115         final ActorUtils context = mock(ActorUtils.class);
116         doReturn(context).when(distributedDataStore).getActorUtils();
117         doReturn(SchemaContextHelper.full()).when(context).getSchemaContext();
118
119         final DistributedShardFrontend rootShard = new DistributedShardFrontend(distributedDataStore, client, ROOT);
120
121         try (DOMDataTreeProducer producer = shardedDOMDataTree.createProducer(Collections.singletonList(ROOT))) {
122             shardedDOMDataTree.registerDataTreeShard(ROOT, rootShard, producer);
123         }
124
125         final DataStoreClient outerListClient = mock(DataStoreClient.class);
126         final ClientTransaction outerListClientTransaction = mock(ClientTransaction.class);
127         final ClientLocalHistory outerListClientHistory = mock(ClientLocalHistory.class);
128         final DOMDataTreeWriteCursor outerListCursor = mock(DOMDataTreeWriteCursor.class);
129
130         doNothing().when(outerListCursor).close();
131         doNothing().when(outerListCursor).write(any(), any());
132         doNothing().when(outerListCursor).merge(any(), any());
133         doNothing().when(outerListCursor).delete(any());
134
135         doReturn(outerListCursor).when(outerListClientTransaction).openCursor();
136         doReturn(outerListClientTransaction).when(outerListClient).createTransaction();
137         doReturn(outerListClientHistory).when(outerListClient).createLocalHistory();
138         doReturn(outerListClientTransaction).when(outerListClientHistory).createTransaction();
139
140         doReturn(commitCohort).when(outerListClientTransaction).ready();
141
142         doNothing().when(outerListClientHistory).close();
143         doNothing().when(outerListClient).close();
144
145         final DistributedShardFrontend outerListShard = new DistributedShardFrontend(
146                 distributedDataStore, outerListClient, OUTER_LIST_ID);
147         try (DOMDataTreeProducer producer =
148                      shardedDOMDataTree.createProducer(Collections.singletonList(OUTER_LIST_ID))) {
149             shardedDOMDataTree.registerDataTreeShard(OUTER_LIST_ID, outerListShard, producer);
150         }
151
152         final DOMDataTreeProducer producer = shardedDOMDataTree.createProducer(Collections.singletonList(ROOT));
153         final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
154         final DOMDataTreeWriteCursor txCursor = tx.createCursor(ROOT);
155
156         assertNotNull(txCursor);
157         txCursor.write(TestModel.TEST_PATH.getLastPathArgument(), createCrossShardContainer());
158
159         //check the lower shard got the correct modification
160         verify(outerListCursor, times(2)).write(pathArgumentCaptor.capture(), nodeCaptor.capture());
161
162         final List<PathArgument> capturedArgs = pathArgumentCaptor.getAllValues();
163         assertEquals(2, capturedArgs.size());
164         assertThat(capturedArgs,
165             hasItems(new NodeIdentifier(TestModel.ID_QNAME), new NodeIdentifier(TestModel.INNER_LIST_QNAME)));
166
167         final List<NormalizedNode<?, ?>> capturedValues = nodeCaptor.getAllValues();
168         assertEquals(2, capturedValues.size());
169         assertThat(capturedValues,
170             hasItems(ImmutableNodes.leafNode(TestModel.ID_QNAME, 1), createInnerMapNode(1)));
171
172         txCursor.close();
173         tx.commit().get();
174
175         verify(commitCohort, times(2)).canCommit();
176         verify(commitCohort, times(2)).preCommit();
177         verify(commitCohort, times(2)).commit();
178     }
179
180     private static MapNode createInnerMapNode(final int id) {
181         final MapEntryNode listEntry = ImmutableNodes
182                 .mapEntryBuilder(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "name-" + id)
183                 .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, "name-" + id))
184                 .withChild(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "value-" + id))
185                 .build();
186
187         return ImmutableNodes.mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(listEntry).build();
188     }
189
190     private static ContainerNode createCrossShardContainer() {
191
192         final MapEntryNode outerListEntry1 =
193                 ImmutableNodes.mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1)
194                         .withChild(createInnerMapNode(1))
195                         .build();
196         final MapEntryNode outerListEntry2 =
197                 ImmutableNodes.mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2)
198                         .withChild(createInnerMapNode(2))
199                         .build();
200
201         final MapNode outerList = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
202                 .withChild(outerListEntry1)
203                 .withChild(outerListEntry2)
204                 .build();
205
206         final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
207                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
208                 .withChild(outerList)
209                 .build();
210
211         return testContainer;
212     }
213 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.