blob: 5ecdc7a2ff735c878accb540abfb7d60e92b649f [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;
Saurav Das8a0732e2015-11-20 15:27:53 -080057import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070058import java.util.Set;
Jonathan Hart17d00452015-04-21 17:10:00 -070059import java.util.concurrent.ExecutorService;
alshabib77b88482015-04-07 15:47:50 -070060
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -070061import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska866b46a2015-04-30 00:26:55 -070062import static java.util.concurrent.Executors.newFixedThreadPool;
Jonathan Hart17d00452015-04-21 17:10:00 -070063import static org.onlab.util.Tools.groupedThreads;
Changhoon Yoon541ef712015-05-23 17:18:34 +090064import static org.onosproject.security.AppGuard.checkPermission;
Changhoon Yoonb856b812015-08-10 03:47:19 +090065import static org.onosproject.security.AppPermission.Type.*;
66
Changhoon Yoon541ef712015-05-23 17:18:34 +090067
alshabib77b88482015-04-07 15:47:50 -070068
69/**
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070070 * Provides implementation of the flow objective programming service.
alshabib77b88482015-04-07 15:47:50 -070071 */
alshabib2a441c62015-04-13 18:39:38 -070072@Component(immediate = true)
alshabib77b88482015-04-07 15:47:50 -070073@Service
74public class FlowObjectiveManager implements FlowObjectiveService {
75
Saurav Dasbd7f7422015-04-23 16:31:47 -070076 public static final int INSTALL_RETRY_ATTEMPTS = 5;
Jonathan Hart17d00452015-04-21 17:10:00 -070077 public static final long INSTALL_RETRY_INTERVAL = 1000; // ms
alshabib77b88482015-04-07 15:47:50 -070078
Jonathan Hart17d00452015-04-21 17:10:00 -070079 private final Logger log = LoggerFactory.getLogger(getClass());
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070080
alshabib77b88482015-04-07 15:47:50 -070081 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected DriverService driverService;
83
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected DeviceService deviceService;
86
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
88 protected MastershipService mastershipService;
89
90 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 protected ClusterService clusterService;
92
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070093 // Note: The following dependencies are added on behalf of the pipeline
94 // driver behaviours to assure these services are available for their
95 // initialization.
96 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
97 protected FlowRuleService flowRuleService;
98
99 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected GroupService groupService;
101
alshabib2a441c62015-04-13 18:39:38 -0700102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected FlowObjectiveStore flowObjectiveStore;
104
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700105 // Note: This must remain an optional dependency to allow re-install of default drivers.
106 // Note: For now disabled until we can move to OPTIONAL_UNARY dependency
107 // @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, policy = ReferencePolicy.DYNAMIC)
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected DefaultDriverProviderService defaultDriverService;
110
alshabib2a441c62015-04-13 18:39:38 -0700111 private final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700112
113 private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap();
alshabib910aff12015-04-09 16:55:57 -0700114 private final Map<DeviceId, Pipeliner> pipeliners = Maps.newConcurrentMap();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700115
116 private final PipelinerContext context = new InnerPipelineContext();
117 private final MastershipListener mastershipListener = new InnerMastershipListener();
118 private final DeviceListener deviceListener = new InnerDeviceListener();
119
alshabib77b88482015-04-07 15:47:50 -0700120 protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
alshabib910aff12015-04-09 16:55:57 -0700121
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700122 private Map<Integer, Set<PendingNext>> pendingForwards = Maps.newConcurrentMap();
alshabib2a441c62015-04-13 18:39:38 -0700123
Jonathan Hart17d00452015-04-21 17:10:00 -0700124 private ExecutorService executorService;
alshabib2a441c62015-04-13 18:39:38 -0700125
alshabib77b88482015-04-07 15:47:50 -0700126 @Activate
127 protected void activate() {
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700128 executorService = newFixedThreadPool(4, groupedThreads("onos/objective-installer", "%d"));
alshabib2a441c62015-04-13 18:39:38 -0700129 flowObjectiveStore.setDelegate(delegate);
alshabib77b88482015-04-07 15:47:50 -0700130 mastershipService.addListener(mastershipListener);
alshabibaebe7752015-04-07 17:45:42 -0700131 deviceService.addListener(deviceListener);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700132 deviceService.getDevices().forEach(device -> setupPipelineHandler(device.id()));
alshabib77b88482015-04-07 15:47:50 -0700133 log.info("Started");
134 }
135
136 @Deactivate
137 protected void deactivate() {
alshabib2a441c62015-04-13 18:39:38 -0700138 flowObjectiveStore.unsetDelegate(delegate);
alshabib77b88482015-04-07 15:47:50 -0700139 mastershipService.removeListener(mastershipListener);
alshabibaebe7752015-04-07 17:45:42 -0700140 deviceService.removeListener(deviceListener);
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700141 executorService.shutdown();
142 pipeliners.clear();
143 driverHandlers.clear();
alshabib77b88482015-04-07 15:47:50 -0700144 log.info("Stopped");
145 }
146
Jonathan Hart17d00452015-04-21 17:10:00 -0700147 /**
148 * Task that passes the flow objective down to the driver. The task will
149 * make a few attempts to find the appropriate driver, then eventually give
150 * up and report an error if no suitable driver could be found.
151 */
152 private class ObjectiveInstaller implements Runnable {
153 private final DeviceId deviceId;
154 private final Objective objective;
155
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700156 private final int numAttempts;
Jonathan Hart17d00452015-04-21 17:10:00 -0700157
158 public ObjectiveInstaller(DeviceId deviceId, Objective objective) {
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700159 this(deviceId, objective, 1);
160 }
161
162 public ObjectiveInstaller(DeviceId deviceId, Objective objective, int attemps) {
163 this.deviceId = checkNotNull(deviceId);
164 this.objective = checkNotNull(objective);
165 this.numAttempts = checkNotNull(attemps);
alshabib910aff12015-04-09 16:55:57 -0700166 }
alshabib77b88482015-04-07 15:47:50 -0700167
Jonathan Hart17d00452015-04-21 17:10:00 -0700168 @Override
169 public void run() {
170 try {
Jonathan Hart17d00452015-04-21 17:10:00 -0700171 Pipeliner pipeliner = getDevicePipeliner(deviceId);
172
173 if (pipeliner != null) {
174 if (objective instanceof NextObjective) {
175 pipeliner.next((NextObjective) objective);
176 } else if (objective instanceof ForwardingObjective) {
177 pipeliner.forward((ForwardingObjective) objective);
178 } else {
179 pipeliner.filter((FilteringObjective) objective);
180 }
181 } else if (numAttempts < INSTALL_RETRY_ATTEMPTS) {
Saurav Das3d038262015-04-23 12:36:58 -0700182 Thread.sleep(INSTALL_RETRY_INTERVAL);
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700183 executorService.submit(new ObjectiveInstaller(deviceId, objective, numAttempts + 1));
Jonathan Hart17d00452015-04-21 17:10:00 -0700184 } else {
185 // Otherwise we've tried a few times and failed, report an
186 // error back to the user.
187 objective.context().ifPresent(
188 c -> c.onError(objective, ObjectiveError.DEVICEMISSING));
189 }
190 } catch (Exception e) {
191 log.warn("Exception while installing flow objective", e);
192 }
193 }
194 }
195
196 @Override
197 public void filter(DeviceId deviceId, FilteringObjective filteringObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900198 checkPermission(FLOWRULE_WRITE);
Jonathan Hart17d00452015-04-21 17:10:00 -0700199 executorService.submit(new ObjectiveInstaller(deviceId, filteringObjective));
alshabib77b88482015-04-07 15:47:50 -0700200 }
201
202 @Override
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700203 public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900204 checkPermission(FLOWRULE_WRITE);
alshabib2a441c62015-04-13 18:39:38 -0700205 if (queueObjective(deviceId, forwardingObjective)) {
206 return;
alshabib910aff12015-04-09 16:55:57 -0700207 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700208 executorService.submit(new ObjectiveInstaller(deviceId, forwardingObjective));
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700209 }
210
alshabib2a441c62015-04-13 18:39:38 -0700211 @Override
Jonathan Hart17d00452015-04-21 17:10:00 -0700212 public void next(DeviceId deviceId, NextObjective nextObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900213 checkPermission(FLOWRULE_WRITE);
Jonathan Hart17d00452015-04-21 17:10:00 -0700214 executorService.submit(new ObjectiveInstaller(deviceId, nextObjective));
alshabib2a441c62015-04-13 18:39:38 -0700215 }
216
alshabibf6ea9e62015-04-21 17:08:26 -0700217 @Override
218 public int allocateNextId() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900219 checkPermission(FLOWRULE_WRITE);
alshabibf6ea9e62015-04-21 17:08:26 -0700220 return flowObjectiveStore.allocateNextId();
221 }
222
Xin Jin313708b2015-07-09 13:43:04 -0700223 @Override
224 public void initPolicy(String policy) {}
225
alshabib2a441c62015-04-13 18:39:38 -0700226 private boolean queueObjective(DeviceId deviceId, ForwardingObjective fwd) {
227 if (fwd.nextId() != null &&
228 flowObjectiveStore.getNextGroup(fwd.nextId()) == null) {
Saurav Das3ea46622015-04-22 14:01:34 -0700229 log.trace("Queuing forwarding objective for nextId {}", fwd.nextId());
Saurav Das8a0732e2015-11-20 15:27:53 -0800230 // TODO: change to computeIfAbsent
231 Set<PendingNext> pnext = pendingForwards.putIfAbsent(fwd.nextId(),
232 Sets.newHashSet(new PendingNext(deviceId, fwd)));
233 if (pnext != null) {
234 pnext.add(new PendingNext(deviceId, fwd));
alshabib2a441c62015-04-13 18:39:38 -0700235 }
236 return true;
237 }
238 return false;
239 }
240
alshabib910aff12015-04-09 16:55:57 -0700241 // Retrieves the device pipeline behaviour from the cache.
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700242 private Pipeliner getDevicePipeliner(DeviceId deviceId) {
Thomas Vachuska94c3cf42015-07-20 13:01:12 -0700243 return pipeliners.get(deviceId);
alshabib77b88482015-04-07 15:47:50 -0700244 }
245
alshabib2a441c62015-04-13 18:39:38 -0700246 private void setupPipelineHandler(DeviceId deviceId) {
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700247 if (defaultDriverService == null) {
248 // We're not ready to go to work yet.
249 return;
250 }
251
Jonathan Hart17d00452015-04-21 17:10:00 -0700252 // Attempt to lookup the handler in the cache
253 DriverHandler handler = driverHandlers.get(deviceId);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700254 cTime = now();
255
Jonathan Hart17d00452015-04-21 17:10:00 -0700256 if (handler == null) {
257 try {
258 // Otherwise create it and if it has pipeline behaviour, cache it
259 handler = driverService.createHandler(deviceId);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700260 dTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700261 if (!handler.driver().hasBehaviour(Pipeliner.class)) {
262 log.warn("Pipeline behaviour not supported for device {}",
263 deviceId);
alshabib2a441c62015-04-13 18:39:38 -0700264 return;
265 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700266 } catch (ItemNotFoundException e) {
267 log.warn("No applicable driver for device {}", deviceId);
268 return;
alshabib2a441c62015-04-13 18:39:38 -0700269 }
270
Jonathan Hart17d00452015-04-21 17:10:00 -0700271 driverHandlers.put(deviceId, handler);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700272 eTime = now();
alshabib2a441c62015-04-13 18:39:38 -0700273 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700274
275 // Always (re)initialize the pipeline behaviour
276 log.info("Driver {} bound to device {} ... initializing driver",
277 handler.driver().name(), deviceId);
Thomas Vachuska0121a612015-07-21 11:18:09 -0700278 hTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700279 Pipeliner pipeliner = handler.behaviour(Pipeliner.class);
Thomas Vachuska94c3cf42015-07-20 13:01:12 -0700280 hbTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700281 pipeliner.init(deviceId, context);
282 pipeliners.putIfAbsent(deviceId, pipeliner);
alshabib2a441c62015-04-13 18:39:38 -0700283 }
alshabibaebe7752015-04-07 17:45:42 -0700284
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700285 // Triggers driver setup when the local node becomes a device master.
alshabib77b88482015-04-07 15:47:50 -0700286 private class InnerMastershipListener implements MastershipListener {
287 @Override
288 public void event(MastershipEvent event) {
289 switch (event.type()) {
alshabib77b88482015-04-07 15:47:50 -0700290 case MASTER_CHANGED:
Madan Jampani0174f452015-05-29 11:52:05 -0700291 log.debug("mastership changed on device {}", event.subject());
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700292 start = now();
Saurav Das3d038262015-04-23 12:36:58 -0700293 if (deviceService.isAvailable(event.subject())) {
294 setupPipelineHandler(event.subject());
295 }
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700296 stopWatch();
alshabib4313d102015-04-08 18:55:08 -0700297 break;
298 case BACKUPS_CHANGED:
alshabib77b88482015-04-07 15:47:50 -0700299 break;
300 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700301 break;
alshabib77b88482015-04-07 15:47:50 -0700302 }
303 }
alshabibaebe7752015-04-07 17:45:42 -0700304 }
alshabib77b88482015-04-07 15:47:50 -0700305
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700306 // Triggers driver setup when a device is (re)detected.
alshabibaebe7752015-04-07 17:45:42 -0700307 private class InnerDeviceListener implements DeviceListener {
308 @Override
309 public void event(DeviceEvent event) {
310 switch (event.type()) {
311 case DEVICE_ADDED:
312 case DEVICE_AVAILABILITY_CHANGED:
Madan Jampani0174f452015-05-29 11:52:05 -0700313 log.debug("Device either added or availability changed {}",
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700314 event.subject().id());
315 start = now();
alshabib4313d102015-04-08 18:55:08 -0700316 if (deviceService.isAvailable(event.subject().id())) {
Madan Jampani0174f452015-05-29 11:52:05 -0700317 log.debug("Device is now available {}", event.subject().id());
alshabib4313d102015-04-08 18:55:08 -0700318 setupPipelineHandler(event.subject().id());
319 }
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700320 stopWatch();
alshabib4313d102015-04-08 18:55:08 -0700321 break;
322 case DEVICE_UPDATED:
323 break;
324 case DEVICE_REMOVED:
325 break;
326 case DEVICE_SUSPENDED:
327 break;
328 case PORT_ADDED:
329 break;
330 case PORT_UPDATED:
331 break;
332 case PORT_REMOVED:
alshabibaebe7752015-04-07 17:45:42 -0700333 break;
334 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700335 break;
alshabibaebe7752015-04-07 17:45:42 -0700336 }
alshabib77b88482015-04-07 15:47:50 -0700337 }
338 }
alshabibaebe7752015-04-07 17:45:42 -0700339
Thomas Vachuska174bb912015-07-16 21:27:14 -0700340 // Temporary mechanism to monitor pipeliner setup time-cost; there are
341 // intermittent time where this takes in excess of 2 seconds. Why?
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700342 private long start = 0, totals = 0, count = 0;
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700343 private long cTime, dTime, eTime, hTime, hbTime;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700344 private static final long LIMIT = 500;
Thomas Vachuska174bb912015-07-16 21:27:14 -0700345
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700346 private long now() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700347 return System.currentTimeMillis();
348 }
349
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700350 private void stopWatch() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700351 long duration = System.currentTimeMillis() - start;
352 totals += duration;
353 count += 1;
354 if (duration > LIMIT) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700355 log.info("Pipeline setup took {} ms; avg {} ms; cTime={}, dTime={}, eTime={}, hTime={}, hbTime={}",
356 duration, totals / count, diff(cTime), diff(dTime), diff(eTime), diff(hTime), diff(hbTime));
Thomas Vachuska174bb912015-07-16 21:27:14 -0700357 }
358 }
359
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700360 private long diff(long bTime) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700361 long diff = bTime - start;
362 return diff < 0 ? 0 : diff;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700363 }
364
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700365 // Processing context for initializing pipeline driver behaviours.
366 private class InnerPipelineContext implements PipelinerContext {
367 @Override
368 public ServiceDirectory directory() {
369 return serviceDirectory;
alshabibaebe7752015-04-07 17:45:42 -0700370 }
alshabib2a441c62015-04-13 18:39:38 -0700371
372 @Override
373 public FlowObjectiveStore store() {
374 return flowObjectiveStore;
375 }
alshabib2a441c62015-04-13 18:39:38 -0700376 }
377
378 private class InternalStoreDelegate implements FlowObjectiveStoreDelegate {
379 @Override
380 public void notify(ObjectiveEvent event) {
Saurav Das3ea46622015-04-22 14:01:34 -0700381 log.debug("Received notification of obj event {}", event);
alshabib2a441c62015-04-13 18:39:38 -0700382 Set<PendingNext> pending = pendingForwards.remove(event.subject());
383
384 if (pending == null) {
Saurav Das3ea46622015-04-22 14:01:34 -0700385 log.debug("Nothing pending for this obj event");
alshabib2a441c62015-04-13 18:39:38 -0700386 return;
387 }
388
Saurav Das3ea46622015-04-22 14:01:34 -0700389 log.debug("Processing pending forwarding objectives {}", pending.size());
alshabib2a441c62015-04-13 18:39:38 -0700390
391 pending.forEach(p -> getDevicePipeliner(p.deviceId())
392 .forward(p.forwardingObjective()));
393
394 }
395 }
396
397 /**
398 * Data class used to hold a pending forwarding objective that could not
399 * be processed because the associated next object was not present.
400 */
401 private class PendingNext {
402 private final DeviceId deviceId;
403 private final ForwardingObjective fwd;
404
405 public PendingNext(DeviceId deviceId, ForwardingObjective fwd) {
406 this.deviceId = deviceId;
407 this.fwd = fwd;
408 }
409
410 public DeviceId deviceId() {
411 return deviceId;
412 }
413
414 public ForwardingObjective forwardingObjective() {
415 return fwd;
416 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800417
418 @Override
419 public int hashCode() {
420 return Objects.hash(deviceId, fwd);
421 }
422
423 @Override
424 public boolean equals(final Object obj) {
425 if (this == obj) {
426 return true;
427 }
428 if (!(obj instanceof PendingNext)) {
429 return false;
430 }
431 final PendingNext other = (PendingNext) obj;
432 if (this.deviceId.equals(other.deviceId) &&
433 this.fwd.equals(other.fwd)) {
434 return true;
435 }
436 return false;
437 }
alshabibaebe7752015-04-07 17:45:42 -0700438 }
alshabib77b88482015-04-07 15:47:50 -0700439}