blob: 27e523d69b4ee4b8db598ebd7da813a62a13023b [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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
Ray Milkeyda36c402015-02-18 10:06:06 -080018import java.util.Set;
19import java.util.stream.Collectors;
20
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.core.ApplicationId;
22import org.onosproject.net.DeviceId;
Brian O'Connor5811ac22015-02-09 19:17:07 -080023import org.onosproject.net.flow.DefaultFlowEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.flow.FlowEntry;
25import org.onosproject.net.flow.FlowRule;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080026import org.onosproject.net.flow.FlowRuleOperations;
Ray Milkeycc53abd2015-02-19 12:31:33 -080027import org.onosproject.net.flow.FlowRuleServiceAdapter;
Brian O'Connor427a1762014-11-19 18:40:32 -080028
Ray Milkeyda36c402015-02-18 10:06:06 -080029import com.google.common.collect.Sets;
Brian O'Connor427a1762014-11-19 18:40:32 -080030
31
Ray Milkeycc53abd2015-02-19 12:31:33 -080032public class MockFlowRuleService extends FlowRuleServiceAdapter {
Brian O'Connor427a1762014-11-19 18:40:32 -080033
Brian O'Connor427a1762014-11-19 18:40:32 -080034 final Set<FlowRule> flows = Sets.newHashSet();
Brian O'Connor5811ac22015-02-09 19:17:07 -080035 boolean success;
Brian O'Connor427a1762014-11-19 18:40:32 -080036
37 public void setFuture(boolean success) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080038 this.success = success;
Brian O'Connor427a1762014-11-19 18:40:32 -080039 }
40
41 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -080042 public void apply(FlowRuleOperations ops) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080043 ops.stages().forEach(stage -> stage.forEach(flow -> {
44 switch (flow.type()) {
45 case ADD:
46 case MODIFY: //TODO is this the right behavior for modify?
47 flows.add(flow.rule());
48 break;
49 case REMOVE:
50 flows.remove(flow.rule());
51 break;
52 default:
53 break;
54 }
55 }));
56 if (success) {
57 ops.callback().onSuccess(ops);
58 } else {
59 ops.callback().onError(ops);
60 }
61 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -080062
Brian O'Connor5811ac22015-02-09 19:17:07 -080063 @Override
Brian O'Connor427a1762014-11-19 18:40:32 -080064 public int getFlowRuleCount() {
65 return flows.size();
66 }
67
68 @Override
69 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080070 return flows.stream()
71 .filter(flow -> flow.deviceId().equals(deviceId))
72 .map(DefaultFlowEntry::new)
73 .collect(Collectors.toList());
Brian O'Connor427a1762014-11-19 18:40:32 -080074 }
75
76 @Override
77 public void applyFlowRules(FlowRule... flowRules) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080078 for (FlowRule flow : flowRules) {
79 flows.add(flow);
80 }
Brian O'Connor427a1762014-11-19 18:40:32 -080081 }
82
83 @Override
84 public void removeFlowRules(FlowRule... flowRules) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080085 for (FlowRule flow : flowRules) {
86 flows.remove(flow);
87 }
Brian O'Connor427a1762014-11-19 18:40:32 -080088 }
89
90 @Override
Brian O'Connor427a1762014-11-19 18:40:32 -080091 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080092 return flows.stream()
93 .filter(flow -> flow.appId() == id.id())
94 .collect(Collectors.toList());
Brian O'Connor427a1762014-11-19 18:40:32 -080095 }
96
97 @Override
98 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080099 return flows.stream()
100 .filter(flow -> flow.appId() == appId.id() && flow.groupId().id() == groupId)
101 .collect(Collectors.toList());
Brian O'Connor427a1762014-11-19 18:40:32 -0800102 }
103}
104