Andrea Campanella | 01e886e | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | package org.onosproject.t3.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
Andrea Campanella | 37d1062 | 2018-01-18 17:11:42 +0100 | [diff] [blame^] | 22 | import org.onlab.packet.ChassisId; |
Andrea Campanella | 01e886e | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 23 | import org.onlab.packet.EthType; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
| 27 | import org.onosproject.net.ConnectPoint; |
Andrea Campanella | 37d1062 | 2018-01-18 17:11:42 +0100 | [diff] [blame^] | 28 | import org.onosproject.net.DefaultAnnotations; |
| 29 | import org.onosproject.net.DefaultDevice; |
Andrea Campanella | 01e886e | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 30 | import org.onosproject.net.DefaultLink; |
Andrea Campanella | 37d1062 | 2018-01-18 17:11:42 +0100 | [diff] [blame^] | 31 | import org.onosproject.net.Device; |
Andrea Campanella | 01e886e | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.Host; |
| 34 | import org.onosproject.net.Link; |
Andrea Campanella | 37d1062 | 2018-01-18 17:11:42 +0100 | [diff] [blame^] | 35 | import org.onosproject.net.device.DeviceServiceAdapter; |
Andrea Campanella | 01e886e | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 36 | import org.onosproject.net.driver.DefaultDriver; |
| 37 | import org.onosproject.net.driver.Driver; |
| 38 | import org.onosproject.net.driver.DriverServiceAdapter; |
| 39 | import org.onosproject.net.flow.FlowEntry; |
| 40 | import org.onosproject.net.flow.FlowRuleServiceAdapter; |
| 41 | import org.onosproject.net.flow.TrafficSelector; |
| 42 | import org.onosproject.net.flow.criteria.Criterion; |
| 43 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 44 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 45 | import org.onosproject.net.group.Group; |
| 46 | import org.onosproject.net.group.GroupServiceAdapter; |
| 47 | import org.onosproject.net.host.HostServiceAdapter; |
| 48 | import org.onosproject.net.link.LinkServiceAdapter; |
| 49 | import org.onosproject.net.provider.ProviderId; |
| 50 | import org.onosproject.t3.api.StaticPacketTrace; |
| 51 | import org.slf4j.Logger; |
| 52 | |
| 53 | import java.util.HashMap; |
| 54 | import java.util.Set; |
| 55 | |
| 56 | import static org.junit.Assert.assertEquals; |
| 57 | import static org.junit.Assert.assertNotNull; |
| 58 | import static org.junit.Assert.assertNull; |
| 59 | import static org.junit.Assert.assertTrue; |
Andrea Campanella | 37d1062 | 2018-01-18 17:11:42 +0100 | [diff] [blame^] | 60 | import static org.onosproject.net.Device.Type.SWITCH; |
Andrea Campanella | 01e886e | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 61 | import static org.onosproject.t3.impl.T3TestObjects.*; |
| 62 | import static org.slf4j.LoggerFactory.getLogger; |
| 63 | |
| 64 | /** |
| 65 | * Test Class for Troubleshoot Manager. |
| 66 | */ |
| 67 | public class TroubleshootManagerTest { |
| 68 | |
| 69 | private static final Logger log = getLogger(TroubleshootManager.class); |
| 70 | |
| 71 | private TroubleshootManager mngr; |
| 72 | |
| 73 | @Before |
| 74 | public void setUp() throws Exception { |
| 75 | mngr = new TroubleshootManager(); |
| 76 | mngr.flowRuleService = new TestFlowRuleService(); |
| 77 | mngr.hostService = new TestHostService(); |
| 78 | mngr.linkService = new TestLinkService(); |
| 79 | mngr.driverService = new TestDriverService(); |
| 80 | mngr.groupService = new TestGroupService(); |
Andrea Campanella | 37d1062 | 2018-01-18 17:11:42 +0100 | [diff] [blame^] | 81 | mngr.deviceService = new TestDeviceService(); |
Andrea Campanella | 01e886e | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 82 | |
| 83 | assertNotNull("Manager should not be null", mngr); |
| 84 | |
| 85 | assertNotNull("Flow rule Service should not be null", mngr.flowRuleService); |
| 86 | assertNotNull("Host Service should not be null", mngr.hostService); |
| 87 | assertNotNull("Group Service should not be null", mngr.groupService); |
| 88 | assertNotNull("Driver Service should not be null", mngr.driverService); |
| 89 | assertNotNull("Link Service should not be null", mngr.linkService); |
Andrea Campanella | 37d1062 | 2018-01-18 17:11:42 +0100 | [diff] [blame^] | 90 | assertNotNull("Device Service should not be null", mngr.deviceService); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Tests failure on non existent device. |
| 95 | */ |
| 96 | @Test(expected = NullPointerException.class) |
| 97 | public void nonExistentDevice() { |
| 98 | StaticPacketTrace traceFail = mngr.trace(PACKET_OK, ConnectPoint.deviceConnectPoint("nonexistent" + "/1")); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Tests failure on offline device. |
| 103 | */ |
| 104 | @Test |
| 105 | public void offlineDevice() { |
| 106 | StaticPacketTrace traceFail = mngr.trace(PACKET_OK, ConnectPoint.deviceConnectPoint(OFFLINE_DEVICE + "/1")); |
| 107 | assertNotNull("Trace should not be null", traceFail); |
| 108 | assertNull("Trace should have 0 output", traceFail.getGroupOuputs(SINGLE_FLOW_DEVICE)); |
| 109 | log.info("trace {}", traceFail.resultMessage()); |
Andrea Campanella | 01e886e | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
Andrea Campanella | 2bc55dd | 2018-01-18 16:54:34 +0100 | [diff] [blame] | 113 | * Tests failure on device with no flows. |
Andrea Campanella | 01e886e | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 114 | */ |
| 115 | @Test |
| 116 | public void noFlows() { |
| 117 | StaticPacketTrace traceFail = mngr.trace(PACKET_OK, ConnectPoint.deviceConnectPoint("test/1")); |
| 118 | assertNotNull("Trace should not be null", traceFail); |
| 119 | assertNull("Trace should have 0 output", traceFail.getGroupOuputs(SINGLE_FLOW_DEVICE)); |
| 120 | log.info("trace {}", traceFail.resultMessage()); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Test a single flow rule that has output port in it. |
| 125 | */ |
| 126 | @Test |
| 127 | public void testSingleFlowRule() { |
| 128 | |
| 129 | testSuccess(PACKET_OK, SINGLE_FLOW_IN_CP, SINGLE_FLOW_DEVICE, SINGLE_FLOW_OUT_CP, 1); |
| 130 | |
| 131 | testFaliure(PACKET_FAIL, SINGLE_FLOW_IN_CP, SINGLE_FLOW_DEVICE); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Tests two flow rule the last one of which has output port in it. |
| 136 | */ |
| 137 | @Test |
| 138 | public void testDualFlowRule() { |
| 139 | |
| 140 | //Test Success |
| 141 | |
Andrea Campanella | 2bc55dd | 2018-01-18 16:54:34 +0100 | [diff] [blame] | 142 | StaticPacketTrace traceSuccess = testSuccess(PACKET_OK, DUAL_FLOW_IN_CP, DUAL_FLOW_DEVICE, |
| 143 | DUAL_FLOW_OUT_CP, 1); |
Andrea Campanella | 01e886e | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 144 | |
| 145 | //Testing Vlan |
| 146 | Criterion criterion = traceSuccess.getGroupOuputs(DUAL_FLOW_DEVICE).get(0). |
| 147 | getFinalPacket().getCriterion(Criterion.Type.VLAN_VID); |
| 148 | assertNotNull("Packet Should have Vlan", criterion); |
| 149 | |
| 150 | VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) criterion; |
| 151 | |
| 152 | assertEquals("Vlan should be 100", VlanId.vlanId((short) 100), vlanIdCriterion.vlanId()); |
| 153 | |
| 154 | //Test Faliure |
| 155 | testFaliure(PACKET_FAIL, DUAL_FLOW_IN_CP, DUAL_FLOW_DEVICE); |
| 156 | |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Test a single flow rule that points to a group with output port in it. |
| 161 | */ |
| 162 | @Test |
| 163 | public void flowAndGroup() throws Exception { |
| 164 | |
Andrea Campanella | 2bc55dd | 2018-01-18 16:54:34 +0100 | [diff] [blame] | 165 | StaticPacketTrace traceSuccess = testSuccess(PACKET_OK, GROUP_FLOW_IN_CP, GROUP_FLOW_DEVICE, |
| 166 | GROUP_FLOW_OUT_CP, 1); |
Andrea Campanella | 01e886e | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 167 | |
| 168 | assertTrue("Wrong Output Group", traceSuccess.getGroupOuputs(GROUP_FLOW_DEVICE) |
| 169 | .get(0).getGroups().contains(GROUP)); |
| 170 | |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Test path through a 3 device topology. |
| 175 | */ |
| 176 | @Test |
| 177 | public void singlePathTopology() throws Exception { |
| 178 | |
| 179 | StaticPacketTrace traceSuccess = testSuccess(PACKET_OK_TOPO, TOPO_FLOW_1_IN_CP, |
| 180 | TOPO_FLOW_3_DEVICE, TOPO_FLOW_3_OUT_CP, 1); |
| 181 | |
| 182 | assertTrue("Incorrect path", |
| 183 | traceSuccess.getCompletePaths().get(0).contains(TOPO_FLOW_2_IN_CP)); |
| 184 | assertTrue("Incorrect path", |
| 185 | traceSuccess.getCompletePaths().get(0).contains(TOPO_FLOW_2_OUT_CP)); |
| 186 | assertTrue("Incorrect path", |
| 187 | traceSuccess.getCompletePaths().get(0).contains(TOPO_FLOW_3_IN_CP)); |
| 188 | |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Test path through a 4 device topology with first device that has groups with multiple output buckets. |
| 193 | */ |
| 194 | @Test |
| 195 | public void testGroupTopo() throws Exception { |
| 196 | |
| 197 | StaticPacketTrace traceSuccess = testSuccess(PACKET_OK_TOPO, TOPO_FLOW_IN_CP, |
| 198 | TOPO_FLOW_3_DEVICE, TOPO_FLOW_3_OUT_CP, 2); |
| 199 | |
| 200 | assertTrue("Incorrect groups", |
| 201 | traceSuccess.getGroupOuputs(TOPO_GROUP_FLOW_DEVICE).get(0).getGroups().contains(TOPO_GROUP)); |
| 202 | assertTrue("Incorrect bucket", |
| 203 | traceSuccess.getGroupOuputs(TOPO_GROUP_FLOW_DEVICE).get(1).getGroups().contains(TOPO_GROUP)); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Test HW support in a single device with 2 flow rules to check hit of static HW rules. |
| 208 | */ |
| 209 | @Test |
| 210 | public void hardwareTest() throws Exception { |
| 211 | |
| 212 | StaticPacketTrace traceSuccess = testSuccess(PACKET_OK, HARDWARE_DEVICE_IN_CP, |
| 213 | HARDWARE_DEVICE, HARDWARE_DEVICE_OUT_CP, 1); |
| 214 | |
| 215 | assertEquals("wrong ETH type", EthType.EtherType.IPV4.ethType(), |
| 216 | ((EthTypeCriterion) traceSuccess.getGroupOuputs(HARDWARE_DEVICE).get(0).getFinalPacket() |
| 217 | .getCriterion(Criterion.Type.ETH_TYPE)).ethType()); |
| 218 | |
| 219 | } |
| 220 | |
| 221 | private StaticPacketTrace testSuccess(TrafficSelector packet, ConnectPoint in, DeviceId deviceId, ConnectPoint out, |
| 222 | int paths) { |
| 223 | StaticPacketTrace traceSuccess = mngr.trace(packet, in); |
| 224 | |
| 225 | log.info("trace {}", traceSuccess); |
| 226 | |
| 227 | log.info("trace {}", traceSuccess.resultMessage()); |
| 228 | |
| 229 | assertNotNull("trace should not be null", traceSuccess); |
| 230 | assertEquals("Trace should have " + paths + " output", paths, traceSuccess.getGroupOuputs(deviceId).size()); |
| 231 | assertEquals("Trace should only have " + paths + "output", paths, traceSuccess.getCompletePaths().size()); |
| 232 | assertTrue("Trace should be successful", |
| 233 | traceSuccess.resultMessage().contains("Reached required destination Host")); |
| 234 | assertEquals("Incorrect Output CP", out, |
| 235 | traceSuccess.getGroupOuputs(deviceId).get(0).getOutput()); |
| 236 | |
| 237 | return traceSuccess; |
| 238 | } |
| 239 | |
| 240 | private void testFaliure(TrafficSelector packet, ConnectPoint in, DeviceId deviceId) { |
| 241 | StaticPacketTrace traceFail = mngr.trace(packet, in); |
| 242 | |
| 243 | log.info("trace {}", traceFail.resultMessage()); |
| 244 | |
| 245 | assertNotNull("Trace should not be null", traceFail); |
| 246 | assertNull("Trace should have 0 output", traceFail.getGroupOuputs(deviceId)); |
| 247 | } |
| 248 | |
| 249 | private class TestFlowRuleService extends FlowRuleServiceAdapter { |
| 250 | @Override |
| 251 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
| 252 | if (deviceId.equals(SINGLE_FLOW_DEVICE)) { |
| 253 | return ImmutableList.of(SINGLE_FLOW_ENTRY); |
| 254 | } else if (deviceId.equals(DUAL_FLOW_DEVICE)) { |
| 255 | return ImmutableList.of(FIRST_FLOW_ENTRY, SECOND_FLOW_ENTRY); |
| 256 | } else if (deviceId.equals(GROUP_FLOW_DEVICE)) { |
| 257 | return ImmutableList.of(GROUP_FLOW_ENTRY); |
| 258 | } else if (deviceId.equals(TOPO_FLOW_DEVICE) || |
| 259 | deviceId.equals(TOPO_FLOW_2_DEVICE) || |
| 260 | deviceId.equals(TOPO_FLOW_3_DEVICE) || |
| 261 | deviceId.equals(TOPO_FLOW_4_DEVICE)) { |
| 262 | return ImmutableList.of(TOPO_SINGLE_FLOW_ENTRY, TOPO_SECOND_INPUT_FLOW_ENTRY); |
| 263 | } else if (deviceId.equals(TOPO_GROUP_FLOW_DEVICE)) { |
| 264 | return ImmutableList.of(TOPO_GROUP_FLOW_ENTRY); |
Andrea Campanella | 2bc55dd | 2018-01-18 16:54:34 +0100 | [diff] [blame] | 265 | } else if (deviceId.equals(HARDWARE_DEVICE)) { |
Andrea Campanella | 01e886e | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 266 | return ImmutableList.of(HARDWARE_ETH_FLOW_ENTRY, HARDWARE_FLOW_ENTRY); |
| 267 | } |
| 268 | return ImmutableList.of(); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | private class TestDriverService extends DriverServiceAdapter { |
| 273 | @Override |
| 274 | public Driver getDriver(DeviceId deviceId) { |
Andrea Campanella | 2bc55dd | 2018-01-18 16:54:34 +0100 | [diff] [blame] | 275 | if (deviceId.equals(HARDWARE_DEVICE)) { |
Andrea Campanella | 01e886e | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 276 | return new DefaultDriver("ofdpa", ImmutableList.of(), |
| 277 | "test", "test", "test", new HashMap<>(), new HashMap<>()); |
| 278 | } |
| 279 | return new DefaultDriver("NotHWDriver", ImmutableList.of(), |
| 280 | "test", "test", "test", new HashMap<>(), new HashMap<>()); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | private class TestGroupService extends GroupServiceAdapter { |
| 285 | @Override |
| 286 | public Iterable<Group> getGroups(DeviceId deviceId) { |
| 287 | if (deviceId.equals(GROUP_FLOW_DEVICE)) { |
| 288 | return ImmutableList.of(GROUP); |
| 289 | } else if (deviceId.equals(TOPO_GROUP_FLOW_DEVICE)) { |
| 290 | return ImmutableList.of(TOPO_GROUP); |
| 291 | } |
| 292 | return ImmutableList.of(); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | private class TestHostService extends HostServiceAdapter { |
| 297 | @Override |
| 298 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
| 299 | if (connectPoint.equals(TOPO_FLOW_3_OUT_CP)) { |
| 300 | return ImmutableSet.of(H2); |
| 301 | } |
| 302 | return ImmutableSet.of(H1); |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public Set<Host> getHostsByMac(MacAddress mac) { |
| 307 | if (mac.equals(H1.mac())) { |
| 308 | return ImmutableSet.of(H1); |
| 309 | } else if (mac.equals(H2.mac())) { |
| 310 | return ImmutableSet.of(H2); |
| 311 | } |
| 312 | return ImmutableSet.of(); |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public Set<Host> getHostsByIp(IpAddress ip) { |
| 317 | if ((H1.ipAddresses().contains(ip))) { |
| 318 | return ImmutableSet.of(H1); |
| 319 | } else if ((H2.ipAddresses().contains(ip))) { |
| 320 | return ImmutableSet.of(H2); |
| 321 | } |
| 322 | return ImmutableSet.of(); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | private class TestLinkService extends LinkServiceAdapter { |
| 327 | @Override |
| 328 | public Set<Link> getEgressLinks(ConnectPoint connectPoint) { |
| 329 | if (connectPoint.equals(TOPO_FLOW_1_OUT_CP) |
| 330 | || connectPoint.equals(TOPO_FLOW_OUT_CP_1)) { |
| 331 | return ImmutableSet.of(DefaultLink.builder() |
| 332 | .providerId(ProviderId.NONE) |
| 333 | .type(Link.Type.DIRECT) |
| 334 | .src(connectPoint) |
| 335 | .dst(TOPO_FLOW_2_IN_CP) |
| 336 | .build()); |
| 337 | } else if (connectPoint.equals(TOPO_FLOW_2_OUT_CP)) { |
| 338 | return ImmutableSet.of(DefaultLink.builder() |
| 339 | .providerId(ProviderId.NONE) |
| 340 | .type(Link.Type.DIRECT) |
| 341 | .src(TOPO_FLOW_2_OUT_CP) |
| 342 | .dst(TOPO_FLOW_3_IN_CP) |
| 343 | .build()); |
| 344 | } else if (connectPoint.equals(TOPO_FLOW_OUT_CP_2)) { |
| 345 | return ImmutableSet.of(DefaultLink.builder() |
| 346 | .providerId(ProviderId.NONE) |
| 347 | .type(Link.Type.DIRECT) |
| 348 | .src(TOPO_FLOW_OUT_CP_2) |
| 349 | .dst(TOPO_FLOW_4_IN_CP) |
| 350 | .build()); |
| 351 | } else if (connectPoint.equals(TOPO_FLOW_4_OUT_CP)) { |
| 352 | return ImmutableSet.of(DefaultLink.builder() |
| 353 | .providerId(ProviderId.NONE) |
| 354 | .type(Link.Type.DIRECT) |
| 355 | .src(TOPO_FLOW_4_OUT_CP) |
| 356 | .dst(TOPO_FLOW_3_IN_2_CP) |
| 357 | .build()); |
| 358 | } |
| 359 | return ImmutableSet.of(); |
| 360 | } |
| 361 | } |
Andrea Campanella | 37d1062 | 2018-01-18 17:11:42 +0100 | [diff] [blame^] | 362 | |
| 363 | private class TestDeviceService extends DeviceServiceAdapter { |
| 364 | @Override |
| 365 | public Device getDevice(DeviceId deviceId) { |
| 366 | if (deviceId.equals(DeviceId.deviceId("nonexistent"))) { |
| 367 | return null; |
| 368 | } |
| 369 | return new DefaultDevice(ProviderId.NONE, DeviceId.deviceId("test"), SWITCH, |
| 370 | "test", "test", "test", "test", new ChassisId(), |
| 371 | DefaultAnnotations.builder().set("foo", "bar").build()); |
| 372 | } |
| 373 | |
| 374 | @Override |
| 375 | public boolean isAvailable(DeviceId deviceId) { |
| 376 | if (deviceId.equals(OFFLINE_DEVICE)) { |
| 377 | return false; |
| 378 | } |
| 379 | return true; |
| 380 | } |
| 381 | } |
Andrea Campanella | 01e886e | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 382 | } |