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 | |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.base.Objects; |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 21 | import com.google.common.cache.Cache; |
| 22 | import com.google.common.cache.CacheBuilder; |
| 23 | import com.google.common.cache.RemovalCause; |
| 24 | import com.google.common.cache.RemovalListener; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableList; |
| 26 | import com.google.common.collect.Lists; |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 27 | import com.google.common.collect.Sets; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 28 | import org.onlab.util.KryoNamespace; |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 29 | import org.onlab.util.Tools; |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 30 | import org.onosproject.core.GroupId; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.behaviour.NextGroup; |
| 34 | import org.onosproject.net.behaviour.Pipeliner; |
| 35 | import org.onosproject.net.behaviour.PipelinerContext; |
| 36 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 37 | import org.onosproject.net.driver.Driver; |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 38 | import org.onosproject.net.flow.FlowId; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flow.FlowRule; |
| 40 | import org.onosproject.net.flow.FlowRuleOperations; |
| 41 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 42 | import org.onosproject.net.flow.FlowRuleService; |
| 43 | import org.onosproject.net.flow.instructions.Instruction; |
| 44 | import org.onosproject.net.flow.instructions.Instructions; |
| 45 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 46 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 47 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 48 | import org.onosproject.net.flowobjective.NextObjective; |
| 49 | import org.onosproject.net.flowobjective.Objective; |
| 50 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 51 | import org.onosproject.net.group.GroupDescription; |
| 52 | import org.onosproject.net.group.GroupEvent; |
| 53 | import org.onosproject.net.group.GroupListener; |
| 54 | import org.onosproject.net.group.GroupService; |
| 55 | import org.onosproject.store.serializers.KryoNamespaces; |
| 56 | import org.slf4j.Logger; |
| 57 | |
| 58 | import java.util.Collection; |
| 59 | import java.util.List; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 60 | import java.util.Map; |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 61 | import java.util.Set; |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 62 | import java.util.concurrent.CompletableFuture; |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 63 | import java.util.concurrent.ConcurrentHashMap; |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 64 | import java.util.concurrent.ExecutorService; |
| 65 | import java.util.concurrent.Executors; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 66 | import java.util.concurrent.TimeUnit; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 67 | import java.util.function.Consumer; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 68 | import java.util.stream.Collectors; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 69 | |
| 70 | import static org.slf4j.LoggerFactory.getLogger; |
| 71 | |
| 72 | /** |
| 73 | * Pipeliner for fabric pipeline. |
| 74 | */ |
| 75 | public class FabricPipeliner extends AbstractHandlerBehaviour implements Pipeliner { |
| 76 | private static final Logger log = getLogger(FabricPipeliner.class); |
| 77 | |
| 78 | protected static final KryoNamespace KRYO = new KryoNamespace.Builder() |
| 79 | .register(KryoNamespaces.API) |
| 80 | .register(FabricNextGroup.class) |
| 81 | .build("FabricPipeliner"); |
| 82 | |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 83 | private static final Set<GroupEvent.Type> GROUP_FAILED_TYPES = |
| 84 | Sets.newHashSet(GroupEvent.Type.GROUP_ADD_FAILED, |
| 85 | GroupEvent.Type.GROUP_REMOVE_FAILED, |
| 86 | GroupEvent.Type.GROUP_UPDATE_FAILED); |
| 87 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 88 | // TODO: make this configurable |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 89 | private static final long DEFAULT_INSTALLATION_TIME_OUT = 40; |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 90 | private static final int NUM_CALLBACK_THREAD = 2; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 91 | |
| 92 | protected DeviceId deviceId; |
| 93 | protected FlowRuleService flowRuleService; |
| 94 | protected GroupService groupService; |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 95 | protected GroupListener groupListener = new InternalGroupListener(); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 96 | protected FlowObjectiveStore flowObjectiveStore; |
| 97 | protected FabricFilteringPipeliner pipelinerFilter; |
| 98 | protected FabricForwardingPipeliner pipelinerForward; |
| 99 | protected FabricNextPipeliner pipelinerNext; |
| 100 | |
Charles Chan | 68e5973 | 2018-08-21 15:56:07 -0700 | [diff] [blame] | 101 | private Map<PendingFlowKey, PendingInstallObjective> pendingInstallObjectiveFlows = new ConcurrentHashMap<>(); |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 102 | private Map<PendingGroupKey, PendingInstallObjective> pendingInstallObjectiveGroups = new ConcurrentHashMap<>(); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 103 | private Cache<Objective, PendingInstallObjective> pendingInstallObjectives = CacheBuilder.newBuilder() |
| 104 | .expireAfterWrite(DEFAULT_INSTALLATION_TIME_OUT, TimeUnit.SECONDS) |
| 105 | .removalListener((RemovalListener<Objective, PendingInstallObjective>) removalNotification -> { |
| 106 | RemovalCause cause = removalNotification.getCause(); |
| 107 | PendingInstallObjective pio = removalNotification.getValue(); |
| 108 | if (cause == RemovalCause.EXPIRED && pio != null) { |
Charles Chan | 68e5973 | 2018-08-21 15:56:07 -0700 | [diff] [blame] | 109 | pio.failed(pio.objective, ObjectiveError.INSTALLATIONTIMEOUT); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 110 | } |
| 111 | }) |
| 112 | .build(); |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 113 | private static ExecutorService flowObjCallbackExecutor = |
| 114 | Executors.newFixedThreadPool(NUM_CALLBACK_THREAD, Tools.groupedThreads("fabric-pipeliner", "cb-", log)); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 115 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 116 | |
| 117 | @Override |
| 118 | public void init(DeviceId deviceId, PipelinerContext context) { |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 119 | Driver driver = handler().driver(); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 120 | this.deviceId = deviceId; |
| 121 | this.flowRuleService = context.directory().get(FlowRuleService.class); |
| 122 | this.groupService = context.directory().get(GroupService.class); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 123 | this.groupService.addListener(groupListener); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 124 | this.flowObjectiveStore = context.directory().get(FlowObjectiveStore.class); |
| 125 | this.pipelinerFilter = new FabricFilteringPipeliner(deviceId); |
| 126 | this.pipelinerForward = new FabricForwardingPipeliner(deviceId); |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 127 | this.pipelinerNext = new FabricNextPipeliner(deviceId, driver); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void filter(FilteringObjective filterObjective) { |
| 132 | PipelinerTranslationResult result = pipelinerFilter.filter(filterObjective); |
| 133 | if (result.error().isPresent()) { |
| 134 | fail(filterObjective, result.error().get()); |
| 135 | return; |
| 136 | } |
| 137 | |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 138 | applyTranslationResult(filterObjective, result, error -> { |
| 139 | if (error == null) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 140 | success(filterObjective); |
| 141 | } else { |
| 142 | fail(filterObjective, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 143 | } |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void forward(ForwardingObjective forwardObjective) { |
| 149 | PipelinerTranslationResult result = pipelinerForward.forward(forwardObjective); |
| 150 | if (result.error().isPresent()) { |
| 151 | fail(forwardObjective, result.error().get()); |
| 152 | return; |
| 153 | } |
| 154 | |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 155 | applyTranslationResult(forwardObjective, result, error -> { |
| 156 | if (error == null) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 157 | success(forwardObjective); |
| 158 | } else { |
| 159 | fail(forwardObjective, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 160 | } |
| 161 | }); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void next(NextObjective nextObjective) { |
| 166 | PipelinerTranslationResult result = pipelinerNext.next(nextObjective); |
| 167 | |
| 168 | if (result.error().isPresent()) { |
| 169 | fail(nextObjective, result.error().get()); |
| 170 | return; |
| 171 | } |
| 172 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 173 | if (nextObjective.op() == Objective.Operation.VERIFY) { |
| 174 | // TODO: support VERIFY operation |
| 175 | log.debug("Currently we don't support VERIFY operation, return success directly to the context"); |
| 176 | success(nextObjective); |
| 177 | return; |
| 178 | } |
| 179 | |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 180 | applyTranslationResult(nextObjective, result, error -> { |
| 181 | if (error != null) { |
| 182 | fail(nextObjective, error); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 183 | return; |
| 184 | } |
| 185 | |
| 186 | // Success, put next group to objective store |
| 187 | List<PortNumber> portNumbers = Lists.newArrayList(); |
| 188 | nextObjective.next().forEach(treatment -> { |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 189 | treatment.allInstructions() |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 190 | .stream() |
| 191 | .filter(inst -> inst.type() == Instruction.Type.OUTPUT) |
| 192 | .map(inst -> (Instructions.OutputInstruction) inst) |
| 193 | .findFirst() |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 194 | .map(Instructions.OutputInstruction::port) |
| 195 | .ifPresent(portNumbers::add); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 196 | }); |
| 197 | FabricNextGroup nextGroup = new FabricNextGroup(nextObjective.type(), |
| 198 | portNumbers); |
| 199 | flowObjectiveStore.putNextGroup(nextObjective.id(), nextGroup); |
| 200 | success(nextObjective); |
| 201 | }); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public List<String> getNextMappings(NextGroup nextGroup) { |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 206 | FabricNextGroup fabricNextGroup = KRYO.deserialize(nextGroup.data()); |
| 207 | NextObjective.Type type = fabricNextGroup.type(); |
| 208 | Collection<PortNumber> outputPorts = fabricNextGroup.outputPorts(); |
| 209 | |
| 210 | return outputPorts.stream() |
| 211 | .map(port -> String.format("%s -> %s", type, port)) |
| 212 | .collect(Collectors.toList()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | private void applyTranslationResult(Objective objective, |
| 216 | PipelinerTranslationResult result, |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 217 | Consumer<ObjectiveError> callback) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 218 | Collection<GroupDescription> groups = result.groups(); |
| 219 | Collection<FlowRule> flowRules = result.flowRules(); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 220 | |
| 221 | Set<FlowId> flowIds = flowRules.stream().map(FlowRule::id).collect(Collectors.toSet()); |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 222 | Set<PendingGroupKey> pendingGroupKeys = groups.stream().map(GroupDescription::givenGroupId) |
| 223 | .map(GroupId::new) |
| 224 | .map(gid -> new PendingGroupKey(gid, objective.op())) |
| 225 | .collect(Collectors.toSet()); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 226 | |
| 227 | PendingInstallObjective pio = |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 228 | new PendingInstallObjective(objective, flowIds, pendingGroupKeys, callback); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 229 | |
| 230 | flowIds.forEach(flowId -> { |
Charles Chan | 68e5973 | 2018-08-21 15:56:07 -0700 | [diff] [blame] | 231 | PendingFlowKey pfk = new PendingFlowKey(flowId, objective.id()); |
| 232 | pendingInstallObjectiveFlows.put(pfk, pio); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 233 | }); |
| 234 | |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 235 | pendingGroupKeys.forEach(pendingGroupKey -> { |
| 236 | pendingInstallObjectiveGroups.put(pendingGroupKey, pio); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 237 | }); |
| 238 | |
| 239 | pendingInstallObjectives.put(objective, pio); |
| 240 | installGroups(objective, groups); |
| 241 | installFlows(objective, flowRules); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 244 | private void installFlows(Objective objective, Collection<FlowRule> flowRules) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 245 | if (flowRules.isEmpty()) { |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 246 | return; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 247 | } |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 248 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 249 | FlowRuleOperationsContext ctx = new FlowRuleOperationsContext() { |
| 250 | @Override |
| 251 | public void onSuccess(FlowRuleOperations ops) { |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 252 | ops.stages().forEach(stage -> { |
| 253 | stage.forEach(op -> { |
| 254 | FlowId flowId = op.rule().id(); |
Charles Chan | 68e5973 | 2018-08-21 15:56:07 -0700 | [diff] [blame] | 255 | PendingFlowKey pfk = new PendingFlowKey(flowId, objective.id()); |
| 256 | PendingInstallObjective pio = pendingInstallObjectiveFlows.remove(pfk); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 257 | |
| 258 | if (pio != null) { |
| 259 | pio.flowInstalled(flowId); |
| 260 | } |
| 261 | }); |
| 262 | }); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void onError(FlowRuleOperations ops) { |
| 267 | log.warn("Failed to install flow rules: {}", flowRules); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 268 | PendingInstallObjective pio = pendingInstallObjectives.getIfPresent(objective); |
| 269 | if (pio != null) { |
Charles Chan | 68e5973 | 2018-08-21 15:56:07 -0700 | [diff] [blame] | 270 | pio.failed(objective, ObjectiveError.FLOWINSTALLATIONFAILED); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 271 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 272 | } |
| 273 | }; |
| 274 | |
| 275 | FlowRuleOperations ops = buildFlowRuleOps(objective, flowRules, ctx); |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 276 | if (ops != null) { |
| 277 | flowRuleService.apply(ops); |
| 278 | } else { |
| 279 | // remove pendings |
Charles Chan | 68e5973 | 2018-08-21 15:56:07 -0700 | [diff] [blame] | 280 | flowRules.forEach(flowRule -> { |
| 281 | PendingFlowKey pfk = new PendingFlowKey(flowRule.id(), objective.id()); |
| 282 | pendingInstallObjectiveFlows.remove(pfk); |
| 283 | }); |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 284 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 287 | private void installGroups(Objective objective, Collection<GroupDescription> groups) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 288 | if (groups.isEmpty()) { |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 289 | return; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 290 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 291 | |
| 292 | switch (objective.op()) { |
| 293 | case ADD: |
| 294 | groups.forEach(groupService::addGroup); |
| 295 | break; |
| 296 | case REMOVE: |
| 297 | groups.forEach(group -> groupService.removeGroup(deviceId, group.appCookie(), objective.appId())); |
| 298 | break; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 299 | case ADD_TO_EXISTING: |
| 300 | groups.forEach(group -> { |
| 301 | groupService.addBucketsToGroup(deviceId, group.appCookie(), |
| 302 | group.buckets(), |
| 303 | group.appCookie(), |
| 304 | group.appId()); |
| 305 | }); |
| 306 | break; |
| 307 | case REMOVE_FROM_EXISTING: |
| 308 | groups.forEach(group -> { |
| 309 | groupService.removeBucketsFromGroup(deviceId, group.appCookie(), |
| 310 | group.buckets(), |
| 311 | group.appCookie(), |
| 312 | group.appId()); |
| 313 | }); |
| 314 | break; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 315 | default: |
| 316 | log.warn("Unsupported objective operation {}", objective.op()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | static void fail(Objective objective, ObjectiveError error) { |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 321 | CompletableFuture.runAsync(() -> { |
| 322 | objective.context().ifPresent(ctx -> ctx.onError(objective, error)); |
| 323 | }, flowObjCallbackExecutor); |
| 324 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static void success(Objective objective) { |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 328 | CompletableFuture.runAsync(() -> { |
| 329 | objective.context().ifPresent(ctx -> ctx.onSuccess(objective)); |
| 330 | }, flowObjCallbackExecutor); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static FlowRuleOperations buildFlowRuleOps(Objective objective, Collection<FlowRule> flowRules, |
| 334 | FlowRuleOperationsContext ctx) { |
| 335 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 336 | switch (objective.op()) { |
| 337 | case ADD: |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 338 | case ADD_TO_EXISTING: // For egress VLAN |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 339 | flowRules.forEach(ops::add); |
| 340 | break; |
| 341 | case REMOVE: |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 342 | case REMOVE_FROM_EXISTING: // For egress VLAN |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 343 | flowRules.forEach(ops::remove); |
| 344 | break; |
| 345 | default: |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 346 | log.warn("Unsupported op {} for {}", objective.op(), objective); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 347 | fail(objective, ObjectiveError.BADPARAMS); |
| 348 | return null; |
| 349 | } |
| 350 | return ops.build(ctx); |
| 351 | } |
| 352 | |
| 353 | class FabricNextGroup implements NextGroup { |
| 354 | private NextObjective.Type type; |
| 355 | private Collection<PortNumber> outputPorts; |
| 356 | |
| 357 | public FabricNextGroup(NextObjective.Type type, Collection<PortNumber> outputPorts) { |
| 358 | this.type = type; |
| 359 | this.outputPorts = ImmutableList.copyOf(outputPorts); |
| 360 | } |
| 361 | |
| 362 | public NextObjective.Type type() { |
| 363 | return type; |
| 364 | } |
| 365 | |
| 366 | public Collection<PortNumber> outputPorts() { |
| 367 | return outputPorts; |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public byte[] data() { |
| 372 | return KRYO.serialize(this); |
| 373 | } |
| 374 | } |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 375 | |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 376 | class InternalGroupListener implements GroupListener { |
| 377 | @Override |
| 378 | public void event(GroupEvent event) { |
| 379 | GroupId groupId = event.subject().id(); |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 380 | PendingGroupKey pendingGroupKey = new PendingGroupKey(groupId, event.type()); |
| 381 | PendingInstallObjective pio = pendingInstallObjectiveGroups.remove(pendingGroupKey); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 382 | if (GROUP_FAILED_TYPES.contains(event.type())) { |
Charles Chan | 68e5973 | 2018-08-21 15:56:07 -0700 | [diff] [blame] | 383 | pio.failed(pio.objective, ObjectiveError.GROUPINSTALLATIONFAILED); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 384 | } |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 385 | pio.groupInstalled(pendingGroupKey); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public boolean isRelevant(GroupEvent event) { |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 390 | PendingGroupKey pendingGroupKey = new PendingGroupKey(event.subject().id(), event.type()); |
| 391 | return pendingInstallObjectiveGroups.containsKey(pendingGroupKey); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | class PendingInstallObjective { |
| 396 | Objective objective; |
| 397 | Collection<FlowId> flowIds; |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 398 | Collection<PendingGroupKey> pendingGroupKeys; |
| 399 | Consumer<ObjectiveError> callback; |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 400 | |
| 401 | public PendingInstallObjective(Objective objective, Collection<FlowId> flowIds, |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 402 | Collection<PendingGroupKey> pendingGroupKeys, |
| 403 | Consumer<ObjectiveError> callback) { |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 404 | this.objective = objective; |
| 405 | this.flowIds = flowIds; |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 406 | this.pendingGroupKeys = pendingGroupKeys; |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 407 | this.callback = callback; |
| 408 | } |
| 409 | |
| 410 | void flowInstalled(FlowId flowId) { |
Charles Chan | 68e5973 | 2018-08-21 15:56:07 -0700 | [diff] [blame] | 411 | synchronized (this) { |
| 412 | flowIds.remove(flowId); |
| 413 | checkIfFinished(); |
| 414 | } |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 415 | } |
| 416 | |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 417 | void groupInstalled(PendingGroupKey pendingGroupKey) { |
Charles Chan | 68e5973 | 2018-08-21 15:56:07 -0700 | [diff] [blame] | 418 | synchronized (this) { |
| 419 | pendingGroupKeys.remove(pendingGroupKey); |
| 420 | checkIfFinished(); |
| 421 | } |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | private void checkIfFinished() { |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 425 | if (flowIds.isEmpty() && pendingGroupKeys.isEmpty()) { |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 426 | pendingInstallObjectives.invalidate(objective); |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 427 | callback.accept(null); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Charles Chan | 68e5973 | 2018-08-21 15:56:07 -0700 | [diff] [blame] | 431 | void failed(Objective obj, ObjectiveError error) { |
| 432 | flowIds.forEach(flowId -> { |
| 433 | PendingFlowKey pfk = new PendingFlowKey(flowId, obj.id()); |
| 434 | pendingInstallObjectiveFlows.remove(pfk); |
| 435 | }); |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 436 | pendingGroupKeys.forEach(pendingInstallObjectiveGroups::remove); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 437 | pendingInstallObjectives.invalidate(objective); |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 438 | callback.accept(error); |
| 439 | } |
Charles Chan | 68e5973 | 2018-08-21 15:56:07 -0700 | [diff] [blame] | 440 | |
| 441 | @Override |
| 442 | public boolean equals(Object o) { |
| 443 | if (this == o) { |
| 444 | return true; |
| 445 | } |
| 446 | if (o == null || getClass() != o.getClass()) { |
| 447 | return false; |
| 448 | } |
| 449 | PendingInstallObjective pio = (PendingInstallObjective) o; |
| 450 | return Objects.equal(objective, pio.objective) && |
| 451 | Objects.equal(flowIds, pio.flowIds) && |
| 452 | Objects.equal(pendingGroupKeys, pio.pendingGroupKeys) && |
| 453 | Objects.equal(callback, pio.callback); |
| 454 | } |
| 455 | |
| 456 | @Override |
| 457 | public int hashCode() { |
| 458 | return Objects.hashCode(objective, flowIds, pendingGroupKeys, callback); |
| 459 | } |
| 460 | |
| 461 | @Override |
| 462 | public String toString() { |
| 463 | return MoreObjects.toStringHelper(this) |
| 464 | .add("obj", objective) |
| 465 | .add("flowIds", flowIds) |
| 466 | .add("pendingGroupKeys", pendingGroupKeys) |
| 467 | .add("callback", callback) |
| 468 | .toString(); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | class PendingFlowKey { |
| 473 | private FlowId flowId; |
| 474 | private int objId; |
| 475 | |
| 476 | PendingFlowKey(FlowId flowId, int objId) { |
| 477 | this.flowId = flowId; |
| 478 | this.objId = objId; |
| 479 | } |
| 480 | |
| 481 | @Override |
| 482 | public boolean equals(Object o) { |
| 483 | if (this == o) { |
| 484 | return true; |
| 485 | } |
| 486 | if (o == null || getClass() != o.getClass()) { |
| 487 | return false; |
| 488 | } |
| 489 | PendingFlowKey pendingFlowKey = (PendingFlowKey) o; |
| 490 | return Objects.equal(flowId, pendingFlowKey.flowId) && |
| 491 | objId == pendingFlowKey.objId; |
| 492 | } |
| 493 | |
| 494 | @Override |
| 495 | public int hashCode() { |
| 496 | return Objects.hashCode(flowId, objId); |
| 497 | } |
| 498 | |
| 499 | @Override |
| 500 | public String toString() { |
| 501 | return MoreObjects.toStringHelper(this) |
| 502 | .add("flowId", flowId) |
| 503 | .add("objId", objId) |
| 504 | .toString(); |
| 505 | } |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | class PendingGroupKey { |
| 509 | private GroupId groupId; |
| 510 | private GroupEvent.Type expectedEventType; |
| 511 | |
| 512 | PendingGroupKey(GroupId groupId, GroupEvent.Type expectedEventType) { |
| 513 | this.groupId = groupId; |
| 514 | this.expectedEventType = expectedEventType; |
| 515 | } |
| 516 | |
| 517 | PendingGroupKey(GroupId groupId, NextObjective.Operation objOp) { |
| 518 | this.groupId = groupId; |
| 519 | |
| 520 | switch (objOp) { |
| 521 | case ADD: |
| 522 | expectedEventType = GroupEvent.Type.GROUP_ADDED; |
| 523 | break; |
| 524 | case REMOVE: |
| 525 | expectedEventType = GroupEvent.Type.GROUP_REMOVED; |
| 526 | break; |
| 527 | case MODIFY: |
| 528 | case ADD_TO_EXISTING: |
| 529 | case REMOVE_FROM_EXISTING: |
| 530 | expectedEventType = GroupEvent.Type.GROUP_UPDATED; |
| 531 | break; |
| 532 | default: |
| 533 | expectedEventType = null; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public boolean equals(Object o) { |
| 539 | if (this == o) { |
| 540 | return true; |
| 541 | } |
| 542 | if (o == null || getClass() != o.getClass()) { |
| 543 | return false; |
| 544 | } |
| 545 | PendingGroupKey pendingGroupKey = (PendingGroupKey) o; |
| 546 | return Objects.equal(groupId, pendingGroupKey.groupId) && |
| 547 | expectedEventType == pendingGroupKey.expectedEventType; |
| 548 | } |
| 549 | |
| 550 | @Override |
| 551 | public int hashCode() { |
| 552 | return Objects.hashCode(groupId, expectedEventType); |
| 553 | } |
| 554 | |
| 555 | @Override |
| 556 | public String toString() { |
| 557 | return MoreObjects.toStringHelper(this) |
| 558 | .add("groupId", groupId) |
| 559 | .add("expectedEventType", expectedEventType) |
| 560 | .toString(); |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 561 | } |
| 562 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 563 | } |