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.collect.Lists;
30 import com.google.common.net.InetAddresses;
31 import com.google.common.util.concurrent.CheckedFuture;
32 import com.google.common.util.concurrent.Futures;
33 import java.net.InetSocketAddress;
34 import java.util.List;
35 import java.util.concurrent.TimeUnit;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.mockito.Mock;
42 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
43 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
44 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
45 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
46 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
47 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
48 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
49 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
50 import org.opendaylight.netconf.topology.singleton.impl.ProxyDOMDataBroker;
51 import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor;
52 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
53 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
54 import org.opendaylight.netconf.topology.singleton.messages.MasterActorDataInitialized;
55 import org.opendaylight.yangtools.yang.common.QName;
56 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
57 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
58 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
59 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
60 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
61 import scala.concurrent.Await;
62 import scala.concurrent.Future;
63 import scala.concurrent.duration.Duration;
65 public class WriteOnlyTransactionTest {
66 private static final Timeout TIMEOUT = new Timeout(Duration.create(5, "seconds"));
67 private static final int TIMEOUT_SEC = 5;
68 private static ActorSystem system;
71 public final ExpectedException exception = ExpectedException.none();
74 private DOMDataBroker deviceDataBroker;
76 private DOMDataWriteTransaction writeTx;
78 private DOMRpcService domRpcService;
80 private DOMMountPointService mountPointService;
81 private ActorRef masterRef;
82 private ProxyDOMDataBroker slaveDataBroker;
83 private List<SourceIdentifier> sourceIdentifiers;
84 private NormalizedNode<?, ?> testNode;
85 private YangInstanceIdentifier instanceIdentifier;
86 private LogicalDatastoreType storeType;
89 public void setup() throws Exception {
92 system = ActorSystem.create();
94 final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
95 new InetSocketAddress(InetAddresses.forString("127.0.0.1"), 9999));
97 final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
98 doReturn(Duration.apply(0, TimeUnit.SECONDS)).when(setup).getIdleTimeout();
99 final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
100 DEFAULT_SCHEMA_REPOSITORY, TIMEOUT, mountPointService);
102 masterRef = TestActorRef.create(system, props, "master_read");
104 sourceIdentifiers = Lists.newArrayList();
106 final DOMDataReadOnlyTransaction readTx = mock(DOMDataReadOnlyTransaction.class);
108 doReturn(writeTx).when(deviceDataBroker).newWriteOnlyTransaction();
109 doReturn(readTx).when(deviceDataBroker).newReadOnlyTransaction();
110 doNothing().when(writeTx).put(storeType, instanceIdentifier, testNode);
111 doNothing().when(writeTx).merge(storeType, instanceIdentifier, testNode);
112 doNothing().when(writeTx).delete(storeType, instanceIdentifier);
114 // Create slave data broker for testing proxy
116 new ProxyDOMDataBroker(system, remoteDeviceId, masterRef, Timeout.apply(5, TimeUnit.SECONDS));
117 initializeDataTest();
118 testNode = ImmutableContainerNodeBuilder.create()
119 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("", "TestQname")))
120 .withChild(ImmutableNodes.leafNode(QName.create("", "NodeQname"), "foo")).build();
121 instanceIdentifier = YangInstanceIdentifier.EMPTY;
122 storeType = LogicalDatastoreType.CONFIGURATION;
126 public void teardown() {
127 JavaTestKit.shutdownActorSystem(system, null, true);
132 public void testPut() throws Exception {
133 // Test of invoking put on master through slave proxy
134 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
135 wTx.put(storeType, instanceIdentifier, testNode);
137 verify(writeTx, timeout(2000)).put(storeType, instanceIdentifier, testNode);
143 public void testMerge() throws Exception {
144 // Test of invoking merge on master through slave proxy
145 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
146 wTx.merge(storeType, instanceIdentifier, testNode);
148 verify(writeTx, timeout(2000)).merge(storeType, instanceIdentifier, testNode);
154 public void testDelete() throws Exception {
155 // Test of invoking delete on master through slave proxy
156 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
157 wTx.delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
160 verify(writeTx, timeout(2000)).delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
164 public void testSubmit() throws Exception {
165 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
166 doReturn(resultSubmit).when(writeTx).submit();
169 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
171 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitResponse = wTx.submit();
173 final Object result = resultSubmitResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
179 public void testSubmitWithOperation() throws Exception {
180 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTx =
181 Futures.immediateCheckedFuture(null);
182 doReturn(resultSubmitTx).when(writeTx).submit();
184 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
185 wTx.delete(LogicalDatastoreType.CONFIGURATION,
186 YangInstanceIdentifier.EMPTY);
188 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTxResponse = wTx.submit();
190 final Object resultTx = resultSubmitTxResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
192 assertNull(resultTx);
196 public void testSubmitFail() throws Exception {
197 final TransactionCommitFailedException throwable = new TransactionCommitFailedException("Fail", null);
198 final CheckedFuture<Void, TransactionCommitFailedException> resultThrowable =
199 Futures.immediateFailedCheckedFuture(throwable);
200 doReturn(resultThrowable).when(writeTx).submit();
202 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
203 wTx.delete(LogicalDatastoreType.CONFIGURATION,
204 YangInstanceIdentifier.EMPTY);
205 final CheckedFuture<Void, TransactionCommitFailedException> resultThrowableResponse =
207 exception.expect(TransactionCommitFailedException.class);
208 resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
212 public void testCancel() throws Exception {
213 doReturn(true).when(writeTx).cancel();
216 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
217 final Boolean resultFalseNoTx = wTx.cancel();
218 assertEquals(true, resultFalseNoTx);
222 public void testCancelWithOperation() throws Exception {
223 doReturn(true).when(writeTx).cancel();
225 // With Tx, readWriteTx test
226 final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
227 wTx.delete(LogicalDatastoreType.CONFIGURATION,
228 YangInstanceIdentifier.EMPTY);
230 final Boolean resultTrue = wTx.cancel();
231 assertEquals(true, resultTrue);
233 final Boolean resultFalse = wTx.cancel();
234 assertEquals(false, resultFalse);
237 private void initializeDataTest() throws Exception {
238 final Future<Object> initialDataToActor =
239 Patterns.ask(masterRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
240 domRpcService), TIMEOUT);
242 final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
244 assertTrue(success instanceof MasterActorDataInitialized);