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 | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 20 | import com.google.common.collect.ImmutableMap; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
| 22 | import org.onlab.util.KryoNamespace; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.PortNumber; |
| 25 | import org.onosproject.net.behaviour.NextGroup; |
| 26 | import org.onosproject.net.behaviour.Pipeliner; |
| 27 | import org.onosproject.net.behaviour.PipelinerContext; |
| 28 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 29 | import org.onosproject.net.flow.FlowRule; |
| 30 | import org.onosproject.net.flow.FlowRuleOperations; |
| 31 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 32 | import org.onosproject.net.flow.FlowRuleService; |
| 33 | import org.onosproject.net.flow.instructions.Instruction; |
| 34 | import org.onosproject.net.flow.instructions.Instructions; |
| 35 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 36 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 37 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 38 | import org.onosproject.net.flowobjective.NextObjective; |
| 39 | import org.onosproject.net.flowobjective.Objective; |
| 40 | import org.onosproject.net.flowobjective.ObjectiveError; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 41 | import org.onosproject.net.group.Group; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 42 | import org.onosproject.net.group.GroupDescription; |
| 43 | import org.onosproject.net.group.GroupEvent; |
| 44 | import org.onosproject.net.group.GroupListener; |
| 45 | import org.onosproject.net.group.GroupService; |
| 46 | import org.onosproject.store.serializers.KryoNamespaces; |
| 47 | import org.slf4j.Logger; |
| 48 | |
| 49 | import java.util.Collection; |
| 50 | import java.util.List; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 51 | import java.util.Map; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 52 | import java.util.concurrent.CompletableFuture; |
| 53 | import java.util.concurrent.ExecutionException; |
| 54 | import java.util.concurrent.TimeUnit; |
| 55 | import java.util.concurrent.TimeoutException; |
| 56 | import java.util.concurrent.atomic.AtomicInteger; |
| 57 | import java.util.function.Consumer; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 58 | import java.util.stream.Collectors; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 59 | |
| 60 | import static org.slf4j.LoggerFactory.getLogger; |
| 61 | |
| 62 | /** |
| 63 | * Pipeliner for fabric pipeline. |
| 64 | */ |
| 65 | public class FabricPipeliner extends AbstractHandlerBehaviour implements Pipeliner { |
| 66 | private static final Logger log = getLogger(FabricPipeliner.class); |
| 67 | |
| 68 | protected static final KryoNamespace KRYO = new KryoNamespace.Builder() |
| 69 | .register(KryoNamespaces.API) |
| 70 | .register(FabricNextGroup.class) |
| 71 | .build("FabricPipeliner"); |
| 72 | |
| 73 | // TODO: make this configurable |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 74 | private static final long DEFAULT_INSTALLATION_TIME_OUT = 40; |
| 75 | private static final Map<Objective.Operation, GroupEvent.Type> OBJ_OP_TO_GRP_EVENT_TYPE = |
| 76 | ImmutableMap.<Objective.Operation, GroupEvent.Type>builder() |
| 77 | .put(Objective.Operation.ADD, GroupEvent.Type.GROUP_ADDED) |
| 78 | .put(Objective.Operation.ADD_TO_EXISTING, GroupEvent.Type.GROUP_UPDATED) |
| 79 | .put(Objective.Operation.REMOVE, GroupEvent.Type.GROUP_REMOVED) |
| 80 | .put(Objective.Operation.REMOVE_FROM_EXISTING, GroupEvent.Type.GROUP_UPDATED) |
| 81 | .build(); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 82 | |
| 83 | protected DeviceId deviceId; |
| 84 | protected FlowRuleService flowRuleService; |
| 85 | protected GroupService groupService; |
| 86 | protected FlowObjectiveStore flowObjectiveStore; |
| 87 | protected FabricFilteringPipeliner pipelinerFilter; |
| 88 | protected FabricForwardingPipeliner pipelinerForward; |
| 89 | protected FabricNextPipeliner pipelinerNext; |
| 90 | |
| 91 | |
| 92 | @Override |
| 93 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 94 | this.deviceId = deviceId; |
| 95 | this.flowRuleService = context.directory().get(FlowRuleService.class); |
| 96 | this.groupService = context.directory().get(GroupService.class); |
| 97 | this.flowObjectiveStore = context.directory().get(FlowObjectiveStore.class); |
| 98 | this.pipelinerFilter = new FabricFilteringPipeliner(deviceId); |
| 99 | this.pipelinerForward = new FabricForwardingPipeliner(deviceId); |
| 100 | this.pipelinerNext = new FabricNextPipeliner(deviceId); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void filter(FilteringObjective filterObjective) { |
| 105 | PipelinerTranslationResult result = pipelinerFilter.filter(filterObjective); |
| 106 | if (result.error().isPresent()) { |
| 107 | fail(filterObjective, result.error().get()); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | applyTranslationResult(filterObjective, result, success -> { |
| 112 | if (success) { |
| 113 | success(filterObjective); |
| 114 | } else { |
| 115 | fail(filterObjective, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 116 | } |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void forward(ForwardingObjective forwardObjective) { |
| 122 | PipelinerTranslationResult result = pipelinerForward.forward(forwardObjective); |
| 123 | if (result.error().isPresent()) { |
| 124 | fail(forwardObjective, result.error().get()); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | applyTranslationResult(forwardObjective, result, success -> { |
| 129 | if (success) { |
| 130 | success(forwardObjective); |
| 131 | } else { |
| 132 | fail(forwardObjective, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 133 | } |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void next(NextObjective nextObjective) { |
| 139 | PipelinerTranslationResult result = pipelinerNext.next(nextObjective); |
| 140 | |
| 141 | if (result.error().isPresent()) { |
| 142 | fail(nextObjective, result.error().get()); |
| 143 | return; |
| 144 | } |
| 145 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 146 | if (nextObjective.op() == Objective.Operation.VERIFY) { |
| 147 | // TODO: support VERIFY operation |
| 148 | log.debug("Currently we don't support VERIFY operation, return success directly to the context"); |
| 149 | success(nextObjective); |
| 150 | return; |
| 151 | } |
| 152 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 153 | applyTranslationResult(nextObjective, result, success -> { |
| 154 | if (!success) { |
| 155 | fail(nextObjective, ObjectiveError.GROUPINSTALLATIONFAILED); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | // Success, put next group to objective store |
| 160 | List<PortNumber> portNumbers = Lists.newArrayList(); |
| 161 | nextObjective.next().forEach(treatment -> { |
| 162 | Instructions.OutputInstruction outputInst = treatment.allInstructions() |
| 163 | .stream() |
| 164 | .filter(inst -> inst.type() == Instruction.Type.OUTPUT) |
| 165 | .map(inst -> (Instructions.OutputInstruction) inst) |
| 166 | .findFirst() |
| 167 | .orElse(null); |
| 168 | |
| 169 | if (outputInst != null) { |
| 170 | portNumbers.add(outputInst.port()); |
| 171 | } |
| 172 | }); |
| 173 | FabricNextGroup nextGroup = new FabricNextGroup(nextObjective.type(), |
| 174 | portNumbers); |
| 175 | flowObjectiveStore.putNextGroup(nextObjective.id(), nextGroup); |
| 176 | success(nextObjective); |
| 177 | }); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public List<String> getNextMappings(NextGroup nextGroup) { |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 182 | FabricNextGroup fabricNextGroup = KRYO.deserialize(nextGroup.data()); |
| 183 | NextObjective.Type type = fabricNextGroup.type(); |
| 184 | Collection<PortNumber> outputPorts = fabricNextGroup.outputPorts(); |
| 185 | |
| 186 | return outputPorts.stream() |
| 187 | .map(port -> String.format("%s -> %s", type, port)) |
| 188 | .collect(Collectors.toList()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | private void applyTranslationResult(Objective objective, |
| 192 | PipelinerTranslationResult result, |
| 193 | Consumer<Boolean> callback) { |
| 194 | Collection<GroupDescription> groups = result.groups(); |
| 195 | Collection<FlowRule> flowRules = result.flowRules(); |
| 196 | CompletableFuture.supplyAsync(() -> installGroups(objective, groups)) |
| 197 | .thenApplyAsync(groupSuccess -> groupSuccess && installFlows(objective, flowRules)) |
| 198 | .thenAcceptAsync(callback) |
| 199 | .exceptionally((ex) -> { |
| 200 | log.warn("Got unexpected exception while applying translation result {}", |
| 201 | result); |
| 202 | fail(objective, ObjectiveError.UNKNOWN); |
| 203 | return null; |
| 204 | }); |
| 205 | } |
| 206 | |
| 207 | private boolean installFlows(Objective objective, Collection<FlowRule> flowRules) { |
| 208 | if (flowRules.isEmpty()) { |
| 209 | return true; |
| 210 | } |
| 211 | CompletableFuture<Boolean> flowInstallFuture = new CompletableFuture<>(); |
| 212 | FlowRuleOperationsContext ctx = new FlowRuleOperationsContext() { |
| 213 | @Override |
| 214 | public void onSuccess(FlowRuleOperations ops) { |
| 215 | flowInstallFuture.complete(true); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public void onError(FlowRuleOperations ops) { |
| 220 | log.warn("Failed to install flow rules: {}", flowRules); |
| 221 | flowInstallFuture.complete(false); |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | FlowRuleOperations ops = buildFlowRuleOps(objective, flowRules, ctx); |
| 226 | flowRuleService.apply(ops); |
| 227 | |
| 228 | try { |
| 229 | return flowInstallFuture.get(DEFAULT_INSTALLATION_TIME_OUT, TimeUnit.SECONDS); |
| 230 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 231 | log.warn("Got exception while installing flows:{}", e.getMessage()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | private boolean installGroups(Objective objective, Collection<GroupDescription> groups) { |
| 237 | if (groups.isEmpty()) { |
| 238 | return true; |
| 239 | } |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 240 | Collection<Integer> groupIds = groups.stream() |
| 241 | .map(GroupDescription::givenGroupId) |
| 242 | .collect(Collectors.toSet()); |
| 243 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 244 | int numGroupsToBeInstalled = groups.size(); |
| 245 | CompletableFuture<Boolean> groupInstallFuture = new CompletableFuture<>(); |
| 246 | AtomicInteger numGroupsInstalled = new AtomicInteger(0); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 247 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 248 | GroupListener listener = new GroupListener() { |
| 249 | @Override |
| 250 | public void event(GroupEvent event) { |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 251 | log.debug("Receive group event for group {}", event.subject()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 252 | int currentNumGroupInstalled = numGroupsInstalled.incrementAndGet(); |
| 253 | if (currentNumGroupInstalled == numGroupsToBeInstalled) { |
| 254 | // install completed |
| 255 | groupService.removeListener(this); |
| 256 | groupInstallFuture.complete(true); |
| 257 | } |
| 258 | } |
| 259 | @Override |
| 260 | public boolean isRelevant(GroupEvent event) { |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 261 | Group group = event.subject(); |
| 262 | return groupIds.contains(group.givenGroupId()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 263 | } |
| 264 | }; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 265 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 266 | groupService.addListener(listener); |
| 267 | |
| 268 | switch (objective.op()) { |
| 269 | case ADD: |
| 270 | groups.forEach(groupService::addGroup); |
| 271 | break; |
| 272 | case REMOVE: |
| 273 | groups.forEach(group -> groupService.removeGroup(deviceId, group.appCookie(), objective.appId())); |
| 274 | break; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 275 | case ADD_TO_EXISTING: |
| 276 | groups.forEach(group -> { |
| 277 | groupService.addBucketsToGroup(deviceId, group.appCookie(), |
| 278 | group.buckets(), |
| 279 | group.appCookie(), |
| 280 | group.appId()); |
| 281 | }); |
| 282 | break; |
| 283 | case REMOVE_FROM_EXISTING: |
| 284 | groups.forEach(group -> { |
| 285 | groupService.removeBucketsFromGroup(deviceId, group.appCookie(), |
| 286 | group.buckets(), |
| 287 | group.appCookie(), |
| 288 | group.appId()); |
| 289 | }); |
| 290 | break; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 291 | default: |
| 292 | log.warn("Unsupported objective operation {}", objective.op()); |
| 293 | groupService.removeListener(listener); |
| 294 | } |
| 295 | try { |
| 296 | return groupInstallFuture.get(DEFAULT_INSTALLATION_TIME_OUT, TimeUnit.SECONDS); |
| 297 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 298 | groupService.removeListener(listener); |
| 299 | log.warn("Got exception while installing groups: {}", e.getMessage()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 300 | return false; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | static void fail(Objective objective, ObjectiveError error) { |
| 305 | objective.context().ifPresent(ctx -> ctx.onError(objective, error)); |
| 306 | } |
| 307 | |
| 308 | static void success(Objective objective) { |
| 309 | objective.context().ifPresent(ctx -> ctx.onSuccess(objective)); |
| 310 | } |
| 311 | |
| 312 | static FlowRuleOperations buildFlowRuleOps(Objective objective, Collection<FlowRule> flowRules, |
| 313 | FlowRuleOperationsContext ctx) { |
| 314 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 315 | switch (objective.op()) { |
| 316 | case ADD: |
| 317 | flowRules.forEach(ops::add); |
| 318 | break; |
| 319 | case REMOVE: |
| 320 | flowRules.forEach(ops::remove); |
| 321 | break; |
| 322 | default: |
| 323 | log.warn("Unsupported op {} for {}", objective); |
| 324 | fail(objective, ObjectiveError.BADPARAMS); |
| 325 | return null; |
| 326 | } |
| 327 | return ops.build(ctx); |
| 328 | } |
| 329 | |
| 330 | class FabricNextGroup implements NextGroup { |
| 331 | private NextObjective.Type type; |
| 332 | private Collection<PortNumber> outputPorts; |
| 333 | |
| 334 | public FabricNextGroup(NextObjective.Type type, Collection<PortNumber> outputPorts) { |
| 335 | this.type = type; |
| 336 | this.outputPorts = ImmutableList.copyOf(outputPorts); |
| 337 | } |
| 338 | |
| 339 | public NextObjective.Type type() { |
| 340 | return type; |
| 341 | } |
| 342 | |
| 343 | public Collection<PortNumber> outputPorts() { |
| 344 | return outputPorts; |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public byte[] data() { |
| 349 | return KRYO.serialize(this); |
| 350 | } |
| 351 | } |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame^] | 352 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 353 | } |