Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [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 | */ |
| 16 | package org.onosproject.net.intent.util; |
| 17 | |
| 18 | import org.onosproject.net.DeviceId; |
| 19 | import org.onosproject.net.flow.DefaultFlowRule; |
| 20 | import org.onosproject.net.flow.FlowEntry; |
| 21 | import org.onosproject.net.flow.FlowRule; |
| 22 | import org.onosproject.net.flow.FlowRuleService; |
| 23 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 24 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 25 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 26 | import org.onosproject.net.flowobjective.NextObjective; |
| 27 | import org.onosproject.net.flowobjective.Objective; |
| 28 | import org.onosproject.net.intent.FlowObjectiveIntent; |
| 29 | import org.onosproject.net.intent.FlowRuleIntent; |
| 30 | import org.onosproject.net.intent.Intent; |
| 31 | import org.onosproject.net.intent.IntentService; |
| 32 | |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.Collection; |
| 35 | import java.util.Iterator; |
| 36 | import java.util.List; |
| 37 | |
| 38 | /** |
| 39 | * Utility to get flow entries corresponding to specified intent. |
| 40 | */ |
| 41 | public class IntentFilter { |
| 42 | |
| 43 | private final IntentService intentService; |
| 44 | private final FlowRuleService flowRuleService; |
| 45 | |
| 46 | /** |
| 47 | * Creates an intent filter. |
| 48 | * |
| 49 | * @param intentService intent service object |
| 50 | * @param flowRuleService flow service object |
| 51 | */ |
| 52 | public IntentFilter(IntentService intentService, |
| 53 | FlowRuleService flowRuleService) { |
| 54 | this.intentService = intentService; |
| 55 | this.flowRuleService = flowRuleService; |
| 56 | } |
| 57 | |
| 58 | //checks whether the collection is empty or not. |
| 59 | private boolean nonEmpty(Collection<?> c) { |
| 60 | return c != null && !c.isEmpty(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Finds all path (flow entries) corresponding to intent installables. |
| 65 | * |
| 66 | * @param installables set of installables |
| 67 | * @return set of flow entries |
| 68 | */ |
| 69 | public List<List<FlowEntry>> readIntentFlows(List<Intent> installables) { |
| 70 | List<List<FlowEntry>> paths = new ArrayList<>(); |
| 71 | |
| 72 | for (Intent installable : installables) { |
| 73 | |
| 74 | if (installable instanceof FlowRuleIntent) { |
| 75 | List<FlowEntry> flowEntries = |
| 76 | getFlowEntries((FlowRuleIntent) installable); |
| 77 | if (nonEmpty(flowEntries)) { |
| 78 | paths.add(flowEntries); |
| 79 | } |
| 80 | |
| 81 | } else if (installable instanceof FlowObjectiveIntent) { |
| 82 | List<FlowEntry> flowEntries = getFlowEntries( |
| 83 | (FlowObjectiveIntent) installable); |
| 84 | if (nonEmpty(flowEntries)) { |
| 85 | paths.add(flowEntries); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | return paths; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Finds all flow entries created by FlowRuleIntent. |
| 94 | * |
| 95 | * @param intent FlowRuleIntent Object |
| 96 | * @return set of flow entries created by FlowRuleIntent |
| 97 | */ |
| 98 | private List<FlowEntry> getFlowEntries(FlowRuleIntent intent) { |
| 99 | List<FlowEntry> flowEntries = new ArrayList<>(); |
| 100 | Collection<FlowRule> flowRules = intent.flowRules(); |
| 101 | FlowEntry flowEntry; |
| 102 | |
| 103 | for (FlowRule flowRule : flowRules) { |
| 104 | flowEntry = getFlowEntry(flowRule); |
| 105 | |
| 106 | if (flowEntry != null) { |
| 107 | flowEntries.add(flowEntry); |
| 108 | } |
| 109 | } |
| 110 | return flowEntries; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Finds all flow entries created by FlowObjectiveIntent. |
| 115 | * |
| 116 | * @param intent FlowObjectiveIntent Object |
| 117 | * @return set of flow entries created by FlowObjectiveIntent |
| 118 | */ |
| 119 | private List<FlowEntry> getFlowEntries(FlowObjectiveIntent intent) { |
| 120 | List<FlowEntry> flowEntries = new ArrayList<>(); |
| 121 | Iterator<Objective> objectives = intent.objectives().iterator(); |
| 122 | Iterator<DeviceId> devices = intent.devices().iterator(); |
| 123 | DefaultNextObjective nextObjective = null; |
| 124 | DefaultForwardingObjective forwardObjective; |
| 125 | Objective objective; |
| 126 | DeviceId deviceId; |
| 127 | FlowEntry flowEntry; |
| 128 | |
| 129 | while (objectives.hasNext()) { |
| 130 | objective = objectives.next(); |
| 131 | deviceId = devices.next(); |
| 132 | |
| 133 | if (objective instanceof NextObjective) { |
| 134 | nextObjective = (DefaultNextObjective) objective; |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 135 | } else if (objective instanceof ForwardingObjective) { |
| 136 | forwardObjective = (DefaultForwardingObjective) objective; |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 137 | FlowRule.Builder builder = DefaultFlowRule.builder() |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 138 | .forDevice(deviceId) |
| 139 | .withSelector(forwardObjective.selector()) |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 140 | .withPriority(intent.priority()) |
| 141 | .fromApp(intent.appId()) |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 142 | .makePermanent(); |
| 143 | if (nextObjective != null) { |
| 144 | builder.withTreatment(nextObjective.next().iterator().next()); |
| 145 | } |
| 146 | FlowRule flowRule = builder.build(); |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 147 | flowEntry = getFlowEntry(flowRule); |
| 148 | |
| 149 | if (flowEntry != null) { |
| 150 | flowEntries.add(flowEntry); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | return flowEntries; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Finds FlowEntry matching with the FlowRule. |
| 159 | * |
| 160 | * @param flowRule FlowRule object |
| 161 | * @return flow entry matching to FlowRule |
| 162 | */ |
| 163 | private FlowEntry getFlowEntry(FlowRule flowRule) { |
| 164 | Iterable<FlowEntry> flowEntries = |
| 165 | flowRuleService.getFlowEntries(flowRule.deviceId()); |
| 166 | |
| 167 | if (flowEntries != null) { |
| 168 | for (FlowEntry entry : flowEntries) { |
| 169 | if (entry.exactMatch(flowRule)) { |
| 170 | return entry; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | return null; |
| 175 | } |
| 176 | |
| 177 | } |