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