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