blob: 31cebe0193b8bfcbd271a83d7d409be9a5a9d83e [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
Sho SHIMIZU883354c2015-09-10 11:02:39 -070027import java.util.Collections;
Brian O'Connor3c58e962015-04-28 23:21:51 -070028import java.util.Set;
29import java.util.concurrent.atomic.AtomicBoolean;
30import java.util.stream.Collectors;
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'Connor3c58e962015-04-28 23:21:51 -070048 AtomicBoolean thisSuccess = new AtomicBoolean(success);
Brian O'Connor5811ac22015-02-09 19:17:07 -080049 ops.stages().forEach(stage -> stage.forEach(flow -> {
Ray Milkey77a455f2015-03-27 10:08:17 -070050 if (errorFlow == flow.rule().id().value()) {
Brian O'Connor3c58e962015-04-28 23:21:51 -070051 thisSuccess.set(false);
Ray Milkey77a455f2015-03-27 10:08:17 -070052 } else {
53 switch (flow.type()) {
54 case ADD:
55 case MODIFY: //TODO is this the right behavior for modify?
56 flows.add(flow.rule());
57 break;
58 case REMOVE:
59 flows.remove(flow.rule());
60 break;
61 default:
62 break;
63 }
Brian O'Connor5811ac22015-02-09 19:17:07 -080064 }
65 }));
Brian O'Connor3c58e962015-04-28 23:21:51 -070066 if (thisSuccess.get()) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080067 ops.callback().onSuccess(ops);
68 } else {
69 ops.callback().onError(ops);
70 }
71 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -080072
Brian O'Connor5811ac22015-02-09 19:17:07 -080073 @Override
Brian O'Connor427a1762014-11-19 18:40:32 -080074 public int getFlowRuleCount() {
75 return flows.size();
76 }
77
78 @Override
79 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080080 return flows.stream()
81 .filter(flow -> flow.deviceId().equals(deviceId))
82 .map(DefaultFlowEntry::new)
83 .collect(Collectors.toList());
Brian O'Connor427a1762014-11-19 18:40:32 -080084 }
85
86 @Override
87 public void applyFlowRules(FlowRule... flowRules) {
Sho SHIMIZU883354c2015-09-10 11:02:39 -070088 Collections.addAll(flows, flowRules);
Brian O'Connor427a1762014-11-19 18:40:32 -080089 }
90
91 @Override
92 public void removeFlowRules(FlowRule... flowRules) {
Brian O'Connor5811ac22015-02-09 19:17:07 -080093 for (FlowRule flow : flowRules) {
94 flows.remove(flow);
95 }
Brian O'Connor427a1762014-11-19 18:40:32 -080096 }
97
98 @Override
Brian O'Connor427a1762014-11-19 18:40:32 -080099 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Brian O'Connor5811ac22015-02-09 19:17:07 -0800100 return flows.stream()
101 .filter(flow -> flow.appId() == appId.id() && flow.groupId().id() == groupId)
102 .collect(Collectors.toList());
Brian O'Connor427a1762014-11-19 18:40:32 -0800103 }
104}
105