Bug 8032 - Initialization in sal failed, disconnecting from device
[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.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.timeout;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.MockitoAnnotations.initMocks;
20 import static org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
21
22 import akka.actor.ActorRef;
23 import akka.actor.ActorSystem;
24 import akka.actor.Props;
25 import akka.pattern.Patterns;
26 import akka.testkit.JavaTestKit;
27 import akka.testkit.TestActorRef;
28 import akka.util.Timeout;
29 import com.google.common.collect.Lists;
30 import com.google.common.util.concurrent.CheckedFuture;
31 import com.google.common.util.concurrent.Futures;
32 import java.net.InetAddress;
33 import java.net.InetSocketAddress;
34 import java.util.List;
35 import java.util.concurrent.TimeUnit;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.mockito.Mock;
42 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
43 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
44 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
45 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
46 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
47 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
48 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
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     @Mock
84     private DOMMountPointService mountPointService;
85
86     private NormalizedNode<?, ?> testNode;
87     private YangInstanceIdentifier instanceIdentifier;
88     private LogicalDatastoreType storeType;
89
90     @Before
91     public void setup() throws Exception {
92         initMocks(this);
93
94         system = ActorSystem.create();
95
96         final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
97                 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
98
99         final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
100         doReturn(Duration.apply(0, TimeUnit.SECONDS)).when(setup).getIdleTimeout();
101         final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
102                 DEFAULT_SCHEMA_REPOSITORY, TIMEOUT, mountPointService);
103
104         masterRef = TestActorRef.create(system, props, "master_read");
105
106         sourceIdentifiers = Lists.newArrayList();
107
108         final DOMDataReadOnlyTransaction readTx = mock(DOMDataReadOnlyTransaction.class);
109
110         doReturn(writeTx).when(deviceDataBroker).newWriteOnlyTransaction();
111         doReturn(readTx).when(deviceDataBroker).newReadOnlyTransaction();
112         doNothing().when(writeTx).put(storeType, instanceIdentifier, testNode);
113         doNothing().when(writeTx).merge(storeType, instanceIdentifier, testNode);
114         doNothing().when(writeTx).delete(storeType, instanceIdentifier);
115
116         // Create slave data broker for testing proxy
117         slaveDataBroker =
118                 new ProxyDOMDataBroker(system, remoteDeviceId, masterRef, Timeout.apply(5, TimeUnit.SECONDS));
119         initializeDataTest();
120         testNode = ImmutableContainerNodeBuilder.create()
121                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
122                 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
123         instanceIdentifier = YangInstanceIdentifier.EMPTY;
124         storeType = LogicalDatastoreType.CONFIGURATION;
125     }
126
127     @After
128     public void teardown() {
129         JavaTestKit.shutdownActorSystem(system, null, true);
130         system = null;
131     }
132
133     @Test
134     public void testPut() throws Exception {
135         // Test of invoking put on master through slave proxy
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     @Test
145     public void testMerge() throws Exception {
146         // Test of invoking merge on master through slave proxy
147         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
148         wTx.merge(storeType, instanceIdentifier, testNode);
149
150         verify(writeTx, timeout(2000)).merge(storeType, instanceIdentifier, testNode);
151
152         wTx.cancel();
153     }
154
155     @Test
156     public void testDelete() throws Exception {
157         final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
158         final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
159
160         // Test of invoking delete on master through slave proxy
161         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
162         wTx.delete(storeType, instanceIdentifier);
163         wTx.cancel();
164
165         verify(writeTx, timeout(2000)).delete(storeType, instanceIdentifier);
166     }
167
168     @Test
169     public void testSubmit() throws Exception {
170         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
171         doReturn(resultSubmit).when(writeTx).submit();
172
173         // Without Tx
174         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
175
176         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitResponse = wTx.submit();
177
178         final Object result = resultSubmitResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
179
180         assertNull(result);
181     }
182
183     @Test
184     public void testSubmitWithOperation() throws Exception {
185         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTx = Futures.immediateCheckedFuture(null);
186         doReturn(resultSubmitTx).when(writeTx).submit();
187         // With Tx
188         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
189         wTx.delete(LogicalDatastoreType.CONFIGURATION,
190                 YangInstanceIdentifier.EMPTY);
191
192         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTxResponse = wTx.submit();
193
194         final Object resultTx = resultSubmitTxResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
195
196         assertNull(resultTx);
197     }
198
199     @Test
200     public void testSubmitFail() throws Exception {
201         final TransactionCommitFailedException throwable = new TransactionCommitFailedException("Fail", null);
202         final CheckedFuture<Void, TransactionCommitFailedException> resultThrowable =
203                 Futures.immediateFailedCheckedFuture(throwable);
204         doReturn(resultThrowable).when(writeTx).submit();
205
206         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
207         wTx.delete(LogicalDatastoreType.CONFIGURATION,
208                 YangInstanceIdentifier.EMPTY);
209         final CheckedFuture<Void, TransactionCommitFailedException> resultThrowableResponse =
210                 wTx.submit();
211         exception.expect(TransactionCommitFailedException.class);
212         resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
213     }
214
215     @Test
216     public void testCancel() throws Exception {
217         doReturn(true).when(writeTx).cancel();
218
219         // Without Tx
220         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
221         final Boolean resultFalseNoTx = wTx.cancel();
222         assertEquals(true, resultFalseNoTx);
223     }
224
225     @Test
226     public void testCancelWithOperation() throws Exception {
227         doReturn(true).when(writeTx).cancel();
228
229         // With Tx, readWriteTx test
230         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
231         wTx.delete(LogicalDatastoreType.CONFIGURATION,
232                 YangInstanceIdentifier.EMPTY);
233
234         final Boolean resultTrue = wTx.cancel();
235         assertEquals(true, resultTrue);
236
237         final Boolean resultFalse = wTx.cancel();
238         assertEquals(false, resultFalse);
239     }
240
241     private void initializeDataTest() throws Exception {
242         final Future<Object> initialDataToActor =
243                 Patterns.ask(masterRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
244                                 domRpcService), TIMEOUT);
245
246         final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
247
248         assertTrue(success instanceof MasterActorDataInitialized);
249     }
250
251 }