alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
| 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'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 | |
Ray Milkey | da36c40 | 2015-02-18 10:06:06 -0800 | [diff] [blame] | 18 | import java.util.Set; |
| 19 | import java.util.stream.Collectors; |
| 20 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.net.DeviceId; |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 23 | import org.onosproject.net.flow.DefaultFlowEntry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.flow.FlowEntry; |
| 25 | import org.onosproject.net.flow.FlowRule; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 26 | import org.onosproject.net.flow.FlowRuleOperations; |
Ray Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame^] | 27 | import org.onosproject.net.flow.FlowRuleServiceAdapter; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 28 | |
Ray Milkey | da36c40 | 2015-02-18 10:06:06 -0800 | [diff] [blame] | 29 | import com.google.common.collect.Sets; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 30 | |
| 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 | |
| 37 | public void setFuture(boolean success) { |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 38 | this.success = success; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 42 | public void apply(FlowRuleOperations ops) { |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 43 | 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'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 62 | |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 63 | @Override |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 64 | public int getFlowRuleCount() { |
| 65 | return flows.size(); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 70 | return flows.stream() |
| 71 | .filter(flow -> flow.deviceId().equals(deviceId)) |
| 72 | .map(DefaultFlowEntry::new) |
| 73 | .collect(Collectors.toList()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void applyFlowRules(FlowRule... flowRules) { |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 78 | for (FlowRule flow : flowRules) { |
| 79 | flows.add(flow); |
| 80 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void removeFlowRules(FlowRule... flowRules) { |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 85 | for (FlowRule flow : flowRules) { |
| 86 | flows.remove(flow); |
| 87 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 91 | public Iterable<FlowRule> getFlowRulesById(ApplicationId id) { |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 92 | return flows.stream() |
| 93 | .filter(flow -> flow.appId() == id.id()) |
| 94 | .collect(Collectors.toList()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) { |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 99 | return flows.stream() |
| 100 | .filter(flow -> flow.appId() == appId.id() && flow.groupId().id() == groupId) |
| 101 | .collect(Collectors.toList()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |