deb8235d7d0d8d0863769a35f85d278cfa10fa13
[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.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;
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.impl.ProxyDOMDataBroker;
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 ProxyDOMDataBroker slaveDataBroker;
77     private List<SourceIdentifier> sourceIdentifiers;
78     @Mock
79     private DOMDataBroker deviceDataBroker;
80     @Mock
81     private DOMDataWriteTransaction writeTx;
82     @Mock
83     private DOMRpcService domRpcService;
84
85     @Before
86     public void setup() throws UnknownHostException {
87         initMocks(this);
88
89         system = ActorSystem.create();
90
91         final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
92                 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
93
94         final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
95         doReturn(Duration.apply(0, TimeUnit.SECONDS)).when(setup).getIdleTimeout();
96         final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
97                 DEFAULT_SCHEMA_REPOSITORY);
98
99         masterRef = TestActorRef.create(system, props, "master_read");
100
101         sourceIdentifiers = Lists.newArrayList();
102
103         writeTx = mock(DOMDataWriteTransaction.class);
104         final DOMDataReadOnlyTransaction readTx = mock(DOMDataReadOnlyTransaction.class);
105
106         doReturn(writeTx).when(deviceDataBroker).newWriteOnlyTransaction();
107         doReturn(readTx).when(deviceDataBroker).newReadOnlyTransaction();
108
109         // Create slave data broker for testing proxy
110         slaveDataBroker =
111                 new ProxyDOMDataBroker(system, remoteDeviceId, masterRef, Timeout.apply(5, TimeUnit.SECONDS));
112     }
113
114     @After
115     public void teardown() {
116         JavaTestKit.shutdownActorSystem(system, null, true);
117         system = null;
118     }
119
120     @Test
121     public void testPut() throws Exception {
122         /* Initialize data on master */
123
124         initializeDataTest();
125
126         final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
127         final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
128         final NormalizedNode<?, ?> testNode = ImmutableContainerNodeBuilder.create()
129                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
130                 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
131
132         // Test of invoking put on master through slave proxy
133
134         doNothing().when(writeTx).put(storeType, instanceIdentifier, testNode);
135
136         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
137         wTx.put(storeType, instanceIdentifier, testNode);
138
139         verify(writeTx, timeout(2000)).put(storeType, instanceIdentifier, testNode);
140
141         wTx.cancel();
142
143     }
144
145     @Test
146     public void testMerge() throws Exception {
147
148         /* Initialize data on master */
149
150         initializeDataTest();
151
152         final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
153         final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
154         final NormalizedNode<?, ?> testNode = ImmutableContainerNodeBuilder.create()
155                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
156                 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
157         // Test of invoking merge on master through slave proxy
158
159         doNothing().when(writeTx).merge(storeType, instanceIdentifier, testNode);
160         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
161         wTx.merge(storeType, instanceIdentifier, testNode);
162
163         verify(writeTx, timeout(2000)).merge(storeType, instanceIdentifier, testNode);
164
165         wTx.cancel();
166
167     }
168
169     @Test
170     public void testDelete() throws Exception {
171
172         /* Initialize data on master */
173
174         initializeDataTest();
175
176         final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
177         final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
178         // Test of invoking delete on master through slave proxy
179
180         doNothing().when(writeTx).delete(storeType, instanceIdentifier);
181         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
182         wTx.delete(storeType, instanceIdentifier);
183         wTx.cancel();
184
185         verify(writeTx, timeout(2000)).delete(storeType, instanceIdentifier);
186
187     }
188
189     @Test
190     public void testSubmit() throws Exception {
191
192         /* Initialize data on master */
193
194         initializeDataTest();
195
196         // Without Tx
197
198         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
199         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
200         doReturn(resultSubmit).when(writeTx).submit();
201
202         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitResponse = wTx.submit();
203
204         final Object result = resultSubmitResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
205
206         assertNull(result);
207     }
208
209     @Test
210     public void testSubmitWithOperation() throws Exception {
211
212         /* Initialize data on master */
213
214         initializeDataTest();
215         // With Tx
216         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
217         doNothing().when(writeTx).delete(any(), any());
218         wTx.delete(LogicalDatastoreType.CONFIGURATION,
219                 YangInstanceIdentifier.EMPTY);
220
221         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTx = Futures.immediateCheckedFuture(null);
222         doReturn(resultSubmitTx).when(writeTx).submit();
223
224         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTxResponse = wTx.submit();
225
226         final Object resultTx = resultSubmitTxResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
227
228         assertNull(resultTx);
229     }
230
231     @Test
232     public void testSubmitFail() throws Exception {
233
234         /* Initialize data on master */
235
236         initializeDataTest();
237         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
238         wTx.delete(LogicalDatastoreType.CONFIGURATION,
239                 YangInstanceIdentifier.EMPTY);
240
241         final TransactionCommitFailedException throwable = new TransactionCommitFailedException("Fail", null);
242         final CheckedFuture<Void,TransactionCommitFailedException> resultThrowable =
243                 Futures.immediateFailedCheckedFuture(throwable);
244
245         doReturn(resultThrowable).when(writeTx).submit();
246
247         final CheckedFuture<Void, TransactionCommitFailedException> resultThrowableResponse =
248                 wTx.submit();
249
250         exception.expect(TransactionCommitFailedException.class);
251         resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
252     }
253
254     @Test
255     public void testCancel() throws Exception {
256
257         /* Initialize data on master */
258
259         initializeDataTest();
260
261         // Without Tx
262         doReturn(true).when(writeTx).cancel();
263         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
264         final Boolean resultFalseNoTx = wTx.cancel();
265         assertEquals(true, resultFalseNoTx);
266     }
267
268     @Test
269     public void testCancelWithOperation() throws Exception {
270
271         /* Initialize data on master */
272
273         initializeDataTest();
274
275         // With Tx, readWriteTx test
276
277         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
278         doNothing().when(writeTx).delete(any(), any());
279         wTx.delete(LogicalDatastoreType.CONFIGURATION,
280                 YangInstanceIdentifier.EMPTY);
281
282         doReturn(true).when(writeTx).cancel();
283         final Boolean resultTrue = wTx.cancel();
284         assertEquals(true, resultTrue);
285
286         final Boolean resultFalse = wTx.cancel();
287         assertEquals(false, resultFalse);
288
289     }
290
291     private void initializeDataTest() throws Exception {
292         final Future<Object> initialDataToActor =
293                 Patterns.ask(masterRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
294                                 domRpcService), TIMEOUT);
295
296         final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
297
298         assertTrue(success instanceof MasterActorDataInitialized);
299     }
300
301 }