shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.incubator.protobuf.services.nb; |
| 18 | |
| 19 | import com.google.common.collect.FluentIterable; |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
Jian Li | bfad56f2 | 2017-10-20 18:41:18 +0900 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import io.grpc.BindableService; |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 23 | import io.grpc.ManagedChannel; |
| 24 | import io.grpc.inprocess.InProcessChannelBuilder; |
Jian Li | bfad56f2 | 2017-10-20 18:41:18 +0900 | [diff] [blame] | 25 | import org.junit.AfterClass; |
| 26 | import org.junit.BeforeClass; |
| 27 | import org.junit.Test; |
| 28 | import org.onosproject.grpc.nb.net.link.LinkServiceGrpc; |
| 29 | import org.onosproject.grpc.nb.net.link.LinkServiceGrpc.LinkServiceBlockingStub; |
| 30 | import org.onosproject.grpc.nb.net.link.LinkServiceNb; |
| 31 | import org.onosproject.grpc.net.models.LinkProtoOuterClass; |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 32 | import org.onosproject.incubator.protobuf.models.net.ConnectPointProtoTranslator; |
| 33 | import org.onosproject.incubator.protobuf.models.net.LinkProtoTranslator; |
| 34 | import org.onosproject.net.ConnectPoint; |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 35 | import org.onosproject.net.DefaultLink; |
| 36 | import org.onosproject.net.DeviceId; |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 37 | import org.onosproject.net.Link; |
| 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.link.LinkService; |
| 40 | import org.onosproject.net.provider.ProviderId; |
| 41 | |
| 42 | import java.io.IOException; |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.HashSet; |
| 45 | import java.util.List; |
| 46 | import java.util.Set; |
| 47 | |
Jian Li | bfad56f2 | 2017-10-20 18:41:18 +0900 | [diff] [blame] | 48 | import static org.junit.Assert.assertTrue; |
| 49 | import static org.onosproject.net.DeviceId.deviceId; |
| 50 | import static org.onosproject.net.PortNumber.portNumber; |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 51 | |
| 52 | public class GrpcNbLinkServiceTest { |
Jian Li | bfad56f2 | 2017-10-20 18:41:18 +0900 | [diff] [blame] | 53 | private static InProcessServer<BindableService> inprocessServer; |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 54 | private static ManagedChannel channel; |
| 55 | private static LinkServiceBlockingStub blockingStub; |
| 56 | |
| 57 | private static final String D1 = "d1"; |
| 58 | private static final String D2 = "d2"; |
| 59 | private static final String D3 = "d3"; |
| 60 | private static final String D4 = "d4"; |
| 61 | private static final String D5 = "d5"; |
| 62 | private static final String D6 = "d6"; |
| 63 | private static final String D7 = "d7"; |
| 64 | private static final String D8 = "d8"; |
| 65 | private static final String D9 = "d9"; |
| 66 | |
| 67 | private static final String[][] LINK_CONNECT_DATA = { |
| 68 | {D1, "12", D2, "21"}, |
| 69 | {D2, "23", D3, "32"}, |
| 70 | {D4, "41", D1, "14"}, |
| 71 | {D5, "51", D1, "15"}, |
| 72 | {D6, "61", D1, "16"}, |
| 73 | {D7, "73", D3, "37"}, |
| 74 | {D8, "83", D3, "38"}, |
| 75 | {D9, "93", D3, "39"}, |
| 76 | }; |
| 77 | |
| 78 | private static final DeviceId DEVID_1 = deviceId(D1); |
| 79 | private static final PortNumber PORT_14 = portNumber("14"); |
| 80 | |
| 81 | private static final DeviceId DEVID_4 = deviceId(D4); |
| 82 | private static final PortNumber PORT_41 = portNumber("41"); |
| 83 | |
| 84 | private static final DeviceId DEVID_3 = deviceId(D3); |
| 85 | private static final PortNumber PORT_32 = portNumber("32"); |
| 86 | |
| 87 | private static final ConnectPoint DEVID_1_14 = new ConnectPoint(DEVID_1, PORT_14); |
| 88 | private static final ConnectPoint DEVID_4_41 = new ConnectPoint(DEVID_4, PORT_41); |
| 89 | |
| 90 | private static final ProviderId PID = new ProviderId("Test", "Test"); |
| 91 | |
| 92 | private static List<Link> allLinks = new ArrayList<Link>(); |
| 93 | |
| 94 | private static final LinkService MOCK_LINK = new MockLinkService(); |
| 95 | |
| 96 | /** |
| 97 | * Creates a list of links. |
| 98 | * |
| 99 | */ |
| 100 | private static void populateLinks() { |
| 101 | for (String[] linkPair : LINK_CONNECT_DATA) { |
| 102 | allLinks.addAll(makeLinkPair(linkPair)); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Synthesizes a pair of unidirectional links between two devices. The |
| 108 | * string array should be of the form: |
| 109 | * <pre> |
| 110 | * { "device-A-id", "device-A-port", "device-B-id", "device-B-port" } |
| 111 | * </pre> |
| 112 | * |
| 113 | * @param linkPairData device ids and ports |
| 114 | * @return pair of synthesized links |
| 115 | */ |
| 116 | private static List<Link> makeLinkPair(String[] linkPairData) { |
| 117 | DeviceId devA = deviceId(linkPairData[0]); |
| 118 | PortNumber portA = portNumber(linkPairData[1]); |
| 119 | |
| 120 | DeviceId devB = deviceId(linkPairData[2]); |
| 121 | PortNumber portB = portNumber(linkPairData[3]); |
| 122 | |
| 123 | Link linkA = DefaultLink.builder() |
| 124 | .providerId(PID) |
| 125 | .type(Link.Type.DIRECT) |
| 126 | .src(new ConnectPoint(devA, portA)) |
| 127 | .dst(new ConnectPoint(devB, portB)) |
| 128 | .build(); |
| 129 | |
| 130 | Link linkB = DefaultLink.builder() |
| 131 | .providerId(PID) |
| 132 | .type(Link.Type.DIRECT) |
| 133 | .src(new ConnectPoint(devB, portB)) |
| 134 | .dst(new ConnectPoint(devA, portA)) |
| 135 | .build(); |
| 136 | |
| 137 | return ImmutableList.of(linkA, linkB); |
| 138 | } |
| 139 | |
| 140 | public GrpcNbLinkServiceTest() { |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testGetLinkCount() throws InterruptedException { |
| 145 | LinkServiceNb.getLinkCountRequest request = LinkServiceNb.getLinkCountRequest.getDefaultInstance(); |
| 146 | LinkServiceNb.getLinkCountReply response; |
| 147 | |
| 148 | try { |
| 149 | response = blockingStub.getLinkCount(request); |
| 150 | int linkCount = response.getLinkCount(); |
| 151 | assertTrue(allLinks.size() == linkCount); |
| 152 | } catch (Exception e) { |
| 153 | e.printStackTrace(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void testGetLink() throws InterruptedException { |
| 159 | LinkServiceNb.getLinkRequest request = LinkServiceNb.getLinkRequest.newBuilder() |
| 160 | .setSrc(ConnectPointProtoTranslator.translate(DEVID_1_14)) |
| 161 | .setDst(ConnectPointProtoTranslator.translate(DEVID_4_41)) |
| 162 | .build(); |
| 163 | LinkServiceNb.getLinkReply response; |
| 164 | |
| 165 | try { |
| 166 | response = blockingStub.getLink(request); |
| 167 | LinkProtoOuterClass.LinkProto link = response.getLink(); |
| 168 | assertTrue(LinkProtoTranslator.translate(allLinks.get(5)).equals(link)); |
| 169 | } catch (Exception e) { |
| 170 | e.printStackTrace(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | @Test |
| 175 | public void testGetLinks() throws InterruptedException { |
| 176 | LinkServiceNb.getLinksRequest request = LinkServiceNb.getLinksRequest.newBuilder() |
| 177 | .setConnectPoint(ConnectPointProtoTranslator.translate(DEVID_1_14)) |
| 178 | .build(); |
| 179 | LinkServiceNb.getLinksReply response; |
| 180 | |
| 181 | try { |
| 182 | response = blockingStub.getLinks(request); |
| 183 | |
| 184 | Set<Link> actualLinks = new HashSet<Link>(); |
| 185 | for (LinkProtoOuterClass.LinkProto link : response.getLinkList()) { |
| 186 | actualLinks.add(LinkProtoTranslator.translate(link)); |
| 187 | } |
| 188 | Set<Link> expectedLinks = new HashSet<Link>(); |
| 189 | expectedLinks.add(allLinks.get(4)); |
| 190 | expectedLinks.add(allLinks.get(5)); |
| 191 | |
| 192 | assertTrue(expectedLinks.equals(actualLinks)); |
| 193 | } catch (Exception e) { |
| 194 | e.printStackTrace(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | @Test |
| 199 | public void testGetActiveLinks() throws InterruptedException { |
| 200 | LinkServiceNb.getActiveLinksRequest request = LinkServiceNb.getActiveLinksRequest.getDefaultInstance(); |
| 201 | LinkServiceNb.getActiveLinksReply response; |
| 202 | |
| 203 | try { |
| 204 | response = blockingStub.getActiveLinks(request); |
| 205 | |
| 206 | Set<Link> actualLinks = new HashSet<Link>(); |
| 207 | for (LinkProtoOuterClass.LinkProto link : response.getLinkList()) { |
| 208 | actualLinks.add(LinkProtoTranslator.translate(link)); |
| 209 | } |
| 210 | |
| 211 | Set<Link> expectedLinks = new HashSet<Link>(allLinks); |
| 212 | assertTrue((expectedLinks).equals(actualLinks)); |
| 213 | } catch (Exception e) { |
| 214 | e.printStackTrace(); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | @Test |
| 219 | public void testGetDeviceLinks() throws InterruptedException { |
| 220 | LinkServiceNb.getDeviceLinksRequest request = LinkServiceNb.getDeviceLinksRequest.newBuilder() |
| 221 | .setDeviceId(D1) |
| 222 | .build(); |
| 223 | LinkServiceNb.getDeviceLinksReply response; |
| 224 | |
| 225 | try { |
| 226 | response = blockingStub.getDeviceLinks(request); |
| 227 | |
| 228 | Set<Link> actualLinks = new HashSet<Link>(); |
| 229 | for (LinkProtoOuterClass.LinkProto link : response.getLinkList()) { |
| 230 | actualLinks.add(LinkProtoTranslator.translate(link)); |
| 231 | } |
| 232 | |
| 233 | Set<Link> expectedLinks = new HashSet<Link>(); |
| 234 | expectedLinks.add(allLinks.get(4)); |
| 235 | expectedLinks.add(allLinks.get(5)); |
| 236 | expectedLinks.add(allLinks.get(6)); |
| 237 | expectedLinks.add(allLinks.get(7)); |
| 238 | expectedLinks.add(allLinks.get(8)); |
| 239 | expectedLinks.add(allLinks.get(9)); |
| 240 | expectedLinks.add(allLinks.get(0)); |
| 241 | expectedLinks.add(allLinks.get(1)); |
| 242 | |
| 243 | assertTrue((expectedLinks).equals(actualLinks)); |
| 244 | } catch (Exception e) { |
| 245 | e.printStackTrace(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | @Test |
| 250 | public void testGetDeviceEgressLinks() throws InterruptedException { |
| 251 | LinkServiceNb.getDeviceEgressLinksRequest request = LinkServiceNb.getDeviceEgressLinksRequest.newBuilder() |
| 252 | .setDeviceId(D1) |
| 253 | .build(); |
| 254 | LinkServiceNb.getDeviceEgressLinksReply response; |
| 255 | |
| 256 | try { |
| 257 | response = blockingStub.getDeviceEgressLinks(request); |
| 258 | |
| 259 | Set<Link> actualLinks = new HashSet<Link>(); |
| 260 | for (LinkProtoOuterClass.LinkProto link : response.getLinkList()) { |
| 261 | actualLinks.add(LinkProtoTranslator.translate(link)); |
| 262 | } |
| 263 | Set<Link> expectedLinks = new HashSet<Link>(); |
| 264 | expectedLinks.add(allLinks.get(0)); |
| 265 | expectedLinks.add(allLinks.get(5)); |
| 266 | expectedLinks.add(allLinks.get(7)); |
| 267 | expectedLinks.add(allLinks.get(9)); |
| 268 | |
| 269 | assertTrue((expectedLinks).equals(actualLinks)); |
| 270 | } catch (Exception e) { |
| 271 | e.printStackTrace(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | @Test |
| 276 | public void testGetDeviceIngressLinks() throws InterruptedException { |
| 277 | LinkServiceNb.getDeviceIngressLinksRequest request = LinkServiceNb.getDeviceIngressLinksRequest.newBuilder() |
| 278 | .setDeviceId(D1) |
| 279 | .build(); |
| 280 | LinkServiceNb.getDeviceIngressLinksReply response; |
| 281 | |
| 282 | try { |
| 283 | response = blockingStub.getDeviceIngressLinks(request); |
| 284 | |
| 285 | Set<Link> actualLinks = new HashSet<Link>(); |
| 286 | for (LinkProtoOuterClass.LinkProto link : response.getLinkList()) { |
| 287 | actualLinks.add(LinkProtoTranslator.translate(link)); |
| 288 | } |
| 289 | |
| 290 | Set<Link> expectedLinks = new HashSet<Link>(); |
| 291 | expectedLinks.add(allLinks.get(1)); |
| 292 | expectedLinks.add(allLinks.get(4)); |
| 293 | expectedLinks.add(allLinks.get(6)); |
| 294 | expectedLinks.add(allLinks.get(8)); |
| 295 | |
| 296 | assertTrue((expectedLinks).equals(actualLinks)); |
| 297 | } catch (Exception e) { |
| 298 | e.printStackTrace(); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | @Test |
| 303 | public void testGetEgressLinks() throws InterruptedException { |
| 304 | LinkServiceNb.getEgressLinksRequest request = LinkServiceNb.getEgressLinksRequest.newBuilder() |
| 305 | .setConnectPoint(ConnectPointProtoTranslator.translate(DEVID_1_14)) |
| 306 | .build(); |
| 307 | LinkServiceNb.getEgressLinksReply response; |
| 308 | |
| 309 | try { |
| 310 | response = blockingStub.getEgressLinks(request); |
| 311 | |
| 312 | Set<Link> actualLinks = new HashSet<Link>(); |
| 313 | for (LinkProtoOuterClass.LinkProto link : response.getLinkList()) { |
| 314 | actualLinks.add(LinkProtoTranslator.translate(link)); |
| 315 | } |
| 316 | Set<Link> expectedLinks = new HashSet<Link>(); |
| 317 | expectedLinks.add(allLinks.get(5)); |
| 318 | |
| 319 | assertTrue((expectedLinks).equals(actualLinks)); |
| 320 | } catch (Exception e) { |
| 321 | e.printStackTrace(); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | @Test |
| 326 | public void testGetIngressLinks() throws InterruptedException { |
| 327 | LinkServiceNb.getIngressLinksRequest request = LinkServiceNb.getIngressLinksRequest.newBuilder() |
| 328 | .setConnectPoint(ConnectPointProtoTranslator.translate(DEVID_1_14)) |
| 329 | .build(); |
| 330 | LinkServiceNb.getIngressLinksReply response; |
| 331 | |
| 332 | try { |
| 333 | response = blockingStub.getIngressLinks(request); |
| 334 | |
| 335 | Set<Link> actualLinks = new HashSet<Link>(); |
| 336 | for (LinkProtoOuterClass.LinkProto link : response.getLinkList()) { |
| 337 | actualLinks.add(LinkProtoTranslator.translate(link)); |
| 338 | } |
| 339 | Set<Link> expectedLinks = new HashSet<Link>(); |
| 340 | expectedLinks.add(allLinks.get(4)); |
| 341 | |
| 342 | assertTrue((expectedLinks).equals(actualLinks)); |
| 343 | } catch (Exception e) { |
| 344 | e.printStackTrace(); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | |
| 349 | @BeforeClass |
| 350 | public static void beforeClass() throws InstantiationException, IllegalAccessException, IOException { |
Jian Li | bfad56f2 | 2017-10-20 18:41:18 +0900 | [diff] [blame] | 351 | GrpcNbLinkService linkService = new GrpcNbLinkService(); |
| 352 | linkService.linkService = MOCK_LINK; |
| 353 | inprocessServer = linkService.registerInProcessServer(); |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 354 | inprocessServer.start(); |
Jian Li | bfad56f2 | 2017-10-20 18:41:18 +0900 | [diff] [blame] | 355 | |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 356 | channel = InProcessChannelBuilder.forName("test").directExecutor() |
| 357 | // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid |
| 358 | // needing certificates. |
| 359 | .usePlaintext(true).build(); |
| 360 | blockingStub = LinkServiceGrpc.newBlockingStub(channel); |
| 361 | |
| 362 | populateLinks(); |
| 363 | } |
| 364 | |
| 365 | @AfterClass |
| 366 | public static void afterClass() { |
| 367 | channel.shutdownNow(); |
| 368 | |
| 369 | inprocessServer.stop(); |
| 370 | } |
| 371 | |
| 372 | private static class MockLinkService implements LinkService { |
| 373 | MockLinkService() { |
| 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public int getLinkCount() { |
| 378 | return allLinks.size(); |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public Iterable<Link> getLinks() { |
| 383 | return ImmutableSet.copyOf(allLinks); |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public Iterable<Link> getActiveLinks() { |
| 388 | return FluentIterable.from(getLinks()) |
| 389 | .filter(input -> input.state() == Link.State.ACTIVE); |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public Set<Link> getDeviceLinks(DeviceId deviceId) { |
| 394 | return FluentIterable.from(getLinks()) |
| 395 | .filter(input -> (input.src().deviceId().equals(deviceId)) || |
| 396 | (input.dst().deviceId().equals(deviceId))).toSet(); |
| 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public Set<Link> getDeviceEgressLinks(DeviceId deviceId) { |
| 401 | return FluentIterable.from(getLinks()) |
| 402 | .filter(input -> (input.src().deviceId().equals(deviceId))).toSet(); |
| 403 | } |
| 404 | |
| 405 | @Override |
| 406 | public Set<Link> getDeviceIngressLinks(DeviceId deviceId) { |
| 407 | return FluentIterable.from(getLinks()) |
| 408 | .filter(input -> (input.dst().deviceId().equals(deviceId))).toSet(); |
| 409 | } |
| 410 | |
| 411 | @Override |
| 412 | public Set<Link> getLinks(ConnectPoint connectPoint) { |
| 413 | return FluentIterable.from(getLinks()) |
| 414 | .filter(input -> (input.src().equals(connectPoint)) || (input.dst().equals(connectPoint))).toSet(); |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public Set<Link> getEgressLinks(ConnectPoint connectPoint) { |
| 419 | return FluentIterable.from(getLinks()) |
| 420 | .filter(input -> (input.src().equals(connectPoint))).toSet(); |
| 421 | } |
| 422 | |
| 423 | @Override |
| 424 | public Set<Link> getIngressLinks(ConnectPoint connectPoint) { |
| 425 | return FluentIterable.from(getLinks()) |
| 426 | .filter(input -> (input.dst().equals(connectPoint))).toSet(); |
| 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public Link getLink(ConnectPoint src, ConnectPoint dst) { |
| 431 | return FluentIterable.from(getLinks()) |
| 432 | .filter(input -> (input.src().equals(src)) && (input.dst().equals(dst))) |
| 433 | .first().get(); |
| 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public void addListener(org.onosproject.net.link.LinkListener listener) { |
| 438 | } |
| 439 | |
| 440 | @Override |
| 441 | public void removeListener(org.onosproject.net.link.LinkListener listener) { |
| 442 | } |
| 443 | } |
| 444 | } |