a77104e28dde8f96b2c1678ea9001bcdc80a933d
[netconf.git] / netconf / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / NetconfNodeActorTest.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;
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.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
16
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;
60
61 public class NetconfNodeActorTest {
62
63     private static final Timeout TIMEOUT = new Timeout(Duration.create(5, "seconds"));
64     private static ActorSystem system;
65
66     @Rule
67     public final ExpectedException exception = ExpectedException.none();
68
69     private ActorRef masterRef;
70     private RemoteDeviceId remoteDeviceId;
71
72     @Before
73     public void setup() throws UnknownHostException {
74
75         remoteDeviceId = new RemoteDeviceId("netconf-topology",
76                 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
77
78         final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
79
80         final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
81                 DEFAULT_SCHEMA_REPOSITORY);
82
83         system = ActorSystem.create();
84
85         masterRef = TestActorRef.create(system, props, "master_messages");
86     }
87
88     @After
89     public void teardown() {
90         JavaTestKit.shutdownActorSystem(system);
91         system = null;
92     }
93
94     @Test
95     public void testInitDataMessages() throws Exception {
96
97         final DOMDataBroker domDataBroker = mock(DOMDataBroker.class);
98         final List<SourceIdentifier> sourceIdentifiers = Lists.newArrayList();
99
100         /* Test init master data */
101
102         final Future<Object> initialDataToActor =
103                 Patterns.ask(masterRef, new CreateInitialMasterActorData(domDataBroker, sourceIdentifiers),
104                         TIMEOUT);
105
106         final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
107         assertTrue(success instanceof MasterActorDataInitialized);
108
109
110         /* Test refresh master data */
111
112         final RemoteDeviceId remoteDeviceId2 = new RemoteDeviceId("netconf-topology2",
113                 new InetSocketAddress(InetAddress.getByName("127.0.0.2"), 9999));
114
115         final NetconfTopologySetup setup2 = mock(NetconfTopologySetup.class);
116
117         final Future<Object> refreshDataToActor =
118                 Patterns.ask(masterRef, new RefreshSetupMasterActorData(setup2, remoteDeviceId2),
119                         TIMEOUT);
120
121         final Object success2 = Await.result(refreshDataToActor, TIMEOUT.duration());
122         assertTrue(success2 instanceof MasterActorDataInitialized);
123
124     }
125
126     @Test
127     public void testRegisterMountPointMessage() throws Exception {
128
129         final DOMDataBroker domDataBroker = mock(DOMDataBroker.class);
130         final List<SourceIdentifier> sourceIdentifiers =
131                 Lists.newArrayList(SourceIdentifier.create("testID", Optional.absent()));
132
133         // init master data
134
135         final Future<Object> initialDataToActor =
136                 Patterns.ask(masterRef, new CreateInitialMasterActorData(domDataBroker, sourceIdentifiers),
137                         TIMEOUT);
138
139         final Object successInit = Await.result(initialDataToActor, TIMEOUT.duration());
140
141         assertTrue(successInit instanceof MasterActorDataInitialized);
142
143         // test if slave get right identifiers from master
144
145         final Future<Object> registerMountPointFuture =
146                 Patterns.ask(masterRef, new AskForMasterMountPoint(),
147                         TIMEOUT);
148
149         final RegisterMountPoint success =
150                 (RegisterMountPoint) Await.result(registerMountPointFuture, TIMEOUT.duration());
151
152         assertEquals(sourceIdentifiers, success.getSourceIndentifiers());
153
154     }
155
156     @Test
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);
162
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();
166
167         final ProxyYangTextSourceProvider proxyYang =
168                 new ProxyYangTextSourceProvider(actorRefSchemaRepo, actorContext);
169         // test if asking for source is resolved and sended back
170
171         final YangTextSchemaSource yangTextSchemaSource = new YangTextSchemaSource(sourceIdentifier) {
172             @Override
173             protected MoreObjects.ToStringHelper addToStringAttributes(MoreObjects.ToStringHelper toStringHelper) {
174                 return null;
175             }
176
177             @Override
178             public InputStream openStream() throws IOException {
179                 return new ByteArrayInputStream("YANG".getBytes());
180             }
181         };
182
183
184         final CheckedFuture<YangTextSchemaSource, SchemaSourceException> result =
185                 Futures.immediateCheckedFuture(yangTextSchemaSource);
186
187         doReturn(result).when(schemaRepository).getSchemaSource(sourceIdentifier, YangTextSchemaSource.class);
188
189         final Future<YangTextSchemaSourceSerializationProxy> resolvedSchema =
190                 proxyYang.getYangTextSchemaSource(sourceIdentifier);
191
192         final YangTextSchemaSourceSerializationProxy success = Await.result(resolvedSchema, TIMEOUT.duration());
193
194         assertEquals(sourceIdentifier, success.getRepresentation().getIdentifier());
195         assertEquals("YANG", convertStreamToString(success.getRepresentation().openStream()));
196
197
198         // test if asking for source is missing
199         exception.expect(MissingSchemaSourceException.class);
200
201         final SchemaSourceException schemaSourceException =
202                 new MissingSchemaSourceException("Fail", sourceIdentifier);
203
204         final CheckedFuture<YangTextSchemaSource, SchemaSourceException> resultFail =
205                 Futures.immediateFailedCheckedFuture(schemaSourceException);
206
207         doReturn(resultFail).when(schemaRepository).getSchemaSource(sourceIdentifier, YangTextSchemaSource.class);
208
209         final Future<YangTextSchemaSourceSerializationProxy> failedSchema =
210                 proxyYang.getYangTextSchemaSource(sourceIdentifier);
211
212         Await.result(failedSchema, TIMEOUT.duration());
213
214     }
215
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() : "";
219     }
220
221 }