blob: 80666d6cc7f445af7a3064f56ca69612bd3a3cfc [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;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.provider.ProviderId;
Simon Huntc13082f2016-08-03 21:20:23 -070028import org.onosproject.net.region.RegionId;
Simon Huntc0f20c12016-05-09 09:30:20 -070029import org.onosproject.ui.model.AbstractUiModelTest;
30
31import static org.junit.Assert.assertEquals;
Simon Hunt58a0dd02016-05-17 11:54:23 -070032import static org.junit.Assert.assertNotEquals;
Simon Huntc13082f2016-08-03 21:20:23 -070033import static org.junit.Assert.assertNull;
Simon Huntc0f20c12016-05-09 09:30:20 -070034import static org.onosproject.net.DeviceId.deviceId;
Simon Huntc13082f2016-08-03 21:20:23 -070035import static org.onosproject.net.HostId.hostId;
36import static org.onosproject.net.PortNumber.P0;
Simon Huntc0f20c12016-05-09 09:30:20 -070037import static org.onosproject.net.PortNumber.portNumber;
38
39/**
40 * Unit tests for {@link UiLinkId}.
41 */
42public class UiLinkIdTest extends AbstractUiModelTest {
43
Simon Huntc13082f2016-08-03 21:20:23 -070044 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 Huntc0f20c12016-05-09 09:30:20 -070050 private static final DeviceId DEV_X = deviceId("device-X");
51 private static final DeviceId DEV_Y = deviceId("device-Y");
Simon Huntc13082f2016-08-03 21:20:23 -070052
Simon Huntc0f20c12016-05-09 09:30:20 -070053 private static final PortNumber P1 = portNumber(1);
54 private static final PortNumber P2 = portNumber(2);
Simon Hunt58a0dd02016-05-17 11:54:23 -070055 private static final PortNumber P3 = portNumber(3);
Simon Huntc0f20c12016-05-09 09:30:20 -070056
Simon Hunt58a0dd02016-05-17 11:54:23 -070057 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 Huntc0f20c12016-05-09 09:30:20 -070060
Simon Huntc13082f2016-08-03 21:20:23 -070061 private static final ConnectPoint CP_HA = new ConnectPoint(HOST_A, P0);
62
Simon Hunt58a0dd02016-05-17 11:54:23 -070063 private static final Link LINK_X1_TO_Y2 = DefaultLink.builder()
Simon Huntc0f20c12016-05-09 09:30:20 -070064 .providerId(ProviderId.NONE)
Simon Hunt58a0dd02016-05-17 11:54:23 -070065 .src(CP_X1)
66 .dst(CP_Y2)
Simon Huntc0f20c12016-05-09 09:30:20 -070067 .type(Link.Type.DIRECT)
68 .build();
69
Simon Hunt58a0dd02016-05-17 11:54:23 -070070 private static final Link LINK_Y2_TO_X1 = DefaultLink.builder()
Simon Huntc0f20c12016-05-09 09:30:20 -070071 .providerId(ProviderId.NONE)
Simon Hunt58a0dd02016-05-17 11:54:23 -070072 .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 Huntc0f20c12016-05-09 09:30:20 -070081 .type(Link.Type.DIRECT)
82 .build();
83
Simon Huntc13082f2016-08-03 21:20:23 -070084 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 Huntc0f20c12016-05-09 09:30:20 -070090
91 @Test
92 public void canonical() {
93 title("canonical");
Simon Hunt58a0dd02016-05-17 11:54:23 -070094 UiLinkId one = UiLinkId.uiLinkId(LINK_X1_TO_Y2);
95 UiLinkId two = UiLinkId.uiLinkId(LINK_Y2_TO_X1);
Simon Huntc0f20c12016-05-09 09:30:20 -070096 print("link one: %s", one);
97 print("link two: %s", two);
98 assertEquals("not equiv", one, two);
99 }
Simon Hunt58a0dd02016-05-17 11:54:23 -0700100
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 Huntc13082f2016-08-03 21:20:23 -0700110
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 Huntc0f20c12016-05-09 09:30:20 -0700167}