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