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