blob: d33e9880093ed5a1b5f1e899f793f930d1dfd13b [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
Sbhat353548bb22017-07-13 10:27:10 -070019
Simon Huntc13082f2016-08-03 21:20:23 -070020import org.junit.Before;
Simon Huntc0f20c12016-05-09 09:30:20 -070021import org.junit.Test;
Sbhat353548bb22017-07-13 10:27:10 -070022import org.onlab.packet.ChassisId;
23import org.onlab.packet.IpAddress;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.VlanId;
26import org.onosproject.cluster.DefaultControllerNode;
27import org.onosproject.cluster.NodeId;
Simon Huntc13082f2016-08-03 21:20:23 -070028import org.onosproject.net.ConnectPoint;
Sbhat353548bb22017-07-13 10:27:10 -070029import org.onosproject.net.DefaultAnnotations;
30import org.onosproject.net.DefaultDevice;
31import org.onosproject.net.DefaultEdgeLink;
32import org.onosproject.net.DefaultHost;
Simon Huntc13082f2016-08-03 21:20:23 -070033import org.onosproject.net.DefaultLink;
Sbhat353548bb22017-07-13 10:27:10 -070034import org.onosproject.net.Device;
Simon Huntc13082f2016-08-03 21:20:23 -070035import org.onosproject.net.DeviceId;
Sbhat353548bb22017-07-13 10:27:10 -070036import org.onosproject.net.EdgeLink;
37import org.onosproject.net.Host;
38import org.onosproject.net.HostId;
39import org.onosproject.net.HostLocation;
Simon Huntc13082f2016-08-03 21:20:23 -070040import org.onosproject.net.Link;
41import org.onosproject.net.PortNumber;
42import org.onosproject.net.provider.ProviderId;
Sbhat353548bb22017-07-13 10:27:10 -070043import org.onosproject.net.region.DefaultRegion;
44import org.onosproject.net.region.Region;
Simon Huntc13082f2016-08-03 21:20:23 -070045import org.onosproject.net.region.RegionId;
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070046import org.onosproject.ui.model.AbstractUiModelTest;
Simon Huntc0f20c12016-05-09 09:30:20 -070047
Simon Huntc13082f2016-08-03 21:20:23 -070048import java.util.ArrayList;
49import java.util.Collections;
Sbhat353548bb22017-07-13 10:27:10 -070050import java.util.HashSet;
Simon Huntc13082f2016-08-03 21:20:23 -070051import java.util.List;
Sbhat353548bb22017-07-13 10:27:10 -070052import java.util.Set;
53
54import com.google.common.collect.Sets;
Simon Huntc13082f2016-08-03 21:20:23 -070055
56import static org.junit.Assert.assertEquals;
Sbhat353548bb22017-07-13 10:27:10 -070057import static org.junit.Assert.assertThat;
58import static org.junit.Assert.assertTrue;
Simon Huntc13082f2016-08-03 21:20:23 -070059import static org.onosproject.net.DeviceId.deviceId;
60import static org.onosproject.net.PortNumber.portNumber;
61
Sbhat353548bb22017-07-13 10:27:10 -070062import static org.hamcrest.Matchers.empty;
63import static org.hamcrest.Matchers.is;
64
Simon Huntc0f20c12016-05-09 09:30:20 -070065/**
66 * Unit tests for {@link UiTopology}.
67 */
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070068public class UiTopologyTest extends AbstractUiModelTest {
Simon Huntc0f20c12016-05-09 09:30:20 -070069
Simon Huntc13082f2016-08-03 21:20:23 -070070 private static final DeviceId DEV_X = deviceId("dev-X");
71 private static final DeviceId DEV_Y = deviceId("dev-Y");
72 private static final PortNumber P1 = portNumber(1);
73 private static final PortNumber P2 = portNumber(2);
74
75 private static final String DEV_X_ID = "dev-x/1";
76 private static final String DEV_Y_ID = "dev-y/2";
77
78 private static final ConnectPoint CP_X1 = new ConnectPoint(DEV_X, P1);
79 private static final ConnectPoint CP_Y2 = new ConnectPoint(DEV_Y, P2);
80
81 private static final Link LINK_X1_TO_Y2 = DefaultLink.builder()
82 .providerId(ProviderId.NONE)
83 .src(CP_X1)
84 .dst(CP_Y2)
85 .type(Link.Type.DIRECT)
86 .build();
87
88 private static final UiLinkId DX1_DY2 = UiLinkId.uiLinkId(LINK_X1_TO_Y2);
89
90 private static final RegionId ROOT = UiRegion.NULL_ID;
91 private static final RegionId R1 = RegionId.regionId("R1");
92 private static final RegionId R2 = RegionId.regionId("R2");
93 private static final RegionId R3 = RegionId.regionId("R3");
94
95 private static final String DEV_LINK_CLASS = "UiDeviceLink";
96 private static final String REG_LINK_CLASS = "UiRegionLink";
97 private static final String REG_DEV_LINK_CLASS = "UiRegionDeviceLink";
98
Sbhat353548bb22017-07-13 10:27:10 -070099 private Host host;
100 private UiHost uiHost;
101 private UiDeviceLink deviceLink;
102 private EdgeLink edgeLink;
103 private UiEdgeLink uiEdgeLink;
104 private UiClusterMember mem1;
105 private UiRegion region1;
106 private UiDevice dev1;
107 private UiLinkId linkId;
108
109 private static final ProviderId PID = new ProviderId("of", "bar");
110 private static final VlanId VID = VlanId.vlanId((short) 20);
111 private static final MacAddress MAC_ADDRESS = MacAddress.valueOf("00:11:00:00:00:01");
112 private static final HostId HID = HostId.hostId(MAC_ADDRESS, VID);
113 private static final DeviceId DID = deviceId("of:foo");
114 private static final HostLocation LOC = new HostLocation(DID, PortNumber.portNumber(8), 123L);
115 private static final ConnectPoint CP = new ConnectPoint(DEV_X, PortNumber.portNumber(8));
116 private static final Set<IpAddress> IPSET = Sets.newHashSet(IpAddress.valueOf("10.0.0.1"),
117 IpAddress.valueOf("10.0.0.2"));
118 private static final String MID = "id1";
Simon Huntc13082f2016-08-03 21:20:23 -0700119
Simon Huntc0f20c12016-05-09 09:30:20 -0700120 private UiTopology topo;
Simon Huntc13082f2016-08-03 21:20:23 -0700121 private UiDeviceLink devLink;
122
123 private List<RegionId> xBranch;
124 private List<RegionId> yBranch;
125 private UiSynthLink synth;
126
127 @Before
128 public void setUp() {
Thomas Vachuskab877a6f2017-04-14 11:43:30 -0700129 topo = new UiTopology(MOCK_SERVICES);
Simon Huntc13082f2016-08-03 21:20:23 -0700130 devLink = new UiDeviceLink(null, DX1_DY2);
131 devLink.attachBackingLink(LINK_X1_TO_Y2);
132 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700133
134 @Test
135 public void basic() {
136 title("basic");
Simon Huntc0f20c12016-05-09 09:30:20 -0700137 print(topo);
138 }
Simon Huntc13082f2016-08-03 21:20:23 -0700139
140 private List<RegionId> branch(RegionId... ids) {
141 List<RegionId> result = new ArrayList<>(ids.length);
142 Collections.addAll(result, ids);
143 return result;
144 }
145
146 private void verifySynth(RegionId id, String cls, String epA, String epB) {
147 synth = topo.makeSynthLink(devLink, xBranch, yBranch);
148 UiLink ulink = synth.link();
149 print(synth);
150 print("EpA{%s} EpB{%s}", ulink.endPointA(), ulink.endPointB());
151
152 assertEquals("wrong region", id, synth.regionId());
153 assertEquals("wrong link class", cls, ulink.type());
154 assertEquals("wrong EP A", epA, ulink.endPointA());
155 assertEquals("wrong EP B", epB, ulink.endPointB());
156 }
157
158 @Test
159 public void makeSynthDevToDevRoot() {
160 title("makeSynthDevToDevRoot");
161 xBranch = branch(ROOT);
162 yBranch = branch(ROOT);
163 verifySynth(ROOT, DEV_LINK_CLASS, DEV_X_ID, DEV_Y_ID);
164 }
165
166 @Test
167 public void makeSynthDevToDevR1() {
168 title("makeSynthDevToDevR1");
169 xBranch = branch(ROOT, R1);
170 yBranch = branch(ROOT, R1);
171 verifySynth(R1, DEV_LINK_CLASS, DEV_X_ID, DEV_Y_ID);
172 }
173
174 @Test
175 public void makeSynthDevToDevR2() {
176 title("makeSynthDevToDevR2");
177 xBranch = branch(ROOT, R1, R2);
178 yBranch = branch(ROOT, R1, R2);
179 verifySynth(R2, DEV_LINK_CLASS, DEV_X_ID, DEV_Y_ID);
180 }
181
182 @Test
183 public void makeSynthRegToRegRoot() {
184 title("makeSynthRegToRegRoot");
185 xBranch = branch(ROOT, R1);
186 yBranch = branch(ROOT, R2);
187 verifySynth(ROOT, REG_LINK_CLASS, R1.id(), R2.id());
188 }
189
190 @Test
191 public void makeSynthRegToRegR1() {
192 title("makeSynthRegToRegR1");
193 xBranch = branch(ROOT, R1, R2);
194 yBranch = branch(ROOT, R1, R3);
195 verifySynth(R1, REG_LINK_CLASS, R2.id(), R3.id());
196 }
197
198 @Test
199 public void makeSynthRegToDevRoot() {
200 title("makeSynthRegToDevRoot");
201
202 // Note: link is canonicalized to region--device order
203
204 xBranch = branch(ROOT);
205 yBranch = branch(ROOT, R1);
206 verifySynth(ROOT, REG_DEV_LINK_CLASS, R1.id(), DEV_X_ID);
207
208 xBranch = branch(ROOT, R1);
209 yBranch = branch(ROOT);
210 verifySynth(ROOT, REG_DEV_LINK_CLASS, R1.id(), DEV_Y_ID);
211 }
212
213 @Test
214 public void makeSynthRegToDevR3() {
215 title("makeSynthRegToDevR3");
216
217 // Note: link is canonicalized to region--device order
218
219 xBranch = branch(ROOT, R3);
220 yBranch = branch(ROOT, R3, R1);
221 verifySynth(R3, REG_DEV_LINK_CLASS, R1.id(), DEV_X_ID);
222
223 xBranch = branch(ROOT, R3, R1);
224 yBranch = branch(ROOT, R3);
225 verifySynth(R3, REG_DEV_LINK_CLASS, R1.id(), DEV_Y_ID);
226 }
Sbhat353548bb22017-07-13 10:27:10 -0700227
228 @Test
229 public void mockTopology() {
230 host = new DefaultHost(PID, HID, MAC_ADDRESS, VID, LOC, IPSET);
231 uiHost = new UiHost(topo, host);
232 deviceLink = new UiDeviceLink(topo, DX1_DY2);
233 edgeLink = DefaultEdgeLink.createEdgeLink(CP, true);
234 linkId = UiLinkId.uiLinkId(edgeLink);
235 uiEdgeLink = new UiEdgeLink(topo, linkId);
236 mem1 = clusterMember(MID, 001);
237 region1 = region();
238 dev1 = device();
239 Set<DeviceId> deviceIds = new HashSet<>();
240 Set<HostId> hostIds = new HashSet<>();
241
242 topo.add(uiHost);
243 topo.add(mem1);
244 topo.add(region1);
245 topo.add(dev1);
246 topo.add(deviceLink);
247 topo.add(uiEdgeLink);
248
249
250 assertTrue(topo.allRegions().contains(region1));
251 assertTrue(topo.allClusterMembers().contains(mem1));
252 assertTrue(topo.findClusterMember(NodeId.nodeId("id1")).equals(mem1));
253 assertTrue(topo.findRegion(R1).equals(region1));
254 assertTrue(topo.findDevice(DID).equals(dev1));
255 assertTrue(topo.findHost(HID).equals(uiHost));
256 assertTrue(topo.findDeviceLink(DX1_DY2).equals(deviceLink));
257
258 deviceIds.add(DID);
259 assertTrue(topo.deviceSet(deviceIds).contains(dev1));
260 hostIds.add(HID);
261 assertTrue(topo.hostSet(hostIds).contains(uiHost));
262
263 topo.clear();
264 assertThat(topo.allClusterMembers(), is(empty()));
265 assertThat(topo.allDeviceLinks(), is(empty()));
266 assertThat(topo.allDevices(), is(empty()));
267 assertThat(topo.allHosts(), is(empty()));
268 assertThat(topo.allRegions(), is(empty()));
269 }
270 private UiClusterMember clusterMember(String nodeId, int ipAddress) {
271 return new UiClusterMember(topo, new DefaultControllerNode(NodeId.nodeId(nodeId),
272 IpAddress.valueOf(ipAddress)));
273 }
274 private UiRegion region() {
275 return new UiRegion(topo, (new DefaultRegion(R1, "reg1", Region.Type.METRO, DefaultAnnotations.EMPTY,
276 null)));
277 }
278 private UiDevice device() {
279 return new UiDevice(topo, new DefaultDevice(PID, DID, Device.Type.SWITCH, "whitebox",
280 "1.1.x", "3.9.1", "43331", new ChassisId()));
281 }
282
283
Simon Huntc0f20c12016-05-09 09:30:20 -0700284}