blob: 87ab21a0144cda35edf3bc7f1967af53ec688414 [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
Ray Milkey77a455f2015-03-27 10:08:17 -070037 int errorFlow = -1;
38 public void setErrorFlow(int errorFlow) {
39 this.errorFlow = errorFlow;
40 }
41
Brian O'Connor427a1762014-11-19 18:40:32 -080042 public void setFuture(boolean success) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080043 this.success = success;
Brian O'Connor427a1762014-11-19 18:40:32 -080044 }
45
46 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -080047 public void apply(FlowRuleOperations ops) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080048 ops.stages().forEach(stage -> stage.forEach(flow -> {
Ray Milkey77a455f2015-03-27 10:08:17 -070049 if (errorFlow == flow.rule().id().value()) {
50 success = false;
51 } else {
52 switch (flow.type()) {
53 case ADD:
54 case MODIFY: //TODO is this the right behavior for modify?
55 flows.add(flow.rule());
56 break;
57 case REMOVE:
58 flows.remove(flow.rule());
59 break;
60 default:
61 break;
62 }
Brian O'Connor5811ac22015-02-09 19:17:07 -080063 }
64 }));
65 if (success) {
66 ops.callback().onSuccess(ops);
67 } else {
68 ops.callback().onError(ops);
69 }
70 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -080071
Brian O'Connor5811ac22015-02-09 19:17:07 -080072 @Override
Brian O'Connor427a1762014-11-19 18:40:32 -080073 public int getFlowRuleCount() {
74 return flows.size();
75 }
76
77 @Override
78 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080079 return flows.stream()
80 .filter(flow -> flow.deviceId().equals(deviceId))
81 .map(DefaultFlowEntry::new)
82 .collect(Collectors.toList());
Brian O'Connor427a1762014-11-19 18:40:32 -080083 }
84
85 @Override
86 public void applyFlowRules(FlowRule... flowRules) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080087 for (FlowRule flow : flowRules) {
88 flows.add(flow);
89 }
Brian O'Connor427a1762014-11-19 18:40:32 -080090 }
91
92 @Override
93 public void removeFlowRules(FlowRule... flowRules) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080094 for (FlowRule flow : flowRules) {
95 flows.remove(flow);
96 }
Brian O'Connor427a1762014-11-19 18:40:32 -080097 }
98
99 @Override
Brian O'Connor427a1762014-11-19 18:40:32 -0800100 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Brian O'Connor5811ac22015-02-09 19:17:07 -0800101 return flows.stream()
102 .filter(flow -> flow.appId() == id.id())
103 .collect(Collectors.toList());
Brian O'Connor427a1762014-11-19 18:40:32 -0800104 }
105
106 @Override
107 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Brian O'Connor5811ac22015-02-09 19:17:07 -0800108 return flows.stream()
109 .filter(flow -> flow.appId() == appId.id() && flow.groupId().id() == groupId)
110 .collect(Collectors.toList());
Brian O'Connor427a1762014-11-19 18:40:32 -0800111 }
112}
113