2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.topology.singleton.impl.tx;
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;
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;
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;
68 public final ExpectedException exception = ExpectedException.none();
70 private ActorRef masterRef;
71 private NetconfDOMDataBroker slaveDataBroker;
72 private DOMDataBroker masterDataBroker;
73 private List<SourceIdentifier> sourceIdentifiers;
76 private DOMDataReadOnlyTransaction readTx;
79 private DOMRpcService domRpcService;
82 public void setup() throws UnknownHostException {
85 system = ActorSystem.create();
87 final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
88 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
90 final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
91 final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
92 DEFAULT_SCHEMA_REPOSITORY);
94 masterRef = TestActorRef.create(system, props, "master_read");
96 sourceIdentifiers = Lists.newArrayList();
98 // Create master data broker
100 final DOMDataBroker delegateDataBroker = mock(DOMDataBroker.class);
101 readTx = mock(DOMDataReadOnlyTransaction.class);
103 doReturn(readTx).when(delegateDataBroker).newReadOnlyTransaction();
105 final NetconfDOMTransaction masterDOMTransactions =
106 new NetconfMasterDOMTransaction(remoteDeviceId, delegateDataBroker);
109 new NetconfDOMDataBroker(system, remoteDeviceId, masterDOMTransactions);
111 // Create slave data broker for testing proxy
113 final NetconfDOMTransaction proxyDOMTransactions =
114 new NetconfProxyDOMTransaction(remoteDeviceId, system, masterRef);
116 slaveDataBroker = new NetconfDOMDataBroker(system, remoteDeviceId, proxyDOMTransactions);
122 public void teardown() {
123 JavaTestKit.shutdownActorSystem(system);
128 public void testRead() throws Exception {
130 /* Initialize data on master */
132 initializeDataTest();
134 final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
135 final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
137 // Message: EmptyReadResponse
139 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultEmpty =
140 Futures.immediateCheckedFuture(Optional.absent());
142 doReturn(resultEmpty).when(readTx).read(storeType, instanceIdentifier);
144 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultEmptyResponse =
145 slaveDataBroker.newReadOnlyTransaction().read(storeType,
148 final Optional<NormalizedNode<?, ?>> resultEmptyMessage =
149 resultEmptyResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
151 assertEquals(resultEmptyMessage, Optional.absent());
153 // Message: NormalizedNodeMessage
155 final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
156 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
157 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
159 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNormalizedNodeMessage =
160 Futures.immediateCheckedFuture(Optional.of(outputNode));
162 doReturn(resultNormalizedNodeMessage).when(readTx).read(storeType, instanceIdentifier);
164 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNodeMessageResponse =
165 slaveDataBroker.newReadOnlyTransaction().read(storeType, instanceIdentifier);
167 final Optional<NormalizedNode<?, ?>> resultNodeMessage =
168 resultNodeMessageResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
170 assertTrue(resultNodeMessage.isPresent());
171 assertEquals(resultNodeMessage.get(), outputNode);
173 // Message: Throwable
175 final ReadFailedException readFailedException = new ReadFailedException("Fail", null);
176 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultThrowable =
177 Futures.immediateFailedCheckedFuture(readFailedException);
179 doReturn(resultThrowable).when(readTx).read(storeType, instanceIdentifier);
181 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultThrowableResponse =
182 slaveDataBroker.newReadOnlyTransaction().read(storeType, instanceIdentifier);
184 exception.expect(ReadFailedException.class);
185 resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
190 public void testExist() throws Exception {
192 /* Initialize data on master */
194 initializeDataTest();
196 final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
197 final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
201 final CheckedFuture<Boolean, ReadFailedException> resultTrue =
202 Futures.immediateCheckedFuture(true);
204 doReturn(resultTrue).when(readTx).exists(storeType, instanceIdentifier);
206 final CheckedFuture<Boolean, ReadFailedException> trueResponse =
207 slaveDataBroker.newReadOnlyTransaction().exists(storeType, instanceIdentifier);
209 final Boolean trueMessage = trueResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
211 assertEquals(true, trueMessage);
215 final CheckedFuture<Boolean, ReadFailedException> resultFalse = Futures.immediateCheckedFuture(false);
217 doReturn(resultFalse).when(readTx).exists(storeType, instanceIdentifier);
219 final CheckedFuture<Boolean, ReadFailedException> falseResponse =
220 slaveDataBroker.newReadOnlyTransaction().exists(storeType,
223 final Boolean falseMessage = falseResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
225 assertEquals(false, falseMessage);
227 // Message: False, result null
229 final CheckedFuture<Boolean, ReadFailedException> resultNull = Futures.immediateCheckedFuture(null);
231 doReturn(resultNull).when(readTx).exists(storeType, instanceIdentifier);
233 final CheckedFuture<Boolean, ReadFailedException> nullResponse =
234 slaveDataBroker.newReadOnlyTransaction().exists(storeType,
237 final Boolean nullFalseMessage = nullResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
239 assertEquals(false, nullFalseMessage);
241 // Message: Throwable
243 final ReadFailedException readFailedException = new ReadFailedException("Fail", null);
244 final CheckedFuture<Boolean, ReadFailedException> resultThrowable =
245 Futures.immediateFailedCheckedFuture(readFailedException);
247 doReturn(resultThrowable).when(readTx).exists(storeType, instanceIdentifier);
249 final CheckedFuture<Boolean, ReadFailedException> resultThrowableResponse =
250 slaveDataBroker.newReadOnlyTransaction().exists(storeType, instanceIdentifier);
252 exception.expect(ReadFailedException.class);
253 resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
257 private void initializeDataTest() throws Exception {
258 final Future<Object> initialDataToActor =
259 Patterns.ask(masterRef, new CreateInitialMasterActorData(masterDataBroker, sourceIdentifiers,
260 domRpcService), TIMEOUT);
262 final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
264 assertTrue(success instanceof MasterActorDataInitialized);