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 doReturn(Duration.apply(0, TimeUnit.SECONDS)).when(setup).getIdleTimeout();
95 final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
96 DEFAULT_SCHEMA_REPOSITORY, TIMEOUT);
98 masterRef = TestActorRef.create(system, props, "master_read");
100 sourceIdentifiers = Lists.newArrayList();
102 writeTx = mock(DOMDataWriteTransaction.class);
103 final DOMDataReadOnlyTransaction readTx = mock(DOMDataReadOnlyTransaction.class);
105 doReturn(writeTx).when(deviceDataBroker).newWriteOnlyTransaction();
106 doReturn(readTx).when(deviceDataBroker).newReadOnlyTransaction();
108 // Create slave data broker for testing proxy
110 new ProxyDOMDataBroker(system, remoteDeviceId, masterRef, Timeout.apply(5, TimeUnit.SECONDS));
114 public void teardown() {
115 JavaTestKit.shutdownActorSystem(system, null, true);
120 public void testPut() throws Exception {
121 /* Initialize data on master */
123 initializeDataTest();
125 final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
126 final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
127 final NormalizedNode<?, ?> testNode = ImmutableContainerNodeBuilder.create()
128 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
129 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
131 // Test of invoking put on master through slave proxy
133 doNothing().when(writeTx).put(storeType, instanceIdentifier, testNode);
135 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
136 wTx.put(storeType, instanceIdentifier, testNode);
138 verify(writeTx, timeout(2000)).put(storeType, instanceIdentifier, testNode);
145 public void testMerge() throws Exception {
147 /* Initialize data on master */
149 initializeDataTest();
151 final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
152 final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
153 final NormalizedNode<?, ?> testNode = ImmutableContainerNodeBuilder.create()
154 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
155 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
156 // Test of invoking merge on master through slave proxy
158 doNothing().when(writeTx).merge(storeType, instanceIdentifier, testNode);
159 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
160 wTx.merge(storeType, instanceIdentifier, testNode);
162 verify(writeTx, timeout(2000)).merge(storeType, instanceIdentifier, testNode);
169 public void testDelete() throws Exception {
171 /* Initialize data on master */
173 initializeDataTest();
175 final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
176 final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
177 // Test of invoking delete on master through slave proxy
179 doNothing().when(writeTx).delete(storeType, instanceIdentifier);
180 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
181 wTx.delete(storeType, instanceIdentifier);
184 verify(writeTx, timeout(2000)).delete(storeType, instanceIdentifier);
189 public void testSubmit() throws Exception {
191 /* Initialize data on master */
193 initializeDataTest();
197 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
198 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
199 doReturn(resultSubmit).when(writeTx).submit();
201 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitResponse = wTx.submit();
203 final Object result = resultSubmitResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
209 public void testSubmitWithOperation() throws Exception {
211 /* Initialize data on master */
213 initializeDataTest();
215 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
216 doNothing().when(writeTx).delete(any(), any());
217 wTx.delete(LogicalDatastoreType.CONFIGURATION,
218 YangInstanceIdentifier.EMPTY);
220 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTx = Futures.immediateCheckedFuture(null);
221 doReturn(resultSubmitTx).when(writeTx).submit();
223 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTxResponse = wTx.submit();
225 final Object resultTx = resultSubmitTxResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
227 assertNull(resultTx);
231 public void testSubmitFail() throws Exception {
233 /* Initialize data on master */
235 initializeDataTest();
236 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
237 wTx.delete(LogicalDatastoreType.CONFIGURATION,
238 YangInstanceIdentifier.EMPTY);
240 final TransactionCommitFailedException throwable = new TransactionCommitFailedException("Fail", null);
241 final CheckedFuture<Void,TransactionCommitFailedException> resultThrowable =
242 Futures.immediateFailedCheckedFuture(throwable);
244 doReturn(resultThrowable).when(writeTx).submit();
246 final CheckedFuture<Void, TransactionCommitFailedException> resultThrowableResponse =
249 exception.expect(TransactionCommitFailedException.class);
250 resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
254 public void testCancel() throws Exception {
256 /* Initialize data on master */
258 initializeDataTest();
261 doReturn(true).when(writeTx).cancel();
262 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
263 final Boolean resultFalseNoTx = wTx.cancel();
264 assertEquals(true, resultFalseNoTx);
268 public void testCancelWithOperation() throws Exception {
270 /* Initialize data on master */
272 initializeDataTest();
274 // With Tx, readWriteTx test
276 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
277 doNothing().when(writeTx).delete(any(), any());
278 wTx.delete(LogicalDatastoreType.CONFIGURATION,
279 YangInstanceIdentifier.EMPTY);
281 doReturn(true).when(writeTx).cancel();
282 final Boolean resultTrue = wTx.cancel();
283 assertEquals(true, resultTrue);
285 final Boolean resultFalse = wTx.cancel();
286 assertEquals(false, resultFalse);
290 private void initializeDataTest() throws Exception {
291 final Future<Object> initialDataToActor =
292 Patterns.ask(masterRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
293 domRpcService), TIMEOUT);
295 final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
297 assertTrue(success instanceof MasterActorDataInitialized);