58422776f55b2a3329fef144d980581dc4b05858
[netconf.git] / netconf / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / tx / ReadOnlyTransactionTest.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 org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.MockitoAnnotations.initMocks;
16 import static org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
17
18 import akka.actor.ActorRef;
19 import akka.actor.ActorSystem;
20 import akka.actor.Props;
21 import akka.pattern.Patterns;
22 import akka.testkit.JavaTestKit;
23 import akka.testkit.TestActorRef;
24 import akka.util.Timeout;
25 import com.google.common.base.Optional;
26 import com.google.common.collect.Lists;
27 import com.google.common.util.concurrent.CheckedFuture;
28 import com.google.common.util.concurrent.Futures;
29 import java.net.InetAddress;
30 import java.net.InetSocketAddress;
31 import java.net.UnknownHostException;
32 import java.util.List;
33 import java.util.concurrent.TimeUnit;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.mockito.Mock;
40 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
41 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
42 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
43 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
44 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
45 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
46 import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
47 import org.opendaylight.netconf.topology.singleton.impl.NetconfDOMDataBroker;
48 import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor;
49 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
50 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
51 import org.opendaylight.netconf.topology.singleton.messages.MasterActorDataInitialized;
52 import org.opendaylight.yangtools.yang.common.QName;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
54 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
55 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
56 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
57 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
58 import scala.concurrent.Await;
59 import scala.concurrent.Future;
60 import scala.concurrent.duration.Duration;
61
62 public class ReadOnlyTransactionTest {
63     private static final Timeout TIMEOUT = new Timeout(Duration.create(5, "seconds"));
64     private static final int TIMEOUT_SEC = 5;
65     private static ActorSystem system;
66
67     @Rule
68     public final ExpectedException exception = ExpectedException.none();
69
70     private ActorRef masterRef;
71     private NetconfDOMDataBroker slaveDataBroker;
72     private DOMDataBroker masterDataBroker;
73     private List<SourceIdentifier> sourceIdentifiers;
74
75     @Mock
76     private DOMDataReadOnlyTransaction readTx;
77
78     @Mock
79     private DOMRpcService domRpcService;
80
81     @Before
82     public void setup() throws UnknownHostException {
83         initMocks(this);
84
85         system = ActorSystem.create();
86
87         final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
88                 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
89
90         final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
91         final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
92                 DEFAULT_SCHEMA_REPOSITORY, TIMEOUT);
93
94         masterRef = TestActorRef.create(system, props, "master_read");
95
96         sourceIdentifiers = Lists.newArrayList();
97
98         // Create master data broker
99
100         final DOMDataBroker delegateDataBroker = mock(DOMDataBroker.class);
101         readTx = mock(DOMDataReadOnlyTransaction.class);
102
103         doReturn(readTx).when(delegateDataBroker).newReadOnlyTransaction();
104
105         final NetconfDOMTransaction masterDOMTransactions =
106                 new NetconfMasterDOMTransaction(remoteDeviceId, delegateDataBroker);
107
108         masterDataBroker =
109                 new NetconfDOMDataBroker(system, remoteDeviceId, masterDOMTransactions);
110
111         // Create slave data broker for testing proxy
112
113         final NetconfDOMTransaction proxyDOMTransactions =
114                 new NetconfProxyDOMTransaction(remoteDeviceId, system, masterRef, TIMEOUT);
115
116         slaveDataBroker = new NetconfDOMDataBroker(system, remoteDeviceId, proxyDOMTransactions);
117
118
119     }
120
121     @After
122     public void teardown() {
123         JavaTestKit.shutdownActorSystem(system);
124         system = null;
125     }
126
127     @Test
128     public void testRead() throws Exception {
129
130         /* Initialize data on master */
131
132         initializeDataTest();
133
134         final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
135         final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
136
137         // Message: EmptyReadResponse
138
139         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultEmpty =
140                 Futures.immediateCheckedFuture(Optional.absent());
141
142         doReturn(resultEmpty).when(readTx).read(storeType, instanceIdentifier);
143
144         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultEmptyResponse =
145                 slaveDataBroker.newReadOnlyTransaction().read(storeType,
146                         instanceIdentifier);
147
148         final Optional<NormalizedNode<?, ?>> resultEmptyMessage =
149                 resultEmptyResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
150
151         assertEquals(resultEmptyMessage, Optional.absent());
152
153         // Message: NormalizedNodeMessage
154
155         final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
156                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
157                 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
158
159         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNormalizedNodeMessage =
160                 Futures.immediateCheckedFuture(Optional.of(outputNode));
161
162         doReturn(resultNormalizedNodeMessage).when(readTx).read(storeType, instanceIdentifier);
163
164         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNodeMessageResponse =
165                 slaveDataBroker.newReadOnlyTransaction().read(storeType, instanceIdentifier);
166
167         final Optional<NormalizedNode<?, ?>> resultNodeMessage =
168                 resultNodeMessageResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
169
170         assertTrue(resultNodeMessage.isPresent());
171         assertEquals(resultNodeMessage.get(), outputNode);
172
173         // Message: Throwable
174
175         final ReadFailedException readFailedException = new ReadFailedException("Fail", null);
176         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultThrowable =
177                 Futures.immediateFailedCheckedFuture(readFailedException);
178
179         doReturn(resultThrowable).when(readTx).read(storeType, instanceIdentifier);
180
181         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultThrowableResponse =
182                 slaveDataBroker.newReadOnlyTransaction().read(storeType, instanceIdentifier);
183
184         exception.expect(ReadFailedException.class);
185         resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
186
187     }
188
189     @Test
190     public void testExist() throws Exception {
191
192         /* Initialize data on master */
193
194         initializeDataTest();
195
196         final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
197         final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
198
199         // Message: True
200
201         final CheckedFuture<Boolean, ReadFailedException> resultTrue =
202                 Futures.immediateCheckedFuture(true);
203
204         doReturn(resultTrue).when(readTx).exists(storeType, instanceIdentifier);
205
206         final CheckedFuture<Boolean, ReadFailedException> trueResponse =
207                 slaveDataBroker.newReadOnlyTransaction().exists(storeType, instanceIdentifier);
208
209         final Boolean trueMessage = trueResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
210
211         assertEquals(true, trueMessage);
212
213         // Message: False
214
215         final CheckedFuture<Boolean, ReadFailedException> resultFalse = Futures.immediateCheckedFuture(false);
216
217         doReturn(resultFalse).when(readTx).exists(storeType, instanceIdentifier);
218
219         final CheckedFuture<Boolean, ReadFailedException> falseResponse =
220                 slaveDataBroker.newReadOnlyTransaction().exists(storeType,
221                         instanceIdentifier);
222
223         final Boolean falseMessage = falseResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
224
225         assertEquals(false, falseMessage);
226
227         // Message: False, result null
228
229         final CheckedFuture<Boolean, ReadFailedException> resultNull = Futures.immediateCheckedFuture(null);
230
231         doReturn(resultNull).when(readTx).exists(storeType, instanceIdentifier);
232
233         final CheckedFuture<Boolean, ReadFailedException> nullResponse =
234                 slaveDataBroker.newReadOnlyTransaction().exists(storeType,
235                         instanceIdentifier);
236
237         final Boolean nullFalseMessage = nullResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
238
239         assertEquals(false, nullFalseMessage);
240
241         // Message: Throwable
242
243         final ReadFailedException readFailedException = new ReadFailedException("Fail", null);
244         final CheckedFuture<Boolean, ReadFailedException> resultThrowable =
245                 Futures.immediateFailedCheckedFuture(readFailedException);
246
247         doReturn(resultThrowable).when(readTx).exists(storeType, instanceIdentifier);
248
249         final CheckedFuture<Boolean, ReadFailedException> resultThrowableResponse =
250                 slaveDataBroker.newReadOnlyTransaction().exists(storeType, instanceIdentifier);
251
252         exception.expect(ReadFailedException.class);
253         resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
254
255     }
256
257     private void initializeDataTest() throws Exception {
258         final Future<Object> initialDataToActor =
259                 Patterns.ask(masterRef, new CreateInitialMasterActorData(masterDataBroker, sourceIdentifiers,
260                                 domRpcService), TIMEOUT);
261
262         final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
263
264         assertTrue(success instanceof MasterActorDataInitialized);
265     }
266 }