blob: 382a4687d54e6bab12ba002ce7ed2a8e9e7d9526 [file] [log] [blame]
Andrea Campanella01e886e2017-12-15 15:27:31 +01001/*
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 */
16package org.onosproject.t3.impl;
17
18import com.google.common.collect.ImmutableList;
19import com.google.common.collect.ImmutableSet;
20import org.junit.Before;
21import org.junit.Test;
Andrea Campanella37d10622018-01-18 17:11:42 +010022import org.onlab.packet.ChassisId;
Andrea Campanella01e886e2017-12-15 15:27:31 +010023import org.onlab.packet.EthType;
24import org.onlab.packet.IpAddress;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
27import org.onosproject.net.ConnectPoint;
Andrea Campanella37d10622018-01-18 17:11:42 +010028import org.onosproject.net.DefaultAnnotations;
29import org.onosproject.net.DefaultDevice;
Andrea Campanella01e886e2017-12-15 15:27:31 +010030import org.onosproject.net.DefaultLink;
Andrea Campanella37d10622018-01-18 17:11:42 +010031import org.onosproject.net.Device;
Andrea Campanella01e886e2017-12-15 15:27:31 +010032import org.onosproject.net.DeviceId;
33import org.onosproject.net.Host;
34import org.onosproject.net.Link;
Andrea Campanella37d10622018-01-18 17:11:42 +010035import org.onosproject.net.device.DeviceServiceAdapter;
Andrea Campanella01e886e2017-12-15 15:27:31 +010036import org.onosproject.net.driver.DefaultDriver;
37import org.onosproject.net.driver.Driver;
38import org.onosproject.net.driver.DriverServiceAdapter;
39import org.onosproject.net.flow.FlowEntry;
40import org.onosproject.net.flow.FlowRuleServiceAdapter;
41import org.onosproject.net.flow.TrafficSelector;
42import org.onosproject.net.flow.criteria.Criterion;
43import org.onosproject.net.flow.criteria.EthTypeCriterion;
44import org.onosproject.net.flow.criteria.VlanIdCriterion;
45import org.onosproject.net.group.Group;
46import org.onosproject.net.group.GroupServiceAdapter;
47import org.onosproject.net.host.HostServiceAdapter;
48import org.onosproject.net.link.LinkServiceAdapter;
49import org.onosproject.net.provider.ProviderId;
50import org.onosproject.t3.api.StaticPacketTrace;
51import org.slf4j.Logger;
52
53import java.util.HashMap;
54import java.util.Set;
55
56import static org.junit.Assert.assertEquals;
57import static org.junit.Assert.assertNotNull;
58import static org.junit.Assert.assertNull;
59import static org.junit.Assert.assertTrue;
Andrea Campanella37d10622018-01-18 17:11:42 +010060import static org.onosproject.net.Device.Type.SWITCH;
Andrea Campanella01e886e2017-12-15 15:27:31 +010061import static org.onosproject.t3.impl.T3TestObjects.*;
62import static org.slf4j.LoggerFactory.getLogger;
63
64/**
65 * Test Class for Troubleshoot Manager.
66 */
67public 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 Campanella37d10622018-01-18 17:11:42 +010081 mngr.deviceService = new TestDeviceService();
Andrea Campanella01e886e2017-12-15 15:27:31 +010082
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 Campanella37d10622018-01-18 17:11:42 +010090 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 Campanella01e886e2017-12-15 15:27:31 +0100110 }
111
112 /**
Andrea Campanella2bc55dd2018-01-18 16:54:34 +0100113 * Tests failure on device with no flows.
Andrea Campanella01e886e2017-12-15 15:27:31 +0100114 */
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 Campanella2bc55dd2018-01-18 16:54:34 +0100142 StaticPacketTrace traceSuccess = testSuccess(PACKET_OK, DUAL_FLOW_IN_CP, DUAL_FLOW_DEVICE,
143 DUAL_FLOW_OUT_CP, 1);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100144
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 Campanella2bc55dd2018-01-18 16:54:34 +0100165 StaticPacketTrace traceSuccess = testSuccess(PACKET_OK, GROUP_FLOW_IN_CP, GROUP_FLOW_DEVICE,
166 GROUP_FLOW_OUT_CP, 1);
Andrea Campanella01e886e2017-12-15 15:27:31 +0100167
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 Campanella2bc55dd2018-01-18 16:54:34 +0100265 } else if (deviceId.equals(HARDWARE_DEVICE)) {
Andrea Campanella01e886e2017-12-15 15:27:31 +0100266 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 Campanella2bc55dd2018-01-18 16:54:34 +0100275 if (deviceId.equals(HARDWARE_DEVICE)) {
Andrea Campanella01e886e2017-12-15 15:27:31 +0100276 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 Campanella37d10622018-01-18 17:11:42 +0100362
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 Campanella01e886e2017-12-15 15:27:31 +0100382}