441a4caaa3a708432233650c051625ac80843a60
[netconf.git] / netconf / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / tx / ReadWriteTransactionTest.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.netconf.topology.singleton.impl.tx;
10
11 import static junit.framework.TestCase.assertNull;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
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.timeout;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.MockitoAnnotations.initMocks;
20 import static org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
21
22 import akka.actor.ActorRef;
23 import akka.actor.ActorSystem;
24 import akka.actor.Props;
25 import akka.pattern.Patterns;
26 import akka.testkit.JavaTestKit;
27 import akka.testkit.TestActorRef;
28 import akka.util.Timeout;
29 import com.google.common.base.Optional;
30 import com.google.common.collect.Lists;
31 import com.google.common.util.concurrent.CheckedFuture;
32 import com.google.common.util.concurrent.Futures;
33 import java.net.InetAddress;
34 import java.net.InetSocketAddress;
35 import java.util.List;
36 import java.util.concurrent.TimeUnit;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.mockito.Mock;
43 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
44 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
45 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
46 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
47 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
48 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
49 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
50 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
51 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
52 import org.opendaylight.netconf.topology.singleton.impl.ProxyDOMDataBroker;
53 import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor;
54 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
55 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
56 import org.opendaylight.netconf.topology.singleton.messages.MasterActorDataInitialized;
57 import org.opendaylight.yangtools.yang.common.QName;
58 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
59 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
60 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
61 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
62 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
63 import scala.concurrent.Await;
64 import scala.concurrent.Future;
65 import scala.concurrent.duration.Duration;
66
67 public class ReadWriteTransactionTest {
68     private static final Timeout TIMEOUT = new Timeout(Duration.create(5, "seconds"));
69     private static final int TIMEOUT_SEC = 5;
70     private static ActorSystem system;
71
72     @Rule
73     public final ExpectedException exception = ExpectedException.none();
74
75     @Mock
76     private DOMDataBroker deviceDataBroker;
77     @Mock
78     private DOMDataReadWriteTransaction readWriteTx;
79     @Mock
80     private DOMRpcService domRpcService;
81     @Mock
82     private DOMMountPointService mountPointService;
83     private ActorRef masterRef;
84     private ProxyDOMDataBroker slaveDataBroker;
85     private List<SourceIdentifier> sourceIdentifiers;
86     private NormalizedNode<?, ?> testNode;
87     private YangInstanceIdentifier instanceIdentifier;
88     private LogicalDatastoreType storeType;
89
90     @Before
91     public void setup() throws Exception {
92         initMocks(this);
93
94         system = ActorSystem.create();
95
96         final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
97                 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
98
99         final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
100         doReturn(Duration.apply(0, TimeUnit.SECONDS)).when(setup).getIdleTimeout();
101         final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
102                 DEFAULT_SCHEMA_REPOSITORY, TIMEOUT, mountPointService);
103
104         masterRef = TestActorRef.create(system, props, "master_read");
105
106         sourceIdentifiers = Lists.newArrayList();
107
108         doReturn(readWriteTx).when(deviceDataBroker).newReadWriteTransaction();
109         doNothing().when(readWriteTx).put(storeType, instanceIdentifier, testNode);
110         doNothing().when(readWriteTx).merge(storeType, instanceIdentifier, testNode);
111         doNothing().when(readWriteTx).delete(storeType, instanceIdentifier);
112
113         // Create slave data broker for testing proxy
114         slaveDataBroker =
115                 new ProxyDOMDataBroker(system, remoteDeviceId, masterRef, Timeout.apply(5, TimeUnit.SECONDS));
116         initializeDataTest();
117         testNode = ImmutableContainerNodeBuilder.create()
118                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("", "TestQname")))
119                 .withChild(ImmutableNodes.leafNode(QName.create("", "NodeQname"), "foo")).build();
120         instanceIdentifier = YangInstanceIdentifier.EMPTY;
121         storeType = LogicalDatastoreType.CONFIGURATION;
122     }
123
124     @After
125     public void teardown() {
126         JavaTestKit.shutdownActorSystem(system, null, true);
127         system = null;
128     }
129
130     @Test
131     public void testPut() throws Exception {
132         // Test of invoking put on master through slave proxy
133         final DOMDataWriteTransaction wTx = slaveDataBroker.newReadWriteTransaction();
134         wTx.put(storeType, instanceIdentifier, testNode);
135
136         verify(readWriteTx, timeout(2000)).put(storeType, instanceIdentifier, testNode);
137
138         wTx.cancel();
139     }
140
141     @Test
142     public void testMerge() throws Exception {
143         // Test of invoking merge on master through slave proxy
144         final DOMDataWriteTransaction wTx = slaveDataBroker.newReadWriteTransaction();
145         wTx.merge(storeType, instanceIdentifier, testNode);
146
147         verify(readWriteTx, timeout(2000)).merge(storeType, instanceIdentifier, testNode);
148
149         wTx.cancel();
150     }
151
152     @Test
153     public void testDelete() throws Exception {
154         // Test of invoking delete on master through slave proxy
155         final DOMDataWriteTransaction wTx = slaveDataBroker.newReadWriteTransaction();
156         wTx.delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
157         wTx.cancel();
158
159         verify(readWriteTx, timeout(2000)).delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
160     }
161
162     @Test
163     public void testSubmit() throws Exception {
164         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
165         doReturn(resultSubmit).when(readWriteTx).submit();
166
167         // Without Tx
168         final DOMDataWriteTransaction wTx = slaveDataBroker.newReadWriteTransaction();
169
170         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitResponse = wTx.submit();
171
172         final Object result = resultSubmitResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
173
174         assertNull(result);
175     }
176
177     @Test
178     public void testSubmitWithOperation() throws Exception {
179         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTx =
180                 Futures.immediateCheckedFuture(null);
181         doReturn(resultSubmitTx).when(readWriteTx).submit();
182         // With Tx
183         final DOMDataWriteTransaction wTx = slaveDataBroker.newReadWriteTransaction();
184         wTx.delete(LogicalDatastoreType.CONFIGURATION,
185                 YangInstanceIdentifier.EMPTY);
186
187         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTxResponse = wTx.submit();
188
189         final Object resultTx = resultSubmitTxResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
190
191         assertNull(resultTx);
192     }
193
194     @Test
195     public void testSubmitFail() throws Exception {
196         final TransactionCommitFailedException throwable = new TransactionCommitFailedException("Fail", null);
197         final CheckedFuture<Void, TransactionCommitFailedException> resultThrowable =
198                 Futures.immediateFailedCheckedFuture(throwable);
199         doReturn(resultThrowable).when(readWriteTx).submit();
200
201         final DOMDataWriteTransaction wTx = slaveDataBroker.newReadWriteTransaction();
202         wTx.delete(LogicalDatastoreType.CONFIGURATION,
203                 YangInstanceIdentifier.EMPTY);
204         final CheckedFuture<Void, TransactionCommitFailedException> resultThrowableResponse =
205                 wTx.submit();
206         exception.expect(TransactionCommitFailedException.class);
207         resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
208     }
209
210     @Test
211     public void testCancel() throws Exception {
212         doReturn(true).when(readWriteTx).cancel();
213
214         // Without Tx
215         final DOMDataWriteTransaction wTx = slaveDataBroker.newReadWriteTransaction();
216         final Boolean resultFalseNoTx = wTx.cancel();
217         assertEquals(true, resultFalseNoTx);
218     }
219
220     @Test
221     public void testCancelWithOperation() throws Exception {
222         doReturn(true).when(readWriteTx).cancel();
223
224         // With Tx, readWriteTx test
225         final DOMDataWriteTransaction wTx = slaveDataBroker.newReadWriteTransaction();
226         wTx.delete(LogicalDatastoreType.CONFIGURATION,
227                 YangInstanceIdentifier.EMPTY);
228
229         final Boolean resultTrue = wTx.cancel();
230         assertEquals(true, resultTrue);
231
232         final Boolean resultFalse = wTx.cancel();
233         assertEquals(false, resultFalse);
234     }
235
236     @Test
237     public void testRead() throws Exception {
238         // Message: NormalizedNodeMessage
239         final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
240                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("", "TestQname")))
241                 .withChild(ImmutableNodes.leafNode(QName.create("", "NodeQname"), "foo")).build();
242         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNormalizedNodeMessage =
243                 Futures.immediateCheckedFuture(Optional.of(outputNode));
244         doReturn(resultNormalizedNodeMessage).when(readWriteTx).read(storeType, instanceIdentifier);
245
246         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNodeMessageResponse =
247                 slaveDataBroker.newReadWriteTransaction().read(storeType, instanceIdentifier);
248
249         final Optional<NormalizedNode<?, ?>> resultNodeMessage =
250                 resultNodeMessageResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
251
252         assertTrue(resultNodeMessage.isPresent());
253         assertEquals(resultNodeMessage.get(), outputNode);
254     }
255
256     @Test
257     public void testReadEmpty() throws Exception {
258         // Message: EmptyReadResponse
259         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultEmpty =
260                 Futures.immediateCheckedFuture(Optional.absent());
261         doReturn(resultEmpty).when(readWriteTx).read(storeType, instanceIdentifier);
262
263         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultEmptyResponse =
264                 slaveDataBroker.newReadWriteTransaction().read(storeType,
265                         instanceIdentifier);
266
267         final Optional<NormalizedNode<?, ?>> resultEmptyMessage =
268                 resultEmptyResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
269
270         assertEquals(resultEmptyMessage, Optional.absent());
271     }
272
273     @Test
274     public void testReadFail() throws Exception {
275         // Message: Throwable
276         final ReadFailedException readFailedException = new ReadFailedException("Fail", null);
277         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultThrowable =
278                 Futures.immediateFailedCheckedFuture(readFailedException);
279
280         doReturn(resultThrowable).when(readWriteTx).read(storeType, instanceIdentifier);
281
282         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultThrowableResponse =
283                 slaveDataBroker.newReadWriteTransaction().read(storeType, instanceIdentifier);
284
285         exception.expect(ReadFailedException.class);
286         resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
287     }
288
289     @Test
290     public void testExist() throws Exception {
291         // Message: True
292         final CheckedFuture<Boolean, ReadFailedException> resultTrue =
293                 Futures.immediateCheckedFuture(true);
294         doReturn(resultTrue).when(readWriteTx).exists(storeType, instanceIdentifier);
295
296         final CheckedFuture<Boolean, ReadFailedException> trueResponse =
297                 slaveDataBroker.newReadWriteTransaction().exists(storeType, instanceIdentifier);
298
299         final Boolean trueMessage = trueResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
300
301         assertEquals(true, trueMessage);
302     }
303
304     @Test
305     public void testExistsNull() throws Exception {
306         // Message: False, result null
307         final CheckedFuture<Boolean, ReadFailedException> resultNull = Futures.immediateCheckedFuture(null);
308         doReturn(resultNull).when(readWriteTx).exists(storeType, instanceIdentifier);
309
310         final CheckedFuture<Boolean, ReadFailedException> nullResponse =
311                 slaveDataBroker.newReadWriteTransaction().exists(storeType,
312                         instanceIdentifier);
313
314         final Boolean nullFalseMessage = nullResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
315
316         assertEquals(false, nullFalseMessage);
317     }
318
319     @Test
320     public void testExistsFalse() throws Exception {
321         // Message: False
322         final CheckedFuture<Boolean, ReadFailedException> resultFalse = Futures.immediateCheckedFuture(false);
323         doReturn(resultFalse).when(readWriteTx).exists(storeType, instanceIdentifier);
324
325         final CheckedFuture<Boolean, ReadFailedException> falseResponse =
326                 slaveDataBroker.newReadWriteTransaction().exists(storeType,
327                         instanceIdentifier);
328
329         final Boolean falseMessage = falseResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
330
331         assertEquals(false, falseMessage);
332     }
333
334     @Test
335     public void testExistsFail() throws Exception {
336         // Message: Throwable
337         final ReadFailedException readFailedException = new ReadFailedException("Fail", null);
338         final CheckedFuture<Boolean, ReadFailedException> resultThrowable =
339                 Futures.immediateFailedCheckedFuture(readFailedException);
340         doReturn(resultThrowable).when(readWriteTx).exists(storeType, instanceIdentifier);
341
342         final CheckedFuture<Boolean, ReadFailedException> resultThrowableResponse =
343                 slaveDataBroker.newReadWriteTransaction().exists(storeType, instanceIdentifier);
344
345         exception.expect(ReadFailedException.class);
346         resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
347     }
348
349     private void initializeDataTest() throws Exception {
350         final Future<Object> initialDataToActor =
351                 Patterns.ask(masterRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
352                         domRpcService), TIMEOUT);
353
354         final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
355
356         assertTrue(success instanceof MasterActorDataInitialized);
357     }
358
359 }