alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 17 | |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.net.DeviceId; |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 21 | import org.onosproject.net.flow.DefaultFlowEntry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.net.flow.FlowEntry; |
| 23 | import org.onosproject.net.flow.FlowRule; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 24 | import org.onosproject.net.flow.FlowRuleOperations; |
Ray Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 25 | import org.onosproject.net.flow.FlowRuleServiceAdapter; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 26 | |
Sho SHIMIZU | 883354c | 2015-09-10 11:02:39 -0700 | [diff] [blame] | 27 | import java.util.Collections; |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 28 | import java.util.Set; |
| 29 | import java.util.concurrent.atomic.AtomicBoolean; |
| 30 | import java.util.stream.Collectors; |
| 31 | |
Ray Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 32 | public class MockFlowRuleService extends FlowRuleServiceAdapter { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 33 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 34 | final Set<FlowRule> flows = Sets.newHashSet(); |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 35 | boolean success; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 36 | |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 37 | int errorFlow = -1; |
| 38 | public void setErrorFlow(int errorFlow) { |
| 39 | this.errorFlow = errorFlow; |
| 40 | } |
| 41 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 42 | public void setFuture(boolean success) { |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 43 | this.success = success; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 47 | public void apply(FlowRuleOperations ops) { |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 48 | AtomicBoolean thisSuccess = new AtomicBoolean(success); |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 49 | ops.stages().forEach(stage -> stage.forEach(flow -> { |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 50 | if (errorFlow == flow.rule().id().value()) { |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 51 | thisSuccess.set(false); |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 52 | } 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'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 64 | } |
| 65 | })); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 66 | if (thisSuccess.get()) { |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 67 | ops.callback().onSuccess(ops); |
| 68 | } else { |
| 69 | ops.callback().onError(ops); |
| 70 | } |
| 71 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 72 | |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 73 | @Override |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 74 | public int getFlowRuleCount() { |
| 75 | return flows.size(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 80 | return flows.stream() |
| 81 | .filter(flow -> flow.deviceId().equals(deviceId)) |
| 82 | .map(DefaultFlowEntry::new) |
| 83 | .collect(Collectors.toList()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void applyFlowRules(FlowRule... flowRules) { |
Sho SHIMIZU | 883354c | 2015-09-10 11:02:39 -0700 | [diff] [blame] | 88 | Collections.addAll(flows, flowRules); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void removeFlowRules(FlowRule... flowRules) { |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 93 | for (FlowRule flow : flowRules) { |
| 94 | flows.remove(flow); |
| 95 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 99 | public Iterable<FlowRule> getFlowRulesById(ApplicationId id) { |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 100 | return flows.stream() |
| 101 | .filter(flow -> flow.appId() == id.id()) |
| 102 | .collect(Collectors.toList()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) { |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 107 | return flows.stream() |
| 108 | .filter(flow -> flow.appId() == appId.id() && flow.groupId().id() == groupId) |
| 109 | .collect(Collectors.toList()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |