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 | |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 19 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 20 | import org.junit.Before; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 21 | import org.junit.Test; |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 22 | import org.onlab.packet.ChassisId; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onlab.packet.VlanId; |
| 26 | import org.onosproject.cluster.DefaultControllerNode; |
| 27 | import org.onosproject.cluster.NodeId; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 28 | import org.onosproject.net.ConnectPoint; |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 29 | import org.onosproject.net.DefaultAnnotations; |
| 30 | import org.onosproject.net.DefaultDevice; |
| 31 | import org.onosproject.net.DefaultEdgeLink; |
| 32 | import org.onosproject.net.DefaultHost; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 33 | import org.onosproject.net.DefaultLink; |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 34 | import org.onosproject.net.Device; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 36 | import org.onosproject.net.EdgeLink; |
| 37 | import org.onosproject.net.Host; |
| 38 | import org.onosproject.net.HostId; |
| 39 | import org.onosproject.net.HostLocation; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 40 | import org.onosproject.net.Link; |
| 41 | import org.onosproject.net.PortNumber; |
| 42 | import org.onosproject.net.provider.ProviderId; |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 43 | import org.onosproject.net.region.DefaultRegion; |
| 44 | import org.onosproject.net.region.Region; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 45 | import org.onosproject.net.region.RegionId; |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 46 | import org.onosproject.ui.model.AbstractUiModelTest; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 47 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 48 | import java.util.ArrayList; |
| 49 | import java.util.Collections; |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 50 | import java.util.HashSet; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 51 | import java.util.List; |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 52 | import java.util.Set; |
| 53 | |
| 54 | import com.google.common.collect.Sets; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 55 | |
| 56 | import static org.junit.Assert.assertEquals; |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 57 | import static org.junit.Assert.assertThat; |
| 58 | import static org.junit.Assert.assertTrue; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 59 | import static org.onosproject.net.DeviceId.deviceId; |
| 60 | import static org.onosproject.net.PortNumber.portNumber; |
| 61 | |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 62 | import static org.hamcrest.Matchers.empty; |
| 63 | import static org.hamcrest.Matchers.is; |
| 64 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Unit tests for {@link UiTopology}. |
| 67 | */ |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 68 | public class UiTopologyTest extends AbstractUiModelTest { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 69 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 70 | 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 | |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 99 | 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 Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 119 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 120 | private UiTopology topo; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 121 | 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 Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 129 | topo = new UiTopology(MOCK_SERVICES); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 130 | devLink = new UiDeviceLink(null, DX1_DY2); |
| 131 | devLink.attachBackingLink(LINK_X1_TO_Y2); |
| 132 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 133 | |
| 134 | @Test |
| 135 | public void basic() { |
| 136 | title("basic"); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 137 | print(topo); |
| 138 | } |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 139 | |
| 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 | } |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 227 | |
| 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 Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 284 | } |