blob: 40bb00439f885764c08bd0a58b62c524476bafa5 [file] [log] [blame]
Nikhil Cheerlad6734f62015-07-21 10:41:44 -07001package org.onosproject.flowanalyzer;
2
3import com.google.common.collect.Sets;
4import org.onosproject.core.ApplicationId;
5import org.onosproject.net.DeviceId;
6import org.onosproject.net.flow.DefaultFlowEntry;
7import org.onosproject.net.flow.FlowEntry;
8import org.onosproject.net.flow.FlowRule;
9import org.onosproject.net.flow.FlowRuleOperations;
10import org.onosproject.net.flow.FlowRuleServiceAdapter;
11
12import java.util.Set;
13import java.util.concurrent.atomic.AtomicBoolean;
14import java.util.stream.Collectors;
15
16/**
17 * Created by nikcheerla on 7/20/15.
18 */
19
20public class MockFlowRuleService extends FlowRuleServiceAdapter {
21
22 final Set<FlowRule> flows = Sets.newHashSet();
23 boolean success;
24
25 int errorFlow = -1;
26 public void setErrorFlow(int errorFlow) {
27 this.errorFlow = errorFlow;
28 }
29
30 public void setFuture(boolean success) {
31 this.success = success;
32 }
33
34 @Override
35 public void apply(FlowRuleOperations ops) {
36 AtomicBoolean thisSuccess = new AtomicBoolean(success);
37 ops.stages().forEach(stage -> stage.forEach(flow -> {
38 if (errorFlow == flow.rule().id().value()) {
39 thisSuccess.set(false);
40 } else {
41 switch (flow.type()) {
42 case ADD:
43 case MODIFY: //TODO is this the right behavior for modify?
44 flows.add(flow.rule());
45 break;
46 case REMOVE:
47 flows.remove(flow.rule());
48 break;
49 default:
50 break;
51 }
52 }
53 }));
54 if (thisSuccess.get()) {
55 ops.callback().onSuccess(ops);
56 } else {
57 ops.callback().onError(ops);
58 }
59 }
60
61 @Override
62 public int getFlowRuleCount() {
63 return flows.size();
64 }
65
66 @Override
67 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
68 return flows.stream()
69 .filter(flow -> flow.deviceId().equals(deviceId))
70 .map(DefaultFlowEntry::new)
71 .collect(Collectors.toList());
72 }
73
74 @Override
75 public void applyFlowRules(FlowRule... flowRules) {
76 for (FlowRule flow : flowRules) {
77 flows.add(flow);
78 }
79 }
80
81 @Override
82 public void removeFlowRules(FlowRule... flowRules) {
83 for (FlowRule flow : flowRules) {
84 flows.remove(flow);
85 }
86 }
87
88 @Override
89 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
90 return flows.stream()
91 .filter(flow -> flow.appId() == id.id())
92 .collect(Collectors.toList());
93 }
94
95 @Override
96 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
97 return flows.stream()
98 .filter(flow -> flow.appId() == appId.id() && flow.groupId().id() == groupId)
99 .collect(Collectors.toList());
100 }
101}
102
103