blob: 65e0750a954e1091e159ae635219401eefd4ddd0 [file] [log] [blame]
Simon Huntc0f20c12016-05-09 09:30:20 -07001/*
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
17package org.onosproject.ui.model.topo;
18
19import org.junit.Test;
Simon Huntc13082f2016-08-03 21:20:23 -070020import org.onlab.packet.MacAddress;
Simon Huntc0f20c12016-05-09 09:30:20 -070021import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.DefaultLink;
23import org.onosproject.net.DeviceId;
Simon Huntc13082f2016-08-03 21:20:23 -070024import org.onosproject.net.HostId;
Simon Huntc0f20c12016-05-09 09:30:20 -070025import org.onosproject.net.Link;
Simon Hunt0e161092017-05-08 17:41:38 -070026import org.onosproject.net.LinkKey;
Simon Huntc0f20c12016-05-09 09:30:20 -070027import org.onosproject.net.PortNumber;
28import org.onosproject.net.provider.ProviderId;
Simon Huntc13082f2016-08-03 21:20:23 -070029import org.onosproject.net.region.RegionId;
Simon Huntc0f20c12016-05-09 09:30:20 -070030import org.onosproject.ui.model.AbstractUiModelTest;
31
32import static org.junit.Assert.assertEquals;
Simon Huntbf59db22017-05-12 13:26:35 -070033import static org.junit.Assert.assertFalse;
Simon Hunt58a0dd02016-05-17 11:54:23 -070034import static org.junit.Assert.assertNotEquals;
Simon Huntc13082f2016-08-03 21:20:23 -070035import static org.junit.Assert.assertNull;
Simon Huntbf59db22017-05-12 13:26:35 -070036import static org.junit.Assert.assertTrue;
Simon Huntc0f20c12016-05-09 09:30:20 -070037import static org.onosproject.net.DeviceId.deviceId;
Simon Huntc13082f2016-08-03 21:20:23 -070038import static org.onosproject.net.HostId.hostId;
39import static org.onosproject.net.PortNumber.P0;
Simon Huntc0f20c12016-05-09 09:30:20 -070040import static org.onosproject.net.PortNumber.portNumber;
41
42/**
43 * Unit tests for {@link UiLinkId}.
44 */
45public class UiLinkIdTest extends AbstractUiModelTest {
46
Simon Huntc13082f2016-08-03 21:20:23 -070047 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 Huntc0f20c12016-05-09 09:30:20 -070053 private static final DeviceId DEV_X = deviceId("device-X");
54 private static final DeviceId DEV_Y = deviceId("device-Y");
Simon Huntc13082f2016-08-03 21:20:23 -070055
Simon Huntc0f20c12016-05-09 09:30:20 -070056 private static final PortNumber P1 = portNumber(1);
57 private static final PortNumber P2 = portNumber(2);
Simon Hunt58a0dd02016-05-17 11:54:23 -070058 private static final PortNumber P3 = portNumber(3);
Simon Huntc0f20c12016-05-09 09:30:20 -070059
Simon Hunt58a0dd02016-05-17 11:54:23 -070060 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 Huntc0f20c12016-05-09 09:30:20 -070063
Simon Huntc13082f2016-08-03 21:20:23 -070064 private static final ConnectPoint CP_HA = new ConnectPoint(HOST_A, P0);
65
Simon Hunt58a0dd02016-05-17 11:54:23 -070066 private static final Link LINK_X1_TO_Y2 = DefaultLink.builder()
Simon Huntc0f20c12016-05-09 09:30:20 -070067 .providerId(ProviderId.NONE)
Simon Hunt58a0dd02016-05-17 11:54:23 -070068 .src(CP_X1)
69 .dst(CP_Y2)
Simon Huntc0f20c12016-05-09 09:30:20 -070070 .type(Link.Type.DIRECT)
71 .build();
72
Simon Hunt58a0dd02016-05-17 11:54:23 -070073 private static final Link LINK_Y2_TO_X1 = DefaultLink.builder()
Simon Huntc0f20c12016-05-09 09:30:20 -070074 .providerId(ProviderId.NONE)
Simon Hunt58a0dd02016-05-17 11:54:23 -070075 .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 Huntc0f20c12016-05-09 09:30:20 -070084 .type(Link.Type.DIRECT)
85 .build();
86
Simon Huntc13082f2016-08-03 21:20:23 -070087 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 Huntc0f20c12016-05-09 09:30:20 -070093
94 @Test
95 public void canonical() {
96 title("canonical");
Simon Hunt58a0dd02016-05-17 11:54:23 -070097 UiLinkId one = UiLinkId.uiLinkId(LINK_X1_TO_Y2);
98 UiLinkId two = UiLinkId.uiLinkId(LINK_Y2_TO_X1);
Simon Huntc0f20c12016-05-09 09:30:20 -070099 print("link one: %s", one);
100 print("link two: %s", two);
101 assertEquals("not equiv", one, two);
102 }
Simon Hunt58a0dd02016-05-17 11:54:23 -0700103
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 Huntc13082f2016-08-03 21:20:23 -0700113
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 Huntbf59db22017-05-12 13:26:35 -0700125 assertEquals("not H-D", UiLinkId.Type.HOST_DEVICE, id.type());
126 assertTrue("not host-dev", id.isHostDevice());
127 assertFalse("dev-dev?", id.isDeviceDevice());
Simon Huntc13082f2016-08-03 21:20:23 -0700128 }
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 Huntbf59db22017-05-12 13:26:35 -0700141 assertEquals("not D-D", UiLinkId.Type.DEVICE_DEVICE, id.type());
142 assertTrue("not dev-dev", id.isDeviceDevice());
Simon Huntc13082f2016-08-03 21:20:23 -0700143 }
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 Huntbf59db22017-05-12 13:26:35 -0700153 assertEquals("not R-R", UiLinkId.Type.REGION_REGION, idFirst.type());
154 assertTrue("not reg-reg", idFirst.isRegionRegion());
Simon Huntc13082f2016-08-03 21:20:23 -0700155 }
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 Huntbf59db22017-05-12 13:26:35 -0700175 assertEquals("not R-D", UiLinkId.Type.REGION_DEVICE, id.type());
176 assertTrue("not reg-dev", id.isRegionDevice());
Simon Huntc13082f2016-08-03 21:20:23 -0700177 }
178
Simon Hunt0e161092017-05-08 17:41:38 -0700179 @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 Huntbf59db22017-05-12 13:26:35 -0700195
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 Huntc0f20c12016-05-09 09:30:20 -0700230}