Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.net.flowobjective.impl; |
| 18 | |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 19 | import com.google.common.cache.Cache; |
| 20 | import com.google.common.cache.CacheBuilder; |
Charles Chan | a724c513 | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 21 | import com.google.common.cache.RemovalListener; |
| 22 | import com.google.common.cache.RemovalListeners; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ArrayListMultimap; |
| 24 | import com.google.common.collect.ListMultimap; |
| 25 | import com.google.common.collect.Multimaps; |
Charles Chan | c09ad6d | 2018-04-04 16:31:23 -0700 | [diff] [blame] | 26 | import org.onlab.util.Tools; |
| 27 | import org.onlab.util.Tools.LogLevel; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 29 | import org.onosproject.net.flowobjective.FilteringObjQueueKey; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flowobjective.FilteringObjective; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 32 | import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate; |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 33 | import org.onosproject.net.flowobjective.ForwardingObjQueueKey; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 35 | import org.onosproject.net.flowobjective.NextObjQueueKey; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 36 | import org.onosproject.net.flowobjective.NextObjective; |
| 37 | import org.onosproject.net.flowobjective.Objective; |
| 38 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 39 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 40 | import org.onosproject.net.flowobjective.ObjectiveEvent; |
Charles Chan | a724c513 | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 41 | import org.onosproject.net.flowobjective.ObjectiveQueueKey; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 42 | import org.osgi.service.component.annotations.Activate; |
| 43 | import org.osgi.service.component.annotations.Component; |
| 44 | import org.osgi.service.component.annotations.Deactivate; |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 45 | import org.osgi.service.component.ComponentContext; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
| 49 | import java.util.List; |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 50 | import java.util.Map; |
Charles Chan | a724c513 | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 51 | import java.util.Objects; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 52 | import java.util.Optional; |
| 53 | import java.util.Set; |
Charles Chan | a724c513 | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 54 | import java.util.concurrent.ExecutorService; |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 55 | import java.util.concurrent.ScheduledExecutorService; |
| 56 | import java.util.concurrent.TimeUnit; |
Charles Chan | c51852e | 2019-01-07 19:59:10 -0800 | [diff] [blame] | 57 | import java.util.concurrent.atomic.AtomicBoolean; |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 58 | |
Charles Chan | a724c513 | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 59 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 60 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 61 | import static org.onlab.util.Tools.groupedThreads; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 62 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 63 | @Component(immediate = true, service = FlowObjectiveService.class) |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 64 | public class InOrderFlowObjectiveManager extends FlowObjectiveManager { |
| 65 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 66 | |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 67 | // TODO Make queue timeout configurable |
Charles Chan | b4f4fdb | 2018-12-21 13:55:29 -0800 | [diff] [blame] | 68 | static final int DEFAULT_OBJ_TIMEOUT = 15000; |
| 69 | int objTimeoutMs = DEFAULT_OBJ_TIMEOUT; |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 70 | |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 71 | private Cache<FilteringObjQueueKey, Objective> filtObjQueueHead; |
| 72 | private Cache<ForwardingObjQueueKey, Objective> fwdObjQueueHead; |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 73 | private Cache<NextObjQueueKey, Objective> nextObjQueueHead; |
| 74 | private ScheduledExecutorService cacheCleaner; |
Charles Chan | a724c513 | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 75 | private ExecutorService filtCacheEventExecutor; |
| 76 | private ExecutorService fwdCacheEventExecutor; |
| 77 | private ExecutorService nextCacheEventExecutor; |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 78 | |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 79 | private ListMultimap<FilteringObjQueueKey, Objective> filtObjQueue = |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 80 | Multimaps.synchronizedListMultimap(ArrayListMultimap.create()); |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 81 | private ListMultimap<ForwardingObjQueueKey, Objective> fwdObjQueue = |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 82 | Multimaps.synchronizedListMultimap(ArrayListMultimap.create()); |
| 83 | private ListMultimap<NextObjQueueKey, Objective> nextObjQueue = |
| 84 | Multimaps.synchronizedListMultimap(ArrayListMultimap.create()); |
| 85 | |
| 86 | final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate(); |
| 87 | |
| 88 | @Activate |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 89 | protected void activate(ComponentContext context) { |
| 90 | super.activate(context); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 91 | |
Charles Chan | a724c513 | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 92 | filtCacheEventExecutor = newSingleThreadExecutor(groupedThreads("onos/flowobj", "cache-event-filt", log)); |
| 93 | fwdCacheEventExecutor = newSingleThreadExecutor(groupedThreads("onos/flowobj", "cache-event-fwd", log)); |
| 94 | nextCacheEventExecutor = newSingleThreadExecutor(groupedThreads("onos/flowobj", "cache-event-next", log)); |
| 95 | |
| 96 | RemovalListener<ObjectiveQueueKey, Objective> removalListener = notification -> { |
| 97 | Objective obj = notification.getValue(); |
| 98 | switch (notification.getCause()) { |
| 99 | case EXPIRED: |
| 100 | case COLLECTED: |
| 101 | case SIZE: |
| 102 | obj.context().ifPresent(c -> c.onError(obj, ObjectiveError.INSTALLATIONTIMEOUT)); |
| 103 | break; |
| 104 | case EXPLICIT: // No action when the objective completes correctly |
| 105 | case REPLACED: // No action when a pending forward or next objective gets executed |
| 106 | default: |
| 107 | break; |
| 108 | } |
| 109 | }; |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 110 | filtObjQueueHead = CacheBuilder.newBuilder() |
Charles Chan | 1491b9b | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 111 | .expireAfterWrite(objTimeoutMs, TimeUnit.MILLISECONDS) |
Charles Chan | a724c513 | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 112 | .removalListener(RemovalListeners.asynchronous(removalListener, filtCacheEventExecutor)) |
| 113 | .build(); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 114 | fwdObjQueueHead = CacheBuilder.newBuilder() |
Charles Chan | 1491b9b | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 115 | .expireAfterWrite(objTimeoutMs, TimeUnit.MILLISECONDS) |
Charles Chan | a724c513 | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 116 | .removalListener(RemovalListeners.asynchronous(removalListener, fwdCacheEventExecutor)) |
| 117 | .build(); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 118 | nextObjQueueHead = CacheBuilder.newBuilder() |
Charles Chan | 1491b9b | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 119 | .expireAfterWrite(objTimeoutMs, TimeUnit.MILLISECONDS) |
Charles Chan | a724c513 | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 120 | .removalListener(RemovalListeners.asynchronous(removalListener, nextCacheEventExecutor)) |
| 121 | .build(); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 122 | |
| 123 | cacheCleaner = newSingleThreadScheduledExecutor(groupedThreads("onos/flowobj", "cache-cleaner", log)); |
| 124 | cacheCleaner.scheduleAtFixedRate(() -> { |
| 125 | filtObjQueueHead.cleanUp(); |
| 126 | fwdObjQueueHead.cleanUp(); |
| 127 | nextObjQueueHead.cleanUp(); |
Charles Chan | 1491b9b | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 128 | }, 0, objTimeoutMs, TimeUnit.MILLISECONDS); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 129 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 130 | // Replace store delegate to make sure pendingForward and pendingNext are resubmitted to |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 131 | // execute() |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 132 | flowObjectiveStore.unsetDelegate(super.delegate); |
| 133 | flowObjectiveStore.setDelegate(delegate); |
| 134 | } |
| 135 | |
| 136 | @Deactivate |
| 137 | protected void deactivate() { |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 138 | cacheCleaner.shutdown(); |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 139 | clearQueue(); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 140 | |
Charles Chan | a724c513 | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 141 | filtCacheEventExecutor.shutdown(); |
| 142 | fwdCacheEventExecutor.shutdown(); |
| 143 | nextCacheEventExecutor.shutdown(); |
| 144 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 145 | super.deactivate(); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Processes given objective on given device. |
| 150 | * Objectives submitted through this method are guaranteed to be executed in order. |
| 151 | * |
| 152 | * @param deviceId Device ID |
| 153 | * @param originalObjective Flow objective to be executed |
| 154 | */ |
| 155 | private void process(DeviceId deviceId, Objective originalObjective) { |
| 156 | // Inject ObjectiveContext such that we can get notified when it is completed |
| 157 | Objective.Builder objBuilder = originalObjective.copy(); |
| 158 | Optional<ObjectiveContext> originalContext = originalObjective.context(); |
Charles Chan | c51852e | 2019-01-07 19:59:10 -0800 | [diff] [blame] | 159 | ObjectiveContext context = new InOrderObjectiveContext(deviceId, originalContext.orElse(null)); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 160 | |
| 161 | // Preserve Objective.Operation |
| 162 | Objective objective; |
| 163 | switch (originalObjective.op()) { |
| 164 | case ADD: |
| 165 | objective = objBuilder.add(context); |
| 166 | break; |
| 167 | case ADD_TO_EXISTING: |
| 168 | objective = ((NextObjective.Builder) objBuilder).addToExisting(context); |
| 169 | break; |
| 170 | case REMOVE: |
| 171 | objective = objBuilder.remove(context); |
| 172 | break; |
| 173 | case REMOVE_FROM_EXISTING: |
| 174 | objective = ((NextObjective.Builder) objBuilder).removeFromExisting(context); |
| 175 | break; |
| 176 | case MODIFY: |
| 177 | objective = ((NextObjective.Builder) objBuilder).modify(context); |
| 178 | break; |
| 179 | case VERIFY: |
| 180 | objective = ((NextObjective.Builder) objBuilder).verify(context); |
| 181 | break; |
| 182 | default: |
| 183 | log.error("Unknown flow objecitve operation {}", originalObjective.op()); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | enqueue(deviceId, objective); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void filter(DeviceId deviceId, FilteringObjective filteringObjective) { |
| 192 | process(deviceId, filteringObjective); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) { |
| 197 | process(deviceId, forwardingObjective); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void next(DeviceId deviceId, NextObjective nextObjective) { |
| 202 | process(deviceId, nextObjective); |
| 203 | } |
| 204 | |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 205 | @Override |
| 206 | public ListMultimap<FilteringObjQueueKey, Objective> getFilteringObjQueue() { |
| 207 | return filtObjQueue; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public ListMultimap<ForwardingObjQueueKey, Objective> getForwardingObjQueue() { |
| 212 | return fwdObjQueue; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public ListMultimap<NextObjQueueKey, Objective> getNextObjQueue() { |
| 217 | return nextObjQueue; |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public Map<FilteringObjQueueKey, Objective> getFilteringObjQueueHead() { |
| 222 | return filtObjQueueHead.asMap(); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public Map<ForwardingObjQueueKey, Objective> getForwardingObjQueueHead() { |
| 227 | return fwdObjQueueHead.asMap(); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public Map<NextObjQueueKey, Objective> getNextObjQueueHead() { |
| 232 | return nextObjQueueHead.asMap(); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public void clearQueue() { |
| 237 | filtObjQueueHead.invalidateAll(); |
| 238 | fwdObjQueueHead.invalidateAll(); |
| 239 | nextObjQueueHead.invalidateAll(); |
| 240 | |
| 241 | filtObjQueueHead.cleanUp(); |
| 242 | fwdObjQueueHead.cleanUp(); |
| 243 | nextObjQueueHead.cleanUp(); |
| 244 | |
| 245 | filtObjQueue.clear(); |
| 246 | fwdObjQueue.clear(); |
| 247 | nextObjQueue.clear(); |
| 248 | } |
| 249 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 250 | /** |
| 251 | * Enqueue flow objective. Execute the flow objective if there is no pending objective ahead. |
| 252 | * |
| 253 | * @param deviceId Device ID |
| 254 | * @param obj Flow objective |
| 255 | */ |
| 256 | private synchronized void enqueue(DeviceId deviceId, Objective obj) { |
| 257 | int queueSize; |
| 258 | int priority = obj.priority(); |
| 259 | |
Charles Chan | c09ad6d | 2018-04-04 16:31:23 -0700 | [diff] [blame] | 260 | LogLevel logLevel = (obj.op() == Objective.Operation.VERIFY) ? LogLevel.TRACE : LogLevel.DEBUG; |
| 261 | Tools.log(log, logLevel, "Enqueue {}", obj); |
| 262 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 263 | if (obj instanceof FilteringObjective) { |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 264 | FilteringObjQueueKey k = new FilteringObjQueueKey(deviceId, priority, ((FilteringObjective) obj).key()); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 265 | filtObjQueue.put(k, obj); |
| 266 | queueSize = filtObjQueue.get(k).size(); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 267 | } else if (obj instanceof ForwardingObjective) { |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 268 | ForwardingObjQueueKey k = |
| 269 | new ForwardingObjQueueKey(deviceId, priority, ((ForwardingObjective) obj).selector()); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 270 | fwdObjQueue.put(k, obj); |
| 271 | queueSize = fwdObjQueue.get(k).size(); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 272 | } else if (obj instanceof NextObjective) { |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 273 | NextObjQueueKey k = new NextObjQueueKey(deviceId, obj.id()); |
| 274 | nextObjQueue.put(k, obj); |
| 275 | queueSize = nextObjQueue.get(k).size(); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 276 | } else { |
| 277 | log.error("Unknown flow objective instance: {}", obj.getClass().getName()); |
| 278 | return; |
| 279 | } |
Charles Chan | c09ad6d | 2018-04-04 16:31:23 -0700 | [diff] [blame] | 280 | log.trace("{} queue size {}", obj.getClass().getSimpleName(), queueSize); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 281 | |
| 282 | // Execute immediately if there is no pending obj ahead |
| 283 | if (queueSize == 1) { |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 284 | execute(deviceId, obj); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Dequeue flow objective. Execute the next flow objective in the queue, if any. |
| 290 | * |
| 291 | * @param deviceId Device ID |
| 292 | * @param obj Flow objective |
Charles Chan | 35b589c | 2018-04-26 16:00:23 -0400 | [diff] [blame] | 293 | * @param error ObjectiveError that triggers this dequeue. Null if this is not triggered by an error. |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 294 | */ |
Charles Chan | 35b589c | 2018-04-26 16:00:23 -0400 | [diff] [blame] | 295 | private synchronized void dequeue(DeviceId deviceId, Objective obj, ObjectiveError error) { |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 296 | List<Objective> remaining; |
| 297 | int priority = obj.priority(); |
| 298 | |
Charles Chan | c09ad6d | 2018-04-04 16:31:23 -0700 | [diff] [blame] | 299 | LogLevel logLevel = (obj.op() == Objective.Operation.VERIFY) ? LogLevel.TRACE : LogLevel.DEBUG; |
| 300 | Tools.log(log, logLevel, "Dequeue {}", obj); |
| 301 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 302 | if (obj instanceof FilteringObjective) { |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 303 | FilteringObjQueueKey k = new FilteringObjQueueKey(deviceId, priority, ((FilteringObjective) obj).key()); |
Charles Chan | a724c513 | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 304 | if (!Objects.equals(ObjectiveError.INSTALLATIONTIMEOUT, error)) { |
Charles Chan | 5cf77f5 | 2018-11-07 15:05:59 -0800 | [diff] [blame] | 305 | filtObjQueueHead.invalidate(k); |
| 306 | } |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 307 | filtObjQueue.remove(k, obj); |
| 308 | remaining = filtObjQueue.get(k); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 309 | } else if (obj instanceof ForwardingObjective) { |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 310 | ForwardingObjQueueKey k = |
| 311 | new ForwardingObjQueueKey(deviceId, priority, ((ForwardingObjective) obj).selector()); |
Charles Chan | a724c513 | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 312 | if (!Objects.equals(ObjectiveError.INSTALLATIONTIMEOUT, error)) { |
Charles Chan | 5cf77f5 | 2018-11-07 15:05:59 -0800 | [diff] [blame] | 313 | fwdObjQueueHead.invalidate(k); |
| 314 | } |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 315 | fwdObjQueue.remove(k, obj); |
| 316 | remaining = fwdObjQueue.get(k); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 317 | } else if (obj instanceof NextObjective) { |
Charles Chan | 35b589c | 2018-04-26 16:00:23 -0400 | [diff] [blame] | 318 | if (error != null) { |
| 319 | // Remove pendingForwards and pendingNexts if next objective failed |
| 320 | Set<PendingFlowObjective> removedForwards = pendingForwards.remove(obj.id()); |
| 321 | List<PendingFlowObjective> removedNexts = pendingNexts.remove(obj.id()); |
| 322 | |
| 323 | if (removedForwards != null) { |
| 324 | removedForwards.stream().map(PendingFlowObjective::flowObjective) |
| 325 | .forEach(pendingObj -> pendingObj.context().ifPresent(c -> |
| 326 | c.onError(pendingObj, error))); |
| 327 | } |
| 328 | if (removedNexts != null) { |
| 329 | removedNexts.stream().map(PendingFlowObjective::flowObjective) |
| 330 | .forEach(pendingObj -> pendingObj.context().ifPresent(c -> |
| 331 | c.onError(pendingObj, error))); |
| 332 | } |
| 333 | } |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 334 | NextObjQueueKey k = new NextObjQueueKey(deviceId, obj.id()); |
Charles Chan | a724c513 | 2018-11-27 21:33:33 +0800 | [diff] [blame] | 335 | if (!Objects.equals(ObjectiveError.INSTALLATIONTIMEOUT, error)) { |
Charles Chan | 5cf77f5 | 2018-11-07 15:05:59 -0800 | [diff] [blame] | 336 | nextObjQueueHead.invalidate(k); |
| 337 | } |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 338 | nextObjQueue.remove(k, obj); |
| 339 | remaining = nextObjQueue.get(k); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 340 | } else { |
| 341 | log.error("Unknown flow objective instance: {}", obj.getClass().getName()); |
| 342 | return; |
| 343 | } |
Charles Chan | c09ad6d | 2018-04-04 16:31:23 -0700 | [diff] [blame] | 344 | log.trace("{} queue size {}", obj.getClass().getSimpleName(), remaining.size()); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 345 | |
| 346 | // Submit the next one in the queue, if any |
| 347 | if (remaining.size() > 0) { |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 348 | execute(deviceId, remaining.get(0)); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Submit the flow objective. Starting from this point on, the execution order is not guaranteed. |
| 354 | * Therefore we must be certain that this method is called in-order. |
| 355 | * |
| 356 | * @param deviceId Device ID |
| 357 | * @param obj Flow objective |
| 358 | */ |
| 359 | private void execute(DeviceId deviceId, Objective obj) { |
Charles Chan | c09ad6d | 2018-04-04 16:31:23 -0700 | [diff] [blame] | 360 | LogLevel logLevel = (obj.op() == Objective.Operation.VERIFY) ? LogLevel.TRACE : LogLevel.DEBUG; |
| 361 | Tools.log(log, logLevel, "Submit objective installer, deviceId {}, obj {}", deviceId, obj); |
| 362 | |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 363 | int priority = obj.priority(); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 364 | if (obj instanceof FilteringObjective) { |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 365 | FilteringObjQueueKey k = new FilteringObjQueueKey(deviceId, priority, ((FilteringObjective) obj).key()); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 366 | filtObjQueueHead.put(k, obj); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 367 | super.filter(deviceId, (FilteringObjective) obj); |
| 368 | } else if (obj instanceof ForwardingObjective) { |
Charles Chan | 33f4a91 | 2018-04-19 23:35:30 -0700 | [diff] [blame] | 369 | ForwardingObjQueueKey k = |
| 370 | new ForwardingObjQueueKey(deviceId, priority, ((ForwardingObjective) obj).selector()); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 371 | fwdObjQueueHead.put(k, obj); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 372 | super.forward(deviceId, (ForwardingObjective) obj); |
| 373 | } else if (obj instanceof NextObjective) { |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 374 | NextObjQueueKey k = new NextObjQueueKey(deviceId, obj.id()); |
| 375 | nextObjQueueHead.put(k, obj); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 376 | super.next(deviceId, (NextObjective) obj); |
| 377 | } else { |
| 378 | log.error("Unknown flow objective instance: {}", obj.getClass().getName()); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | private class InternalStoreDelegate implements FlowObjectiveStoreDelegate { |
| 383 | @Override |
| 384 | public void notify(ObjectiveEvent event) { |
| 385 | if (event.type() == ObjectiveEvent.Type.ADD) { |
| 386 | log.debug("Received notification of obj event {}", event); |
| 387 | Set<PendingFlowObjective> pending; |
| 388 | |
| 389 | // first send all pending flows |
| 390 | synchronized (pendingForwards) { |
| 391 | // needs to be synchronized for queueObjective lookup |
| 392 | pending = pendingForwards.remove(event.subject()); |
| 393 | } |
| 394 | if (pending == null) { |
Charles Chan | c09ad6d | 2018-04-04 16:31:23 -0700 | [diff] [blame] | 395 | log.debug("No forwarding objectives pending for this obj event {}", event); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 396 | } else { |
| 397 | log.debug("Processing {} pending forwarding objectives for nextId {}", |
| 398 | pending.size(), event.subject()); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 399 | // execute pending forwards one by one |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 400 | pending.forEach(p -> execute(p.deviceId(), p.flowObjective())); |
| 401 | } |
| 402 | |
| 403 | // now check for pending next-objectives |
| 404 | // Note: This is still necessary despite the existence of in-order execution. |
| 405 | // Since the in-order execution does not handle the case of |
| 406 | // ADD_TO_EXISTING coming before ADD |
| 407 | List<PendingFlowObjective> pendNexts; |
| 408 | synchronized (pendingNexts) { |
| 409 | // needs to be synchronized for queueObjective lookup |
| 410 | pendNexts = pendingNexts.remove(event.subject()); |
| 411 | } |
| 412 | if (pendNexts == null) { |
Charles Chan | c09ad6d | 2018-04-04 16:31:23 -0700 | [diff] [blame] | 413 | log.debug("No next objectives pending for this obj event {}", event); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 414 | } else { |
| 415 | log.debug("Processing {} pending next objectives for nextId {}", |
| 416 | pendNexts.size(), event.subject()); |
Charles Chan | 45c19d7 | 2018-04-19 21:38:40 -0700 | [diff] [blame] | 417 | // execute pending nexts one by one |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 418 | pendNexts.forEach(p -> execute(p.deviceId(), p.flowObjective())); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | } |
Charles Chan | c51852e | 2019-01-07 19:59:10 -0800 | [diff] [blame] | 423 | |
| 424 | final class InOrderObjectiveContext implements ObjectiveContext { |
| 425 | private final DeviceId deviceId; |
| 426 | private final ObjectiveContext originalContext; |
Charles Chan | 0a6330f | 2019-01-16 11:11:02 -0800 | [diff] [blame] | 427 | // Prevent onSuccess from being executed after onError is called |
| 428 | // i.e. when the context actually succeed after the cache timeout |
| 429 | private final AtomicBoolean failed; |
Charles Chan | c51852e | 2019-01-07 19:59:10 -0800 | [diff] [blame] | 430 | |
| 431 | InOrderObjectiveContext(DeviceId deviceId, ObjectiveContext originalContext) { |
| 432 | this.deviceId = deviceId; |
| 433 | this.originalContext = originalContext; |
Charles Chan | 0a6330f | 2019-01-16 11:11:02 -0800 | [diff] [blame] | 434 | this.failed = new AtomicBoolean(false); |
Charles Chan | c51852e | 2019-01-07 19:59:10 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | @Override |
| 438 | public void onSuccess(Objective objective) { |
| 439 | log.trace("Flow objective onSuccess {}", objective); |
| 440 | |
Charles Chan | 0a6330f | 2019-01-16 11:11:02 -0800 | [diff] [blame] | 441 | if (!failed.get()) { |
Charles Chan | c51852e | 2019-01-07 19:59:10 -0800 | [diff] [blame] | 442 | dequeue(deviceId, objective, null); |
| 443 | if (originalContext != null) { |
| 444 | originalContext.onSuccess(objective); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | } |
| 449 | @Override |
| 450 | public void onError(Objective objective, ObjectiveError error) { |
| 451 | log.warn("Flow objective onError {}. Reason = {}", objective, error); |
| 452 | |
Charles Chan | 0a6330f | 2019-01-16 11:11:02 -0800 | [diff] [blame] | 453 | if (!failed.getAndSet(true)) { |
Charles Chan | c51852e | 2019-01-07 19:59:10 -0800 | [diff] [blame] | 454 | dequeue(deviceId, objective, error); |
| 455 | if (originalContext != null) { |
| 456 | originalContext.onError(objective, error); |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | } |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 461 | } |