blob: 33200b1abe10f8123fe95d2513c779f72ff93422 [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;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onlab.osgi.DefaultServiceDirectory;
26import org.onlab.osgi.ServiceDirectory;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070027import org.onlab.util.ItemNotFoundException;
alshabib77b88482015-04-07 15:47:50 -070028import org.onosproject.cluster.ClusterService;
29import org.onosproject.mastership.MastershipEvent;
30import org.onosproject.mastership.MastershipListener;
31import org.onosproject.mastership.MastershipService;
alshabib77b88482015-04-07 15:47:50 -070032import org.onosproject.net.DeviceId;
33import org.onosproject.net.behaviour.Pipeliner;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070034import org.onosproject.net.behaviour.PipelinerContext;
alshabibaebe7752015-04-07 17:45:42 -070035import org.onosproject.net.device.DeviceEvent;
36import org.onosproject.net.device.DeviceListener;
alshabib77b88482015-04-07 15:47:50 -070037import org.onosproject.net.device.DeviceService;
Thomas Vachuska866b46a2015-04-30 00:26:55 -070038import org.onosproject.net.driver.DefaultDriverProviderService;
alshabib77b88482015-04-07 15:47:50 -070039import org.onosproject.net.driver.DriverHandler;
40import org.onosproject.net.driver.DriverService;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070041import org.onosproject.net.flow.FlowRuleService;
alshabib77b88482015-04-07 15:47:50 -070042import org.onosproject.net.flowobjective.FilteringObjective;
43import org.onosproject.net.flowobjective.FlowObjectiveService;
alshabib2a441c62015-04-13 18:39:38 -070044import org.onosproject.net.flowobjective.FlowObjectiveStore;
45import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate;
alshabib77b88482015-04-07 15:47:50 -070046import org.onosproject.net.flowobjective.ForwardingObjective;
47import org.onosproject.net.flowobjective.NextObjective;
alshabib910aff12015-04-09 16:55:57 -070048import org.onosproject.net.flowobjective.Objective;
Jonathan Hart17d00452015-04-21 17:10:00 -070049import org.onosproject.net.flowobjective.ObjectiveError;
alshabib2a441c62015-04-13 18:39:38 -070050import org.onosproject.net.flowobjective.ObjectiveEvent;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070051import org.onosproject.net.group.GroupService;
alshabib77b88482015-04-07 15:47:50 -070052import org.slf4j.Logger;
53import org.slf4j.LoggerFactory;
54
Saurav Das4ce45962015-11-24 23:21:05 -080055import java.util.Collections;
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;
Saurav Das4ce45962015-11-24 23:21:05 -080059import java.util.concurrent.ConcurrentHashMap;
Jonathan Hart17d00452015-04-21 17:10:00 -070060import java.util.concurrent.ExecutorService;
alshabib77b88482015-04-07 15:47:50 -070061
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -070062import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska866b46a2015-04-30 00:26:55 -070063import static java.util.concurrent.Executors.newFixedThreadPool;
Jonathan Hart17d00452015-04-21 17:10:00 -070064import static org.onlab.util.Tools.groupedThreads;
Changhoon Yoon541ef712015-05-23 17:18:34 +090065import static org.onosproject.security.AppGuard.checkPermission;
Changhoon Yoonb856b812015-08-10 03:47:19 +090066import static org.onosproject.security.AppPermission.Type.*;
67
Changhoon Yoon541ef712015-05-23 17:18:34 +090068
alshabib77b88482015-04-07 15:47:50 -070069
70/**
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070071 * Provides implementation of the flow objective programming service.
alshabib77b88482015-04-07 15:47:50 -070072 */
alshabib2a441c62015-04-13 18:39:38 -070073@Component(immediate = true)
alshabib77b88482015-04-07 15:47:50 -070074@Service
75public class FlowObjectiveManager implements FlowObjectiveService {
76
Saurav Dasbd7f7422015-04-23 16:31:47 -070077 public static final int INSTALL_RETRY_ATTEMPTS = 5;
Jonathan Hart17d00452015-04-21 17:10:00 -070078 public static final long INSTALL_RETRY_INTERVAL = 1000; // ms
alshabib77b88482015-04-07 15:47:50 -070079
Jonathan Hart17d00452015-04-21 17:10:00 -070080 private final Logger log = LoggerFactory.getLogger(getClass());
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070081
alshabib77b88482015-04-07 15:47:50 -070082 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected DriverService driverService;
84
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected DeviceService deviceService;
87
88 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected MastershipService mastershipService;
90
91 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
92 protected ClusterService clusterService;
93
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070094 // Note: The following dependencies are added on behalf of the pipeline
95 // driver behaviours to assure these services are available for their
96 // initialization.
97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 protected FlowRuleService flowRuleService;
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected GroupService groupService;
102
alshabib2a441c62015-04-13 18:39:38 -0700103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected FlowObjectiveStore flowObjectiveStore;
105
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700106 // Note: This must remain an optional dependency to allow re-install of default drivers.
107 // Note: For now disabled until we can move to OPTIONAL_UNARY dependency
108 // @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, policy = ReferencePolicy.DYNAMIC)
109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
110 protected DefaultDriverProviderService defaultDriverService;
111
alshabib2a441c62015-04-13 18:39:38 -0700112 private final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700113
114 private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap();
alshabib910aff12015-04-09 16:55:57 -0700115 private final Map<DeviceId, Pipeliner> pipeliners = Maps.newConcurrentMap();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700116
117 private final PipelinerContext context = new InnerPipelineContext();
118 private final MastershipListener mastershipListener = new InnerMastershipListener();
119 private final DeviceListener deviceListener = new InnerDeviceListener();
120
alshabib77b88482015-04-07 15:47:50 -0700121 protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
alshabib910aff12015-04-09 16:55:57 -0700122
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700123 private Map<Integer, Set<PendingNext>> pendingForwards = Maps.newConcurrentMap();
alshabib2a441c62015-04-13 18:39:38 -0700124
Jonathan Hart17d00452015-04-21 17:10:00 -0700125 private ExecutorService executorService;
alshabib2a441c62015-04-13 18:39:38 -0700126
alshabib77b88482015-04-07 15:47:50 -0700127 @Activate
128 protected void activate() {
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700129 executorService = newFixedThreadPool(4, groupedThreads("onos/objective-installer", "%d"));
alshabib2a441c62015-04-13 18:39:38 -0700130 flowObjectiveStore.setDelegate(delegate);
alshabib77b88482015-04-07 15:47:50 -0700131 mastershipService.addListener(mastershipListener);
alshabibaebe7752015-04-07 17:45:42 -0700132 deviceService.addListener(deviceListener);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700133 deviceService.getDevices().forEach(device -> setupPipelineHandler(device.id()));
alshabib77b88482015-04-07 15:47:50 -0700134 log.info("Started");
135 }
136
137 @Deactivate
138 protected void deactivate() {
alshabib2a441c62015-04-13 18:39:38 -0700139 flowObjectiveStore.unsetDelegate(delegate);
alshabib77b88482015-04-07 15:47:50 -0700140 mastershipService.removeListener(mastershipListener);
alshabibaebe7752015-04-07 17:45:42 -0700141 deviceService.removeListener(deviceListener);
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700142 executorService.shutdown();
143 pipeliners.clear();
144 driverHandlers.clear();
alshabib77b88482015-04-07 15:47:50 -0700145 log.info("Stopped");
146 }
147
Jonathan Hart17d00452015-04-21 17:10:00 -0700148 /**
149 * Task that passes the flow objective down to the driver. The task will
150 * make a few attempts to find the appropriate driver, then eventually give
151 * up and report an error if no suitable driver could be found.
152 */
153 private class ObjectiveInstaller implements Runnable {
154 private final DeviceId deviceId;
155 private final Objective objective;
156
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700157 private final int numAttempts;
Jonathan Hart17d00452015-04-21 17:10:00 -0700158
159 public ObjectiveInstaller(DeviceId deviceId, Objective objective) {
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700160 this(deviceId, objective, 1);
161 }
162
163 public ObjectiveInstaller(DeviceId deviceId, Objective objective, int attemps) {
164 this.deviceId = checkNotNull(deviceId);
165 this.objective = checkNotNull(objective);
166 this.numAttempts = checkNotNull(attemps);
alshabib910aff12015-04-09 16:55:57 -0700167 }
alshabib77b88482015-04-07 15:47:50 -0700168
Jonathan Hart17d00452015-04-21 17:10:00 -0700169 @Override
170 public void run() {
171 try {
Jonathan Hart17d00452015-04-21 17:10:00 -0700172 Pipeliner pipeliner = getDevicePipeliner(deviceId);
173
174 if (pipeliner != null) {
175 if (objective instanceof NextObjective) {
176 pipeliner.next((NextObjective) objective);
177 } else if (objective instanceof ForwardingObjective) {
178 pipeliner.forward((ForwardingObjective) objective);
179 } else {
180 pipeliner.filter((FilteringObjective) objective);
181 }
182 } else if (numAttempts < INSTALL_RETRY_ATTEMPTS) {
Saurav Das3d038262015-04-23 12:36:58 -0700183 Thread.sleep(INSTALL_RETRY_INTERVAL);
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700184 executorService.submit(new ObjectiveInstaller(deviceId, objective, numAttempts + 1));
Jonathan Hart17d00452015-04-21 17:10:00 -0700185 } else {
186 // Otherwise we've tried a few times and failed, report an
187 // error back to the user.
188 objective.context().ifPresent(
189 c -> c.onError(objective, ObjectiveError.DEVICEMISSING));
190 }
191 } catch (Exception e) {
192 log.warn("Exception while installing flow objective", e);
193 }
194 }
195 }
196
197 @Override
198 public void filter(DeviceId deviceId, FilteringObjective filteringObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900199 checkPermission(FLOWRULE_WRITE);
Jonathan Hart17d00452015-04-21 17:10:00 -0700200 executorService.submit(new ObjectiveInstaller(deviceId, filteringObjective));
alshabib77b88482015-04-07 15:47:50 -0700201 }
202
203 @Override
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700204 public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900205 checkPermission(FLOWRULE_WRITE);
alshabib2a441c62015-04-13 18:39:38 -0700206 if (queueObjective(deviceId, forwardingObjective)) {
207 return;
alshabib910aff12015-04-09 16:55:57 -0700208 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700209 executorService.submit(new ObjectiveInstaller(deviceId, forwardingObjective));
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700210 }
211
alshabib2a441c62015-04-13 18:39:38 -0700212 @Override
Jonathan Hart17d00452015-04-21 17:10:00 -0700213 public void next(DeviceId deviceId, NextObjective nextObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900214 checkPermission(FLOWRULE_WRITE);
Jonathan Hart17d00452015-04-21 17:10:00 -0700215 executorService.submit(new ObjectiveInstaller(deviceId, nextObjective));
alshabib2a441c62015-04-13 18:39:38 -0700216 }
217
alshabibf6ea9e62015-04-21 17:08:26 -0700218 @Override
219 public int allocateNextId() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900220 checkPermission(FLOWRULE_WRITE);
alshabibf6ea9e62015-04-21 17:08:26 -0700221 return flowObjectiveStore.allocateNextId();
222 }
223
Xin Jin313708b2015-07-09 13:43:04 -0700224 @Override
225 public void initPolicy(String policy) {}
226
alshabib2a441c62015-04-13 18:39:38 -0700227 private boolean queueObjective(DeviceId deviceId, ForwardingObjective fwd) {
228 if (fwd.nextId() != null &&
229 flowObjectiveStore.getNextGroup(fwd.nextId()) == null) {
Saurav Das3ea46622015-04-22 14:01:34 -0700230 log.trace("Queuing forwarding objective for nextId {}", fwd.nextId());
Saurav Das8a0732e2015-11-20 15:27:53 -0800231 // TODO: change to computeIfAbsent
Saurav Das4ce45962015-11-24 23:21:05 -0800232 Set<PendingNext> newset = Collections.newSetFromMap(
233 new ConcurrentHashMap<PendingNext, Boolean>());
234 newset.add(new PendingNext(deviceId, fwd));
235 Set<PendingNext> pnext = pendingForwards.putIfAbsent(fwd.nextId(), newset);
Saurav Das8a0732e2015-11-20 15:27:53 -0800236 if (pnext != null) {
237 pnext.add(new PendingNext(deviceId, fwd));
alshabib2a441c62015-04-13 18:39:38 -0700238 }
239 return true;
240 }
241 return false;
242 }
243
alshabib910aff12015-04-09 16:55:57 -0700244 // Retrieves the device pipeline behaviour from the cache.
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700245 private Pipeliner getDevicePipeliner(DeviceId deviceId) {
Thomas Vachuska94c3cf42015-07-20 13:01:12 -0700246 return pipeliners.get(deviceId);
alshabib77b88482015-04-07 15:47:50 -0700247 }
248
alshabib2a441c62015-04-13 18:39:38 -0700249 private void setupPipelineHandler(DeviceId deviceId) {
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700250 if (defaultDriverService == null) {
251 // We're not ready to go to work yet.
252 return;
253 }
254
Jonathan Hart17d00452015-04-21 17:10:00 -0700255 // Attempt to lookup the handler in the cache
256 DriverHandler handler = driverHandlers.get(deviceId);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700257 cTime = now();
258
Jonathan Hart17d00452015-04-21 17:10:00 -0700259 if (handler == null) {
260 try {
261 // Otherwise create it and if it has pipeline behaviour, cache it
262 handler = driverService.createHandler(deviceId);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700263 dTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700264 if (!handler.driver().hasBehaviour(Pipeliner.class)) {
265 log.warn("Pipeline behaviour not supported for device {}",
266 deviceId);
alshabib2a441c62015-04-13 18:39:38 -0700267 return;
268 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700269 } catch (ItemNotFoundException e) {
270 log.warn("No applicable driver for device {}", deviceId);
271 return;
alshabib2a441c62015-04-13 18:39:38 -0700272 }
273
Jonathan Hart17d00452015-04-21 17:10:00 -0700274 driverHandlers.put(deviceId, handler);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700275 eTime = now();
alshabib2a441c62015-04-13 18:39:38 -0700276 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700277
278 // Always (re)initialize the pipeline behaviour
279 log.info("Driver {} bound to device {} ... initializing driver",
280 handler.driver().name(), deviceId);
Thomas Vachuska0121a612015-07-21 11:18:09 -0700281 hTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700282 Pipeliner pipeliner = handler.behaviour(Pipeliner.class);
Thomas Vachuska94c3cf42015-07-20 13:01:12 -0700283 hbTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700284 pipeliner.init(deviceId, context);
285 pipeliners.putIfAbsent(deviceId, pipeliner);
alshabib2a441c62015-04-13 18:39:38 -0700286 }
alshabibaebe7752015-04-07 17:45:42 -0700287
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700288 // Triggers driver setup when the local node becomes a device master.
alshabib77b88482015-04-07 15:47:50 -0700289 private class InnerMastershipListener implements MastershipListener {
290 @Override
291 public void event(MastershipEvent event) {
292 switch (event.type()) {
alshabib77b88482015-04-07 15:47:50 -0700293 case MASTER_CHANGED:
Madan Jampani0174f452015-05-29 11:52:05 -0700294 log.debug("mastership changed on device {}", event.subject());
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700295 start = now();
Saurav Das3d038262015-04-23 12:36:58 -0700296 if (deviceService.isAvailable(event.subject())) {
297 setupPipelineHandler(event.subject());
298 }
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700299 stopWatch();
alshabib4313d102015-04-08 18:55:08 -0700300 break;
301 case BACKUPS_CHANGED:
alshabib77b88482015-04-07 15:47:50 -0700302 break;
303 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700304 break;
alshabib77b88482015-04-07 15:47:50 -0700305 }
306 }
alshabibaebe7752015-04-07 17:45:42 -0700307 }
alshabib77b88482015-04-07 15:47:50 -0700308
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700309 // Triggers driver setup when a device is (re)detected.
alshabibaebe7752015-04-07 17:45:42 -0700310 private class InnerDeviceListener implements DeviceListener {
311 @Override
312 public void event(DeviceEvent event) {
313 switch (event.type()) {
314 case DEVICE_ADDED:
315 case DEVICE_AVAILABILITY_CHANGED:
Madan Jampani0174f452015-05-29 11:52:05 -0700316 log.debug("Device either added or availability changed {}",
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700317 event.subject().id());
318 start = now();
alshabib4313d102015-04-08 18:55:08 -0700319 if (deviceService.isAvailable(event.subject().id())) {
Madan Jampani0174f452015-05-29 11:52:05 -0700320 log.debug("Device is now available {}", event.subject().id());
alshabib4313d102015-04-08 18:55:08 -0700321 setupPipelineHandler(event.subject().id());
322 }
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700323 stopWatch();
alshabib4313d102015-04-08 18:55:08 -0700324 break;
325 case DEVICE_UPDATED:
326 break;
327 case DEVICE_REMOVED:
328 break;
329 case DEVICE_SUSPENDED:
330 break;
331 case PORT_ADDED:
332 break;
333 case PORT_UPDATED:
334 break;
335 case PORT_REMOVED:
alshabibaebe7752015-04-07 17:45:42 -0700336 break;
337 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700338 break;
alshabibaebe7752015-04-07 17:45:42 -0700339 }
alshabib77b88482015-04-07 15:47:50 -0700340 }
341 }
alshabibaebe7752015-04-07 17:45:42 -0700342
Thomas Vachuska174bb912015-07-16 21:27:14 -0700343 // Temporary mechanism to monitor pipeliner setup time-cost; there are
344 // intermittent time where this takes in excess of 2 seconds. Why?
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700345 private long start = 0, totals = 0, count = 0;
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700346 private long cTime, dTime, eTime, hTime, hbTime;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700347 private static final long LIMIT = 500;
Thomas Vachuska174bb912015-07-16 21:27:14 -0700348
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700349 private long now() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700350 return System.currentTimeMillis();
351 }
352
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700353 private void stopWatch() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700354 long duration = System.currentTimeMillis() - start;
355 totals += duration;
356 count += 1;
357 if (duration > LIMIT) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700358 log.info("Pipeline setup took {} ms; avg {} ms; cTime={}, dTime={}, eTime={}, hTime={}, hbTime={}",
359 duration, totals / count, diff(cTime), diff(dTime), diff(eTime), diff(hTime), diff(hbTime));
Thomas Vachuska174bb912015-07-16 21:27:14 -0700360 }
361 }
362
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700363 private long diff(long bTime) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700364 long diff = bTime - start;
365 return diff < 0 ? 0 : diff;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700366 }
367
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700368 // Processing context for initializing pipeline driver behaviours.
369 private class InnerPipelineContext implements PipelinerContext {
370 @Override
371 public ServiceDirectory directory() {
372 return serviceDirectory;
alshabibaebe7752015-04-07 17:45:42 -0700373 }
alshabib2a441c62015-04-13 18:39:38 -0700374
375 @Override
376 public FlowObjectiveStore store() {
377 return flowObjectiveStore;
378 }
alshabib2a441c62015-04-13 18:39:38 -0700379 }
380
381 private class InternalStoreDelegate implements FlowObjectiveStoreDelegate {
382 @Override
383 public void notify(ObjectiveEvent event) {
Saurav Das3ea46622015-04-22 14:01:34 -0700384 log.debug("Received notification of obj event {}", event);
alshabib2a441c62015-04-13 18:39:38 -0700385 Set<PendingNext> pending = pendingForwards.remove(event.subject());
386
387 if (pending == null) {
Saurav Das3ea46622015-04-22 14:01:34 -0700388 log.debug("Nothing pending for this obj event");
alshabib2a441c62015-04-13 18:39:38 -0700389 return;
390 }
391
Saurav Das3ea46622015-04-22 14:01:34 -0700392 log.debug("Processing pending forwarding objectives {}", pending.size());
alshabib2a441c62015-04-13 18:39:38 -0700393
394 pending.forEach(p -> getDevicePipeliner(p.deviceId())
395 .forward(p.forwardingObjective()));
396
397 }
398 }
399
400 /**
401 * Data class used to hold a pending forwarding objective that could not
402 * be processed because the associated next object was not present.
403 */
404 private class PendingNext {
405 private final DeviceId deviceId;
406 private final ForwardingObjective fwd;
407
408 public PendingNext(DeviceId deviceId, ForwardingObjective fwd) {
409 this.deviceId = deviceId;
410 this.fwd = fwd;
411 }
412
413 public DeviceId deviceId() {
414 return deviceId;
415 }
416
417 public ForwardingObjective forwardingObjective() {
418 return fwd;
419 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800420
421 @Override
422 public int hashCode() {
423 return Objects.hash(deviceId, fwd);
424 }
425
426 @Override
427 public boolean equals(final Object obj) {
428 if (this == obj) {
429 return true;
430 }
431 if (!(obj instanceof PendingNext)) {
432 return false;
433 }
434 final PendingNext other = (PendingNext) obj;
435 if (this.deviceId.equals(other.deviceId) &&
436 this.fwd.equals(other.fwd)) {
437 return true;
438 }
439 return false;
440 }
alshabibaebe7752015-04-07 17:45:42 -0700441 }
alshabib77b88482015-04-07 15:47:50 -0700442}