blob: a76a298f627bdc7cae3095fd6b1b1664111b490d [file] [log] [blame]
alshabib77b88482015-04-07 15:47:50 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.net.flowobjective.impl;
17
18import com.google.common.collect.Maps;
alshabib2a441c62015-04-13 18:39:38 -070019import com.google.common.collect.Sets;
alshabib77b88482015-04-07 15:47:50 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
26import org.onlab.osgi.DefaultServiceDirectory;
27import org.onlab.osgi.ServiceDirectory;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070028import org.onlab.util.ItemNotFoundException;
alshabib77b88482015-04-07 15:47:50 -070029import org.onosproject.cluster.ClusterService;
30import org.onosproject.mastership.MastershipEvent;
31import org.onosproject.mastership.MastershipListener;
32import org.onosproject.mastership.MastershipService;
alshabib77b88482015-04-07 15:47:50 -070033import org.onosproject.net.DeviceId;
34import org.onosproject.net.behaviour.Pipeliner;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070035import org.onosproject.net.behaviour.PipelinerContext;
alshabibaebe7752015-04-07 17:45:42 -070036import org.onosproject.net.device.DeviceEvent;
37import org.onosproject.net.device.DeviceListener;
alshabib77b88482015-04-07 15:47:50 -070038import org.onosproject.net.device.DeviceService;
Thomas Vachuska866b46a2015-04-30 00:26:55 -070039import org.onosproject.net.driver.DefaultDriverProviderService;
alshabib77b88482015-04-07 15:47:50 -070040import org.onosproject.net.driver.DriverHandler;
41import org.onosproject.net.driver.DriverService;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070042import org.onosproject.net.flow.FlowRuleService;
alshabib77b88482015-04-07 15:47:50 -070043import org.onosproject.net.flowobjective.FilteringObjective;
44import org.onosproject.net.flowobjective.FlowObjectiveService;
alshabib2a441c62015-04-13 18:39:38 -070045import org.onosproject.net.flowobjective.FlowObjectiveStore;
46import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate;
alshabib77b88482015-04-07 15:47:50 -070047import org.onosproject.net.flowobjective.ForwardingObjective;
48import org.onosproject.net.flowobjective.NextObjective;
alshabib910aff12015-04-09 16:55:57 -070049import org.onosproject.net.flowobjective.Objective;
Jonathan Hart17d00452015-04-21 17:10:00 -070050import org.onosproject.net.flowobjective.ObjectiveError;
alshabib2a441c62015-04-13 18:39:38 -070051import org.onosproject.net.flowobjective.ObjectiveEvent;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070052import org.onosproject.net.group.GroupService;
alshabib77b88482015-04-07 15:47:50 -070053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
55
alshabib77b88482015-04-07 15:47:50 -070056import java.util.Map;
alshabib2a441c62015-04-13 18:39:38 -070057import java.util.Set;
Jonathan Hart17d00452015-04-21 17:10:00 -070058import java.util.concurrent.ExecutorService;
alshabib77b88482015-04-07 15:47:50 -070059
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -070060import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska866b46a2015-04-30 00:26:55 -070061import static java.util.concurrent.Executors.newFixedThreadPool;
Jonathan Hart17d00452015-04-21 17:10:00 -070062import static org.onlab.util.Tools.groupedThreads;
Changhoon Yoon541ef712015-05-23 17:18:34 +090063import static org.onosproject.security.AppGuard.checkPermission;
Changhoon Yoonb856b812015-08-10 03:47:19 +090064import static org.onosproject.security.AppPermission.Type.*;
65
Changhoon Yoon541ef712015-05-23 17:18:34 +090066
alshabib77b88482015-04-07 15:47:50 -070067
68/**
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070069 * Provides implementation of the flow objective programming service.
alshabib77b88482015-04-07 15:47:50 -070070 */
alshabib2a441c62015-04-13 18:39:38 -070071@Component(immediate = true)
alshabib77b88482015-04-07 15:47:50 -070072@Service
73public class FlowObjectiveManager implements FlowObjectiveService {
74
Saurav Dasbd7f7422015-04-23 16:31:47 -070075 public static final int INSTALL_RETRY_ATTEMPTS = 5;
Jonathan Hart17d00452015-04-21 17:10:00 -070076 public static final long INSTALL_RETRY_INTERVAL = 1000; // ms
alshabib77b88482015-04-07 15:47:50 -070077
Jonathan Hart17d00452015-04-21 17:10:00 -070078 private final Logger log = LoggerFactory.getLogger(getClass());
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070079
alshabib77b88482015-04-07 15:47:50 -070080 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected DriverService driverService;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected DeviceService deviceService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected MastershipService mastershipService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected ClusterService clusterService;
91
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070092 // Note: The following dependencies are added on behalf of the pipeline
93 // driver behaviours to assure these services are available for their
94 // initialization.
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected FlowRuleService flowRuleService;
97
98 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected GroupService groupService;
100
alshabib2a441c62015-04-13 18:39:38 -0700101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected FlowObjectiveStore flowObjectiveStore;
103
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700104 // Note: This must remain an optional dependency to allow re-install of default drivers.
105 // Note: For now disabled until we can move to OPTIONAL_UNARY dependency
106 // @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, policy = ReferencePolicy.DYNAMIC)
107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected DefaultDriverProviderService defaultDriverService;
109
alshabib2a441c62015-04-13 18:39:38 -0700110 private final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700111
112 private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap();
alshabib910aff12015-04-09 16:55:57 -0700113 private final Map<DeviceId, Pipeliner> pipeliners = Maps.newConcurrentMap();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700114
115 private final PipelinerContext context = new InnerPipelineContext();
116 private final MastershipListener mastershipListener = new InnerMastershipListener();
117 private final DeviceListener deviceListener = new InnerDeviceListener();
118
alshabib77b88482015-04-07 15:47:50 -0700119 protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
alshabib910aff12015-04-09 16:55:57 -0700120
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700121 private Map<Integer, Set<PendingNext>> pendingForwards = Maps.newConcurrentMap();
alshabib2a441c62015-04-13 18:39:38 -0700122
Jonathan Hart17d00452015-04-21 17:10:00 -0700123 private ExecutorService executorService;
alshabib2a441c62015-04-13 18:39:38 -0700124
alshabib77b88482015-04-07 15:47:50 -0700125 @Activate
126 protected void activate() {
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700127 executorService = newFixedThreadPool(4, groupedThreads("onos/objective-installer", "%d"));
alshabib2a441c62015-04-13 18:39:38 -0700128 flowObjectiveStore.setDelegate(delegate);
alshabib77b88482015-04-07 15:47:50 -0700129 mastershipService.addListener(mastershipListener);
alshabibaebe7752015-04-07 17:45:42 -0700130 deviceService.addListener(deviceListener);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700131 deviceService.getDevices().forEach(device -> setupPipelineHandler(device.id()));
alshabib77b88482015-04-07 15:47:50 -0700132 log.info("Started");
133 }
134
135 @Deactivate
136 protected void deactivate() {
alshabib2a441c62015-04-13 18:39:38 -0700137 flowObjectiveStore.unsetDelegate(delegate);
alshabib77b88482015-04-07 15:47:50 -0700138 mastershipService.removeListener(mastershipListener);
alshabibaebe7752015-04-07 17:45:42 -0700139 deviceService.removeListener(deviceListener);
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700140 executorService.shutdown();
141 pipeliners.clear();
142 driverHandlers.clear();
alshabib77b88482015-04-07 15:47:50 -0700143 log.info("Stopped");
144 }
145
Jonathan Hart17d00452015-04-21 17:10:00 -0700146 /**
147 * Task that passes the flow objective down to the driver. The task will
148 * make a few attempts to find the appropriate driver, then eventually give
149 * up and report an error if no suitable driver could be found.
150 */
151 private class ObjectiveInstaller implements Runnable {
152 private final DeviceId deviceId;
153 private final Objective objective;
154
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700155 private final int numAttempts;
Jonathan Hart17d00452015-04-21 17:10:00 -0700156
157 public ObjectiveInstaller(DeviceId deviceId, Objective objective) {
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700158 this(deviceId, objective, 1);
159 }
160
161 public ObjectiveInstaller(DeviceId deviceId, Objective objective, int attemps) {
162 this.deviceId = checkNotNull(deviceId);
163 this.objective = checkNotNull(objective);
164 this.numAttempts = checkNotNull(attemps);
alshabib910aff12015-04-09 16:55:57 -0700165 }
alshabib77b88482015-04-07 15:47:50 -0700166
Jonathan Hart17d00452015-04-21 17:10:00 -0700167 @Override
168 public void run() {
169 try {
Jonathan Hart17d00452015-04-21 17:10:00 -0700170 Pipeliner pipeliner = getDevicePipeliner(deviceId);
171
172 if (pipeliner != null) {
173 if (objective instanceof NextObjective) {
174 pipeliner.next((NextObjective) objective);
175 } else if (objective instanceof ForwardingObjective) {
176 pipeliner.forward((ForwardingObjective) objective);
177 } else {
178 pipeliner.filter((FilteringObjective) objective);
179 }
180 } else if (numAttempts < INSTALL_RETRY_ATTEMPTS) {
Saurav Das3d038262015-04-23 12:36:58 -0700181 Thread.sleep(INSTALL_RETRY_INTERVAL);
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700182 executorService.submit(new ObjectiveInstaller(deviceId, objective, numAttempts + 1));
Jonathan Hart17d00452015-04-21 17:10:00 -0700183 } else {
184 // Otherwise we've tried a few times and failed, report an
185 // error back to the user.
186 objective.context().ifPresent(
187 c -> c.onError(objective, ObjectiveError.DEVICEMISSING));
188 }
189 } catch (Exception e) {
190 log.warn("Exception while installing flow objective", e);
191 }
192 }
193 }
194
195 @Override
196 public void filter(DeviceId deviceId, FilteringObjective filteringObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900197 checkPermission(FLOWRULE_WRITE);
Jonathan Hart17d00452015-04-21 17:10:00 -0700198 executorService.submit(new ObjectiveInstaller(deviceId, filteringObjective));
alshabib77b88482015-04-07 15:47:50 -0700199 }
200
201 @Override
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700202 public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900203 checkPermission(FLOWRULE_WRITE);
alshabib2a441c62015-04-13 18:39:38 -0700204 if (queueObjective(deviceId, forwardingObjective)) {
205 return;
alshabib910aff12015-04-09 16:55:57 -0700206 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700207 executorService.submit(new ObjectiveInstaller(deviceId, forwardingObjective));
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700208 }
209
alshabib2a441c62015-04-13 18:39:38 -0700210 @Override
Jonathan Hart17d00452015-04-21 17:10:00 -0700211 public void next(DeviceId deviceId, NextObjective nextObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900212 checkPermission(FLOWRULE_WRITE);
Jonathan Hart17d00452015-04-21 17:10:00 -0700213 executorService.submit(new ObjectiveInstaller(deviceId, nextObjective));
alshabib2a441c62015-04-13 18:39:38 -0700214 }
215
alshabibf6ea9e62015-04-21 17:08:26 -0700216 @Override
217 public int allocateNextId() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900218 checkPermission(FLOWRULE_WRITE);
alshabibf6ea9e62015-04-21 17:08:26 -0700219 return flowObjectiveStore.allocateNextId();
220 }
221
Xin Jin313708b2015-07-09 13:43:04 -0700222 @Override
223 public void initPolicy(String policy) {}
224
alshabib2a441c62015-04-13 18:39:38 -0700225 private boolean queueObjective(DeviceId deviceId, ForwardingObjective fwd) {
226 if (fwd.nextId() != null &&
227 flowObjectiveStore.getNextGroup(fwd.nextId()) == null) {
Saurav Das3ea46622015-04-22 14:01:34 -0700228 log.trace("Queuing forwarding objective for nextId {}", fwd.nextId());
alshabib2a441c62015-04-13 18:39:38 -0700229 if (pendingForwards.putIfAbsent(fwd.nextId(),
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700230 Sets.newHashSet(new PendingNext(deviceId, fwd))) != null) {
alshabib2a441c62015-04-13 18:39:38 -0700231 Set<PendingNext> pending = pendingForwards.get(fwd.nextId());
232 pending.add(new PendingNext(deviceId, fwd));
233 }
234 return true;
235 }
236 return false;
237 }
238
alshabib910aff12015-04-09 16:55:57 -0700239 // Retrieves the device pipeline behaviour from the cache.
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700240 private Pipeliner getDevicePipeliner(DeviceId deviceId) {
Thomas Vachuska94c3cf42015-07-20 13:01:12 -0700241 return pipeliners.get(deviceId);
alshabib77b88482015-04-07 15:47:50 -0700242 }
243
alshabib2a441c62015-04-13 18:39:38 -0700244 private void setupPipelineHandler(DeviceId deviceId) {
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700245 if (defaultDriverService == null) {
246 // We're not ready to go to work yet.
247 return;
248 }
249
Jonathan Hart17d00452015-04-21 17:10:00 -0700250 // Attempt to lookup the handler in the cache
251 DriverHandler handler = driverHandlers.get(deviceId);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700252 cTime = now();
253
Jonathan Hart17d00452015-04-21 17:10:00 -0700254 if (handler == null) {
255 try {
256 // Otherwise create it and if it has pipeline behaviour, cache it
257 handler = driverService.createHandler(deviceId);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700258 dTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700259 if (!handler.driver().hasBehaviour(Pipeliner.class)) {
260 log.warn("Pipeline behaviour not supported for device {}",
261 deviceId);
alshabib2a441c62015-04-13 18:39:38 -0700262 return;
263 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700264 } catch (ItemNotFoundException e) {
265 log.warn("No applicable driver for device {}", deviceId);
266 return;
alshabib2a441c62015-04-13 18:39:38 -0700267 }
268
Jonathan Hart17d00452015-04-21 17:10:00 -0700269 driverHandlers.put(deviceId, handler);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700270 eTime = now();
alshabib2a441c62015-04-13 18:39:38 -0700271 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700272
273 // Always (re)initialize the pipeline behaviour
274 log.info("Driver {} bound to device {} ... initializing driver",
275 handler.driver().name(), deviceId);
Thomas Vachuska0121a612015-07-21 11:18:09 -0700276 hTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700277 Pipeliner pipeliner = handler.behaviour(Pipeliner.class);
Thomas Vachuska94c3cf42015-07-20 13:01:12 -0700278 hbTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700279 pipeliner.init(deviceId, context);
280 pipeliners.putIfAbsent(deviceId, pipeliner);
alshabib2a441c62015-04-13 18:39:38 -0700281 }
alshabibaebe7752015-04-07 17:45:42 -0700282
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700283 // Triggers driver setup when the local node becomes a device master.
alshabib77b88482015-04-07 15:47:50 -0700284 private class InnerMastershipListener implements MastershipListener {
285 @Override
286 public void event(MastershipEvent event) {
287 switch (event.type()) {
alshabib77b88482015-04-07 15:47:50 -0700288 case MASTER_CHANGED:
Madan Jampani0174f452015-05-29 11:52:05 -0700289 log.debug("mastership changed on device {}", event.subject());
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700290 start = now();
Saurav Das3d038262015-04-23 12:36:58 -0700291 if (deviceService.isAvailable(event.subject())) {
292 setupPipelineHandler(event.subject());
293 }
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700294 stopWatch();
alshabib4313d102015-04-08 18:55:08 -0700295 break;
296 case BACKUPS_CHANGED:
alshabib77b88482015-04-07 15:47:50 -0700297 break;
298 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700299 break;
alshabib77b88482015-04-07 15:47:50 -0700300 }
301 }
alshabibaebe7752015-04-07 17:45:42 -0700302 }
alshabib77b88482015-04-07 15:47:50 -0700303
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700304 // Triggers driver setup when a device is (re)detected.
alshabibaebe7752015-04-07 17:45:42 -0700305 private class InnerDeviceListener implements DeviceListener {
306 @Override
307 public void event(DeviceEvent event) {
308 switch (event.type()) {
309 case DEVICE_ADDED:
310 case DEVICE_AVAILABILITY_CHANGED:
Madan Jampani0174f452015-05-29 11:52:05 -0700311 log.debug("Device either added or availability changed {}",
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700312 event.subject().id());
313 start = now();
alshabib4313d102015-04-08 18:55:08 -0700314 if (deviceService.isAvailable(event.subject().id())) {
Madan Jampani0174f452015-05-29 11:52:05 -0700315 log.debug("Device is now available {}", event.subject().id());
alshabib4313d102015-04-08 18:55:08 -0700316 setupPipelineHandler(event.subject().id());
317 }
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700318 stopWatch();
alshabib4313d102015-04-08 18:55:08 -0700319 break;
320 case DEVICE_UPDATED:
321 break;
322 case DEVICE_REMOVED:
323 break;
324 case DEVICE_SUSPENDED:
325 break;
326 case PORT_ADDED:
327 break;
328 case PORT_UPDATED:
329 break;
330 case PORT_REMOVED:
alshabibaebe7752015-04-07 17:45:42 -0700331 break;
332 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700333 break;
alshabibaebe7752015-04-07 17:45:42 -0700334 }
alshabib77b88482015-04-07 15:47:50 -0700335 }
336 }
alshabibaebe7752015-04-07 17:45:42 -0700337
Thomas Vachuska174bb912015-07-16 21:27:14 -0700338 // Temporary mechanism to monitor pipeliner setup time-cost; there are
339 // intermittent time where this takes in excess of 2 seconds. Why?
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700340 private long start = 0, totals = 0, count = 0;
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700341 private long cTime, dTime, eTime, hTime, hbTime;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700342 private static final long LIMIT = 500;
Thomas Vachuska174bb912015-07-16 21:27:14 -0700343
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700344 private long now() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700345 return System.currentTimeMillis();
346 }
347
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700348 private void stopWatch() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700349 long duration = System.currentTimeMillis() - start;
350 totals += duration;
351 count += 1;
352 if (duration > LIMIT) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700353 log.info("Pipeline setup took {} ms; avg {} ms; cTime={}, dTime={}, eTime={}, hTime={}, hbTime={}",
354 duration, totals / count, diff(cTime), diff(dTime), diff(eTime), diff(hTime), diff(hbTime));
Thomas Vachuska174bb912015-07-16 21:27:14 -0700355 }
356 }
357
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700358 private long diff(long bTime) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700359 long diff = bTime - start;
360 return diff < 0 ? 0 : diff;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700361 }
362
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700363 // Processing context for initializing pipeline driver behaviours.
364 private class InnerPipelineContext implements PipelinerContext {
365 @Override
366 public ServiceDirectory directory() {
367 return serviceDirectory;
alshabibaebe7752015-04-07 17:45:42 -0700368 }
alshabib2a441c62015-04-13 18:39:38 -0700369
370 @Override
371 public FlowObjectiveStore store() {
372 return flowObjectiveStore;
373 }
alshabib2a441c62015-04-13 18:39:38 -0700374 }
375
376 private class InternalStoreDelegate implements FlowObjectiveStoreDelegate {
377 @Override
378 public void notify(ObjectiveEvent event) {
Saurav Das3ea46622015-04-22 14:01:34 -0700379 log.debug("Received notification of obj event {}", event);
alshabib2a441c62015-04-13 18:39:38 -0700380 Set<PendingNext> pending = pendingForwards.remove(event.subject());
381
382 if (pending == null) {
Saurav Das3ea46622015-04-22 14:01:34 -0700383 log.debug("Nothing pending for this obj event");
alshabib2a441c62015-04-13 18:39:38 -0700384 return;
385 }
386
Saurav Das3ea46622015-04-22 14:01:34 -0700387 log.debug("Processing pending forwarding objectives {}", pending.size());
alshabib2a441c62015-04-13 18:39:38 -0700388
389 pending.forEach(p -> getDevicePipeliner(p.deviceId())
390 .forward(p.forwardingObjective()));
391
392 }
393 }
394
395 /**
396 * Data class used to hold a pending forwarding objective that could not
397 * be processed because the associated next object was not present.
398 */
399 private class PendingNext {
400 private final DeviceId deviceId;
401 private final ForwardingObjective fwd;
402
403 public PendingNext(DeviceId deviceId, ForwardingObjective fwd) {
404 this.deviceId = deviceId;
405 this.fwd = fwd;
406 }
407
408 public DeviceId deviceId() {
409 return deviceId;
410 }
411
412 public ForwardingObjective forwardingObjective() {
413 return fwd;
414 }
alshabibaebe7752015-04-07 17:45:42 -0700415 }
alshabib77b88482015-04-07 15:47:50 -0700416}