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