blob: 8bd29bf8ef04215c26f2f588d3e0debe9e442bcd [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
alshabibab984662014-12-04 18:56:18 -08003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.impl;
Brian O'Connor427a1762014-11-19 18:40:32 -080017
Brian O'Connor3c58e962015-04-28 23:21:51 -070018import com.google.common.collect.Sets;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.core.ApplicationId;
20import org.onosproject.net.DeviceId;
Brian O'Connor5811ac22015-02-09 19:17:07 -080021import org.onosproject.net.flow.DefaultFlowEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.flow.FlowEntry;
23import org.onosproject.net.flow.FlowRule;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080024import org.onosproject.net.flow.FlowRuleOperations;
Ray Milkeycc53abd2015-02-19 12:31:33 -080025import org.onosproject.net.flow.FlowRuleServiceAdapter;
Brian O'Connor427a1762014-11-19 18:40:32 -080026
Brian O'Connor3c58e962015-04-28 23:21:51 -070027import java.util.Set;
28import java.util.concurrent.atomic.AtomicBoolean;
29import java.util.stream.Collectors;
30
31import static org.onosproject.net.flow.FlowRuleOperation.Type.REMOVE;
Brian O'Connor427a1762014-11-19 18:40:32 -080032
33
Ray Milkeycc53abd2015-02-19 12:31:33 -080034public class MockFlowRuleService extends FlowRuleServiceAdapter {
Brian O'Connor427a1762014-11-19 18:40:32 -080035
Brian O'Connor427a1762014-11-19 18:40:32 -080036 final Set<FlowRule> flows = Sets.newHashSet();
Brian O'Connor5811ac22015-02-09 19:17:07 -080037 boolean success;
Brian O'Connor427a1762014-11-19 18:40:32 -080038
Ray Milkey77a455f2015-03-27 10:08:17 -070039 int errorFlow = -1;
40 public void setErrorFlow(int errorFlow) {
41 this.errorFlow = errorFlow;
42 }
43
Brian O'Connor427a1762014-11-19 18:40:32 -080044 public void setFuture(boolean success) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080045 this.success = success;
Brian O'Connor427a1762014-11-19 18:40:32 -080046 }
47
48 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -080049 public void apply(FlowRuleOperations ops) {
Brian O'Connor3c58e962015-04-28 23:21:51 -070050 AtomicBoolean thisSuccess = new AtomicBoolean(success);
Brian O'Connor5811ac22015-02-09 19:17:07 -080051 ops.stages().forEach(stage -> stage.forEach(flow -> {
Ray Milkey77a455f2015-03-27 10:08:17 -070052 if (errorFlow == flow.rule().id().value()) {
Brian O'Connor3c58e962015-04-28 23:21:51 -070053 thisSuccess.set(false);
Ray Milkey77a455f2015-03-27 10:08:17 -070054 } else {
55 switch (flow.type()) {
56 case ADD:
57 case MODIFY: //TODO is this the right behavior for modify?
58 flows.add(flow.rule());
59 break;
60 case REMOVE:
61 flows.remove(flow.rule());
62 break;
63 default:
64 break;
65 }
Brian O'Connor5811ac22015-02-09 19:17:07 -080066 }
67 }));
Brian O'Connor3c58e962015-04-28 23:21:51 -070068 if (thisSuccess.get()) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080069 ops.callback().onSuccess(ops);
70 } else {
71 ops.callback().onError(ops);
72 }
73 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -080074
Brian O'Connor5811ac22015-02-09 19:17:07 -080075 @Override
Brian O'Connor427a1762014-11-19 18:40:32 -080076 public int getFlowRuleCount() {
77 return flows.size();
78 }
79
80 @Override
81 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080082 return flows.stream()
83 .filter(flow -> flow.deviceId().equals(deviceId))
84 .map(DefaultFlowEntry::new)
85 .collect(Collectors.toList());
Brian O'Connor427a1762014-11-19 18:40:32 -080086 }
87
88 @Override
89 public void applyFlowRules(FlowRule... flowRules) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080090 for (FlowRule flow : flowRules) {
91 flows.add(flow);
92 }
Brian O'Connor427a1762014-11-19 18:40:32 -080093 }
94
95 @Override
96 public void removeFlowRules(FlowRule... flowRules) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080097 for (FlowRule flow : flowRules) {
98 flows.remove(flow);
99 }
Brian O'Connor427a1762014-11-19 18:40:32 -0800100 }
101
102 @Override
Brian O'Connor427a1762014-11-19 18:40:32 -0800103 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Brian O'Connor5811ac22015-02-09 19:17:07 -0800104 return flows.stream()
105 .filter(flow -> flow.appId() == id.id())
106 .collect(Collectors.toList());
Brian O'Connor427a1762014-11-19 18:40:32 -0800107 }
108
109 @Override
110 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Brian O'Connor5811ac22015-02-09 19:17:07 -0800111 return flows.stream()
112 .filter(flow -> flow.appId() == appId.id() && flow.groupId().id() == groupId)
113 .collect(Collectors.toList());
Brian O'Connor427a1762014-11-19 18:40:32 -0800114 }
115}
116