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.Matchers.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.timeout;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.MockitoAnnotations.initMocks;
21 import static org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
23 import akka.actor.ActorRef;
24 import akka.actor.ActorSystem;
25 import akka.actor.Props;
26 import akka.pattern.Patterns;
27 import akka.testkit.JavaTestKit;
28 import akka.testkit.TestActorRef;
29 import akka.util.Timeout;
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.net.UnknownHostException;
36 import java.util.List;
37 import java.util.concurrent.TimeUnit;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.ExpectedException;
43 import org.mockito.Mock;
44 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
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.DOMDataReadOnlyTransaction;
48 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
49 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
50 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
51 import org.opendaylight.netconf.topology.singleton.impl.ProxyDOMDataBroker;
52 import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor;
53 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
54 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
55 import org.opendaylight.netconf.topology.singleton.messages.MasterActorDataInitialized;
56 import org.opendaylight.yangtools.yang.common.QName;
57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
58 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
59 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
60 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
61 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
62 import scala.concurrent.Await;
63 import scala.concurrent.Future;
64 import scala.concurrent.duration.Duration;
66 public class WriteOnlyTransactionTest {
67 private static final Timeout TIMEOUT = new Timeout(Duration.create(5, "seconds"));
68 private static final int TIMEOUT_SEC = 5;
69 private static ActorSystem system;
72 public final ExpectedException exception = ExpectedException.none();
74 private ActorRef masterRef;
75 private ProxyDOMDataBroker slaveDataBroker;
76 private List<SourceIdentifier> sourceIdentifiers;
78 private DOMDataBroker deviceDataBroker;
80 private DOMDataWriteTransaction writeTx;
82 private DOMRpcService domRpcService;
85 public void setup() throws UnknownHostException {
88 system = ActorSystem.create();
90 final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
91 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
93 final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
94 final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
95 DEFAULT_SCHEMA_REPOSITORY, TIMEOUT);
97 masterRef = TestActorRef.create(system, props, "master_read");
99 sourceIdentifiers = Lists.newArrayList();
101 writeTx = mock(DOMDataWriteTransaction.class);
102 final DOMDataReadOnlyTransaction readTx = mock(DOMDataReadOnlyTransaction.class);
104 doReturn(writeTx).when(deviceDataBroker).newWriteOnlyTransaction();
105 doReturn(readTx).when(deviceDataBroker).newReadOnlyTransaction();
107 // Create slave data broker for testing proxy
109 new ProxyDOMDataBroker(system, remoteDeviceId, masterRef, Timeout.apply(5, TimeUnit.SECONDS));
113 public void teardown() {
114 JavaTestKit.shutdownActorSystem(system, null, true);
119 public void testPut() throws Exception {
120 /* Initialize data on master */
122 initializeDataTest();
124 final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
125 final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
126 final NormalizedNode<?, ?> testNode = ImmutableContainerNodeBuilder.create()
127 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
128 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
130 // Test of invoking put on master through slave proxy
132 doNothing().when(writeTx).put(storeType, instanceIdentifier, testNode);
134 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
135 wTx.put(storeType, instanceIdentifier, testNode);
137 verify(writeTx, timeout(2000)).put(storeType, instanceIdentifier, testNode);
144 public void testMerge() throws Exception {
146 /* Initialize data on master */
148 initializeDataTest();
150 final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
151 final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
152 final NormalizedNode<?, ?> testNode = ImmutableContainerNodeBuilder.create()
153 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
154 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
155 // Test of invoking merge on master through slave proxy
157 doNothing().when(writeTx).merge(storeType, instanceIdentifier, testNode);
158 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
159 wTx.merge(storeType, instanceIdentifier, testNode);
161 verify(writeTx, timeout(2000)).merge(storeType, instanceIdentifier, testNode);
168 public void testDelete() throws Exception {
170 /* Initialize data on master */
172 initializeDataTest();
174 final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
175 final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
176 // Test of invoking delete on master through slave proxy
178 doNothing().when(writeTx).delete(storeType, instanceIdentifier);
179 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
180 wTx.delete(storeType, instanceIdentifier);
183 verify(writeTx, timeout(2000)).delete(storeType, instanceIdentifier);
188 public void testSubmit() throws Exception {
190 /* Initialize data on master */
192 initializeDataTest();
196 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
197 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
198 doReturn(resultSubmit).when(writeTx).submit();
200 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitResponse = wTx.submit();
202 final Object result = resultSubmitResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
208 public void testSubmitWithOperation() throws Exception {
210 /* Initialize data on master */
212 initializeDataTest();
214 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
215 doNothing().when(writeTx).delete(any(), any());
216 wTx.delete(LogicalDatastoreType.CONFIGURATION,
217 YangInstanceIdentifier.EMPTY);
219 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTx = Futures.immediateCheckedFuture(null);
220 doReturn(resultSubmitTx).when(writeTx).submit();
222 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTxResponse = wTx.submit();
224 final Object resultTx = resultSubmitTxResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
226 assertNull(resultTx);
230 public void testSubmitFail() throws Exception {
232 /* Initialize data on master */
234 initializeDataTest();
235 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
236 wTx.delete(LogicalDatastoreType.CONFIGURATION,
237 YangInstanceIdentifier.EMPTY);
239 final TransactionCommitFailedException throwable = new TransactionCommitFailedException("Fail", null);
240 final CheckedFuture<Void,TransactionCommitFailedException> resultThrowable =
241 Futures.immediateFailedCheckedFuture(throwable);
243 doReturn(resultThrowable).when(writeTx).submit();
245 final CheckedFuture<Void, TransactionCommitFailedException> resultThrowableResponse =
248 exception.expect(TransactionCommitFailedException.class);
249 resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
253 public void testCancel() throws Exception {
255 /* Initialize data on master */
257 initializeDataTest();
260 doReturn(true).when(writeTx).cancel();
261 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
262 final Boolean resultFalseNoTx = wTx.cancel();
263 assertEquals(true, resultFalseNoTx);
267 public void testCancelWithOperation() throws Exception {
269 /* Initialize data on master */
271 initializeDataTest();
273 // With Tx, readWriteTx test
275 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
276 doNothing().when(writeTx).delete(any(), any());
277 wTx.delete(LogicalDatastoreType.CONFIGURATION,
278 YangInstanceIdentifier.EMPTY);
280 doReturn(true).when(writeTx).cancel();
281 final Boolean resultTrue = wTx.cancel();
282 assertEquals(true, resultTrue);
284 final Boolean resultFalse = wTx.cancel();
285 assertEquals(false, resultFalse);
289 private void initializeDataTest() throws Exception {
290 final Future<Object> initialDataToActor =
291 Patterns.ask(masterRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
292 domRpcService), TIMEOUT);
294 final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
296 assertTrue(success instanceof MasterActorDataInitialized);