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