2 * Copyright (c) 2016 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.netconf.topology.singleton.impl.tx;
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;
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;
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;
73 public final ExpectedException exception = ExpectedException.none();
76 private DOMDataBroker deviceDataBroker;
78 private DOMDataReadWriteTransaction readWriteTx;
80 private DOMRpcService domRpcService;
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;
91 public void setup() throws Exception {
94 system = ActorSystem.create();
96 final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
97 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
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);
104 masterRef = TestActorRef.create(system, props, "master_read");
106 sourceIdentifiers = Lists.newArrayList();
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);
113 // Create slave data broker for testing proxy
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;
125 public void teardown() {
126 JavaTestKit.shutdownActorSystem(system, null, true);
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);
136 verify(readWriteTx, timeout(2000)).put(storeType, instanceIdentifier, testNode);
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);
147 verify(readWriteTx, timeout(2000)).merge(storeType, instanceIdentifier, testNode);
153 public void testDelete() throws Exception {
154 final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
155 final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
157 // Test of invoking delete on master through slave proxy
158 final DOMDataWriteTransaction wTx = slaveDataBroker.newReadWriteTransaction();
159 wTx.delete(storeType, instanceIdentifier);
162 verify(readWriteTx, timeout(2000)).delete(storeType, instanceIdentifier);
166 public void testSubmit() throws Exception {
167 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
168 doReturn(resultSubmit).when(readWriteTx).submit();
171 final DOMDataWriteTransaction wTx = slaveDataBroker.newReadWriteTransaction();
173 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitResponse = wTx.submit();
175 final Object result = resultSubmitResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
181 public void testSubmitWithOperation() throws Exception {
182 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTx =
183 Futures.immediateCheckedFuture(null);
184 doReturn(resultSubmitTx).when(readWriteTx).submit();
186 final DOMDataWriteTransaction wTx = slaveDataBroker.newReadWriteTransaction();
187 wTx.delete(LogicalDatastoreType.CONFIGURATION,
188 YangInstanceIdentifier.EMPTY);
190 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTxResponse = wTx.submit();
192 final Object resultTx = resultSubmitTxResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
194 assertNull(resultTx);
198 public void testSubmitFail() throws Exception {
199 final TransactionCommitFailedException throwable = new TransactionCommitFailedException("Fail", null);
200 final CheckedFuture<Void, TransactionCommitFailedException> resultThrowable =
201 Futures.immediateFailedCheckedFuture(throwable);
202 doReturn(resultThrowable).when(readWriteTx).submit();
204 final DOMDataWriteTransaction wTx = slaveDataBroker.newReadWriteTransaction();
205 wTx.delete(LogicalDatastoreType.CONFIGURATION,
206 YangInstanceIdentifier.EMPTY);
207 final CheckedFuture<Void, TransactionCommitFailedException> resultThrowableResponse =
209 exception.expect(TransactionCommitFailedException.class);
210 resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
214 public void testCancel() throws Exception {
215 doReturn(true).when(readWriteTx).cancel();
218 final DOMDataWriteTransaction wTx = slaveDataBroker.newReadWriteTransaction();
219 final Boolean resultFalseNoTx = wTx.cancel();
220 assertEquals(true, resultFalseNoTx);
224 public void testCancelWithOperation() throws Exception {
225 doReturn(true).when(readWriteTx).cancel();
227 // With Tx, readWriteTx test
228 final DOMDataWriteTransaction wTx = slaveDataBroker.newReadWriteTransaction();
229 wTx.delete(LogicalDatastoreType.CONFIGURATION,
230 YangInstanceIdentifier.EMPTY);
232 final Boolean resultTrue = wTx.cancel();
233 assertEquals(true, resultTrue);
235 final Boolean resultFalse = wTx.cancel();
236 assertEquals(false, resultFalse);
240 public void testRead() throws Exception {
241 // Message: NormalizedNodeMessage
242 final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
243 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
244 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
245 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNormalizedNodeMessage =
246 Futures.immediateCheckedFuture(Optional.of(outputNode));
247 doReturn(resultNormalizedNodeMessage).when(readWriteTx).read(storeType, instanceIdentifier);
249 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNodeMessageResponse =
250 slaveDataBroker.newReadWriteTransaction().read(storeType, instanceIdentifier);
252 final Optional<NormalizedNode<?, ?>> resultNodeMessage =
253 resultNodeMessageResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
255 assertTrue(resultNodeMessage.isPresent());
256 assertEquals(resultNodeMessage.get(), outputNode);
260 public void testReadEmpty() throws Exception {
261 // Message: EmptyReadResponse
262 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultEmpty =
263 Futures.immediateCheckedFuture(Optional.absent());
264 doReturn(resultEmpty).when(readWriteTx).read(storeType, instanceIdentifier);
266 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultEmptyResponse =
267 slaveDataBroker.newReadWriteTransaction().read(storeType,
270 final Optional<NormalizedNode<?, ?>> resultEmptyMessage =
271 resultEmptyResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
273 assertEquals(resultEmptyMessage, Optional.absent());
277 public void testReadFail() throws Exception {
278 // Message: Throwable
279 final ReadFailedException readFailedException = new ReadFailedException("Fail", null);
280 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultThrowable =
281 Futures.immediateFailedCheckedFuture(readFailedException);
283 doReturn(resultThrowable).when(readWriteTx).read(storeType, instanceIdentifier);
285 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultThrowableResponse =
286 slaveDataBroker.newReadWriteTransaction().read(storeType, instanceIdentifier);
288 exception.expect(ReadFailedException.class);
289 resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
293 public void testExist() throws Exception {
295 final CheckedFuture<Boolean, ReadFailedException> resultTrue =
296 Futures.immediateCheckedFuture(true);
297 doReturn(resultTrue).when(readWriteTx).exists(storeType, instanceIdentifier);
299 final CheckedFuture<Boolean, ReadFailedException> trueResponse =
300 slaveDataBroker.newReadWriteTransaction().exists(storeType, instanceIdentifier);
302 final Boolean trueMessage = trueResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
304 assertEquals(true, trueMessage);
308 public void testExistsNull() throws Exception {
309 // Message: False, result null
310 final CheckedFuture<Boolean, ReadFailedException> resultNull = Futures.immediateCheckedFuture(null);
311 doReturn(resultNull).when(readWriteTx).exists(storeType, instanceIdentifier);
313 final CheckedFuture<Boolean, ReadFailedException> nullResponse =
314 slaveDataBroker.newReadWriteTransaction().exists(storeType,
317 final Boolean nullFalseMessage = nullResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
319 assertEquals(false, nullFalseMessage);
323 public void testExistsFalse() throws Exception {
325 final CheckedFuture<Boolean, ReadFailedException> resultFalse = Futures.immediateCheckedFuture(false);
326 doReturn(resultFalse).when(readWriteTx).exists(storeType, instanceIdentifier);
328 final CheckedFuture<Boolean, ReadFailedException> falseResponse =
329 slaveDataBroker.newReadWriteTransaction().exists(storeType,
332 final Boolean falseMessage = falseResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
334 assertEquals(false, falseMessage);
338 public void testExistsFail() throws Exception {
339 // Message: Throwable
340 final ReadFailedException readFailedException = new ReadFailedException("Fail", null);
341 final CheckedFuture<Boolean, ReadFailedException> resultThrowable =
342 Futures.immediateFailedCheckedFuture(readFailedException);
343 doReturn(resultThrowable).when(readWriteTx).exists(storeType, instanceIdentifier);
345 final CheckedFuture<Boolean, ReadFailedException> resultThrowableResponse =
346 slaveDataBroker.newReadWriteTransaction().exists(storeType, instanceIdentifier);
348 exception.expect(ReadFailedException.class);
349 resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
352 private void initializeDataTest() throws Exception {
353 final Future<Object> initialDataToActor =
354 Patterns.ask(masterRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
355 domRpcService), TIMEOUT);
357 final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
359 assertTrue(success instanceof MasterActorDataInitialized);