blob: 66ea27e5f4a9817352e38a7c77019d52837e30fc [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'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.flow.FlowRuleListener;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080027import org.onosproject.net.flow.FlowRuleOperations;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.flow.FlowRuleService;
Brian O'Connor427a1762014-11-19 18:40:32 -080029
Ray Milkeyda36c402015-02-18 10:06:06 -080030import com.google.common.collect.Sets;
Brian O'Connor427a1762014-11-19 18:40:32 -080031
32
33public class MockFlowRuleService implements FlowRuleService {
34
Brian O'Connor427a1762014-11-19 18:40:32 -080035 final Set<FlowRule> flows = Sets.newHashSet();
Brian O'Connor5811ac22015-02-09 19:17:07 -080036 boolean success;
Brian O'Connor427a1762014-11-19 18:40:32 -080037
38 public void setFuture(boolean success) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080039 this.success = success;
Brian O'Connor427a1762014-11-19 18:40:32 -080040 }
41
42 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -080043 public void apply(FlowRuleOperations ops) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080044 ops.stages().forEach(stage -> stage.forEach(flow -> {
45 switch (flow.type()) {
46 case ADD:
47 case MODIFY: //TODO is this the right behavior for modify?
48 flows.add(flow.rule());
49 break;
50 case REMOVE:
51 flows.remove(flow.rule());
52 break;
53 default:
54 break;
55 }
56 }));
57 if (success) {
58 ops.callback().onSuccess(ops);
59 } else {
60 ops.callback().onError(ops);
61 }
62 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -080063
Brian O'Connor5811ac22015-02-09 19:17:07 -080064 @Override
65 public void addListener(FlowRuleListener listener) {
66 //TODO not implemented
67 }
68
69 @Override
70 public void removeListener(FlowRuleListener listener) {
71 //TODO not implemented
Brian O'Connor72cb19a2015-01-16 16:14:41 -080072 }
73
74 @Override
Brian O'Connor427a1762014-11-19 18:40:32 -080075 public int getFlowRuleCount() {
76 return flows.size();
77 }
78
79 @Override
80 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080081 return flows.stream()
82 .filter(flow -> flow.deviceId().equals(deviceId))
83 .map(DefaultFlowEntry::new)
84 .collect(Collectors.toList());
Brian O'Connor427a1762014-11-19 18:40:32 -080085 }
86
87 @Override
88 public void applyFlowRules(FlowRule... flowRules) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080089 for (FlowRule flow : flowRules) {
90 flows.add(flow);
91 }
Brian O'Connor427a1762014-11-19 18:40:32 -080092 }
93
94 @Override
95 public void removeFlowRules(FlowRule... flowRules) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080096 for (FlowRule flow : flowRules) {
97 flows.remove(flow);
98 }
Brian O'Connor427a1762014-11-19 18:40:32 -080099 }
100
101 @Override
102 public void removeFlowRulesById(ApplicationId appId) {
Brian O'Connor5811ac22015-02-09 19:17:07 -0800103 //TODO not implemented
Brian O'Connor427a1762014-11-19 18:40:32 -0800104 }
105
106 @Override
107 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Brian O'Connor5811ac22015-02-09 19:17:07 -0800108 return flows.stream()
109 .filter(flow -> flow.appId() == id.id())
110 .collect(Collectors.toList());
Brian O'Connor427a1762014-11-19 18:40:32 -0800111 }
112
113 @Override
114 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Brian O'Connor5811ac22015-02-09 19:17:07 -0800115 return flows.stream()
116 .filter(flow -> flow.appId() == appId.id() && flow.groupId().id() == groupId)
117 .collect(Collectors.toList());
Brian O'Connor427a1762014-11-19 18:40:32 -0800118 }
119}
120