Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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.ui.model.topo; |
| 18 | |
| 19 | import org.junit.Test; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 20 | import org.onlab.packet.MacAddress; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.DefaultLink; |
| 23 | import org.onosproject.net.DeviceId; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 24 | import org.onosproject.net.HostId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 25 | import org.onosproject.net.Link; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.provider.ProviderId; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 28 | import org.onosproject.net.region.RegionId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.model.AbstractUiModelTest; |
| 30 | |
| 31 | import static org.junit.Assert.assertEquals; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 32 | import static org.junit.Assert.assertNotEquals; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 33 | import static org.junit.Assert.assertNull; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 34 | import static org.onosproject.net.DeviceId.deviceId; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 35 | import static org.onosproject.net.HostId.hostId; |
| 36 | import static org.onosproject.net.PortNumber.P0; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 37 | import static org.onosproject.net.PortNumber.portNumber; |
| 38 | |
| 39 | /** |
| 40 | * Unit tests for {@link UiLinkId}. |
| 41 | */ |
| 42 | public class UiLinkIdTest extends AbstractUiModelTest { |
| 43 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 44 | private static final RegionId REG_1 = RegionId.regionId("Region-1"); |
| 45 | private static final RegionId REG_2 = RegionId.regionId("Region-2"); |
| 46 | |
| 47 | private static final MacAddress MAC_A = MacAddress.valueOf(0x123456L); |
| 48 | private static final HostId HOST_A = hostId(MAC_A); |
| 49 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 50 | private static final DeviceId DEV_X = deviceId("device-X"); |
| 51 | private static final DeviceId DEV_Y = deviceId("device-Y"); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 52 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 53 | private static final PortNumber P1 = portNumber(1); |
| 54 | private static final PortNumber P2 = portNumber(2); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 55 | private static final PortNumber P3 = portNumber(3); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 56 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 57 | private static final ConnectPoint CP_X1 = new ConnectPoint(DEV_X, P1); |
| 58 | private static final ConnectPoint CP_Y2 = new ConnectPoint(DEV_Y, P2); |
| 59 | private static final ConnectPoint CP_Y3 = new ConnectPoint(DEV_Y, P3); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 60 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 61 | private static final ConnectPoint CP_HA = new ConnectPoint(HOST_A, P0); |
| 62 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 63 | private static final Link LINK_X1_TO_Y2 = DefaultLink.builder() |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 64 | .providerId(ProviderId.NONE) |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 65 | .src(CP_X1) |
| 66 | .dst(CP_Y2) |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 67 | .type(Link.Type.DIRECT) |
| 68 | .build(); |
| 69 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 70 | private static final Link LINK_Y2_TO_X1 = DefaultLink.builder() |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 71 | .providerId(ProviderId.NONE) |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 72 | .src(CP_Y2) |
| 73 | .dst(CP_X1) |
| 74 | .type(Link.Type.DIRECT) |
| 75 | .build(); |
| 76 | |
| 77 | private static final Link LINK_X1_TO_Y3 = DefaultLink.builder() |
| 78 | .providerId(ProviderId.NONE) |
| 79 | .src(CP_X1) |
| 80 | .dst(CP_Y3) |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 81 | .type(Link.Type.DIRECT) |
| 82 | .build(); |
| 83 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 84 | private static final Link LINK_HA_TO_X1 = DefaultLink.builder() |
| 85 | .providerId(ProviderId.NONE) |
| 86 | .src(CP_HA) |
| 87 | .dst(CP_X1) |
| 88 | .type(Link.Type.EDGE) |
| 89 | .build(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 90 | |
| 91 | @Test |
| 92 | public void canonical() { |
| 93 | title("canonical"); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 94 | UiLinkId one = UiLinkId.uiLinkId(LINK_X1_TO_Y2); |
| 95 | UiLinkId two = UiLinkId.uiLinkId(LINK_Y2_TO_X1); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 96 | print("link one: %s", one); |
| 97 | print("link two: %s", two); |
| 98 | assertEquals("not equiv", one, two); |
| 99 | } |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 100 | |
| 101 | @Test |
| 102 | public void sameDevsDiffPorts() { |
| 103 | title("sameDevsDiffPorts"); |
| 104 | UiLinkId one = UiLinkId.uiLinkId(LINK_X1_TO_Y2); |
| 105 | UiLinkId other = UiLinkId.uiLinkId(LINK_X1_TO_Y3); |
| 106 | print("link one: %s", one); |
| 107 | print("link other: %s", other); |
| 108 | assertNotEquals("equiv?", one, other); |
| 109 | } |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 110 | |
| 111 | @Test |
| 112 | public void edgeLink() { |
| 113 | title("edgeLink"); |
| 114 | UiLinkId id = UiLinkId.uiLinkId(LINK_HA_TO_X1); |
| 115 | print("link: %s", id); |
| 116 | assertEquals("wrong port A", P0, id.portA()); |
| 117 | assertEquals("wrong element A", HOST_A, id.elementA()); |
| 118 | assertEquals("wrong port B", P1, id.portB()); |
| 119 | assertEquals("wrong element B", DEV_X, id.elementB()); |
| 120 | assertNull("region A?", id.regionA()); |
| 121 | assertNull("region B?", id.regionB()); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void deviceLink() { |
| 126 | title("deviceLink"); |
| 127 | UiLinkId id = UiLinkId.uiLinkId(LINK_X1_TO_Y2); |
| 128 | print("link: %s", id); |
| 129 | assertEquals("wrong port A", P1, id.portA()); |
| 130 | assertEquals("wrong element A", DEV_X, id.elementA()); |
| 131 | assertEquals("wrong port B", P2, id.portB()); |
| 132 | assertEquals("wrong element B", DEV_Y, id.elementB()); |
| 133 | assertNull("region A?", id.regionA()); |
| 134 | assertNull("region B?", id.regionB()); |
| 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void regionLink() { |
| 139 | title("regionLink"); |
| 140 | UiLinkId idFirst = UiLinkId.uiLinkId(REG_1, REG_2); |
| 141 | UiLinkId idSecond = UiLinkId.uiLinkId(REG_2, REG_1); |
| 142 | print(" first: %s", idFirst); |
| 143 | print("second: %s", idSecond); |
| 144 | assertEquals("Not same ID", idFirst, idSecond); |
| 145 | } |
| 146 | |
| 147 | @Test(expected = IllegalArgumentException.class) |
| 148 | public void identicalRegionBad() { |
| 149 | UiLinkId.uiLinkId(REG_1, REG_1); |
| 150 | } |
| 151 | |
| 152 | @Test(expected = NullPointerException.class) |
| 153 | public void nullRegionBad() { |
| 154 | UiLinkId.uiLinkId(REG_1, (RegionId) null); |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void regionDeviceLink() { |
| 159 | title("regionDeviceLink"); |
| 160 | UiLinkId id = UiLinkId.uiLinkId(REG_1, DEV_X, P1); |
| 161 | print("id: %s", id); |
| 162 | assertEquals("region ID", REG_1, id.regionA()); |
| 163 | assertEquals("device ID", DEV_X, id.elementB()); |
| 164 | assertEquals("port", P1, id.portB()); |
| 165 | } |
| 166 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 167 | } |