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