blob: 804a27c2770762aca688ecfc03f40a7e24b2ce74 [file] [log] [blame]
Nikhil Cheerlad6734f62015-07-21 10:41:44 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Nikhil Cheerlad6734f62015-07-21 10:41:44 -07003 *
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.flowanalyzer;
17
18import org.junit.Ignore;
19import org.junit.Test;
20
21import org.onosproject.core.DefaultApplicationId;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.PortNumber;
24import org.onosproject.net.flow.DefaultFlowRule;
25import org.onosproject.net.flow.DefaultTrafficSelector;
26import org.onosproject.net.flow.DefaultTrafficTreatment;
27import org.onosproject.net.flow.FlowRule;
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070028import org.onosproject.net.flow.FlowRuleService;
29import org.onosproject.net.flow.TrafficSelector;
30import org.onosproject.net.flow.TrafficTreatment;
31import org.onosproject.net.flow.instructions.Instructions;
32import org.onosproject.net.topology.TopologyService;
33
34import java.util.Arrays;
35import java.util.TreeSet;
36
37import static org.junit.Assert.assertEquals;
38
39
40/**
41 * Created by nikcheerla on 7/20/15.
42 */
43public 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 Milkey47f09c52019-02-08 18:51:34 -0800107 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 Cheerlad6734f62015-07-21 10:41:44 -0700116 }
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 Milkey47f09c52019-02-08 18:51:34 -0800122 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 Cheerlad6734f62015-07-21 10:41:44 -0700131 }
132
133}