blob: 39d4cb46f094168a0b77f938d2895394b7378317 [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 Hunt58a0dd02016-05-17 11:54:23 -070033import static org.junit.Assert.assertNotEquals;
Simon Huntc13082f2016-08-03 21:20:23 -070034import static org.junit.Assert.assertNull;
Simon Huntc0f20c12016-05-09 09:30:20 -070035import static org.onosproject.net.DeviceId.deviceId;
Simon Huntc13082f2016-08-03 21:20:23 -070036import static org.onosproject.net.HostId.hostId;
37import static org.onosproject.net.PortNumber.P0;
Simon Huntc0f20c12016-05-09 09:30:20 -070038import static org.onosproject.net.PortNumber.portNumber;
39
40/**
41 * Unit tests for {@link UiLinkId}.
42 */
43public class UiLinkIdTest extends AbstractUiModelTest {
44
Simon Huntc13082f2016-08-03 21:20:23 -070045 private static final RegionId REG_1 = RegionId.regionId("Region-1");
46 private static final RegionId REG_2 = RegionId.regionId("Region-2");
47
48 private static final MacAddress MAC_A = MacAddress.valueOf(0x123456L);
49 private static final HostId HOST_A = hostId(MAC_A);
50
Simon Huntc0f20c12016-05-09 09:30:20 -070051 private static final DeviceId DEV_X = deviceId("device-X");
52 private static final DeviceId DEV_Y = deviceId("device-Y");
Simon Huntc13082f2016-08-03 21:20:23 -070053
Simon Huntc0f20c12016-05-09 09:30:20 -070054 private static final PortNumber P1 = portNumber(1);
55 private static final PortNumber P2 = portNumber(2);
Simon Hunt58a0dd02016-05-17 11:54:23 -070056 private static final PortNumber P3 = portNumber(3);
Simon Huntc0f20c12016-05-09 09:30:20 -070057
Simon Hunt58a0dd02016-05-17 11:54:23 -070058 private static final ConnectPoint CP_X1 = new ConnectPoint(DEV_X, P1);
59 private static final ConnectPoint CP_Y2 = new ConnectPoint(DEV_Y, P2);
60 private static final ConnectPoint CP_Y3 = new ConnectPoint(DEV_Y, P3);
Simon Huntc0f20c12016-05-09 09:30:20 -070061
Simon Huntc13082f2016-08-03 21:20:23 -070062 private static final ConnectPoint CP_HA = new ConnectPoint(HOST_A, P0);
63
Simon Hunt58a0dd02016-05-17 11:54:23 -070064 private static final Link LINK_X1_TO_Y2 = DefaultLink.builder()
Simon Huntc0f20c12016-05-09 09:30:20 -070065 .providerId(ProviderId.NONE)
Simon Hunt58a0dd02016-05-17 11:54:23 -070066 .src(CP_X1)
67 .dst(CP_Y2)
Simon Huntc0f20c12016-05-09 09:30:20 -070068 .type(Link.Type.DIRECT)
69 .build();
70
Simon Hunt58a0dd02016-05-17 11:54:23 -070071 private static final Link LINK_Y2_TO_X1 = DefaultLink.builder()
Simon Huntc0f20c12016-05-09 09:30:20 -070072 .providerId(ProviderId.NONE)
Simon Hunt58a0dd02016-05-17 11:54:23 -070073 .src(CP_Y2)
74 .dst(CP_X1)
75 .type(Link.Type.DIRECT)
76 .build();
77
78 private static final Link LINK_X1_TO_Y3 = DefaultLink.builder()
79 .providerId(ProviderId.NONE)
80 .src(CP_X1)
81 .dst(CP_Y3)
Simon Huntc0f20c12016-05-09 09:30:20 -070082 .type(Link.Type.DIRECT)
83 .build();
84
Simon Huntc13082f2016-08-03 21:20:23 -070085 private static final Link LINK_HA_TO_X1 = DefaultLink.builder()
86 .providerId(ProviderId.NONE)
87 .src(CP_HA)
88 .dst(CP_X1)
89 .type(Link.Type.EDGE)
90 .build();
Simon Huntc0f20c12016-05-09 09:30:20 -070091
92 @Test
93 public void canonical() {
94 title("canonical");
Simon Hunt58a0dd02016-05-17 11:54:23 -070095 UiLinkId one = UiLinkId.uiLinkId(LINK_X1_TO_Y2);
96 UiLinkId two = UiLinkId.uiLinkId(LINK_Y2_TO_X1);
Simon Huntc0f20c12016-05-09 09:30:20 -070097 print("link one: %s", one);
98 print("link two: %s", two);
99 assertEquals("not equiv", one, two);
100 }
Simon Hunt58a0dd02016-05-17 11:54:23 -0700101
102 @Test
103 public void sameDevsDiffPorts() {
104 title("sameDevsDiffPorts");
105 UiLinkId one = UiLinkId.uiLinkId(LINK_X1_TO_Y2);
106 UiLinkId other = UiLinkId.uiLinkId(LINK_X1_TO_Y3);
107 print("link one: %s", one);
108 print("link other: %s", other);
109 assertNotEquals("equiv?", one, other);
110 }
Simon Huntc13082f2016-08-03 21:20:23 -0700111
112 @Test
113 public void edgeLink() {
114 title("edgeLink");
115 UiLinkId id = UiLinkId.uiLinkId(LINK_HA_TO_X1);
116 print("link: %s", id);
117 assertEquals("wrong port A", P0, id.portA());
118 assertEquals("wrong element A", HOST_A, id.elementA());
119 assertEquals("wrong port B", P1, id.portB());
120 assertEquals("wrong element B", DEV_X, id.elementB());
121 assertNull("region A?", id.regionA());
122 assertNull("region B?", id.regionB());
123 }
124
125 @Test
126 public void deviceLink() {
127 title("deviceLink");
128 UiLinkId id = UiLinkId.uiLinkId(LINK_X1_TO_Y2);
129 print("link: %s", id);
130 assertEquals("wrong port A", P1, id.portA());
131 assertEquals("wrong element A", DEV_X, id.elementA());
132 assertEquals("wrong port B", P2, id.portB());
133 assertEquals("wrong element B", DEV_Y, id.elementB());
134 assertNull("region A?", id.regionA());
135 assertNull("region B?", id.regionB());
136 }
137
138 @Test
139 public void regionLink() {
140 title("regionLink");
141 UiLinkId idFirst = UiLinkId.uiLinkId(REG_1, REG_2);
142 UiLinkId idSecond = UiLinkId.uiLinkId(REG_2, REG_1);
143 print(" first: %s", idFirst);
144 print("second: %s", idSecond);
145 assertEquals("Not same ID", idFirst, idSecond);
146 }
147
148 @Test(expected = IllegalArgumentException.class)
149 public void identicalRegionBad() {
150 UiLinkId.uiLinkId(REG_1, REG_1);
151 }
152
153 @Test(expected = NullPointerException.class)
154 public void nullRegionBad() {
155 UiLinkId.uiLinkId(REG_1, (RegionId) null);
156 }
157
158 @Test
159 public void regionDeviceLink() {
160 title("regionDeviceLink");
161 UiLinkId id = UiLinkId.uiLinkId(REG_1, DEV_X, P1);
162 print("id: %s", id);
163 assertEquals("region ID", REG_1, id.regionA());
164 assertEquals("device ID", DEV_X, id.elementB());
165 assertEquals("port", P1, id.portB());
166 }
167
Simon Hunt0e161092017-05-08 17:41:38 -0700168 @Test
169 public void fromLinkKey() {
170 title("fromLinkKey");
171
172 LinkKey lk1 = LinkKey.linkKey(CP_X1, CP_Y2);
173 print("link-key-1: %s", lk1);
174 LinkKey lk2 = LinkKey.linkKey(CP_Y2, CP_X1);
175 print("link-key-2: %s", lk2);
176
177 UiLinkId id1 = UiLinkId.uiLinkId(lk1);
178 print("identifier-1: %s", id1);
179 UiLinkId id2 = UiLinkId.uiLinkId(lk2);
180 print("identifier-2: %s", id2);
181
182 assertEquals("unequal canon-ids", id1, id2);
183 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700184}