blob: 81e224bec65fba0932986a9ff473998b973540a4 [file] [log] [blame]
Charles Chana7903c82018-03-15 20:14:16 -07001/*
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
17package org.onosproject.net.flowobjective.impl;
18
Charles Chan45c19d72018-04-19 21:38:40 -070019import com.google.common.cache.Cache;
20import com.google.common.cache.CacheBuilder;
Charles Chana724c5132018-11-27 21:33:33 +080021import com.google.common.cache.RemovalListener;
22import com.google.common.cache.RemovalListeners;
Charles Chana7903c82018-03-15 20:14:16 -070023import com.google.common.collect.ArrayListMultimap;
24import com.google.common.collect.ListMultimap;
25import com.google.common.collect.Multimaps;
Charles Chanc09ad6d2018-04-04 16:31:23 -070026import org.onlab.util.Tools;
27import org.onlab.util.Tools.LogLevel;
Charles Chana7903c82018-03-15 20:14:16 -070028import org.onosproject.net.DeviceId;
Charles Chan33f4a912018-04-19 23:35:30 -070029import org.onosproject.net.flowobjective.FilteringObjQueueKey;
Charles Chana7903c82018-03-15 20:14:16 -070030import org.onosproject.net.flowobjective.FilteringObjective;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chana7903c82018-03-15 20:14:16 -070032import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate;
Charles Chan33f4a912018-04-19 23:35:30 -070033import org.onosproject.net.flowobjective.ForwardingObjQueueKey;
Charles Chana7903c82018-03-15 20:14:16 -070034import org.onosproject.net.flowobjective.ForwardingObjective;
Charles Chan33f4a912018-04-19 23:35:30 -070035import org.onosproject.net.flowobjective.NextObjQueueKey;
Charles Chana7903c82018-03-15 20:14:16 -070036import org.onosproject.net.flowobjective.NextObjective;
37import org.onosproject.net.flowobjective.Objective;
38import org.onosproject.net.flowobjective.ObjectiveContext;
39import org.onosproject.net.flowobjective.ObjectiveError;
40import org.onosproject.net.flowobjective.ObjectiveEvent;
Charles Chana724c5132018-11-27 21:33:33 +080041import org.onosproject.net.flowobjective.ObjectiveQueueKey;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042import org.osgi.service.component.annotations.Activate;
43import org.osgi.service.component.annotations.Component;
44import org.osgi.service.component.annotations.Deactivate;
pier8b3aef42019-03-11 15:14:02 -070045import org.osgi.service.component.ComponentContext;
Charles Chana7903c82018-03-15 20:14:16 -070046import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
48
49import java.util.List;
Charles Chan33f4a912018-04-19 23:35:30 -070050import java.util.Map;
Charles Chana724c5132018-11-27 21:33:33 +080051import java.util.Objects;
Charles Chana7903c82018-03-15 20:14:16 -070052import java.util.Optional;
53import java.util.Set;
Charles Chana724c5132018-11-27 21:33:33 +080054import java.util.concurrent.ExecutorService;
Charles Chan45c19d72018-04-19 21:38:40 -070055import java.util.concurrent.ScheduledExecutorService;
56import java.util.concurrent.TimeUnit;
Charles Chanc51852e2019-01-07 19:59:10 -080057import java.util.concurrent.atomic.AtomicBoolean;
Charles Chan45c19d72018-04-19 21:38:40 -070058
pierventre3433e312020-10-27 16:08:48 +010059import static com.google.common.base.Strings.isNullOrEmpty;
Charles Chana724c5132018-11-27 21:33:33 +080060import static java.util.concurrent.Executors.newSingleThreadExecutor;
Charles Chan45c19d72018-04-19 21:38:40 -070061import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
62import static org.onlab.util.Tools.groupedThreads;
pierventre3433e312020-10-27 16:08:48 +010063import static org.onosproject.net.OsgiPropertyConstants.IFOM_OBJ_TIMEOUT_MS;
64import static org.onosproject.net.OsgiPropertyConstants.IFOM_OBJ_TIMEOUT_MS_DEFAULT;
Charles Chana7903c82018-03-15 20:14:16 -070065
pierventre3433e312020-10-27 16:08:48 +010066/**
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 Chana7903c82018-03-15 20:14:16 -070076public class InOrderFlowObjectiveManager extends FlowObjectiveManager {
77 private final Logger log = LoggerFactory.getLogger(getClass());
78
pierventre3433e312020-10-27 16:08:48 +010079 /** Objective timeout. */
80 int objectiveTimeoutMs = IFOM_OBJ_TIMEOUT_MS_DEFAULT;
Charles Chan45c19d72018-04-19 21:38:40 -070081
Charles Chan33f4a912018-04-19 23:35:30 -070082 private Cache<FilteringObjQueueKey, Objective> filtObjQueueHead;
83 private Cache<ForwardingObjQueueKey, Objective> fwdObjQueueHead;
Charles Chan45c19d72018-04-19 21:38:40 -070084 private Cache<NextObjQueueKey, Objective> nextObjQueueHead;
85 private ScheduledExecutorService cacheCleaner;
Charles Chana724c5132018-11-27 21:33:33 +080086 private ExecutorService filtCacheEventExecutor;
87 private ExecutorService fwdCacheEventExecutor;
88 private ExecutorService nextCacheEventExecutor;
Charles Chan45c19d72018-04-19 21:38:40 -070089
Charles Chan33f4a912018-04-19 23:35:30 -070090 private ListMultimap<FilteringObjQueueKey, Objective> filtObjQueue =
Charles Chana7903c82018-03-15 20:14:16 -070091 Multimaps.synchronizedListMultimap(ArrayListMultimap.create());
Charles Chan33f4a912018-04-19 23:35:30 -070092 private ListMultimap<ForwardingObjQueueKey, Objective> fwdObjQueue =
Charles Chana7903c82018-03-15 20:14:16 -070093 Multimaps.synchronizedListMultimap(ArrayListMultimap.create());
94 private ListMultimap<NextObjQueueKey, Objective> nextObjQueue =
95 Multimaps.synchronizedListMultimap(ArrayListMultimap.create());
96
97 final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate();
98
pierventre3433e312020-10-27 16:08:48 +010099 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 Chana7903c82018-03-15 20:14:16 -0700114 @Activate
pier8b3aef42019-03-11 15:14:02 -0700115 protected void activate(ComponentContext context) {
116 super.activate(context);
Charles Chan45c19d72018-04-19 21:38:40 -0700117
pierventre3433e312020-10-27 16:08:48 +0100118 cfgService.registerProperties(InOrderFlowObjectiveManager.class);
119
Charles Chana724c5132018-11-27 21:33:33 +0800120 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 Chan45c19d72018-04-19 21:38:40 -0700124 filtObjQueueHead = CacheBuilder.newBuilder()
pierventre3433e312020-10-27 16:08:48 +0100125 .expireAfterWrite(objectiveTimeoutMs, TimeUnit.MILLISECONDS)
Charles Chana724c5132018-11-27 21:33:33 +0800126 .removalListener(RemovalListeners.asynchronous(removalListener, filtCacheEventExecutor))
127 .build();
Charles Chan45c19d72018-04-19 21:38:40 -0700128 fwdObjQueueHead = CacheBuilder.newBuilder()
pierventre3433e312020-10-27 16:08:48 +0100129 .expireAfterWrite(objectiveTimeoutMs, TimeUnit.MILLISECONDS)
Charles Chana724c5132018-11-27 21:33:33 +0800130 .removalListener(RemovalListeners.asynchronous(removalListener, fwdCacheEventExecutor))
131 .build();
Charles Chan45c19d72018-04-19 21:38:40 -0700132 nextObjQueueHead = CacheBuilder.newBuilder()
pierventre3433e312020-10-27 16:08:48 +0100133 .expireAfterWrite(objectiveTimeoutMs, TimeUnit.MILLISECONDS)
Charles Chana724c5132018-11-27 21:33:33 +0800134 .removalListener(RemovalListeners.asynchronous(removalListener, nextCacheEventExecutor))
135 .build();
Charles Chan45c19d72018-04-19 21:38:40 -0700136
137 cacheCleaner = newSingleThreadScheduledExecutor(groupedThreads("onos/flowobj", "cache-cleaner", log));
138 cacheCleaner.scheduleAtFixedRate(() -> {
139 filtObjQueueHead.cleanUp();
140 fwdObjQueueHead.cleanUp();
141 nextObjQueueHead.cleanUp();
pierventre3433e312020-10-27 16:08:48 +0100142 }, 0, objectiveTimeoutMs, TimeUnit.MILLISECONDS);
Charles Chan45c19d72018-04-19 21:38:40 -0700143
Charles Chana7903c82018-03-15 20:14:16 -0700144 // Replace store delegate to make sure pendingForward and pendingNext are resubmitted to
Charles Chan45c19d72018-04-19 21:38:40 -0700145 // execute()
Charles Chana7903c82018-03-15 20:14:16 -0700146 flowObjectiveStore.unsetDelegate(super.delegate);
147 flowObjectiveStore.setDelegate(delegate);
148 }
149
150 @Deactivate
151 protected void deactivate() {
pierventre3433e312020-10-27 16:08:48 +0100152 cfgService.unregisterProperties(getClass(), false);
153
Charles Chan45c19d72018-04-19 21:38:40 -0700154 cacheCleaner.shutdown();
Charles Chan33f4a912018-04-19 23:35:30 -0700155 clearQueue();
Charles Chan45c19d72018-04-19 21:38:40 -0700156
Charles Chana724c5132018-11-27 21:33:33 +0800157 filtCacheEventExecutor.shutdown();
158 fwdCacheEventExecutor.shutdown();
159 nextCacheEventExecutor.shutdown();
160
Charles Chana7903c82018-03-15 20:14:16 -0700161 super.deactivate();
162 }
163
164 /**
pierventre3433e312020-10-27 16:08:48 +0100165 * 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 Chana7903c82018-03-15 20:14:16 -0700221 * 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 Chanc51852e2019-01-07 19:59:10 -0800231 ObjectiveContext context = new InOrderObjectiveContext(deviceId, originalContext.orElse(null));
Charles Chana7903c82018-03-15 20:14:16 -0700232
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 Chan33f4a912018-04-19 23:35:30 -0700277 @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 Chana7903c82018-03-15 20:14:16 -0700322 /**
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 Chanc09ad6d2018-04-04 16:31:23 -0700332 LogLevel logLevel = (obj.op() == Objective.Operation.VERIFY) ? LogLevel.TRACE : LogLevel.DEBUG;
333 Tools.log(log, logLevel, "Enqueue {}", obj);
334
Charles Chana7903c82018-03-15 20:14:16 -0700335 if (obj instanceof FilteringObjective) {
Charles Chan33f4a912018-04-19 23:35:30 -0700336 FilteringObjQueueKey k = new FilteringObjQueueKey(deviceId, priority, ((FilteringObjective) obj).key());
Charles Chana7903c82018-03-15 20:14:16 -0700337 filtObjQueue.put(k, obj);
338 queueSize = filtObjQueue.get(k).size();
Charles Chana7903c82018-03-15 20:14:16 -0700339 } else if (obj instanceof ForwardingObjective) {
Charles Chan33f4a912018-04-19 23:35:30 -0700340 ForwardingObjQueueKey k =
341 new ForwardingObjQueueKey(deviceId, priority, ((ForwardingObjective) obj).selector());
Charles Chana7903c82018-03-15 20:14:16 -0700342 fwdObjQueue.put(k, obj);
343 queueSize = fwdObjQueue.get(k).size();
Charles Chana7903c82018-03-15 20:14:16 -0700344 } else if (obj instanceof NextObjective) {
Charles Chana7903c82018-03-15 20:14:16 -0700345 NextObjQueueKey k = new NextObjQueueKey(deviceId, obj.id());
346 nextObjQueue.put(k, obj);
347 queueSize = nextObjQueue.get(k).size();
Charles Chana7903c82018-03-15 20:14:16 -0700348 } else {
349 log.error("Unknown flow objective instance: {}", obj.getClass().getName());
350 return;
351 }
Charles Chanc09ad6d2018-04-04 16:31:23 -0700352 log.trace("{} queue size {}", obj.getClass().getSimpleName(), queueSize);
Charles Chana7903c82018-03-15 20:14:16 -0700353
354 // Execute immediately if there is no pending obj ahead
355 if (queueSize == 1) {
Charles Chana7903c82018-03-15 20:14:16 -0700356 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 Chan35b589c2018-04-26 16:00:23 -0400365 * @param error ObjectiveError that triggers this dequeue. Null if this is not triggered by an error.
Charles Chana7903c82018-03-15 20:14:16 -0700366 */
Charles Chan35b589c2018-04-26 16:00:23 -0400367 private synchronized void dequeue(DeviceId deviceId, Objective obj, ObjectiveError error) {
Charles Chana7903c82018-03-15 20:14:16 -0700368 List<Objective> remaining;
369 int priority = obj.priority();
370
Charles Chanc09ad6d2018-04-04 16:31:23 -0700371 LogLevel logLevel = (obj.op() == Objective.Operation.VERIFY) ? LogLevel.TRACE : LogLevel.DEBUG;
372 Tools.log(log, logLevel, "Dequeue {}", obj);
373
Charles Chana7903c82018-03-15 20:14:16 -0700374 if (obj instanceof FilteringObjective) {
Charles Chan33f4a912018-04-19 23:35:30 -0700375 FilteringObjQueueKey k = new FilteringObjQueueKey(deviceId, priority, ((FilteringObjective) obj).key());
Charles Chana724c5132018-11-27 21:33:33 +0800376 if (!Objects.equals(ObjectiveError.INSTALLATIONTIMEOUT, error)) {
Charles Chan5cf77f52018-11-07 15:05:59 -0800377 filtObjQueueHead.invalidate(k);
378 }
Charles Chana7903c82018-03-15 20:14:16 -0700379 filtObjQueue.remove(k, obj);
380 remaining = filtObjQueue.get(k);
Charles Chana7903c82018-03-15 20:14:16 -0700381 } else if (obj instanceof ForwardingObjective) {
Charles Chan33f4a912018-04-19 23:35:30 -0700382 ForwardingObjQueueKey k =
383 new ForwardingObjQueueKey(deviceId, priority, ((ForwardingObjective) obj).selector());
Charles Chana724c5132018-11-27 21:33:33 +0800384 if (!Objects.equals(ObjectiveError.INSTALLATIONTIMEOUT, error)) {
Charles Chan5cf77f52018-11-07 15:05:59 -0800385 fwdObjQueueHead.invalidate(k);
386 }
Charles Chana7903c82018-03-15 20:14:16 -0700387 fwdObjQueue.remove(k, obj);
388 remaining = fwdObjQueue.get(k);
Charles Chana7903c82018-03-15 20:14:16 -0700389 } else if (obj instanceof NextObjective) {
Charles Chan35b589c2018-04-26 16:00:23 -0400390 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 Chana7903c82018-03-15 20:14:16 -0700406 NextObjQueueKey k = new NextObjQueueKey(deviceId, obj.id());
Charles Chana724c5132018-11-27 21:33:33 +0800407 if (!Objects.equals(ObjectiveError.INSTALLATIONTIMEOUT, error)) {
Charles Chan5cf77f52018-11-07 15:05:59 -0800408 nextObjQueueHead.invalidate(k);
409 }
Charles Chana7903c82018-03-15 20:14:16 -0700410 nextObjQueue.remove(k, obj);
411 remaining = nextObjQueue.get(k);
Charles Chana7903c82018-03-15 20:14:16 -0700412 } else {
413 log.error("Unknown flow objective instance: {}", obj.getClass().getName());
414 return;
415 }
Charles Chanc09ad6d2018-04-04 16:31:23 -0700416 log.trace("{} queue size {}", obj.getClass().getSimpleName(), remaining.size());
Charles Chana7903c82018-03-15 20:14:16 -0700417
418 // Submit the next one in the queue, if any
419 if (remaining.size() > 0) {
Charles Chana7903c82018-03-15 20:14:16 -0700420 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 Chanc09ad6d2018-04-04 16:31:23 -0700432 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 Chan45c19d72018-04-19 21:38:40 -0700435 int priority = obj.priority();
Charles Chana7903c82018-03-15 20:14:16 -0700436 if (obj instanceof FilteringObjective) {
Charles Chan33f4a912018-04-19 23:35:30 -0700437 FilteringObjQueueKey k = new FilteringObjQueueKey(deviceId, priority, ((FilteringObjective) obj).key());
Charles Chan45c19d72018-04-19 21:38:40 -0700438 filtObjQueueHead.put(k, obj);
Charles Chana7903c82018-03-15 20:14:16 -0700439 super.filter(deviceId, (FilteringObjective) obj);
440 } else if (obj instanceof ForwardingObjective) {
Charles Chan33f4a912018-04-19 23:35:30 -0700441 ForwardingObjQueueKey k =
442 new ForwardingObjQueueKey(deviceId, priority, ((ForwardingObjective) obj).selector());
Charles Chan45c19d72018-04-19 21:38:40 -0700443 fwdObjQueueHead.put(k, obj);
Charles Chana7903c82018-03-15 20:14:16 -0700444 super.forward(deviceId, (ForwardingObjective) obj);
445 } else if (obj instanceof NextObjective) {
Charles Chan45c19d72018-04-19 21:38:40 -0700446 NextObjQueueKey k = new NextObjQueueKey(deviceId, obj.id());
447 nextObjQueueHead.put(k, obj);
Charles Chana7903c82018-03-15 20:14:16 -0700448 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 Chanc09ad6d2018-04-04 16:31:23 -0700467 log.debug("No forwarding objectives pending for this obj event {}", event);
Charles Chana7903c82018-03-15 20:14:16 -0700468 } else {
469 log.debug("Processing {} pending forwarding objectives for nextId {}",
470 pending.size(), event.subject());
Charles Chan45c19d72018-04-19 21:38:40 -0700471 // execute pending forwards one by one
Charles Chana7903c82018-03-15 20:14:16 -0700472 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 Chanc09ad6d2018-04-04 16:31:23 -0700485 log.debug("No next objectives pending for this obj event {}", event);
Charles Chana7903c82018-03-15 20:14:16 -0700486 } else {
487 log.debug("Processing {} pending next objectives for nextId {}",
488 pendNexts.size(), event.subject());
Charles Chan45c19d72018-04-19 21:38:40 -0700489 // execute pending nexts one by one
Charles Chana7903c82018-03-15 20:14:16 -0700490 pendNexts.forEach(p -> execute(p.deviceId(), p.flowObjective()));
491 }
492 }
493 }
494 }
Charles Chanc51852e2019-01-07 19:59:10 -0800495
496 final class InOrderObjectiveContext implements ObjectiveContext {
497 private final DeviceId deviceId;
498 private final ObjectiveContext originalContext;
Charles Chan0a6330f2019-01-16 11:11:02 -0800499 // 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 Chanc51852e2019-01-07 19:59:10 -0800502
503 InOrderObjectiveContext(DeviceId deviceId, ObjectiveContext originalContext) {
504 this.deviceId = deviceId;
505 this.originalContext = originalContext;
Charles Chan0a6330f2019-01-16 11:11:02 -0800506 this.failed = new AtomicBoolean(false);
Charles Chanc51852e2019-01-07 19:59:10 -0800507 }
508
509 @Override
510 public void onSuccess(Objective objective) {
511 log.trace("Flow objective onSuccess {}", objective);
512
Charles Chan0a6330f2019-01-16 11:11:02 -0800513 if (!failed.get()) {
Charles Chanc51852e2019-01-07 19:59:10 -0800514 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 Chan0a6330f2019-01-16 11:11:02 -0800525 if (!failed.getAndSet(true)) {
Charles Chanc51852e2019-01-07 19:59:10 -0800526 dequeue(deviceId, objective, error);
527 if (originalContext != null) {
528 originalContext.onError(objective, error);
529 }
530 }
531 }
532 }
Charles Chana7903c82018-03-15 20:14:16 -0700533}