Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.pipelines.fabric.pipeliner; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 20 | import org.onlab.util.KryoNamespace; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 21 | import org.onlab.util.SharedExecutors; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.PortNumber; |
| 24 | import org.onosproject.net.behaviour.NextGroup; |
| 25 | import org.onosproject.net.behaviour.Pipeliner; |
| 26 | import org.onosproject.net.behaviour.PipelinerContext; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 27 | import org.onosproject.net.flow.FlowRule; |
| 28 | import org.onosproject.net.flow.FlowRuleOperations; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 29 | import org.onosproject.net.flow.FlowRuleService; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 31 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 32 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flowobjective.IdNextTreatment; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flowobjective.NextObjective; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flowobjective.NextTreatment; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 36 | import org.onosproject.net.flowobjective.Objective; |
| 37 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 38 | import org.onosproject.net.group.GroupDescription; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 39 | import org.onosproject.net.group.GroupService; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 40 | import org.onosproject.pipelines.fabric.AbstractFabricHandlerBehavior; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 41 | import org.onosproject.store.serializers.KryoNamespaces; |
| 42 | import org.slf4j.Logger; |
| 43 | |
| 44 | import java.util.Collection; |
| 45 | import java.util.List; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 46 | import java.util.Objects; |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 47 | import java.util.concurrent.CompletableFuture; |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 48 | import java.util.concurrent.ExecutorService; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 49 | import java.util.stream.Collectors; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 50 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 51 | import static java.lang.String.format; |
| 52 | import static org.onosproject.pipelines.fabric.FabricUtils.outputPort; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 53 | import static org.slf4j.LoggerFactory.getLogger; |
| 54 | |
| 55 | /** |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 56 | * Pipeliner implementation for fabric pipeline which uses ObjectiveTranslator |
| 57 | * implementations to translate flow objectives for the different blocks, |
| 58 | * filtering, forwarding and next. |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 59 | */ |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 60 | public class FabricPipeliner extends AbstractFabricHandlerBehavior |
| 61 | implements Pipeliner { |
| 62 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 63 | private static final Logger log = getLogger(FabricPipeliner.class); |
| 64 | |
| 65 | protected static final KryoNamespace KRYO = new KryoNamespace.Builder() |
| 66 | .register(KryoNamespaces.API) |
| 67 | .register(FabricNextGroup.class) |
| 68 | .build("FabricPipeliner"); |
| 69 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 70 | protected DeviceId deviceId; |
| 71 | protected FlowRuleService flowRuleService; |
| 72 | protected GroupService groupService; |
| 73 | protected FlowObjectiveStore flowObjectiveStore; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 74 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 75 | private FilteringObjectiveTranslator filteringTranslator; |
| 76 | private ForwardingObjectiveTranslator forwardingTranslator; |
| 77 | private NextObjectiveTranslator nextTranslator; |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 78 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 79 | private final ExecutorService callbackExecutor = SharedExecutors.getPoolThreadExecutor(); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 80 | |
| 81 | @Override |
| 82 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 83 | this.deviceId = deviceId; |
| 84 | this.flowRuleService = context.directory().get(FlowRuleService.class); |
| 85 | this.groupService = context.directory().get(GroupService.class); |
| 86 | this.flowObjectiveStore = context.directory().get(FlowObjectiveStore.class); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 87 | this.filteringTranslator = new FilteringObjectiveTranslator(deviceId, capabilities); |
| 88 | this.forwardingTranslator = new ForwardingObjectiveTranslator(deviceId, capabilities); |
| 89 | this.nextTranslator = new NextObjectiveTranslator(deviceId, capabilities); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 93 | public void filter(FilteringObjective obj) { |
| 94 | final ObjectiveTranslation result = filteringTranslator.translate(obj); |
| 95 | handleResult(obj, result); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 99 | public void forward(ForwardingObjective obj) { |
| 100 | final ObjectiveTranslation result = forwardingTranslator.translate(obj); |
| 101 | handleResult(obj, result); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 105 | public void next(NextObjective obj) { |
| 106 | if (obj.op() == Objective.Operation.VERIFY) { |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 107 | // TODO: support VERIFY operation |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 108 | log.debug("VERIFY operation not yet supported for NextObjective, will return success"); |
| 109 | success(obj); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 113 | if (obj.op() == Objective.Operation.MODIFY) { |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 114 | // TODO: support MODIFY operation |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 115 | log.warn("MODIFY operation not yet supported for NextObjective, will return failure :("); |
| 116 | fail(obj, ObjectiveError.UNSUPPORTED); |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 120 | final ObjectiveTranslation result = nextTranslator.translate(obj); |
| 121 | handleResult(obj, result); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public List<String> getNextMappings(NextGroup nextGroup) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 126 | final FabricNextGroup fabricNextGroup = KRYO.deserialize(nextGroup.data()); |
| 127 | return fabricNextGroup.nextMappings().stream() |
| 128 | .map(m -> format("%s -> %s", fabricNextGroup.type(), m)) |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 129 | .collect(Collectors.toList()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 132 | private void handleResult(Objective obj, ObjectiveTranslation result) { |
| 133 | if (result.error().isPresent()) { |
| 134 | fail(obj, result.error().get()); |
| 135 | return; |
| 136 | } |
| 137 | processGroups(obj, result.groups()); |
| 138 | processFlows(obj, result.flowRules()); |
| 139 | if (obj instanceof NextObjective) { |
| 140 | handleNextGroup((NextObjective) obj); |
| 141 | } |
| 142 | success(obj); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 145 | private void handleNextGroup(NextObjective obj) { |
| 146 | switch (obj.op()) { |
| 147 | case REMOVE: |
| 148 | removeNextGroup(obj); |
| 149 | break; |
| 150 | case ADD: |
| 151 | case ADD_TO_EXISTING: |
| 152 | case REMOVE_FROM_EXISTING: |
| 153 | case MODIFY: |
| 154 | putNextGroup(obj); |
| 155 | break; |
| 156 | case VERIFY: |
| 157 | break; |
| 158 | default: |
| 159 | log.error("Unknown NextObjective operation '{}'", obj.op()); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | private void processFlows(Objective objective, Collection<FlowRule> flowRules) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 164 | if (flowRules.isEmpty()) { |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 165 | return; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 166 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 167 | final FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 168 | switch (objective.op()) { |
| 169 | case ADD: |
| 170 | case ADD_TO_EXISTING: |
| 171 | flowRules.forEach(ops::add); |
| 172 | break; |
| 173 | case REMOVE: |
| 174 | case REMOVE_FROM_EXISTING: |
| 175 | flowRules.forEach(ops::remove); |
| 176 | break; |
| 177 | default: |
| 178 | log.warn("Unsupported Objective operation '{}'", objective.op()); |
| 179 | return; |
wu | 914ed23 | 2018-10-23 11:19:53 +0800 | [diff] [blame] | 180 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 181 | flowRuleService.apply(ops.build()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 184 | private void processGroups(Objective objective, Collection<GroupDescription> groups) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 185 | if (groups.isEmpty()) { |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 186 | return; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 187 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 188 | switch (objective.op()) { |
| 189 | case ADD: |
| 190 | groups.forEach(groupService::addGroup); |
| 191 | break; |
| 192 | case REMOVE: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 193 | groups.forEach(group -> groupService.removeGroup( |
| 194 | deviceId, group.appCookie(), objective.appId())); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 195 | break; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 196 | case ADD_TO_EXISTING: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 197 | groups.forEach(group -> groupService.addBucketsToGroup( |
| 198 | deviceId, group.appCookie(), group.buckets(), |
| 199 | group.appCookie(), group.appId()) |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 200 | ); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 201 | break; |
| 202 | case REMOVE_FROM_EXISTING: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 203 | groups.forEach(group -> groupService.removeBucketsFromGroup( |
| 204 | deviceId, group.appCookie(), group.buckets(), |
| 205 | group.appCookie(), group.appId()) |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 206 | ); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 207 | break; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 208 | default: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 209 | log.warn("Unsupported Objective operation {}", objective.op()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 210 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 211 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 212 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 213 | private void fail(Objective objective, ObjectiveError error) { |
| 214 | CompletableFuture.runAsync( |
| 215 | () -> objective.context().ifPresent( |
| 216 | ctx -> ctx.onError(objective, error)), callbackExecutor); |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 217 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 220 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 221 | private void success(Objective objective) { |
| 222 | CompletableFuture.runAsync( |
| 223 | () -> objective.context().ifPresent( |
| 224 | ctx -> ctx.onSuccess(objective)), callbackExecutor); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 227 | private void removeNextGroup(NextObjective obj) { |
| 228 | final NextGroup removed = flowObjectiveStore.removeNextGroup(obj.id()); |
| 229 | if (removed == null) { |
| 230 | log.debug("NextGroup {} was not found in FlowObjectiveStore"); |
| 231 | } |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 234 | private void putNextGroup(NextObjective obj) { |
| 235 | final List<String> nextMappings = obj.nextTreatments().stream() |
| 236 | .map(this::nextTreatmentToMappingString) |
| 237 | .filter(Objects::nonNull) |
| 238 | .collect(Collectors.toList()); |
| 239 | final FabricNextGroup nextGroup = new FabricNextGroup(obj.type(), nextMappings); |
| 240 | flowObjectiveStore.putNextGroup(obj.id(), nextGroup); |
| 241 | } |
| 242 | |
| 243 | private String nextTreatmentToMappingString(NextTreatment n) { |
| 244 | switch (n.type()) { |
| 245 | case TREATMENT: |
| 246 | final PortNumber p = outputPort(n); |
| 247 | return p == null ? "UNKNOWN" |
| 248 | : format("OUTPUT:%s", p.toString()); |
| 249 | case ID: |
| 250 | final IdNextTreatment id = (IdNextTreatment) n; |
| 251 | return format("NEXT_ID:%d", id.nextId()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 252 | default: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 253 | log.warn("Unknown NextTreatment type '{}'", n.type()); |
| 254 | return "???"; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 255 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 258 | /** |
| 259 | * NextGroup implementation. |
| 260 | */ |
| 261 | private static class FabricNextGroup implements NextGroup { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 262 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 263 | private final NextObjective.Type type; |
| 264 | private final List<String> nextMappings; |
| 265 | |
| 266 | FabricNextGroup(NextObjective.Type type, List<String> nextMappings) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 267 | this.type = type; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 268 | this.nextMappings = ImmutableList.copyOf(nextMappings); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 271 | NextObjective.Type type() { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 272 | return type; |
| 273 | } |
| 274 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 275 | Collection<String> nextMappings() { |
| 276 | return nextMappings; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public byte[] data() { |
| 281 | return KRYO.serialize(this); |
| 282 | } |
| 283 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 284 | } |