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