blob: 62e5cfa696deb2143079bd6d049c000d751ddf71 [file] [log] [blame]
Frank Wangcfffbaa2017-05-06 16:11:08 +08001/*
2* Copyright 2017-present Open Networking Foundation
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17package org.onosproject.incubator.protobuf.services.nb;
18
19import com.google.common.collect.ImmutableSet;
20import io.grpc.BindableService;
21import io.grpc.inprocess.InProcessChannelBuilder;
22import org.junit.AfterClass;
23import org.junit.BeforeClass;
24import org.junit.Test;
25import io.grpc.ManagedChannel;
26import org.onlab.packet.IpAddress;
27import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
29import org.onosproject.grpc.net.models.HostProtoOuterClass;
30import org.onosproject.incubator.protobuf.models.net.HostProtoTranslator;
31import org.onosproject.incubator.protobuf.models.net.HostIdProtoTranslator;
32import org.onosproject.incubator.protobuf.models.net.ConnectPointProtoTranslator;
33import org.onosproject.grpc.nb.net.host.HostServiceGrpc.HostServiceBlockingStub;
34import org.onosproject.net.ConnectPoint;
35import org.onosproject.net.DefaultHost;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.Host;
38import org.onosproject.net.HostId;
39import org.onosproject.net.HostLocation;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.host.HostListener;
42import org.onosproject.net.host.HostService;
43
44import org.onosproject.grpc.nb.net.host.HostServiceGrpc;
45import org.onosproject.net.provider.ProviderId;
46import org.slf4j.Logger;
47
48import java.io.IOException;
49import java.util.ArrayList;
50import java.util.HashSet;
51import java.util.List;
52import java.util.Set;
53import java.util.stream.Collectors;
54
55import static org.junit.Assert.assertTrue;
56import static org.onosproject.grpc.nb.net.host.HostServiceNb.*;
57import static org.slf4j.LoggerFactory.getLogger;
58
59/**
60 * Unit tests of gRPC northbound host service.
61 */
62public class GrpcNbHostServiceTest {
63 private final Logger log = getLogger(getClass());
64
65 private static InProcessServer<BindableService> inprocessServer;
66 private static HostServiceBlockingStub blockingStub;
67 private static ManagedChannel channel;
68 private static final HostService MOCK_HOST = new MockHostService();
69 private static List<Host> allHosts = new ArrayList<>();
70 private static Host h1;
71 private static HostId id1;
72 private static IpAddress ip1;
73 private static MacAddress mac1;
74 private static DeviceId deviceId;
75 private static ConnectPoint c1;
76 private static boolean started = false;
77 private static boolean stopped = false;
78 private static boolean requestMac = false;
79
80 public GrpcNbHostServiceTest() {}
81
82 private static void populateHosts() {
83 ip1 = IpAddress.valueOf("10.1.1.1");
84 IpAddress ip2 = IpAddress.valueOf("10.1.1.2");
85 IpAddress ip3 = IpAddress.valueOf("10.1.1.3");
86 mac1 = MacAddress.valueOf("67:11:23:45:87:11");
87 MacAddress mac2 = MacAddress.valueOf("67:11:23:45:87:12");
88 MacAddress mac3 = MacAddress.valueOf("67:11:23:45:87:13");
89 id1 = HostId.hostId(mac1);
90 HostId id2 = HostId.hostId(mac2);
91 HostId id3 = HostId.hostId(mac3);
92 deviceId = DeviceId.deviceId("test");
93
94 c1 = new ConnectPoint(deviceId, PortNumber.portNumber(101));
95 HostLocation hostLocation1 = new HostLocation(deviceId, PortNumber.portNumber(101), 0);
96 HostLocation hostLocation2 = new HostLocation(deviceId, PortNumber.portNumber(102), 0);
97 HostLocation hostLocation3 = new HostLocation(deviceId, PortNumber.portNumber(103), 0);
98
99 h1 = new DefaultHost(ProviderId.NONE, id1, mac1, VlanId.NONE,
100 hostLocation1, ImmutableSet.of(ip1));
101 allHosts.add(h1);
102 allHosts.add(new DefaultHost(ProviderId.NONE, id2, mac2, VlanId.NONE,
103 hostLocation2, ImmutableSet.of(ip2)));
104 allHosts.add(new DefaultHost(ProviderId.NONE, id3, mac3, VlanId.NONE,
105 hostLocation3, ImmutableSet.of(ip3)));
106 }
107
108 /**
109 * Tests gRPC getComponentNames interface.
110 */
111 @Test
112 public void testGetHostCount() throws InterruptedException {
113 getHostCountRequest request = getHostCountRequest.getDefaultInstance();
114 getHostCountReply reply;
115
116 try {
117 reply = blockingStub.getHostCount(request);
118 assertTrue(allHosts.size() == reply.getHostCount());
119 } catch (Exception e) {
120 log.error("Get host count error! Exception={}", e.toString());
121 }
122 }
123
124 /**
125 * Tests gRPC getComponentNames interface.
126 */
127 @Test
128 public void testGetHosts() throws InterruptedException {
129 getHostsRequest request = getHostsRequest.getDefaultInstance();
130 getHostsReply reply;
131
132 try {
133 reply = blockingStub.getHosts(request);
134 Set<Host> actualHosts = new HashSet<>();
135 for (HostProtoOuterClass.HostProto host : reply.getHostList()) {
136 actualHosts.add(HostProtoTranslator.translate(host));
137 }
138
139 Set<Host> expectedHosts = new HashSet<>();
140 for (Host h : allHosts) {
141 expectedHosts.add(h);
142 }
143 assertTrue(actualHosts.equals(expectedHosts));
144 } catch (Exception e) {
145 log.error("Get all hosts error! Exception={}", e.toString());
146 }
147 }
148
149 /**
150 * Tests gRPC getHost interface.
151 */
152 @Test
153 public void testGetHost() throws InterruptedException {
154 getHostRequest request = getHostRequest.newBuilder().setHostId(HostIdProtoTranslator.translate(id1)).build();
155 getHostReply reply;
156
157 try {
158 reply = blockingStub.getHost(request);
159 assertTrue(HostProtoTranslator.translate(reply.getHost()).equals(h1));
160 } catch (Exception e) {
161 log.error("Get host with hostId error! Exception={}", e.toString());
162 }
163 }
164
165 /**
166 * Tests gRPC getHostsByVlan interface.
167 */
168 @Test
169 public void testGetHostsByVlan() throws InterruptedException {
170 getHostsByVlanRequest request = getHostsByVlanRequest.newBuilder().setVlanId(VlanId.NONE.toString()).build();
171 getHostsByVlanReply reply;
172
173 try {
174 reply = blockingStub.getHostsByVlan(request);
175
176 Set<Host> actualHosts = new HashSet<>();
177 for (HostProtoOuterClass.HostProto host : reply.getHostList()) {
178 actualHosts.add(HostProtoTranslator.translate(host));
179 }
180
181 Set<Host> expectedHosts = new HashSet<>();
182 for (Host h : allHosts) {
183 expectedHosts.add(h);
184 }
185 assertTrue(actualHosts.equals(expectedHosts));
186 } catch (Exception e) {
187 log.error("Get hosts that belong to the specified VLAN error! Exception={}", e.toString());
188 }
189 }
190
191 /**
192 * Tests gRPC getHostsByMac interface.
193 */
194 @Test
195 public void testGetHostsByMac() throws InterruptedException {
196 getHostsByMacRequest request = getHostsByMacRequest.newBuilder().setMac(mac1.toString()).build();
197 getHostsByMacReply reply;
198
199 try {
200 reply = blockingStub.getHostsByMac(request);
201
202 Set<Host> actualHosts = new HashSet<>();
203 for (HostProtoOuterClass.HostProto host : reply.getHostList()) {
204 actualHosts.add(HostProtoTranslator.translate(host));
205 }
206 Set<Host> expectedHosts = new HashSet<>();
207 expectedHosts.add(allHosts.get(0));
208 assertTrue(actualHosts.equals(expectedHosts));
209 } catch (Exception e) {
210 log.error("Get hosts that have the specified MAC address error! Exception={}", e.toString());
211 }
212 }
213
214 /**
215 * Tests gRPC getHostsByIp interface.
216 */
217 @Test
218 public void testGetHostsByIp() throws InterruptedException {
219 getHostsByIpRequest request = getHostsByIpRequest.newBuilder().setIpAddress(ip1.toString()).build();
220 getHostsByIpReply reply;
221
222 try {
223 reply = blockingStub.getHostsByIp(request);
224
225 Set<Host> actualHosts = new HashSet<>();
226 for (HostProtoOuterClass.HostProto host : reply.getHostList()) {
227 actualHosts.add(HostProtoTranslator.translate(host));
228 }
229
230 Set<Host> expectedHosts = new HashSet<>();
231 expectedHosts.add(allHosts.get(0));
232 assertTrue(actualHosts.equals(expectedHosts));
233 } catch (Exception e) {
234 log.error("Get hosts that have the specified IP address error! Exception={}", e.toString());
235 }
236 }
237
238 /**
239 * Tests gRPC getConnectedHosts interface.
240 */
241 @Test
242 public void testGetConnectedHosts() throws InterruptedException {
243 getConnectedHostsRequest request = getConnectedHostsRequest.newBuilder()
244 .setConnectPoint(ConnectPointProtoTranslator.translate(c1))
245 .build();
246 getConnectedHostsReply reply;
247
248 try {
249 reply = blockingStub.getConnectedHosts(request);
250
251 Set<Host> actualHosts = new HashSet<>();
252 for (HostProtoOuterClass.HostProto host : reply.getHostList()) {
253 actualHosts.add(HostProtoTranslator.translate(host));
254 }
255
256 Set<Host> expectedHosts = new HashSet<>();
257 expectedHosts.add(allHosts.get(0));
258 assertTrue(actualHosts.equals(expectedHosts));
259 } catch (Exception e) {
260 log.error("Get connected hosts with connect point error! Exception={}", e.toString());
261 }
262 }
263
264 /**
265 * Tests gRPC getConnectedHostsByDeviceId interface.
266 */
267 @Test
268 public void testGetConnectedHostsByDeviceId() throws InterruptedException {
269 getConnectedHostsRequest request = getConnectedHostsRequest.newBuilder()
270 .setDeviceId(deviceId.toString())
271 .build();
272 getConnectedHostsReply reply;
273
274 try {
275 reply = blockingStub.getConnectedHosts(request);
276
277 Set<Host> actualHosts = new HashSet<>();
278 for (HostProtoOuterClass.HostProto host : reply.getHostList()) {
279 actualHosts.add(HostProtoTranslator.translate(host));
280 }
281
282 Set<Host> expectedHosts = new HashSet<>();
283 for (Host h : allHosts) {
284 expectedHosts.add(h);
285 }
286 assertTrue(actualHosts.equals(expectedHosts));
287 } catch (Exception e) {
288 log.error("Get connected hosts with deviceId error! Exception={}", e.toString());
289 }
290 }
291
292 /**
293 * Tests gRPC startMonitoringIp interface.
294 */
295 @Test
296 public void testStartMonitoringIp() throws InterruptedException {
297 startMonitoringIpRequest request = startMonitoringIpRequest.newBuilder().setIpAddress(ip1.toString()).build();
298
299 try {
300 blockingStub.startMonitoringIp(request);
301 assertTrue(started);
302 } catch (Exception e) {
303 log.error("Start monitoring hosts with the given IP address error! Exception={}", e.toString());
304 }
305 }
306
307 /**
308 * Tests gRPC stopMonitoringIp interface.
309 */
310 @Test
311 public void testStopMonitoringIp() throws InterruptedException {
312 stopMonitoringIpRequest request = stopMonitoringIpRequest.newBuilder().setIpAddress(ip1.toString()).build();
313
314 try {
315 blockingStub.stopMonitoringIp(request);
316 assertTrue(stopped);
317 } catch (Exception e) {
318 log.error("Stop monitoring hosts with the given IP address error! Exception={}", e.toString());
319 }
320 }
321
322 /**
323 * Tests gRPC requestMac interface.
324 */
325 @Test
326 public void testRequestMac() throws InterruptedException {
327 requestMacRequest request = requestMacRequest.newBuilder().setIpAddress(ip1.toString()).build();
328
329 try {
330 blockingStub.requestMac(request);
331 assertTrue(requestMac);
332 } catch (Exception e) {
333 log.error("Resolve the MAC address for the given IP address error! Exception={}", e.toString());
334 }
335 }
336
337 /**
338 * Initialization before start testing gRPC northbound host service.
339 */
340 @BeforeClass
341 public static void beforeClass() throws InstantiationException, IllegalAccessException, IOException {
342 GrpcNbHostService hostService = new GrpcNbHostService();
343 hostService.hostService = MOCK_HOST;
344 inprocessServer = hostService.registerInProcessServer();
345
346 inprocessServer.start();
347 channel = InProcessChannelBuilder.forName("test").directExecutor()
348 // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
349 // needing certificates.
350 .usePlaintext(true).build();
351 blockingStub = HostServiceGrpc.newBlockingStub(channel);
352 populateHosts();
353 }
354
355 /**
356 * Finalization after test gRPC northbound host service.
357 */
358 @AfterClass
359 public static void afterClass() {
360
361 channel.shutdownNow();
362 inprocessServer.stop();
363 }
364
365 private static class MockHostService implements HostService {
366
367 MockHostService() {
368 }
369
370 @Override
371 public int getHostCount() {
372 return allHosts.size();
373 }
374
375 @Override
376 public Iterable<Host> getHosts() {
377 return allHosts;
378 }
379
380 @Override
381 public Host getHost(HostId hostId) {
382 return allHosts.stream().filter(h -> h.id().equals(hostId)).findFirst().get();
383 }
384
385 @Override
386 public Set<Host> getHostsByVlan(VlanId vlanId) {
387 return allHosts.stream().filter(h -> h.vlan().equals(vlanId)).collect(Collectors.toSet());
388 }
389
390 @Override
391 public Set<Host> getHostsByMac(MacAddress mac) {
392 return allHosts.stream().filter(h -> h.mac().equals(mac)).collect(Collectors.toSet());
393 }
394
395 @Override
396 public Set<Host> getHostsByIp(IpAddress ip) {
397 return allHosts.stream().filter(h -> h.ipAddresses().contains(ip)).collect(Collectors.toSet());
398 }
399
400 @Override
401 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
402 return allHosts.stream().filter(h -> h.location().deviceId().equals(connectPoint.deviceId())
403 && h.location().port().equals(connectPoint.port()))
404 .collect(Collectors.toSet());
405 }
406
407 @Override
408 public Set<Host> getConnectedHosts(DeviceId deviceId) {
409 return allHosts.stream().filter(h -> h.location().deviceId().equals(deviceId)).collect(Collectors.toSet());
410 }
411
412 @Override
413 public void startMonitoringIp(IpAddress ip) {
414 started = true;
415 }
416
417 @Override
418 public void stopMonitoringIp(IpAddress ip) {
419 stopped = true;
420 }
421
422 @Override
423 public void requestMac(IpAddress ip) {
424 requestMac = true;
425 }
426
427 @Override
428 public void addListener(HostListener listener) {
429 }
430
431 @Override
432 public void removeListener(HostListener listener) {
433 }
434 }
435}