Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 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; |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 26 | import org.onosproject.net.LinkKey; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.provider.ProviderId; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 29 | import org.onosproject.net.region.RegionId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.model.AbstractUiModelTest; |
| 31 | |
| 32 | import static org.junit.Assert.assertEquals; |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 33 | import static org.junit.Assert.assertFalse; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 34 | import static org.junit.Assert.assertNotEquals; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 35 | import static org.junit.Assert.assertNull; |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 36 | import static org.junit.Assert.assertTrue; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 37 | import static org.onosproject.net.DeviceId.deviceId; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 38 | import static org.onosproject.net.HostId.hostId; |
| 39 | import static org.onosproject.net.PortNumber.P0; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 40 | import static org.onosproject.net.PortNumber.portNumber; |
| 41 | |
| 42 | /** |
| 43 | * Unit tests for {@link UiLinkId}. |
| 44 | */ |
| 45 | public class UiLinkIdTest extends AbstractUiModelTest { |
| 46 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 47 | private static final RegionId REG_1 = RegionId.regionId("Region-1"); |
| 48 | private static final RegionId REG_2 = RegionId.regionId("Region-2"); |
| 49 | |
| 50 | private static final MacAddress MAC_A = MacAddress.valueOf(0x123456L); |
| 51 | private static final HostId HOST_A = hostId(MAC_A); |
| 52 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 53 | private static final DeviceId DEV_X = deviceId("device-X"); |
| 54 | private static final DeviceId DEV_Y = deviceId("device-Y"); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 55 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 56 | private static final PortNumber P1 = portNumber(1); |
| 57 | private static final PortNumber P2 = portNumber(2); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 58 | private static final PortNumber P3 = portNumber(3); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 59 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 60 | private static final ConnectPoint CP_X1 = new ConnectPoint(DEV_X, P1); |
| 61 | private static final ConnectPoint CP_Y2 = new ConnectPoint(DEV_Y, P2); |
| 62 | private static final ConnectPoint CP_Y3 = new ConnectPoint(DEV_Y, P3); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 63 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 64 | private static final ConnectPoint CP_HA = new ConnectPoint(HOST_A, P0); |
| 65 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 66 | private static final Link LINK_X1_TO_Y2 = DefaultLink.builder() |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 67 | .providerId(ProviderId.NONE) |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 68 | .src(CP_X1) |
| 69 | .dst(CP_Y2) |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 70 | .type(Link.Type.DIRECT) |
| 71 | .build(); |
| 72 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 73 | private static final Link LINK_Y2_TO_X1 = DefaultLink.builder() |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 74 | .providerId(ProviderId.NONE) |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 75 | .src(CP_Y2) |
| 76 | .dst(CP_X1) |
| 77 | .type(Link.Type.DIRECT) |
| 78 | .build(); |
| 79 | |
| 80 | private static final Link LINK_X1_TO_Y3 = DefaultLink.builder() |
| 81 | .providerId(ProviderId.NONE) |
| 82 | .src(CP_X1) |
| 83 | .dst(CP_Y3) |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 84 | .type(Link.Type.DIRECT) |
| 85 | .build(); |
| 86 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 87 | private static final Link LINK_HA_TO_X1 = DefaultLink.builder() |
| 88 | .providerId(ProviderId.NONE) |
| 89 | .src(CP_HA) |
| 90 | .dst(CP_X1) |
| 91 | .type(Link.Type.EDGE) |
| 92 | .build(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 93 | |
| 94 | @Test |
| 95 | public void canonical() { |
| 96 | title("canonical"); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 97 | UiLinkId one = UiLinkId.uiLinkId(LINK_X1_TO_Y2); |
| 98 | UiLinkId two = UiLinkId.uiLinkId(LINK_Y2_TO_X1); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 99 | print("link one: %s", one); |
| 100 | print("link two: %s", two); |
| 101 | assertEquals("not equiv", one, two); |
| 102 | } |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 103 | |
| 104 | @Test |
| 105 | public void sameDevsDiffPorts() { |
| 106 | title("sameDevsDiffPorts"); |
| 107 | UiLinkId one = UiLinkId.uiLinkId(LINK_X1_TO_Y2); |
| 108 | UiLinkId other = UiLinkId.uiLinkId(LINK_X1_TO_Y3); |
| 109 | print("link one: %s", one); |
| 110 | print("link other: %s", other); |
| 111 | assertNotEquals("equiv?", one, other); |
| 112 | } |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 113 | |
| 114 | @Test |
| 115 | public void edgeLink() { |
| 116 | title("edgeLink"); |
| 117 | UiLinkId id = UiLinkId.uiLinkId(LINK_HA_TO_X1); |
| 118 | print("link: %s", id); |
| 119 | assertEquals("wrong port A", P0, id.portA()); |
| 120 | assertEquals("wrong element A", HOST_A, id.elementA()); |
| 121 | assertEquals("wrong port B", P1, id.portB()); |
| 122 | assertEquals("wrong element B", DEV_X, id.elementB()); |
| 123 | assertNull("region A?", id.regionA()); |
| 124 | assertNull("region B?", id.regionB()); |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 125 | assertEquals("not H-D", UiLinkId.Type.HOST_DEVICE, id.type()); |
| 126 | assertTrue("not host-dev", id.isHostDevice()); |
| 127 | assertFalse("dev-dev?", id.isDeviceDevice()); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void deviceLink() { |
| 132 | title("deviceLink"); |
| 133 | UiLinkId id = UiLinkId.uiLinkId(LINK_X1_TO_Y2); |
| 134 | print("link: %s", id); |
| 135 | assertEquals("wrong port A", P1, id.portA()); |
| 136 | assertEquals("wrong element A", DEV_X, id.elementA()); |
| 137 | assertEquals("wrong port B", P2, id.portB()); |
| 138 | assertEquals("wrong element B", DEV_Y, id.elementB()); |
| 139 | assertNull("region A?", id.regionA()); |
| 140 | assertNull("region B?", id.regionB()); |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 141 | assertEquals("not D-D", UiLinkId.Type.DEVICE_DEVICE, id.type()); |
| 142 | assertTrue("not dev-dev", id.isDeviceDevice()); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void regionLink() { |
| 147 | title("regionLink"); |
| 148 | UiLinkId idFirst = UiLinkId.uiLinkId(REG_1, REG_2); |
| 149 | UiLinkId idSecond = UiLinkId.uiLinkId(REG_2, REG_1); |
| 150 | print(" first: %s", idFirst); |
| 151 | print("second: %s", idSecond); |
| 152 | assertEquals("Not same ID", idFirst, idSecond); |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 153 | assertEquals("not R-R", UiLinkId.Type.REGION_REGION, idFirst.type()); |
| 154 | assertTrue("not reg-reg", idFirst.isRegionRegion()); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Test(expected = IllegalArgumentException.class) |
| 158 | public void identicalRegionBad() { |
| 159 | UiLinkId.uiLinkId(REG_1, REG_1); |
| 160 | } |
| 161 | |
| 162 | @Test(expected = NullPointerException.class) |
| 163 | public void nullRegionBad() { |
| 164 | UiLinkId.uiLinkId(REG_1, (RegionId) null); |
| 165 | } |
| 166 | |
| 167 | @Test |
| 168 | public void regionDeviceLink() { |
| 169 | title("regionDeviceLink"); |
| 170 | UiLinkId id = UiLinkId.uiLinkId(REG_1, DEV_X, P1); |
| 171 | print("id: %s", id); |
| 172 | assertEquals("region ID", REG_1, id.regionA()); |
| 173 | assertEquals("device ID", DEV_X, id.elementB()); |
| 174 | assertEquals("port", P1, id.portB()); |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 175 | assertEquals("not R-D", UiLinkId.Type.REGION_DEVICE, id.type()); |
| 176 | assertTrue("not reg-dev", id.isRegionDevice()); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 179 | @Test |
| 180 | public void fromLinkKey() { |
| 181 | title("fromLinkKey"); |
| 182 | |
| 183 | LinkKey lk1 = LinkKey.linkKey(CP_X1, CP_Y2); |
| 184 | print("link-key-1: %s", lk1); |
| 185 | LinkKey lk2 = LinkKey.linkKey(CP_Y2, CP_X1); |
| 186 | print("link-key-2: %s", lk2); |
| 187 | |
| 188 | UiLinkId id1 = UiLinkId.uiLinkId(lk1); |
| 189 | print("identifier-1: %s", id1); |
| 190 | UiLinkId id2 = UiLinkId.uiLinkId(lk2); |
| 191 | print("identifier-2: %s", id2); |
| 192 | |
| 193 | assertEquals("unequal canon-ids", id1, id2); |
| 194 | } |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 195 | |
| 196 | @Test |
| 197 | public void devToDevId() { |
| 198 | title("devToDevId"); |
| 199 | UiLinkId id = UiLinkId.uiLinkId(DEV_X, P1, DEV_Y, P2); |
| 200 | print(id); |
| 201 | assertEquals("not dev x", DEV_X, id.elementA()); |
| 202 | assertEquals("not dev y", DEV_Y, id.elementB()); |
| 203 | assertEquals("not port 1", P1, id.portA()); |
| 204 | assertEquals("not port 2", P2, id.portB()); |
| 205 | assertTrue("not dev-dev", id.isDeviceDevice()); |
| 206 | } |
| 207 | |
| 208 | @Test |
| 209 | public void devToDevCanon() { |
| 210 | title("devToDevCanon"); |
| 211 | UiLinkId id1 = UiLinkId.uiLinkId(DEV_X, P1, DEV_Y, P2); |
| 212 | UiLinkId id2 = UiLinkId.uiLinkId(DEV_Y, P2, DEV_X, P1); |
| 213 | print(id1); |
| 214 | print(id2); |
| 215 | assertEquals("not canonical", id1, id2); |
| 216 | assertEquals("not flipped", DEV_X, id2.elementA()); |
| 217 | } |
| 218 | |
| 219 | @Test |
| 220 | public void hostToDevId() { |
| 221 | title("hostToDevId"); |
| 222 | UiLinkId id = UiLinkId.uiLinkId(HOST_A, DEV_Y, P2); |
| 223 | print(id); |
| 224 | assertEquals("not host a", HOST_A, id.elementA()); |
| 225 | assertEquals("not port 0", P0, id.portA()); |
| 226 | assertEquals("not dev y", DEV_Y, id.elementB()); |
| 227 | assertEquals("not port 2", P2, id.portB()); |
| 228 | assertTrue("not host-dev", id.isHostDevice()); |
| 229 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 230 | } |