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.times;
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.api.NetconfDOMTransaction;
52 import org.opendaylight.netconf.topology.singleton.impl.NetconfDOMDataBroker;
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 WriteOnlyTransactionTest {
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();
75 private ActorRef masterRef;
76 private NetconfDOMDataBroker slaveDataBroker;
77 private DOMDataBroker masterDataBroker;
78 private List<SourceIdentifier> sourceIdentifiers;
81 private DOMDataWriteTransaction writeTx;
84 private DOMRpcService domRpcService;
87 public void setup() throws UnknownHostException {
90 system = ActorSystem.create();
92 final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
93 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
95 final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
96 final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
97 DEFAULT_SCHEMA_REPOSITORY);
99 masterRef = TestActorRef.create(system, props, "master_read");
101 sourceIdentifiers = Lists.newArrayList();
103 // Create master data broker
105 final DOMDataBroker delegateDataBroker = mock(DOMDataBroker.class);
106 writeTx = mock(DOMDataWriteTransaction.class);
107 final DOMDataReadOnlyTransaction readTx = mock(DOMDataReadOnlyTransaction.class);
109 doReturn(writeTx).when(delegateDataBroker).newWriteOnlyTransaction();
110 doReturn(readTx).when(delegateDataBroker).newReadOnlyTransaction();
112 final NetconfDOMTransaction masterDOMTransactions =
113 new NetconfMasterDOMTransaction(remoteDeviceId, delegateDataBroker);
116 new NetconfDOMDataBroker(system, remoteDeviceId, masterDOMTransactions);
118 // Create slave data broker for testing proxy
120 final NetconfDOMTransaction proxyDOMTransactions =
121 new NetconfProxyDOMTransaction(remoteDeviceId, system, masterRef);
123 slaveDataBroker = new NetconfDOMDataBroker(system, remoteDeviceId, proxyDOMTransactions);
129 public void teardown() {
130 JavaTestKit.shutdownActorSystem(system);
135 public void testPutMergeDeleteCalls() throws Exception {
137 /* Initialize data on master */
139 initializeDataTest();
141 final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
142 final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
143 final NormalizedNode<?, ?> testNode = ImmutableContainerNodeBuilder.create()
144 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
145 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
147 // Test of invoking put on master through slave proxy
149 doNothing().when(writeTx).put(storeType, instanceIdentifier, testNode);
150 slaveDataBroker.newWriteOnlyTransaction().put(storeType, instanceIdentifier, testNode);
152 verify(writeTx, times(1)).put(storeType, instanceIdentifier, testNode);
154 // Test of invoking merge on master through slave proxy
156 doNothing().when(writeTx).merge(storeType, instanceIdentifier, testNode);
157 slaveDataBroker.newWriteOnlyTransaction().merge(storeType, instanceIdentifier, testNode);
159 verify(writeTx, times(1)).merge(storeType, instanceIdentifier, testNode);
161 // Test of invoking delete on master through slave proxy
163 doNothing().when(writeTx).delete(storeType, instanceIdentifier);
164 slaveDataBroker.newWriteOnlyTransaction().delete(storeType, instanceIdentifier);
166 verify(writeTx, times(1)).delete(storeType, instanceIdentifier);
171 public void testSubmit() throws Exception {
173 /* Initialize data on master */
175 initializeDataTest();
179 final CheckedFuture<Void,TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
180 doReturn(resultSubmit).when(writeTx).submit();
182 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitResponse =
183 slaveDataBroker.newWriteOnlyTransaction().submit();
185 final Object result= resultSubmitResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
191 doNothing().when(writeTx).delete(any(), any());
192 slaveDataBroker.newWriteOnlyTransaction().delete(LogicalDatastoreType.CONFIGURATION,
193 YangInstanceIdentifier.EMPTY);
195 final CheckedFuture<Void,TransactionCommitFailedException> resultSubmitTx = Futures.immediateCheckedFuture(null);
196 doReturn(resultSubmitTx).when(writeTx).submit();
198 final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTxResponse =
199 slaveDataBroker.newWriteOnlyTransaction().submit();
201 final Object resultTx = resultSubmitTxResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
203 assertNull(resultTx);
205 slaveDataBroker.newWriteOnlyTransaction().delete(LogicalDatastoreType.CONFIGURATION,
206 YangInstanceIdentifier.EMPTY);
208 final TransactionCommitFailedException throwable = new TransactionCommitFailedException("Fail", null);
209 final CheckedFuture<Void,TransactionCommitFailedException> resultThrowable =
210 Futures.immediateFailedCheckedFuture(throwable);
212 doReturn(resultThrowable).when(writeTx).submit();
214 final CheckedFuture<Void, TransactionCommitFailedException> resultThrowableResponse =
215 slaveDataBroker.newWriteOnlyTransaction().submit();
217 exception.expect(TransactionCommitFailedException.class);
218 resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
222 public void testCancel() throws Exception {
224 /* Initialize data on master */
226 initializeDataTest();
230 final Boolean resultFalseNoTx = slaveDataBroker.newWriteOnlyTransaction().cancel();
231 assertEquals(false, resultFalseNoTx);
233 // With Tx, readWriteTx test
235 doNothing().when(writeTx).delete(any(), any());
236 slaveDataBroker.newReadWriteTransaction().delete(LogicalDatastoreType.CONFIGURATION,
237 YangInstanceIdentifier.EMPTY);
239 doReturn(true).when(writeTx).cancel();
241 final Boolean resultTrue = slaveDataBroker.newWriteOnlyTransaction().cancel();
242 assertEquals(true, resultTrue);
244 doReturn(false).when(writeTx).cancel();
246 final Boolean resultFalse = slaveDataBroker.newWriteOnlyTransaction().cancel();
247 assertEquals(false, resultFalse);
251 private void initializeDataTest() throws Exception {
252 final Future<Object> initialDataToActor =
253 Patterns.ask(masterRef, new CreateInitialMasterActorData(masterDataBroker, sourceIdentifiers,
254 domRpcService), TIMEOUT);
256 final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
258 assertTrue(success instanceof MasterActorDataInitialized);