Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -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 | package org.onosproject.flowanalyzer; |
| 17 | |
| 18 | import org.junit.Ignore; |
| 19 | import org.junit.Test; |
| 20 | |
| 21 | import org.onosproject.core.DefaultApplicationId; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.PortNumber; |
| 24 | import org.onosproject.net.flow.DefaultFlowRule; |
| 25 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 26 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 27 | import org.onosproject.net.flow.FlowRule; |
| 28 | import org.onosproject.net.flow.FlowRuleExtPayLoad; |
| 29 | import org.onosproject.net.flow.FlowRuleService; |
| 30 | import org.onosproject.net.flow.TrafficSelector; |
| 31 | import org.onosproject.net.flow.TrafficTreatment; |
| 32 | import org.onosproject.net.flow.instructions.Instructions; |
| 33 | import org.onosproject.net.topology.TopologyService; |
| 34 | |
| 35 | import java.util.Arrays; |
| 36 | import java.util.TreeSet; |
| 37 | |
| 38 | import static org.junit.Assert.assertEquals; |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Created by nikcheerla on 7/20/15. |
| 43 | */ |
| 44 | public class FlowAnalyzerTest { |
| 45 | |
| 46 | FlowRuleService flowRuleService = new MockFlowRuleService(); |
| 47 | TopologyService topologyService; |
| 48 | MockLinkService linkService = new MockLinkService(); |
| 49 | |
| 50 | @Test |
| 51 | @Ignore("This needs to be reworked to be more robust") |
| 52 | public void basic() { |
| 53 | flowRuleService = new MockFlowRuleService(); |
| 54 | flowRuleService.applyFlowRules(genFlow("ATL-001", 110, 90)); |
| 55 | flowRuleService.applyFlowRules(genFlow("ATL-001", 110, 100)); |
| 56 | flowRuleService.applyFlowRules(genFlow("ATL-001", 110, 150)); |
| 57 | flowRuleService.applyFlowRules(genFlow("ATL-002", 80, 70)); |
| 58 | flowRuleService.applyFlowRules(genFlow("ATL-003", 120, 130)); |
| 59 | flowRuleService.applyFlowRules(genFlow("ATL-004", 50)); |
| 60 | flowRuleService.applyFlowRules(genFlow("ATL-005", 140, 10)); |
| 61 | |
| 62 | linkService.addLink("H00:00:00:00:00:0660", 160, "ATL-005", 140); |
| 63 | linkService.addLink("ATL-005", 10, "ATL-004", 40); |
| 64 | linkService.addLink("ATL-004", 50, "ATL-002", 80); |
| 65 | linkService.addLink("ATL-002", 70, "ATL-001", 110); |
| 66 | linkService.addLink("ATL-001", 150, "H00:00:00:00:00:0770", 170); |
| 67 | linkService.addLink("ATL-001", 90, "ATL-004", 30); |
| 68 | linkService.addLink("ATL-001", 100, "ATL-003", 120); |
| 69 | linkService.addLink("ATL-003", 130, "ATL-005", 20); |
| 70 | |
| 71 | topologyService = new MockTopologyService(linkService.createdGraph); |
| 72 | |
| 73 | FlowAnalyzer flowAnalyzer = new FlowAnalyzer(); |
| 74 | flowAnalyzer.flowRuleService = flowRuleService; |
| 75 | flowAnalyzer.linkService = linkService; |
| 76 | flowAnalyzer.topologyService = topologyService; |
| 77 | |
| 78 | String labels = flowAnalyzer.analysisOutput(); |
| 79 | String correctOutput = "Flow Rule: Device: atl-005, [IN_PORT{port=140}], [OUTPUT{port=10}]\n" + |
| 80 | "Analysis: Cleared!\n" + |
| 81 | "\n" + |
| 82 | "Flow Rule: Device: atl-003, [IN_PORT{port=120}], [OUTPUT{port=130}]\n" + |
| 83 | "Analysis: Black Hole!\n" + |
| 84 | "\n" + |
| 85 | "Flow Rule: Device: atl-001, [IN_PORT{port=110}], [OUTPUT{port=90}]\n" + |
| 86 | "Analysis: Cycle Critical Point!\n" + |
| 87 | "\n" + |
| 88 | "Flow Rule: Device: atl-004, [], [OUTPUT{port=50}]\n" + |
| 89 | "Analysis: Cycle!\n" + |
| 90 | "\n" + |
| 91 | "Flow Rule: Device: atl-001, [IN_PORT{port=110}], [OUTPUT{port=150}]\n" + |
| 92 | "Analysis: Cleared!\n" + |
| 93 | "\n" + |
| 94 | "Flow Rule: Device: atl-001, [IN_PORT{port=110}], [OUTPUT{port=100}]\n" + |
| 95 | "Analysis: Black Hole!\n" + |
| 96 | "\n" + |
| 97 | "Flow Rule: Device: atl-002, [IN_PORT{port=80}], [OUTPUT{port=70}]\n" + |
| 98 | "Analysis: Cycle!\n"; |
| 99 | assertEquals("Wrong labels", new TreeSet(Arrays.asList(labels.replaceAll("\\s+", "").split("!"))), |
| 100 | new TreeSet(Arrays.asList(correctOutput.replaceAll("\\s+", "").split("!")))); |
| 101 | } |
| 102 | |
| 103 | public FlowRule genFlow(String d, long inPort, long outPort) { |
| 104 | DeviceId device = DeviceId.deviceId(d); |
| 105 | TrafficSelector ts = DefaultTrafficSelector.builder().matchInPort(PortNumber.portNumber(inPort)).build(); |
| 106 | TrafficTreatment tt = DefaultTrafficTreatment.builder() |
| 107 | .add(Instructions.createOutput(PortNumber.portNumber(outPort))).build(); |
| 108 | return new DefaultFlowRule(device, ts, tt, 1, new DefaultApplicationId(5000, "of"), |
| 109 | 50000, true, FlowRuleExtPayLoad.flowRuleExtPayLoad(new byte[5])); |
| 110 | } |
| 111 | public FlowRule genFlow(String d, long outPort) { |
| 112 | DeviceId device = DeviceId.deviceId(d); |
| 113 | TrafficSelector ts = DefaultTrafficSelector.builder().build(); |
| 114 | TrafficTreatment tt = DefaultTrafficTreatment.builder() |
| 115 | .add(Instructions.createOutput(PortNumber.portNumber(outPort))).build(); |
| 116 | return new DefaultFlowRule(device, ts, tt, 1, new DefaultApplicationId(5000, "of"), |
| 117 | 50000, true, FlowRuleExtPayLoad.flowRuleExtPayLoad(new byte[5])); |
| 118 | } |
| 119 | |
| 120 | } |