Bug 7295 - Incorrect handling of device transactions in clustered setting
[netconf.git] / netconf / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / tx / WriteOnlyTransactionTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.topology.singleton.impl.tx;
10
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;
22
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.mockito.Mockito;
45 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
46 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
47 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
48 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
49 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
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.api.NetconfDOMTransaction;
53 import org.opendaylight.netconf.topology.singleton.impl.NetconfDOMDataBroker;
54 import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor;
55 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
56 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
57 import org.opendaylight.netconf.topology.singleton.messages.MasterActorDataInitialized;
58 import org.opendaylight.yangtools.yang.common.QName;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
60 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
61 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
62 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
63 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
64 import scala.concurrent.Await;
65 import scala.concurrent.Future;
66 import scala.concurrent.duration.Duration;
67
68 public class WriteOnlyTransactionTest {
69     private static final Timeout TIMEOUT = new Timeout(Duration.create(5, "seconds"));
70     private static final int TIMEOUT_SEC = 5;
71     private static ActorSystem system;
72
73     @Rule
74     public final ExpectedException exception = ExpectedException.none();
75
76     private ActorRef masterRef;
77     private NetconfDOMDataBroker slaveDataBroker;
78     private DOMDataBroker masterDataBroker;
79     private List<SourceIdentifier> sourceIdentifiers;
80
81     @Mock
82     private DOMDataWriteTransaction writeTx;
83
84     @Mock
85     private DOMRpcService domRpcService;
86
87     @Before
88     public void setup() throws UnknownHostException {
89         initMocks(this);
90
91         system = ActorSystem.create();
92
93         final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
94                 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
95
96         final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
97         final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
98                 DEFAULT_SCHEMA_REPOSITORY);
99
100         masterRef = TestActorRef.create(system, props, "master_read");
101
102         sourceIdentifiers = Lists.newArrayList();
103
104         // Create master data broker
105
106         final DOMDataBroker delegateDataBroker = mock(DOMDataBroker.class);
107         writeTx = mock(DOMDataWriteTransaction.class);
108         final DOMDataReadOnlyTransaction readTx = mock(DOMDataReadOnlyTransaction.class);
109
110         doReturn(writeTx).when(delegateDataBroker).newWriteOnlyTransaction();
111         doReturn(readTx).when(delegateDataBroker).newReadOnlyTransaction();
112
113         final NetconfDOMTransaction masterDOMTransactions =
114                 new NetconfMasterDOMTransaction(remoteDeviceId, delegateDataBroker);
115
116         masterDataBroker =
117                 new NetconfDOMDataBroker(system, remoteDeviceId, masterDOMTransactions);
118
119         // Create slave data broker for testing proxy
120
121         final NetconfDOMTransaction proxyDOMTransactions =
122                 new NetconfProxyDOMTransaction(remoteDeviceId, system, masterRef);
123
124         slaveDataBroker = new NetconfDOMDataBroker(system, remoteDeviceId, proxyDOMTransactions);
125
126
127     }
128
129     @After
130     public void teardown() {
131         JavaTestKit.shutdownActorSystem(system);
132         system = null;
133     }
134
135     @Test
136     public void testPutMergeDeleteCalls() throws Exception {
137
138         /* Initialize data on master */
139
140         initializeDataTest();
141
142         final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
143         final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
144         final NormalizedNode<?, ?> testNode = ImmutableContainerNodeBuilder.create()
145                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
146                 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
147
148         // Test of invoking put on master through slave proxy
149
150         doNothing().when(writeTx).put(storeType, instanceIdentifier, testNode);
151
152         DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
153         wTx.put(storeType, instanceIdentifier, testNode);
154
155         verify(writeTx, times(1)).put(storeType, instanceIdentifier, testNode);
156
157         wTx.cancel();
158         // Test of invoking merge on master through slave proxy
159
160         doNothing().when(writeTx).merge(storeType, instanceIdentifier, testNode);
161         wTx = slaveDataBroker.newWriteOnlyTransaction();
162         wTx.merge(storeType, instanceIdentifier, testNode);
163
164         verify(writeTx, times(1)).merge(storeType, instanceIdentifier, testNode);
165
166         wTx.cancel();
167         // Test of invoking delete on master through slave proxy
168
169         doNothing().when(writeTx).delete(storeType, instanceIdentifier);
170         wTx = slaveDataBroker.newWriteOnlyTransaction();
171         wTx.delete(storeType, instanceIdentifier);
172         wTx.cancel();
173
174         verify(writeTx, times(1)).delete(storeType, instanceIdentifier);
175
176     }
177
178     @Test
179     public void testSubmit() throws Exception {
180
181         /* Initialize data on master */
182
183         initializeDataTest();
184
185         // Without Tx
186
187         DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
188         final CheckedFuture<Void,TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
189         doReturn(resultSubmit).when(writeTx).submit();
190
191         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitResponse = wTx.submit();
192
193         final Object result = resultSubmitResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
194
195         assertNull(result);
196
197         // With Tx
198         wTx = slaveDataBroker.newWriteOnlyTransaction();
199         doNothing().when(writeTx).delete(any(), any());
200         wTx.delete(LogicalDatastoreType.CONFIGURATION,
201                 YangInstanceIdentifier.EMPTY);
202
203         final CheckedFuture<Void,TransactionCommitFailedException> resultSubmitTx = Futures.immediateCheckedFuture(null);
204         doReturn(resultSubmitTx).when(writeTx).submit();
205
206         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTxResponse = wTx.submit();
207
208         final Object resultTx = resultSubmitTxResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
209
210         assertNull(resultTx);
211
212         wTx = slaveDataBroker.newWriteOnlyTransaction();
213         wTx.delete(LogicalDatastoreType.CONFIGURATION,
214                 YangInstanceIdentifier.EMPTY);
215
216         final TransactionCommitFailedException throwable = new TransactionCommitFailedException("Fail", null);
217         final CheckedFuture<Void,TransactionCommitFailedException> resultThrowable =
218                 Futures.immediateFailedCheckedFuture(throwable);
219
220         doReturn(resultThrowable).when(writeTx).submit();
221
222         final CheckedFuture<Void, TransactionCommitFailedException> resultThrowableResponse =
223                 wTx.submit();
224
225         exception.expect(TransactionCommitFailedException.class);
226         resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
227     }
228
229     @Test
230     public void testCancel() throws Exception {
231
232         /* Initialize data on master */
233
234         initializeDataTest();
235
236         // Without Tx
237
238         DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
239         final Boolean resultFalseNoTx = wTx.cancel();
240         assertEquals(false, resultFalseNoTx);
241
242         // With Tx, readWriteTx test
243
244         wTx = slaveDataBroker.newWriteOnlyTransaction();
245         doNothing().when(writeTx).delete(any(), any());
246         wTx.delete(LogicalDatastoreType.CONFIGURATION,
247                 YangInstanceIdentifier.EMPTY);
248
249         doReturn(true).when(writeTx).cancel();
250         final Boolean resultTrue = wTx.cancel();
251         assertEquals(true, resultTrue);
252
253         doReturn(false).when(writeTx).cancel();
254
255         final Boolean resultFalse = wTx.cancel();
256         assertEquals(false, resultFalse);
257
258     }
259
260     private void initializeDataTest() throws Exception {
261         final Future<Object> initialDataToActor =
262                 Patterns.ask(masterRef, new CreateInitialMasterActorData(masterDataBroker, sourceIdentifiers,
263                                 domRpcService), TIMEOUT);
264
265         final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
266
267         assertTrue(success instanceof MasterActorDataInitialized);
268     }
269
270 }