blob: d75470ce59478c16750aec87957995e7a2209db7 [file] [log] [blame]
alshabib77b88482015-04-07 15:47:50 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
alshabib77b88482015-04-07 15:47:50 -07003 *
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;
Thomas Vachuskad27097c2016-06-14 19:10:41 -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;
alshabib77b88482015-04-07 15:47:50 -070030import org.onosproject.net.DeviceId;
Saurav Das24431192016-03-07 19:13:00 -080031import org.onosproject.net.behaviour.NextGroup;
alshabib77b88482015-04-07 15:47:50 -070032import org.onosproject.net.behaviour.Pipeliner;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070033import org.onosproject.net.behaviour.PipelinerContext;
alshabibaebe7752015-04-07 17:45:42 -070034import org.onosproject.net.device.DeviceEvent;
35import org.onosproject.net.device.DeviceListener;
alshabib77b88482015-04-07 15:47:50 -070036import org.onosproject.net.device.DeviceService;
Thomas Vachuska866b46a2015-04-30 00:26:55 -070037import org.onosproject.net.driver.DefaultDriverProviderService;
alshabib77b88482015-04-07 15:47:50 -070038import org.onosproject.net.driver.DriverHandler;
39import org.onosproject.net.driver.DriverService;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070040import org.onosproject.net.flow.FlowRuleService;
alshabib77b88482015-04-07 15:47:50 -070041import org.onosproject.net.flowobjective.FilteringObjective;
42import org.onosproject.net.flowobjective.FlowObjectiveService;
alshabib2a441c62015-04-13 18:39:38 -070043import org.onosproject.net.flowobjective.FlowObjectiveStore;
44import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate;
alshabib77b88482015-04-07 15:47:50 -070045import org.onosproject.net.flowobjective.ForwardingObjective;
46import org.onosproject.net.flowobjective.NextObjective;
alshabib910aff12015-04-09 16:55:57 -070047import org.onosproject.net.flowobjective.Objective;
Jonathan Hart17d00452015-04-21 17:10:00 -070048import org.onosproject.net.flowobjective.ObjectiveError;
alshabib2a441c62015-04-13 18:39:38 -070049import org.onosproject.net.flowobjective.ObjectiveEvent;
Saurav Das423fe2b2015-12-04 10:52:59 -080050import org.onosproject.net.flowobjective.ObjectiveEvent.Type;
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 Das24431192016-03-07 19:13:00 -080055import java.util.ArrayList;
Saurav Das24431192016-03-07 19:13:00 -080056import java.util.List;
alshabib77b88482015-04-07 15:47:50 -070057import java.util.Map;
Saurav Das8a0732e2015-11-20 15:27:53 -080058import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070059import java.util.Set;
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;
Thomas Vachuskad27097c2016-06-14 19:10:41 -070066import static org.onosproject.security.AppPermission.Type.FLOWRULE_WRITE;
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)
alshabib77b88482015-04-07 15:47:50 -070087 protected ClusterService clusterService;
88
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070089 // Note: The following dependencies are added on behalf of the pipeline
90 // driver behaviours to assure these services are available for their
91 // initialization.
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected FlowRuleService flowRuleService;
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected GroupService groupService;
97
alshabib2a441c62015-04-13 18:39:38 -070098 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected FlowObjectiveStore flowObjectiveStore;
100
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700101 // Note: This must remain an optional dependency to allow re-install of default drivers.
102 // Note: For now disabled until we can move to OPTIONAL_UNARY dependency
103 // @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, policy = ReferencePolicy.DYNAMIC)
104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected DefaultDriverProviderService defaultDriverService;
106
alshabib2a441c62015-04-13 18:39:38 -0700107 private final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700108
109 private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap();
alshabib910aff12015-04-09 16:55:57 -0700110 private final Map<DeviceId, Pipeliner> pipeliners = Maps.newConcurrentMap();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700111
112 private final PipelinerContext context = new InnerPipelineContext();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700113 private final DeviceListener deviceListener = new InnerDeviceListener();
114
alshabib77b88482015-04-07 15:47:50 -0700115 protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
alshabib910aff12015-04-09 16:55:57 -0700116
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700117 private final Map<Integer, Set<PendingNext>> pendingForwards = Maps.newConcurrentMap();
alshabib2a441c62015-04-13 18:39:38 -0700118
Saurav Das24431192016-03-07 19:13:00 -0800119 // local store to track which nextObjectives were sent to which device
120 // for debugging purposes
121 private Map<Integer, DeviceId> nextToDevice = Maps.newConcurrentMap();
122
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() {
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700127 executorService = newFixedThreadPool(4, groupedThreads("onos/objective-installer", "%d", log));
alshabib2a441c62015-04-13 18:39:38 -0700128 flowObjectiveStore.setDelegate(delegate);
alshabibaebe7752015-04-07 17:45:42 -0700129 deviceService.addListener(deviceListener);
alshabib77b88482015-04-07 15:47:50 -0700130 log.info("Started");
131 }
132
133 @Deactivate
134 protected void deactivate() {
alshabib2a441c62015-04-13 18:39:38 -0700135 flowObjectiveStore.unsetDelegate(delegate);
alshabibaebe7752015-04-07 17:45:42 -0700136 deviceService.removeListener(deviceListener);
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700137 executorService.shutdown();
138 pipeliners.clear();
139 driverHandlers.clear();
Saurav Das24431192016-03-07 19:13:00 -0800140 nextToDevice.clear();
alshabib77b88482015-04-07 15:47:50 -0700141 log.info("Stopped");
142 }
143
Jonathan Hart17d00452015-04-21 17:10:00 -0700144 /**
145 * Task that passes the flow objective down to the driver. The task will
146 * make a few attempts to find the appropriate driver, then eventually give
147 * up and report an error if no suitable driver could be found.
148 */
149 private class ObjectiveInstaller implements Runnable {
150 private final DeviceId deviceId;
151 private final Objective objective;
152
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700153 private final int numAttempts;
Jonathan Hart17d00452015-04-21 17:10:00 -0700154
155 public ObjectiveInstaller(DeviceId deviceId, Objective objective) {
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700156 this(deviceId, objective, 1);
157 }
158
159 public ObjectiveInstaller(DeviceId deviceId, Objective objective, int attemps) {
160 this.deviceId = checkNotNull(deviceId);
161 this.objective = checkNotNull(objective);
162 this.numAttempts = checkNotNull(attemps);
alshabib910aff12015-04-09 16:55:57 -0700163 }
alshabib77b88482015-04-07 15:47:50 -0700164
Jonathan Hart17d00452015-04-21 17:10:00 -0700165 @Override
166 public void run() {
167 try {
Jonathan Hart17d00452015-04-21 17:10:00 -0700168 Pipeliner pipeliner = getDevicePipeliner(deviceId);
169
170 if (pipeliner != null) {
171 if (objective instanceof NextObjective) {
172 pipeliner.next((NextObjective) objective);
173 } else if (objective instanceof ForwardingObjective) {
174 pipeliner.forward((ForwardingObjective) objective);
175 } else {
176 pipeliner.filter((FilteringObjective) objective);
177 }
Andrea Campanella1f8188d2016-02-29 13:24:54 -0800178 //Attempts to check if pipeliner is null for retry attempts
Jonathan Hart17d00452015-04-21 17:10:00 -0700179 } else if (numAttempts < INSTALL_RETRY_ATTEMPTS) {
Saurav Das3d038262015-04-23 12:36:58 -0700180 Thread.sleep(INSTALL_RETRY_INTERVAL);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700181 executorService.execute(new ObjectiveInstaller(deviceId, objective, numAttempts + 1));
Jonathan Hart17d00452015-04-21 17:10:00 -0700182 } else {
183 // Otherwise we've tried a few times and failed, report an
184 // error back to the user.
185 objective.context().ifPresent(
Andrea Campanella1f8188d2016-02-29 13:24:54 -0800186 c -> c.onError(objective, ObjectiveError.NOPIPELINER));
Jonathan Hart17d00452015-04-21 17:10:00 -0700187 }
Andrea Campanella1f8188d2016-02-29 13:24:54 -0800188 //Excpetion thrown
Jonathan Hart17d00452015-04-21 17:10:00 -0700189 } 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);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700198 executorService.execute(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 }
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700207 executorService.execute(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);
Saurav Das24431192016-03-07 19:13:00 -0800213 nextToDevice.put(nextObjective.id(), deviceId);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700214 executorService.execute(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) {
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700227 if (fwd.nextId() == null ||
228 flowObjectiveStore.getNextGroup(fwd.nextId()) != null) {
229 // fast path
230 return false;
alshabib2a441c62015-04-13 18:39:38 -0700231 }
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700232 boolean queued = false;
233 synchronized (pendingForwards) {
234 // double check the flow objective store, because this block could run
235 // after a notification arrives
236 if (flowObjectiveStore.getNextGroup(fwd.nextId()) == null) {
237 pendingForwards.compute(fwd.nextId(), (id, pending) -> {
238 PendingNext next = new PendingNext(deviceId, fwd);
239 if (pending == null) {
240 return Sets.newHashSet(next);
241 } else {
242 pending.add(next);
243 return pending;
244 }
245 });
246 queued = true;
247 }
248 }
249 if (queued) {
250 log.debug("Queued forwarding objective {} for nextId {} meant for device {}",
251 fwd.id(), fwd.nextId(), deviceId);
252 }
253 return queued;
alshabib2a441c62015-04-13 18:39:38 -0700254 }
255
alshabib910aff12015-04-09 16:55:57 -0700256 // Retrieves the device pipeline behaviour from the cache.
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700257 private Pipeliner getDevicePipeliner(DeviceId deviceId) {
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700258 return pipeliners.computeIfAbsent(deviceId, this::initPipelineHandler);
alshabib77b88482015-04-07 15:47:50 -0700259 }
260
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700261 /**
262 * Creates and initialize {@link Pipeliner}.
263 * <p>
264 * Note: Expected to be called under per-Device lock.
265 * e.g., {@code pipeliners}' Map#compute family methods
266 *
267 * @param deviceId Device to initialize pipeliner
268 * @return {@link Pipeliner} instance or null
269 */
270 private Pipeliner initPipelineHandler(DeviceId deviceId) {
271 start = now();
272 // ?? We never use defaultDriverService, do we still need this check?
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700273 if (defaultDriverService == null) {
274 // We're not ready to go to work yet.
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700275 return null;
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700276 }
277
Jonathan Hart17d00452015-04-21 17:10:00 -0700278 // Attempt to lookup the handler in the cache
279 DriverHandler handler = driverHandlers.get(deviceId);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700280 cTime = now();
281
Jonathan Hart17d00452015-04-21 17:10:00 -0700282 if (handler == null) {
283 try {
284 // Otherwise create it and if it has pipeline behaviour, cache it
285 handler = driverService.createHandler(deviceId);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700286 dTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700287 if (!handler.driver().hasBehaviour(Pipeliner.class)) {
288 log.warn("Pipeline behaviour not supported for device {}",
289 deviceId);
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700290 return null;
alshabib2a441c62015-04-13 18:39:38 -0700291 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700292 } catch (ItemNotFoundException e) {
293 log.warn("No applicable driver for device {}", deviceId);
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700294 return null;
alshabib2a441c62015-04-13 18:39:38 -0700295 }
296
Jonathan Hart17d00452015-04-21 17:10:00 -0700297 driverHandlers.put(deviceId, handler);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700298 eTime = now();
alshabib2a441c62015-04-13 18:39:38 -0700299 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700300
301 // Always (re)initialize the pipeline behaviour
302 log.info("Driver {} bound to device {} ... initializing driver",
303 handler.driver().name(), deviceId);
Thomas Vachuska0121a612015-07-21 11:18:09 -0700304 hTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700305 Pipeliner pipeliner = handler.behaviour(Pipeliner.class);
Thomas Vachuska94c3cf42015-07-20 13:01:12 -0700306 hbTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700307 pipeliner.init(deviceId, context);
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700308 stopWatch();
309 return pipeliner;
alshabibaebe7752015-04-07 17:45:42 -0700310 }
alshabib77b88482015-04-07 15:47:50 -0700311
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700312 // Triggers driver setup when a device is (re)detected.
alshabibaebe7752015-04-07 17:45:42 -0700313 private class InnerDeviceListener implements DeviceListener {
314 @Override
315 public void event(DeviceEvent event) {
316 switch (event.type()) {
317 case DEVICE_ADDED:
318 case DEVICE_AVAILABILITY_CHANGED:
Madan Jampani0174f452015-05-29 11:52:05 -0700319 log.debug("Device either added or availability changed {}",
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700320 event.subject().id());
alshabib4313d102015-04-08 18:55:08 -0700321 if (deviceService.isAvailable(event.subject().id())) {
Madan Jampani0174f452015-05-29 11:52:05 -0700322 log.debug("Device is now available {}", event.subject().id());
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700323 getDevicePipeliner(event.subject().id());
324 } else {
325 log.debug("Device is no longer available {}", event.subject().id());
326 // evict Pipeliner cache.
327 // User might restart Device to assign new Driver/Pipeliner
328 // loaded afterwards.
329 pipeliners.remove(event.subject().id());
alshabib4313d102015-04-08 18:55:08 -0700330 }
331 break;
332 case DEVICE_UPDATED:
333 break;
334 case DEVICE_REMOVED:
alshabib4313d102015-04-08 18:55:08 -0700335 case DEVICE_SUSPENDED:
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700336 // evict Pipeliner cache.
337 pipeliners.remove(event.subject().id());
alshabib4313d102015-04-08 18:55:08 -0700338 break;
339 case PORT_ADDED:
340 break;
341 case PORT_UPDATED:
342 break;
343 case PORT_REMOVED:
alshabibaebe7752015-04-07 17:45:42 -0700344 break;
345 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700346 break;
alshabibaebe7752015-04-07 17:45:42 -0700347 }
alshabib77b88482015-04-07 15:47:50 -0700348 }
349 }
alshabibaebe7752015-04-07 17:45:42 -0700350
Thomas Vachuska174bb912015-07-16 21:27:14 -0700351 // Temporary mechanism to monitor pipeliner setup time-cost; there are
352 // intermittent time where this takes in excess of 2 seconds. Why?
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700353 private long start = 0, totals = 0, count = 0;
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700354 private long cTime, dTime, eTime, hTime, hbTime;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700355 private static final long LIMIT = 500;
Thomas Vachuska174bb912015-07-16 21:27:14 -0700356
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700357 private long now() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700358 return System.currentTimeMillis();
359 }
360
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700361 private void stopWatch() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700362 long duration = System.currentTimeMillis() - start;
363 totals += duration;
364 count += 1;
365 if (duration > LIMIT) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700366 log.info("Pipeline setup took {} ms; avg {} ms; cTime={}, dTime={}, eTime={}, hTime={}, hbTime={}",
367 duration, totals / count, diff(cTime), diff(dTime), diff(eTime), diff(hTime), diff(hbTime));
Thomas Vachuska174bb912015-07-16 21:27:14 -0700368 }
369 }
370
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700371 private long diff(long bTime) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700372 long diff = bTime - start;
373 return diff < 0 ? 0 : diff;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700374 }
375
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700376 // Processing context for initializing pipeline driver behaviours.
377 private class InnerPipelineContext implements PipelinerContext {
378 @Override
379 public ServiceDirectory directory() {
380 return serviceDirectory;
alshabibaebe7752015-04-07 17:45:42 -0700381 }
alshabib2a441c62015-04-13 18:39:38 -0700382
383 @Override
384 public FlowObjectiveStore store() {
385 return flowObjectiveStore;
386 }
alshabib2a441c62015-04-13 18:39:38 -0700387 }
388
389 private class InternalStoreDelegate implements FlowObjectiveStoreDelegate {
390 @Override
391 public void notify(ObjectiveEvent event) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800392 if (event.type() == Type.ADD) {
393 log.debug("Received notification of obj event {}", event);
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700394 Set<PendingNext> pending;
395 synchronized (pendingForwards) {
396 // needs to be synchronized for queueObjective lookup
397 pending = pendingForwards.remove(event.subject());
398 }
alshabib2a441c62015-04-13 18:39:38 -0700399
Saurav Das423fe2b2015-12-04 10:52:59 -0800400 if (pending == null) {
Saurav Das25190812016-05-27 13:54:07 -0700401 log.debug("Nothing pending for this obj event {}", event);
Saurav Das423fe2b2015-12-04 10:52:59 -0800402 return;
403 }
404
Saurav Das49cb5a12016-01-16 22:54:07 -0800405 log.debug("Processing {} pending forwarding objectives for nextId {}",
406 pending.size(), event.subject());
Saurav Das423fe2b2015-12-04 10:52:59 -0800407 pending.forEach(p -> getDevicePipeliner(p.deviceId())
408 .forward(p.forwardingObjective()));
alshabib2a441c62015-04-13 18:39:38 -0700409 }
alshabib2a441c62015-04-13 18:39:38 -0700410 }
411 }
412
413 /**
414 * Data class used to hold a pending forwarding objective that could not
415 * be processed because the associated next object was not present.
416 */
417 private class PendingNext {
418 private final DeviceId deviceId;
419 private final ForwardingObjective fwd;
420
421 public PendingNext(DeviceId deviceId, ForwardingObjective fwd) {
422 this.deviceId = deviceId;
423 this.fwd = fwd;
424 }
425
426 public DeviceId deviceId() {
427 return deviceId;
428 }
429
430 public ForwardingObjective forwardingObjective() {
431 return fwd;
432 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800433
434 @Override
435 public int hashCode() {
436 return Objects.hash(deviceId, fwd);
437 }
438
439 @Override
440 public boolean equals(final Object obj) {
441 if (this == obj) {
442 return true;
443 }
444 if (!(obj instanceof PendingNext)) {
445 return false;
446 }
447 final PendingNext other = (PendingNext) obj;
448 if (this.deviceId.equals(other.deviceId) &&
449 this.fwd.equals(other.fwd)) {
450 return true;
451 }
452 return false;
453 }
alshabibaebe7752015-04-07 17:45:42 -0700454 }
Saurav Das24431192016-03-07 19:13:00 -0800455
456 @Override
457 public List<String> getNextMappings() {
458 List<String> mappings = new ArrayList<>();
459 Map<Integer, NextGroup> allnexts = flowObjectiveStore.getAllGroups();
Saurav Das25190812016-05-27 13:54:07 -0700460 // XXX if the NextGroup after de-serialization actually stored info of the deviceId
Saurav Das24431192016-03-07 19:13:00 -0800461 // then info on any nextObj could be retrieved from one controller instance.
462 // Right now the drivers on one instance can only fetch for next-ids that came
463 // to them.
464 // Also, we still need to send the right next-id to the right driver as potentially
465 // there can be different drivers for different devices. But on that account,
466 // no instance should be decoding for another instance's nextIds.
467
468 for (Map.Entry<Integer, NextGroup> e : allnexts.entrySet()) {
469 // get the device this next Objective was sent to
470 DeviceId deviceId = nextToDevice.get(e.getKey());
471 mappings.add("NextId " + e.getKey() + ": " +
472 ((deviceId != null) ? deviceId : "nextId not in this onos instance"));
473 if (deviceId != null) {
474 // this instance of the controller sent the nextObj to a driver
475 Pipeliner pipeliner = getDevicePipeliner(deviceId);
476 List<String> nextMappings = pipeliner.getNextMappings(e.getValue());
477 if (nextMappings != null) {
478 mappings.addAll(nextMappings);
479 }
480 }
481 }
482 return mappings;
483 }
Saurav Dasb5c236e2016-06-07 10:08:06 -0700484
485 @Override
486 public List<String> getPendingNexts() {
487 List<String> pendingNexts = new ArrayList<>();
488 for (Integer nextId : pendingForwards.keySet()) {
489 Set<PendingNext> pnext = pendingForwards.get(nextId);
Sho SHIMIZU81470a52016-08-12 17:24:55 -0700490 StringBuilder pend = new StringBuilder();
Saurav Dasb5c236e2016-06-07 10:08:06 -0700491 pend.append("Next Id: ").append(Integer.toString(nextId))
492 .append(" :: ");
493 for (PendingNext pn : pnext) {
494 pend.append(Integer.toString(pn.forwardingObjective().id()))
495 .append(" ");
496 }
497 pendingNexts.add(pend.toString());
498 }
499 return pendingNexts;
500 }
alshabib77b88482015-04-07 15:47:50 -0700501}