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.netconf.sal.connect.util.RemoteDeviceId;
45 import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
46 import org.opendaylight.netconf.topology.singleton.impl.NetconfDOMDataBroker;
47 import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor;
48 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
49 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
50 import org.opendaylight.netconf.topology.singleton.messages.MasterActorDataInitialized;
51 import org.opendaylight.yangtools.yang.common.QName;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
54 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
55 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
56 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
57 import scala.concurrent.Await;
58 import scala.concurrent.Future;
59 import scala.concurrent.duration.Duration;
61 public class ReadOnlyTransactionTest {
62 private static final Timeout TIMEOUT = new Timeout(Duration.create(5, "seconds"));
63 private static final int TIMEOUT_SEC = 5;
64 private static ActorSystem system;
67 public final ExpectedException exception = ExpectedException.none();
69 private ActorRef masterRef;
70 private NetconfDOMDataBroker slaveDataBroker;
71 private DOMDataBroker masterDataBroker;
72 private List<SourceIdentifier> sourceIdentifiers;
75 private DOMDataReadOnlyTransaction readTx;
78 public void setup() throws UnknownHostException {
81 system = ActorSystem.create();
83 final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
84 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
86 final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
87 final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
88 DEFAULT_SCHEMA_REPOSITORY);
90 masterRef = TestActorRef.create(system, props, "master_read");
92 sourceIdentifiers = Lists.newArrayList();
94 // Create master data broker
96 final DOMDataBroker delegateDataBroker = mock(DOMDataBroker.class);
97 readTx = mock(DOMDataReadOnlyTransaction.class);
99 doReturn(readTx).when(delegateDataBroker).newReadOnlyTransaction();
101 final NetconfDOMTransaction masterDOMTransactions =
102 new NetconfMasterDOMTransaction(remoteDeviceId, delegateDataBroker);
105 new NetconfDOMDataBroker(system, remoteDeviceId, masterDOMTransactions);
107 // Create slave data broker for testing proxy
109 final NetconfDOMTransaction proxyDOMTransactions =
110 new NetconfProxyDOMTransaction(remoteDeviceId, system, masterRef);
112 slaveDataBroker = new NetconfDOMDataBroker(system, remoteDeviceId, proxyDOMTransactions);
118 public void teardown() {
119 JavaTestKit.shutdownActorSystem(system);
124 public void testRead() throws Exception {
126 /* Initialize data on master */
128 initializeDataTest();
130 final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
131 final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
133 // Message: EmptyReadResponse
135 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultEmpty =
136 Futures.immediateCheckedFuture(Optional.absent());
138 doReturn(resultEmpty).when(readTx).read(storeType, instanceIdentifier);
140 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultEmptyResponse =
141 slaveDataBroker.newReadOnlyTransaction().read(storeType,
144 final Optional<NormalizedNode<?, ?>> resultEmptyMessage =
145 resultEmptyResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
147 assertEquals(resultEmptyMessage, Optional.absent());
149 // Message: NormalizedNodeMessage
151 final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
152 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
153 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
155 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNormalizedNodeMessage =
156 Futures.immediateCheckedFuture(Optional.of(outputNode));
158 doReturn(resultNormalizedNodeMessage).when(readTx).read(storeType, instanceIdentifier);
160 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNodeMessageResponse =
161 slaveDataBroker.newReadOnlyTransaction().read(storeType, instanceIdentifier);
163 final Optional<NormalizedNode<?, ?>> resultNodeMessage =
164 resultNodeMessageResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
166 assertTrue(resultNodeMessage.isPresent());
167 assertEquals(resultNodeMessage.get(), outputNode);
169 // Message: Throwable
171 final ReadFailedException readFailedException = new ReadFailedException("Fail", null);
172 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultThrowable =
173 Futures.immediateFailedCheckedFuture(readFailedException);
175 doReturn(resultThrowable).when(readTx).read(storeType, instanceIdentifier);
177 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultThrowableResponse =
178 slaveDataBroker.newReadOnlyTransaction().read(storeType, instanceIdentifier);
180 exception.expect(ReadFailedException.class);
181 resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
186 public void testExist() throws Exception {
188 /* Initialize data on master */
190 initializeDataTest();
192 final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
193 final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
197 final CheckedFuture<Boolean, ReadFailedException> resultTrue =
198 Futures.immediateCheckedFuture(true);
200 doReturn(resultTrue).when(readTx).exists(storeType, instanceIdentifier);
202 final CheckedFuture<Boolean, ReadFailedException> trueResponse =
203 slaveDataBroker.newReadOnlyTransaction().exists(storeType, instanceIdentifier);
205 final Boolean trueMessage = trueResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
207 assertEquals(true, trueMessage);
211 final CheckedFuture<Boolean, ReadFailedException> resultFalse = Futures.immediateCheckedFuture(false);
213 doReturn(resultFalse).when(readTx).exists(storeType, instanceIdentifier);
215 final CheckedFuture<Boolean, ReadFailedException> falseResponse =
216 slaveDataBroker.newReadOnlyTransaction().exists(storeType,
219 final Boolean falseMessage = falseResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
221 assertEquals(false, falseMessage);
223 // Message: False, result null
225 final CheckedFuture<Boolean, ReadFailedException> resultNull = Futures.immediateCheckedFuture(null);
227 doReturn(resultNull).when(readTx).exists(storeType, instanceIdentifier);
229 final CheckedFuture<Boolean, ReadFailedException> nullResponse =
230 slaveDataBroker.newReadOnlyTransaction().exists(storeType,
233 final Boolean nullFalseMessage = nullResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
235 assertEquals(false, nullFalseMessage);
237 // Message: Throwable
239 final ReadFailedException readFailedException = new ReadFailedException("Fail", null);
240 final CheckedFuture<Boolean, ReadFailedException> resultThrowable =
241 Futures.immediateFailedCheckedFuture(readFailedException);
243 doReturn(resultThrowable).when(readTx).exists(storeType, instanceIdentifier);
245 final CheckedFuture<Boolean, ReadFailedException> resultThrowableResponse =
246 slaveDataBroker.newReadOnlyTransaction().exists(storeType, instanceIdentifier);
248 exception.expect(ReadFailedException.class);
249 resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
253 private void initializeDataTest() throws Exception {
254 final Future<Object> initialDataToActor =
255 Patterns.ask(masterRef, new CreateInitialMasterActorData(masterDataBroker, sourceIdentifiers),
258 final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
260 assertTrue(success instanceof MasterActorDataInitialized);