2 * Copyright (c) 2016, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.sharding;
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;
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.ActorContext;
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;
53 public class DistributedShardFrontendTest {
55 private static final DOMDataTreeIdentifier ROOT =
56 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
57 private static final ListenableFuture<Object> SUCCESS_FUTURE = Futures.immediateFuture(null);
59 private ShardedDOMDataTree shardedDOMDataTree;
61 private DataStoreClient client;
62 private ClientLocalHistory clientHistory;
63 private ClientTransaction clientTransaction;
64 private DOMDataTreeWriteCursor cursor;
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);
72 private ArgumentCaptor<YangInstanceIdentifier.PathArgument> pathArgumentCaptor;
74 private ArgumentCaptor<NormalizedNode<?, ?>> nodeCaptor;
76 private DOMStoreThreePhaseCommitCohort commitCohort;
79 public void setUp() throws Exception {
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);
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();
93 doReturn(clientTransaction).when(client).createTransaction();
94 doReturn(clientTransaction).when(clientHistory).createTransaction();
95 doNothing().when(clientHistory).close();
97 doNothing().when(client).close();
98 doReturn(clientHistory).when(client).createLocalHistory();
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());
106 doReturn(commitCohort).when(clientTransaction).ready();
110 public void testClientTransaction() throws Exception {
112 final DistributedDataStore distributedDataStore = mock(DistributedDataStore.class);
113 final ActorContext context = mock(ActorContext.class);
114 doReturn(context).when(distributedDataStore).getActorContext();
115 doReturn(SchemaContextHelper.full()).when(context).getSchemaContext();
117 final DistributedShardFrontend rootShard = new DistributedShardFrontend(distributedDataStore, client, ROOT);
119 try (DOMDataTreeProducer producer = shardedDOMDataTree.createProducer(Collections.singletonList(ROOT))) {
120 shardedDOMDataTree.registerDataTreeShard(ROOT, rootShard, producer);
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);
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());
133 doReturn(outerListCursor).when(outerListClientTransaction).openCursor();
134 doReturn(outerListClientTransaction).when(outerListClient).createTransaction();
135 doReturn(outerListClientHistory).when(outerListClient).createLocalHistory();
136 doReturn(outerListClientTransaction).when(outerListClientHistory).createTransaction();
138 doReturn(commitCohort).when(outerListClientTransaction).ready();
140 doNothing().when(outerListClientHistory).close();
141 doNothing().when(outerListClient).close();
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);
150 final DOMDataTreeProducer producer = shardedDOMDataTree.createProducer(Collections.singletonList(ROOT));
151 final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
152 final DOMDataTreeWriteCursor cursor = tx.createCursor(ROOT);
154 assertNotNull(cursor);
155 cursor.write(TestModel.TEST_PATH.getLastPathArgument(), createCrossShardContainer());
157 //check the lower shard got the correct modification
158 verify(outerListCursor, times(2)).write(pathArgumentCaptor.capture(), nodeCaptor.capture());
160 final YangInstanceIdentifier.PathArgument expectedYid = new NodeIdentifier(TestModel.ID_QNAME);
161 final YangInstanceIdentifier.PathArgument actualIdYid = pathArgumentCaptor.getAllValues().get(0);
162 assertEquals(expectedYid, actualIdYid);
164 final YangInstanceIdentifier.PathArgument expectedInnerYid = new NodeIdentifier(TestModel.INNER_LIST_QNAME);
165 final YangInstanceIdentifier.PathArgument actualInnerListYid = pathArgumentCaptor.getAllValues().get(1);
166 assertEquals(expectedInnerYid, actualInnerListYid);
168 final LeafNode<Integer> actualIdNode = (LeafNode<Integer>) nodeCaptor.getAllValues().get(0);
169 assertEquals(ImmutableNodes.leafNode(TestModel.ID_QNAME, 1), actualIdNode);
171 final MapNode actualInnerListNode = (MapNode) nodeCaptor.getAllValues().get(1);
172 assertEquals(createInnerMapNode(1), actualInnerListNode);
175 tx.submit().checkedGet();
177 verify(commitCohort, times(2)).canCommit();
178 verify(commitCohort, times(2)).preCommit();
179 verify(commitCohort, times(2)).commit();
183 private static MapNode createInnerMapNode(final int id) {
184 final MapEntryNode listEntry = ImmutableNodes
185 .mapEntryBuilder(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "name-" + id)
186 .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, "name-" + id))
187 .withChild(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "value-" + id))
190 return ImmutableNodes.mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(listEntry).build();
193 private static ContainerNode createCrossShardContainer() {
195 final MapEntryNode outerListEntry1 =
196 ImmutableNodes.mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1)
197 .withChild(createInnerMapNode(1))
199 final MapEntryNode outerListEntry2 =
200 ImmutableNodes.mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2)
201 .withChild(createInnerMapNode(2))
204 final MapNode outerList = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
205 .withChild(outerListEntry1)
206 .withChild(outerListEntry2)
209 final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
210 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
211 .withChild(outerList)
214 return testContainer;