795705ea67fabb942e95f501dfa72bbeab27fd62
[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 public class DistributedShardFrontendTest {
56
57     private static final DOMDataTreeIdentifier ROOT =
58             new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty());
59     private static final ListenableFuture<Object> SUCCESS_FUTURE = Futures.immediateFuture(null);
60
61     private ShardedDOMDataTree shardedDOMDataTree;
62
63     private DataStoreClient client;
64     private ClientLocalHistory clientHistory;
65     private ClientTransaction clientTransaction;
66     private DOMDataTreeWriteCursor cursor;
67
68     private static final YangInstanceIdentifier OUTER_LIST_YID = TestModel.OUTER_LIST_PATH.node(
69             NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
70     private static final DOMDataTreeIdentifier OUTER_LIST_ID =
71             new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, OUTER_LIST_YID);
72
73     @Captor
74     private ArgumentCaptor<PathArgument> pathArgumentCaptor;
75     @Captor
76     private ArgumentCaptor<NormalizedNode<?, ?>> nodeCaptor;
77
78     private DOMStoreThreePhaseCommitCohort commitCohort;
79
80     @Before
81     public void setUp() {
82         MockitoAnnotations.initMocks(this);
83         shardedDOMDataTree = new ShardedDOMDataTree();
84         client = mock(DataStoreClient.class);
85         cursor = mock(DOMDataTreeWriteCursor.class);
86         clientTransaction = mock(ClientTransaction.class);
87         clientHistory = mock(ClientLocalHistory.class);
88         commitCohort = mock(DOMStoreThreePhaseCommitCohort.class);
89
90         doReturn(SUCCESS_FUTURE).when(commitCohort).canCommit();
91         doReturn(SUCCESS_FUTURE).when(commitCohort).preCommit();
92         doReturn(SUCCESS_FUTURE).when(commitCohort).commit();
93         doReturn(SUCCESS_FUTURE).when(commitCohort).abort();
94
95         doReturn(clientTransaction).when(client).createTransaction();
96         doReturn(clientTransaction).when(clientHistory).createTransaction();
97         doNothing().when(clientHistory).close();
98
99         doNothing().when(client).close();
100         doReturn(clientHistory).when(client).createLocalHistory();
101
102         doReturn(cursor).when(clientTransaction).openCursor();
103         doNothing().when(cursor).close();
104         doNothing().when(cursor).write(any(), any());
105         doNothing().when(cursor).merge(any(), any());
106         doNothing().when(cursor).delete(any());
107
108         doReturn(commitCohort).when(clientTransaction).ready();
109     }
110
111     @Test
112     public void testClientTransaction() throws Exception {
113         final DistributedDataStore distributedDataStore = mock(DistributedDataStore.class);
114         final ActorUtils context = mock(ActorUtils.class);
115         doReturn(context).when(distributedDataStore).getActorUtils();
116         doReturn(SchemaContextHelper.full()).when(context).getSchemaContext();
117
118         final DistributedShardFrontend rootShard = new DistributedShardFrontend(distributedDataStore, client, ROOT);
119
120         try (DOMDataTreeProducer producer = shardedDOMDataTree.createProducer(Collections.singletonList(ROOT))) {
121             shardedDOMDataTree.registerDataTreeShard(ROOT, rootShard, producer);
122         }
123
124         final DataStoreClient outerListClient = mock(DataStoreClient.class);
125         final ClientTransaction outerListClientTransaction = mock(ClientTransaction.class);
126         final ClientLocalHistory outerListClientHistory = mock(ClientLocalHistory.class);
127         final DOMDataTreeWriteCursor outerListCursor = mock(DOMDataTreeWriteCursor.class);
128
129         doNothing().when(outerListCursor).close();
130         doNothing().when(outerListCursor).write(any(), any());
131         doNothing().when(outerListCursor).merge(any(), any());
132         doNothing().when(outerListCursor).delete(any());
133
134         doReturn(outerListCursor).when(outerListClientTransaction).openCursor();
135         doReturn(outerListClientTransaction).when(outerListClient).createTransaction();
136         doReturn(outerListClientHistory).when(outerListClient).createLocalHistory();
137         doReturn(outerListClientTransaction).when(outerListClientHistory).createTransaction();
138
139         doReturn(commitCohort).when(outerListClientTransaction).ready();
140
141         doNothing().when(outerListClientHistory).close();
142         doNothing().when(outerListClient).close();
143
144         final DistributedShardFrontend outerListShard = new DistributedShardFrontend(
145                 distributedDataStore, outerListClient, OUTER_LIST_ID);
146         try (DOMDataTreeProducer producer =
147                      shardedDOMDataTree.createProducer(Collections.singletonList(OUTER_LIST_ID))) {
148             shardedDOMDataTree.registerDataTreeShard(OUTER_LIST_ID, outerListShard, producer);
149         }
150
151         final DOMDataTreeProducer producer = shardedDOMDataTree.createProducer(Collections.singletonList(ROOT));
152         final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
153         final DOMDataTreeWriteCursor txCursor = tx.createCursor(ROOT);
154
155         assertNotNull(txCursor);
156         txCursor.write(TestModel.TEST_PATH.getLastPathArgument(), createCrossShardContainer());
157
158         //check the lower shard got the correct modification
159         verify(outerListCursor, times(2)).write(pathArgumentCaptor.capture(), nodeCaptor.capture());
160
161         final List<PathArgument> capturedArgs = pathArgumentCaptor.getAllValues();
162         assertEquals(2, capturedArgs.size());
163         assertThat(capturedArgs,
164             hasItems(new NodeIdentifier(TestModel.ID_QNAME), new NodeIdentifier(TestModel.INNER_LIST_QNAME)));
165
166         final List<NormalizedNode<?, ?>> capturedValues = nodeCaptor.getAllValues();
167         assertEquals(2, capturedValues.size());
168         assertThat(capturedValues,
169             hasItems(ImmutableNodes.leafNode(TestModel.ID_QNAME, 1), createInnerMapNode(1)));
170
171         txCursor.close();
172         tx.commit().get();
173
174         verify(commitCohort, times(2)).canCommit();
175         verify(commitCohort, times(2)).preCommit();
176         verify(commitCohort, times(2)).commit();
177     }
178
179     private static MapNode createInnerMapNode(final int id) {
180         final MapEntryNode listEntry = ImmutableNodes
181                 .mapEntryBuilder(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "name-" + id)
182                 .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, "name-" + id))
183                 .withChild(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "value-" + id))
184                 .build();
185
186         return ImmutableNodes.mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(listEntry).build();
187     }
188
189     private static ContainerNode createCrossShardContainer() {
190
191         final MapEntryNode outerListEntry1 =
192                 ImmutableNodes.mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1)
193                         .withChild(createInnerMapNode(1))
194                         .build();
195         final MapEntryNode outerListEntry2 =
196                 ImmutableNodes.mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2)
197                         .withChild(createInnerMapNode(2))
198                         .build();
199
200         final MapNode outerList = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
201                 .withChild(outerListEntry1)
202                 .withChild(outerListEntry2)
203                 .build();
204
205         final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
206                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
207                 .withChild(outerList)
208                 .build();
209
210         return testContainer;
211     }
212 }