2dc446118c97714c3ab6f5a3fffd706f38fe4118
[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.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;
66
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;
71
72     @Rule
73     public final ExpectedException exception = ExpectedException.none();
74
75     private ActorRef masterRef;
76     private NetconfDOMDataBroker slaveDataBroker;
77     private DOMDataBroker masterDataBroker;
78     private List<SourceIdentifier> sourceIdentifiers;
79
80     @Mock
81     private DOMDataWriteTransaction writeTx;
82
83     @Mock
84     private DOMRpcService domRpcService;
85
86     @Before
87     public void setup() throws UnknownHostException {
88         initMocks(this);
89
90         system = ActorSystem.create();
91
92         final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
93                 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
94
95         final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
96         final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
97                 DEFAULT_SCHEMA_REPOSITORY, TIMEOUT);
98
99         masterRef = TestActorRef.create(system, props, "master_read");
100
101         sourceIdentifiers = Lists.newArrayList();
102
103         // Create master data broker
104
105         final DOMDataBroker delegateDataBroker = mock(DOMDataBroker.class);
106         writeTx = mock(DOMDataWriteTransaction.class);
107         final DOMDataReadOnlyTransaction readTx = mock(DOMDataReadOnlyTransaction.class);
108
109         doReturn(writeTx).when(delegateDataBroker).newWriteOnlyTransaction();
110         doReturn(readTx).when(delegateDataBroker).newReadOnlyTransaction();
111
112         final NetconfDOMTransaction masterDOMTransactions =
113                 new NetconfMasterDOMTransaction(remoteDeviceId, delegateDataBroker);
114
115         masterDataBroker =
116                 new NetconfDOMDataBroker(system, remoteDeviceId, masterDOMTransactions);
117
118         // Create slave data broker for testing proxy
119
120         final NetconfDOMTransaction proxyDOMTransactions =
121                 new NetconfProxyDOMTransaction(remoteDeviceId, system, masterRef, TIMEOUT);
122
123         slaveDataBroker = new NetconfDOMDataBroker(system, remoteDeviceId, proxyDOMTransactions);
124
125
126     }
127
128     @After
129     public void teardown() {
130         JavaTestKit.shutdownActorSystem(system);
131         system = null;
132     }
133
134     @Test
135     public void testPutMergeDeleteCalls() throws Exception {
136
137         /* Initialize data on master */
138
139         initializeDataTest();
140
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();
146
147         // Test of invoking put on master through slave proxy
148
149         doNothing().when(writeTx).put(storeType, instanceIdentifier, testNode);
150
151         DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
152         wTx.put(storeType, instanceIdentifier, testNode);
153
154         verify(writeTx, times(1)).put(storeType, instanceIdentifier, testNode);
155
156         wTx.cancel();
157         // Test of invoking merge on master through slave proxy
158
159         doNothing().when(writeTx).merge(storeType, instanceIdentifier, testNode);
160         wTx = slaveDataBroker.newWriteOnlyTransaction();
161         wTx.merge(storeType, instanceIdentifier, testNode);
162
163         verify(writeTx, times(1)).merge(storeType, instanceIdentifier, testNode);
164
165         wTx.cancel();
166         // Test of invoking delete on master through slave proxy
167
168         doNothing().when(writeTx).delete(storeType, instanceIdentifier);
169         wTx = slaveDataBroker.newWriteOnlyTransaction();
170         wTx.delete(storeType, instanceIdentifier);
171         wTx.cancel();
172
173         verify(writeTx, times(1)).delete(storeType, instanceIdentifier);
174
175     }
176
177     @Test
178     public void testSubmit() throws Exception {
179
180         /* Initialize data on master */
181
182         initializeDataTest();
183
184         // Without Tx
185
186         DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
187         final CheckedFuture<Void,TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
188         doReturn(resultSubmit).when(writeTx).submit();
189
190         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitResponse = wTx.submit();
191
192         final Object result = resultSubmitResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
193
194         assertNull(result);
195
196         // With Tx
197         wTx = slaveDataBroker.newWriteOnlyTransaction();
198         doNothing().when(writeTx).delete(any(), any());
199         wTx.delete(LogicalDatastoreType.CONFIGURATION,
200                 YangInstanceIdentifier.EMPTY);
201
202         final CheckedFuture<Void,TransactionCommitFailedException> resultSubmitTx = Futures.immediateCheckedFuture(null);
203         doReturn(resultSubmitTx).when(writeTx).submit();
204
205         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTxResponse = wTx.submit();
206
207         final Object resultTx = resultSubmitTxResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
208
209         assertNull(resultTx);
210
211         wTx = slaveDataBroker.newWriteOnlyTransaction();
212         wTx.delete(LogicalDatastoreType.CONFIGURATION,
213                 YangInstanceIdentifier.EMPTY);
214
215         final TransactionCommitFailedException throwable = new TransactionCommitFailedException("Fail", null);
216         final CheckedFuture<Void,TransactionCommitFailedException> resultThrowable =
217                 Futures.immediateFailedCheckedFuture(throwable);
218
219         doReturn(resultThrowable).when(writeTx).submit();
220
221         final CheckedFuture<Void, TransactionCommitFailedException> resultThrowableResponse =
222                 wTx.submit();
223
224         exception.expect(TransactionCommitFailedException.class);
225         resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
226     }
227
228     @Test
229     public void testCancel() throws Exception {
230
231         /* Initialize data on master */
232
233         initializeDataTest();
234
235         // Without Tx
236
237         DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
238         final Boolean resultFalseNoTx = wTx.cancel();
239         assertEquals(false, resultFalseNoTx);
240
241         // With Tx, readWriteTx test
242
243         wTx = slaveDataBroker.newWriteOnlyTransaction();
244         doNothing().when(writeTx).delete(any(), any());
245         wTx.delete(LogicalDatastoreType.CONFIGURATION,
246                 YangInstanceIdentifier.EMPTY);
247
248         doReturn(true).when(writeTx).cancel();
249         final Boolean resultTrue = wTx.cancel();
250         assertEquals(true, resultTrue);
251
252         doReturn(false).when(writeTx).cancel();
253
254         final Boolean resultFalse = wTx.cancel();
255         assertEquals(false, resultFalse);
256
257     }
258
259     private void initializeDataTest() throws Exception {
260         final Future<Object> initialDataToActor =
261                 Patterns.ask(masterRef, new CreateInitialMasterActorData(masterDataBroker, sourceIdentifiers,
262                                 domRpcService), TIMEOUT);
263
264         final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
265
266         assertTrue(success instanceof MasterActorDataInitialized);
267     }
268
269 }