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;
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.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
17 import akka.actor.ActorContext;
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.MoreObjects;
26 import com.google.common.base.Optional;
27 import com.google.common.collect.Lists;
28 import com.google.common.util.concurrent.CheckedFuture;
29 import com.google.common.util.concurrent.Futures;
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.net.InetAddress;
34 import java.net.InetSocketAddress;
35 import java.net.UnknownHostException;
36 import java.util.List;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.opendaylight.controller.cluster.schema.provider.impl.YangTextSchemaSourceSerializationProxy;
43 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
44 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
45 import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor;
46 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
47 import org.opendaylight.netconf.topology.singleton.messages.AskForMasterMountPoint;
48 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
49 import org.opendaylight.netconf.topology.singleton.messages.MasterActorDataInitialized;
50 import org.opendaylight.netconf.topology.singleton.messages.RefreshSetupMasterActorData;
51 import org.opendaylight.netconf.topology.singleton.messages.RegisterMountPoint;
52 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
53 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
54 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
55 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
56 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
57 import scala.concurrent.Await;
58 import scala.concurrent.Future;
59 import scala.concurrent.duration.Duration;
61 public class NetconfNodeActorTest {
63 private static final Timeout TIMEOUT = new Timeout(Duration.create(5, "seconds"));
64 private static ActorSystem system;
67 public final ExpectedException exception = ExpectedException.none();
69 private ActorRef masterRef;
70 private RemoteDeviceId remoteDeviceId;
73 public void setup() throws UnknownHostException {
75 remoteDeviceId = new RemoteDeviceId("netconf-topology",
76 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
78 final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
80 final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
81 DEFAULT_SCHEMA_REPOSITORY);
83 system = ActorSystem.create();
85 masterRef = TestActorRef.create(system, props, "master_messages");
89 public void teardown() {
90 JavaTestKit.shutdownActorSystem(system);
95 public void testInitDataMessages() throws Exception {
97 final DOMDataBroker domDataBroker = mock(DOMDataBroker.class);
98 final List<SourceIdentifier> sourceIdentifiers = Lists.newArrayList();
100 /* Test init master data */
102 final Future<Object> initialDataToActor =
103 Patterns.ask(masterRef, new CreateInitialMasterActorData(domDataBroker, sourceIdentifiers),
106 final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
107 assertTrue(success instanceof MasterActorDataInitialized);
110 /* Test refresh master data */
112 final RemoteDeviceId remoteDeviceId2 = new RemoteDeviceId("netconf-topology2",
113 new InetSocketAddress(InetAddress.getByName("127.0.0.2"), 9999));
115 final NetconfTopologySetup setup2 = mock(NetconfTopologySetup.class);
117 final Future<Object> refreshDataToActor =
118 Patterns.ask(masterRef, new RefreshSetupMasterActorData(setup2, remoteDeviceId2),
121 final Object success2 = Await.result(refreshDataToActor, TIMEOUT.duration());
122 assertTrue(success2 instanceof MasterActorDataInitialized);
127 public void testRegisterMountPointMessage() throws Exception {
129 final DOMDataBroker domDataBroker = mock(DOMDataBroker.class);
130 final List<SourceIdentifier> sourceIdentifiers =
131 Lists.newArrayList(SourceIdentifier.create("testID", Optional.absent()));
135 final Future<Object> initialDataToActor =
136 Patterns.ask(masterRef, new CreateInitialMasterActorData(domDataBroker, sourceIdentifiers),
139 final Object successInit = Await.result(initialDataToActor, TIMEOUT.duration());
141 assertTrue(successInit instanceof MasterActorDataInitialized);
143 // test if slave get right identifiers from master
145 final Future<Object> registerMountPointFuture =
146 Patterns.ask(masterRef, new AskForMasterMountPoint(),
149 final RegisterMountPoint success =
150 (RegisterMountPoint) Await.result(registerMountPointFuture, TIMEOUT.duration());
152 assertEquals(sourceIdentifiers, success.getSourceIndentifiers());
157 public void testYangTextSchemaSourceRequestMessage() throws Exception {
158 final SchemaRepository schemaRepository = mock(SchemaRepository.class);
159 final SourceIdentifier sourceIdentifier = SourceIdentifier.create("testID", Optional.absent());
160 final Props props = NetconfNodeActor.props(mock(NetconfTopologySetup.class), remoteDeviceId,
161 DEFAULT_SCHEMA_REPOSITORY, schemaRepository);
163 final ActorRef actorRefSchemaRepo = TestActorRef.create(system, props, "master_mocked_schema_repository");
164 final ActorContext actorContext = mock(ActorContext.class);
165 doReturn(system.dispatcher()).when(actorContext).dispatcher();
167 final ProxyYangTextSourceProvider proxyYang =
168 new ProxyYangTextSourceProvider(actorRefSchemaRepo, actorContext);
169 // test if asking for source is resolved and sended back
171 final YangTextSchemaSource yangTextSchemaSource = new YangTextSchemaSource(sourceIdentifier) {
173 protected MoreObjects.ToStringHelper addToStringAttributes(MoreObjects.ToStringHelper toStringHelper) {
178 public InputStream openStream() throws IOException {
179 return new ByteArrayInputStream("YANG".getBytes());
184 final CheckedFuture<YangTextSchemaSource, SchemaSourceException> result =
185 Futures.immediateCheckedFuture(yangTextSchemaSource);
187 doReturn(result).when(schemaRepository).getSchemaSource(sourceIdentifier, YangTextSchemaSource.class);
189 final Future<YangTextSchemaSourceSerializationProxy> resolvedSchema =
190 proxyYang.getYangTextSchemaSource(sourceIdentifier);
192 final YangTextSchemaSourceSerializationProxy success = Await.result(resolvedSchema, TIMEOUT.duration());
194 assertEquals(sourceIdentifier, success.getRepresentation().getIdentifier());
195 assertEquals("YANG", convertStreamToString(success.getRepresentation().openStream()));
198 // test if asking for source is missing
199 exception.expect(MissingSchemaSourceException.class);
201 final SchemaSourceException schemaSourceException =
202 new MissingSchemaSourceException("Fail", sourceIdentifier);
204 final CheckedFuture<YangTextSchemaSource, SchemaSourceException> resultFail =
205 Futures.immediateFailedCheckedFuture(schemaSourceException);
207 doReturn(resultFail).when(schemaRepository).getSchemaSource(sourceIdentifier, YangTextSchemaSource.class);
209 final Future<YangTextSchemaSourceSerializationProxy> failedSchema =
210 proxyYang.getYangTextSchemaSource(sourceIdentifier);
212 Await.result(failedSchema, TIMEOUT.duration());
216 private String convertStreamToString(java.io.InputStream is) {
217 java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
218 return s.hasNext() ? s.next() : "";