aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/flowanalyzer/src/test/java/org/onosproject/flowanalyzer/FlowAnalyzerTest.java
blob: faa2f5f9f95f231e428ef5e617520616a7614230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.flowanalyzer;

import org.junit.Ignore;
import org.junit.Test;

import org.onosproject.core.DefaultApplicationId;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import org.onosproject.net.flow.DefaultFlowRule;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.FlowRule;
import org.onosproject.net.flow.FlowRuleExtPayLoad;
import org.onosproject.net.flow.FlowRuleService;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.flow.instructions.Instructions;
import org.onosproject.net.topology.TopologyService;

import java.util.Arrays;
import java.util.TreeSet;

import static org.junit.Assert.assertEquals;


/**
 * Created by nikcheerla on 7/20/15.
 */
public class FlowAnalyzerTest {

    FlowRuleService flowRuleService = new MockFlowRuleService();
    TopologyService topologyService;
    MockLinkService linkService = new MockLinkService();

    @Test
    @Ignore("This needs to be reworked to be more robust")
    public void basic() {
        flowRuleService = new MockFlowRuleService();
        flowRuleService.applyFlowRules(genFlow("ATL-001", 110, 90));
        flowRuleService.applyFlowRules(genFlow("ATL-001", 110, 100));
        flowRuleService.applyFlowRules(genFlow("ATL-001", 110, 150));
        flowRuleService.applyFlowRules(genFlow("ATL-002", 80, 70));
        flowRuleService.applyFlowRules(genFlow("ATL-003", 120, 130));
        flowRuleService.applyFlowRules(genFlow("ATL-004", 50));
        flowRuleService.applyFlowRules(genFlow("ATL-005", 140, 10));

        linkService.addLink("H00:00:00:00:00:0660", 160, "ATL-005", 140);
        linkService.addLink("ATL-005", 10, "ATL-004", 40);
        linkService.addLink("ATL-004", 50, "ATL-002", 80);
        linkService.addLink("ATL-002", 70, "ATL-001", 110);
        linkService.addLink("ATL-001", 150, "H00:00:00:00:00:0770", 170);
        linkService.addLink("ATL-001", 90, "ATL-004", 30);
        linkService.addLink("ATL-001", 100, "ATL-003", 120);
        linkService.addLink("ATL-003", 130, "ATL-005", 20);

        topologyService = new MockTopologyService(linkService.createdGraph);

        FlowAnalyzer flowAnalyzer = new FlowAnalyzer();
        flowAnalyzer.flowRuleService = flowRuleService;
        flowAnalyzer.linkService = linkService;
        flowAnalyzer.topologyService = topologyService;

        String labels = flowAnalyzer.analysisOutput();
        String correctOutput = "Flow Rule: Device: atl-005, [IN_PORT{port=140}], [OUTPUT{port=10}]\n" +
                "Analysis: Cleared!\n" +
                "\n" +
                "Flow Rule: Device: atl-003, [IN_PORT{port=120}], [OUTPUT{port=130}]\n" +
                "Analysis: Black Hole!\n" +
                "\n" +
                "Flow Rule: Device: atl-001, [IN_PORT{port=110}], [OUTPUT{port=90}]\n" +
                "Analysis: Cycle Critical Point!\n" +
                "\n" +
                "Flow Rule: Device: atl-004, [], [OUTPUT{port=50}]\n" +
                "Analysis: Cycle!\n" +
                "\n" +
                "Flow Rule: Device: atl-001, [IN_PORT{port=110}], [OUTPUT{port=150}]\n" +
                "Analysis: Cleared!\n" +
                "\n" +
                "Flow Rule: Device: atl-001, [IN_PORT{port=110}], [OUTPUT{port=100}]\n" +
                "Analysis: Black Hole!\n" +
                "\n" +
                "Flow Rule: Device: atl-002, [IN_PORT{port=80}], [OUTPUT{port=70}]\n" +
                "Analysis: Cycle!\n";
        assertEquals("Wrong labels", new TreeSet(Arrays.asList(labels.replaceAll("\\s+", "").split("!"))),
                     new TreeSet(Arrays.asList(correctOutput.replaceAll("\\s+", "").split("!"))));
    }

    public FlowRule genFlow(String d, long inPort, long outPort) {
        DeviceId device = DeviceId.deviceId(d);
        TrafficSelector ts = DefaultTrafficSelector.builder().matchInPort(PortNumber.portNumber(inPort)).build();
        TrafficTreatment tt = DefaultTrafficTreatment.builder()
                .add(Instructions.createOutput(PortNumber.portNumber(outPort))).build();
        return new DefaultFlowRule(device, ts, tt, 1, new DefaultApplicationId(5000, "of"),
                                   50000, true, FlowRuleExtPayLoad.flowRuleExtPayLoad(new byte[5]));
    }
    public FlowRule genFlow(String d, long outPort) {
        DeviceId device = DeviceId.deviceId(d);
        TrafficSelector ts = DefaultTrafficSelector.builder().build();
        TrafficTreatment tt = DefaultTrafficTreatment.builder()
                .add(Instructions.createOutput(PortNumber.portNumber(outPort))).build();
        return new DefaultFlowRule(device, ts, tt, 1, new DefaultApplicationId(5000, "of"),
                                   50000, true, FlowRuleExtPayLoad.flowRuleExtPayLoad(new byte[5]));
    }

}