blob: 3e242d170fbda5fe6e4ca8b8b84c433049f5851b [file] [log] [blame]
alshabib77b88482015-04-07 15:47:50 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Yi Tseng374c5f32017-03-05 22:51:35 -080023import org.apache.felix.scr.annotations.Modified;
24import org.apache.felix.scr.annotations.Property;
alshabib77b88482015-04-07 15:47:50 -070025import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.apache.felix.scr.annotations.Service;
28import org.onlab.osgi.DefaultServiceDirectory;
29import org.onlab.osgi.ServiceDirectory;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070030import org.onlab.util.ItemNotFoundException;
Yi Tseng374c5f32017-03-05 22:51:35 -080031import org.onlab.util.Tools;
32import org.onosproject.cfg.ComponentConfigService;
alshabib77b88482015-04-07 15:47:50 -070033import org.onosproject.cluster.ClusterService;
alshabib77b88482015-04-07 15:47:50 -070034import org.onosproject.net.DeviceId;
Saurav Das24431192016-03-07 19:13:00 -080035import org.onosproject.net.behaviour.NextGroup;
alshabib77b88482015-04-07 15:47:50 -070036import org.onosproject.net.behaviour.Pipeliner;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070037import org.onosproject.net.behaviour.PipelinerContext;
alshabibaebe7752015-04-07 17:45:42 -070038import org.onosproject.net.device.DeviceEvent;
39import org.onosproject.net.device.DeviceListener;
alshabib77b88482015-04-07 15:47:50 -070040import org.onosproject.net.device.DeviceService;
alshabib77b88482015-04-07 15:47:50 -070041import org.onosproject.net.driver.DriverHandler;
42import org.onosproject.net.driver.DriverService;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070043import org.onosproject.net.flow.FlowRuleService;
alshabib77b88482015-04-07 15:47:50 -070044import org.onosproject.net.flowobjective.FilteringObjective;
45import org.onosproject.net.flowobjective.FlowObjectiveService;
alshabib2a441c62015-04-13 18:39:38 -070046import org.onosproject.net.flowobjective.FlowObjectiveStore;
47import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate;
alshabib77b88482015-04-07 15:47:50 -070048import org.onosproject.net.flowobjective.ForwardingObjective;
49import org.onosproject.net.flowobjective.NextObjective;
alshabib910aff12015-04-09 16:55:57 -070050import org.onosproject.net.flowobjective.Objective;
Saurav Das1547b3f2017-05-05 17:01:08 -070051import org.onosproject.net.flowobjective.Objective.Operation;
Jonathan Hart17d00452015-04-21 17:10:00 -070052import org.onosproject.net.flowobjective.ObjectiveError;
alshabib2a441c62015-04-13 18:39:38 -070053import org.onosproject.net.flowobjective.ObjectiveEvent;
Saurav Das423fe2b2015-12-04 10:52:59 -080054import org.onosproject.net.flowobjective.ObjectiveEvent.Type;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070055import org.onosproject.net.group.GroupService;
Yi Tseng374c5f32017-03-05 22:51:35 -080056import org.osgi.service.component.ComponentContext;
alshabib77b88482015-04-07 15:47:50 -070057import org.slf4j.Logger;
58import org.slf4j.LoggerFactory;
59
Saurav Das24431192016-03-07 19:13:00 -080060import java.util.ArrayList;
Saurav Das24431192016-03-07 19:13:00 -080061import java.util.List;
alshabib77b88482015-04-07 15:47:50 -070062import java.util.Map;
Saurav Das8a0732e2015-11-20 15:27:53 -080063import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070064import java.util.Set;
Jonathan Hart17d00452015-04-21 17:10:00 -070065import java.util.concurrent.ExecutorService;
alshabib77b88482015-04-07 15:47:50 -070066
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -070067import static com.google.common.base.Preconditions.checkNotNull;
Yi Tseng374c5f32017-03-05 22:51:35 -080068import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuska866b46a2015-04-30 00:26:55 -070069import static java.util.concurrent.Executors.newFixedThreadPool;
Jonathan Hart17d00452015-04-21 17:10:00 -070070import static org.onlab.util.Tools.groupedThreads;
Changhoon Yoon541ef712015-05-23 17:18:34 +090071import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskad27097c2016-06-14 19:10:41 -070072import static org.onosproject.security.AppPermission.Type.FLOWRULE_WRITE;
alshabib77b88482015-04-07 15:47:50 -070073
74/**
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070075 * Provides implementation of the flow objective programming service.
alshabib77b88482015-04-07 15:47:50 -070076 */
alshabib2a441c62015-04-13 18:39:38 -070077@Component(immediate = true)
alshabib77b88482015-04-07 15:47:50 -070078@Service
79public class FlowObjectiveManager implements FlowObjectiveService {
80
Saurav Dasbd7f7422015-04-23 16:31:47 -070081 public static final int INSTALL_RETRY_ATTEMPTS = 5;
Jonathan Hart17d00452015-04-21 17:10:00 -070082 public static final long INSTALL_RETRY_INTERVAL = 1000; // ms
alshabib77b88482015-04-07 15:47:50 -070083
Yi Tseng374c5f32017-03-05 22:51:35 -080084 private static final String WORKER_PATTERN = "objective-installer-%d";
85 private static final String GROUP_THREAD_NAME = "onos/objective-installer";
86 private static final String NUM_THREAD = "numThreads";
87
Jonathan Hart17d00452015-04-21 17:10:00 -070088 private final Logger log = LoggerFactory.getLogger(getClass());
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070089
Yi Tseng9753fc12017-03-10 18:42:46 -080090 private static final int DEFAULT_NUM_THREADS = 4;
Yi Tseng374c5f32017-03-05 22:51:35 -080091 @Property(name = NUM_THREAD,
92 intValue = DEFAULT_NUM_THREADS,
93 label = "Number of worker threads")
94 private int numThreads = DEFAULT_NUM_THREADS;
95
alshabib77b88482015-04-07 15:47:50 -070096 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
97 protected DriverService driverService;
98
99 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected DeviceService deviceService;
101
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib77b88482015-04-07 15:47:50 -0700103 protected ClusterService clusterService;
104
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700105 // Note: The following dependencies are added on behalf of the pipeline
106 // driver behaviours to assure these services are available for their
107 // initialization.
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected FlowRuleService flowRuleService;
110
111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 protected GroupService groupService;
113
alshabib2a441c62015-04-13 18:39:38 -0700114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
115 protected FlowObjectiveStore flowObjectiveStore;
116
Yi Tseng374c5f32017-03-05 22:51:35 -0800117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
118 protected ComponentConfigService cfgService;
119
alshabib2a441c62015-04-13 18:39:38 -0700120 private final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700121
122 private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap();
alshabib910aff12015-04-09 16:55:57 -0700123 private final Map<DeviceId, Pipeliner> pipeliners = Maps.newConcurrentMap();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700124
125 private final PipelinerContext context = new InnerPipelineContext();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700126 private final DeviceListener deviceListener = new InnerDeviceListener();
127
alshabib77b88482015-04-07 15:47:50 -0700128 protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
alshabib910aff12015-04-09 16:55:57 -0700129
Saurav Das1547b3f2017-05-05 17:01:08 -0700130 // local stores for queuing fwd and next objectives that are waiting for an
131 // associated next objective execution to complete. The signal for completed
132 // execution comes from a pipeline driver, in this or another controller
133 // instance, via the DistributedFlowObjectiveStore.
134 private final Map<Integer, Set<PendingFlowObjective>> pendingForwards =
135 Maps.newConcurrentMap();
136 private final Map<Integer, Set<PendingFlowObjective>> pendingNexts =
137 Maps.newConcurrentMap();
alshabib2a441c62015-04-13 18:39:38 -0700138
Saurav Das24431192016-03-07 19:13:00 -0800139 // local store to track which nextObjectives were sent to which device
140 // for debugging purposes
141 private Map<Integer, DeviceId> nextToDevice = Maps.newConcurrentMap();
142
Jonathan Hart17d00452015-04-21 17:10:00 -0700143 private ExecutorService executorService;
alshabib2a441c62015-04-13 18:39:38 -0700144
alshabib77b88482015-04-07 15:47:50 -0700145 @Activate
146 protected void activate() {
Yi Tseng374c5f32017-03-05 22:51:35 -0800147 cfgService.registerProperties(getClass());
148 executorService = newFixedThreadPool(numThreads,
149 groupedThreads(GROUP_THREAD_NAME, WORKER_PATTERN, log));
alshabib2a441c62015-04-13 18:39:38 -0700150 flowObjectiveStore.setDelegate(delegate);
alshabibaebe7752015-04-07 17:45:42 -0700151 deviceService.addListener(deviceListener);
alshabib77b88482015-04-07 15:47:50 -0700152 log.info("Started");
153 }
154
155 @Deactivate
156 protected void deactivate() {
Yi Tseng374c5f32017-03-05 22:51:35 -0800157 cfgService.unregisterProperties(getClass(), false);
alshabib2a441c62015-04-13 18:39:38 -0700158 flowObjectiveStore.unsetDelegate(delegate);
alshabibaebe7752015-04-07 17:45:42 -0700159 deviceService.removeListener(deviceListener);
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700160 executorService.shutdown();
161 pipeliners.clear();
162 driverHandlers.clear();
Saurav Das24431192016-03-07 19:13:00 -0800163 nextToDevice.clear();
alshabib77b88482015-04-07 15:47:50 -0700164 log.info("Stopped");
165 }
166
Yi Tseng374c5f32017-03-05 22:51:35 -0800167 @Modified
168 protected void modified(ComponentContext context) {
169 String propertyValue =
170 Tools.get(context.getProperties(), NUM_THREAD);
171 int newNumThreads = isNullOrEmpty(propertyValue) ? numThreads : Integer.parseInt(propertyValue);
172
173 if (newNumThreads != numThreads && newNumThreads > 0) {
174 numThreads = newNumThreads;
175 ExecutorService oldWorkerExecutor = executorService;
176 executorService = newFixedThreadPool(numThreads,
177 groupedThreads(GROUP_THREAD_NAME, WORKER_PATTERN, log));
178 if (oldWorkerExecutor != null) {
179 oldWorkerExecutor.shutdown();
180 }
181 log.info("Reconfigured number of worker threads to {}", numThreads);
182 }
183 }
184
Jonathan Hart17d00452015-04-21 17:10:00 -0700185 /**
186 * Task that passes the flow objective down to the driver. The task will
187 * make a few attempts to find the appropriate driver, then eventually give
188 * up and report an error if no suitable driver could be found.
189 */
190 private class ObjectiveInstaller implements Runnable {
191 private final DeviceId deviceId;
192 private final Objective objective;
193
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700194 private final int numAttempts;
Jonathan Hart17d00452015-04-21 17:10:00 -0700195
196 public ObjectiveInstaller(DeviceId deviceId, Objective objective) {
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700197 this(deviceId, objective, 1);
198 }
199
200 public ObjectiveInstaller(DeviceId deviceId, Objective objective, int attemps) {
201 this.deviceId = checkNotNull(deviceId);
202 this.objective = checkNotNull(objective);
203 this.numAttempts = checkNotNull(attemps);
alshabib910aff12015-04-09 16:55:57 -0700204 }
alshabib77b88482015-04-07 15:47:50 -0700205
Jonathan Hart17d00452015-04-21 17:10:00 -0700206 @Override
207 public void run() {
208 try {
Jonathan Hart17d00452015-04-21 17:10:00 -0700209 Pipeliner pipeliner = getDevicePipeliner(deviceId);
210
211 if (pipeliner != null) {
212 if (objective instanceof NextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700213 nextToDevice.put(objective.id(), deviceId);
Jonathan Hart17d00452015-04-21 17:10:00 -0700214 pipeliner.next((NextObjective) objective);
215 } else if (objective instanceof ForwardingObjective) {
216 pipeliner.forward((ForwardingObjective) objective);
217 } else {
218 pipeliner.filter((FilteringObjective) objective);
219 }
Andrea Campanella1f8188d2016-02-29 13:24:54 -0800220 //Attempts to check if pipeliner is null for retry attempts
Jonathan Hart17d00452015-04-21 17:10:00 -0700221 } else if (numAttempts < INSTALL_RETRY_ATTEMPTS) {
Saurav Das3d038262015-04-23 12:36:58 -0700222 Thread.sleep(INSTALL_RETRY_INTERVAL);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700223 executorService.execute(new ObjectiveInstaller(deviceId, objective, numAttempts + 1));
Jonathan Hart17d00452015-04-21 17:10:00 -0700224 } else {
225 // Otherwise we've tried a few times and failed, report an
226 // error back to the user.
227 objective.context().ifPresent(
Andrea Campanella1f8188d2016-02-29 13:24:54 -0800228 c -> c.onError(objective, ObjectiveError.NOPIPELINER));
Jonathan Hart17d00452015-04-21 17:10:00 -0700229 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700230 //Exception thrown
Jonathan Hart17d00452015-04-21 17:10:00 -0700231 } catch (Exception e) {
232 log.warn("Exception while installing flow objective", e);
233 }
234 }
235 }
236
237 @Override
238 public void filter(DeviceId deviceId, FilteringObjective filteringObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900239 checkPermission(FLOWRULE_WRITE);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700240 executorService.execute(new ObjectiveInstaller(deviceId, filteringObjective));
alshabib77b88482015-04-07 15:47:50 -0700241 }
242
243 @Override
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700244 public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900245 checkPermission(FLOWRULE_WRITE);
Yi Tseng1666b502017-05-17 11:05:18 -0700246 if (forwardingObjective.nextId() == null ||
247 forwardingObjective.op() == Objective.Operation.REMOVE ||
248 flowObjectiveStore.getNextGroup(forwardingObjective.nextId()) != null ||
249 !queueFwdObjective(deviceId, forwardingObjective)) {
250 // fast path
251 executorService.execute(new ObjectiveInstaller(deviceId, forwardingObjective));
alshabib910aff12015-04-09 16:55:57 -0700252 }
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700253 }
254
alshabib2a441c62015-04-13 18:39:38 -0700255 @Override
Jonathan Hart17d00452015-04-21 17:10:00 -0700256 public void next(DeviceId deviceId, NextObjective nextObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900257 checkPermission(FLOWRULE_WRITE);
Yi Tseng1666b502017-05-17 11:05:18 -0700258 if (nextObjective.op() == Operation.ADD ||
259 flowObjectiveStore.getNextGroup(nextObjective.id()) != null ||
260 !queueNextObjective(deviceId, nextObjective)) {
261 // either group exists or we are trying to create it - let it through
262 executorService.execute(new ObjectiveInstaller(deviceId, nextObjective));
Saurav Das1547b3f2017-05-05 17:01:08 -0700263 }
alshabib2a441c62015-04-13 18:39:38 -0700264 }
265
alshabibf6ea9e62015-04-21 17:08:26 -0700266 @Override
267 public int allocateNextId() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900268 checkPermission(FLOWRULE_WRITE);
alshabibf6ea9e62015-04-21 17:08:26 -0700269 return flowObjectiveStore.allocateNextId();
270 }
271
Xin Jin313708b2015-07-09 13:43:04 -0700272 @Override
273 public void initPolicy(String policy) {}
274
Saurav Das1547b3f2017-05-05 17:01:08 -0700275 private boolean queueFwdObjective(DeviceId deviceId, ForwardingObjective fwd) {
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700276 boolean queued = false;
277 synchronized (pendingForwards) {
278 // double check the flow objective store, because this block could run
279 // after a notification arrives
280 if (flowObjectiveStore.getNextGroup(fwd.nextId()) == null) {
281 pendingForwards.compute(fwd.nextId(), (id, pending) -> {
Saurav Das1547b3f2017-05-05 17:01:08 -0700282 PendingFlowObjective pendfo = new PendingFlowObjective(deviceId, fwd);
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700283 if (pending == null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700284 return Sets.newHashSet(pendfo);
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700285 } else {
Saurav Das1547b3f2017-05-05 17:01:08 -0700286 pending.add(pendfo);
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700287 return pending;
288 }
289 });
290 queued = true;
291 }
292 }
293 if (queued) {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530294 log.info("Queued forwarding objective {} for nextId {} meant for device {}",
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700295 fwd.id(), fwd.nextId(), deviceId);
296 }
297 return queued;
alshabib2a441c62015-04-13 18:39:38 -0700298 }
299
Saurav Das1547b3f2017-05-05 17:01:08 -0700300 private boolean queueNextObjective(DeviceId deviceId, NextObjective next) {
Yi Tseng1666b502017-05-17 11:05:18 -0700301
Saurav Das1547b3f2017-05-05 17:01:08 -0700302 // we need to hold off on other operations till we get notified that the
303 // initial group creation has succeeded
304 boolean queued = false;
305 synchronized (pendingNexts) {
306 // double check the flow objective store, because this block could run
307 // after a notification arrives
308 if (flowObjectiveStore.getNextGroup(next.id()) == null) {
309 pendingNexts.compute(next.id(), (id, pending) -> {
310 PendingFlowObjective pendfo = new PendingFlowObjective(deviceId, next);
311 if (pending == null) {
312 return Sets.newHashSet(pendfo);
313 } else {
314 pending.add(pendfo);
315 return pending;
316 }
317 });
318 queued = true;
319 }
320 }
321 if (queued) {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530322 log.info("Queued next objective {} with operation {} meant for device {}",
Saurav Das1547b3f2017-05-05 17:01:08 -0700323 next.id(), next.op(), deviceId);
324 }
325 return queued;
326 }
327
Pier Ventre57a61cd2016-09-07 10:55:41 -0700328 /**
329 * Retrieves (if it exists) the device pipeline behaviour from the cache.
330 * Otherwise it warms the caches and triggers the init method of the Pipeline.
331 *
332 * @param deviceId the id of the device associated to the pipeline
333 * @return the implementation of the Pipeliner behaviour
334 */
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700335 private Pipeliner getDevicePipeliner(DeviceId deviceId) {
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700336 return pipeliners.computeIfAbsent(deviceId, this::initPipelineHandler);
alshabib77b88482015-04-07 15:47:50 -0700337 }
338
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700339 /**
Pier Ventre57a61cd2016-09-07 10:55:41 -0700340 * Retrieves (if it exists) the device pipeline behaviour from the cache and
341 * and triggers the init method of the pipeline. Otherwise (DEVICE_ADDED) it warms
342 * the caches and triggers the init method of the Pipeline. The rationale of this
343 * method is for managing the scenario of a switch that goes down for a failure
344 * and goes up after a while.
345 *
346 * @param deviceId the id of the device associated to the pipeline
347 * @return the implementation of the Pipeliner behaviour
348 */
349 private Pipeliner getAndInitDevicePipeliner(DeviceId deviceId) {
350 return pipeliners.compute(deviceId, (deviceIdValue, pipelinerValue) -> {
351 if (pipelinerValue != null) {
352 pipelinerValue.init(deviceId, context);
353 return pipelinerValue;
354 }
355 return this.initPipelineHandler(deviceId);
356 });
357 }
358
359 /**
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700360 * Creates and initialize {@link Pipeliner}.
361 * <p>
362 * Note: Expected to be called under per-Device lock.
363 * e.g., {@code pipeliners}' Map#compute family methods
364 *
365 * @param deviceId Device to initialize pipeliner
366 * @return {@link Pipeliner} instance or null
367 */
368 private Pipeliner initPipelineHandler(DeviceId deviceId) {
369 start = now();
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700370
Jonathan Hart17d00452015-04-21 17:10:00 -0700371 // Attempt to lookup the handler in the cache
372 DriverHandler handler = driverHandlers.get(deviceId);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700373 cTime = now();
374
Jonathan Hart17d00452015-04-21 17:10:00 -0700375 if (handler == null) {
376 try {
377 // Otherwise create it and if it has pipeline behaviour, cache it
378 handler = driverService.createHandler(deviceId);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700379 dTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700380 if (!handler.driver().hasBehaviour(Pipeliner.class)) {
Yuta HIGUCHIa2a4f342017-03-17 11:38:57 -0700381 log.debug("Pipeline behaviour not supported for device {}",
Jonathan Hart17d00452015-04-21 17:10:00 -0700382 deviceId);
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700383 return null;
alshabib2a441c62015-04-13 18:39:38 -0700384 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700385 } catch (ItemNotFoundException e) {
386 log.warn("No applicable driver for device {}", deviceId);
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700387 return null;
alshabib2a441c62015-04-13 18:39:38 -0700388 }
389
Jonathan Hart17d00452015-04-21 17:10:00 -0700390 driverHandlers.put(deviceId, handler);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700391 eTime = now();
alshabib2a441c62015-04-13 18:39:38 -0700392 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700393
394 // Always (re)initialize the pipeline behaviour
395 log.info("Driver {} bound to device {} ... initializing driver",
396 handler.driver().name(), deviceId);
Thomas Vachuska0121a612015-07-21 11:18:09 -0700397 hTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700398 Pipeliner pipeliner = handler.behaviour(Pipeliner.class);
Thomas Vachuska94c3cf42015-07-20 13:01:12 -0700399 hbTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700400 pipeliner.init(deviceId, context);
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700401 stopWatch();
402 return pipeliner;
alshabibaebe7752015-04-07 17:45:42 -0700403 }
alshabib77b88482015-04-07 15:47:50 -0700404
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700405 // Triggers driver setup when a device is (re)detected.
alshabibaebe7752015-04-07 17:45:42 -0700406 private class InnerDeviceListener implements DeviceListener {
407 @Override
408 public void event(DeviceEvent event) {
409 switch (event.type()) {
410 case DEVICE_ADDED:
411 case DEVICE_AVAILABILITY_CHANGED:
Madan Jampani0174f452015-05-29 11:52:05 -0700412 log.debug("Device either added or availability changed {}",
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700413 event.subject().id());
alshabib4313d102015-04-08 18:55:08 -0700414 if (deviceService.isAvailable(event.subject().id())) {
Madan Jampani0174f452015-05-29 11:52:05 -0700415 log.debug("Device is now available {}", event.subject().id());
Pier Ventre57a61cd2016-09-07 10:55:41 -0700416 getAndInitDevicePipeliner(event.subject().id());
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700417 } else {
418 log.debug("Device is no longer available {}", event.subject().id());
alshabib4313d102015-04-08 18:55:08 -0700419 }
420 break;
421 case DEVICE_UPDATED:
422 break;
423 case DEVICE_REMOVED:
Yuta HIGUCHIad0c9902016-08-23 10:37:32 -0700424 // evict Pipeliner and Handler cache, when
425 // the Device was administratively removed.
426 //
427 // System expect the user to clear all existing flows,
428 // before removing device, especially if they intend to
429 // replace driver/pipeliner assigned to the device.
430 driverHandlers.remove(event.subject().id());
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700431 pipeliners.remove(event.subject().id());
alshabib4313d102015-04-08 18:55:08 -0700432 break;
Yuta HIGUCHIad0c9902016-08-23 10:37:32 -0700433 case DEVICE_SUSPENDED:
434 break;
alshabib4313d102015-04-08 18:55:08 -0700435 case PORT_ADDED:
436 break;
437 case PORT_UPDATED:
438 break;
439 case PORT_REMOVED:
alshabibaebe7752015-04-07 17:45:42 -0700440 break;
441 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700442 break;
alshabibaebe7752015-04-07 17:45:42 -0700443 }
alshabib77b88482015-04-07 15:47:50 -0700444 }
445 }
alshabibaebe7752015-04-07 17:45:42 -0700446
Thomas Vachuska174bb912015-07-16 21:27:14 -0700447 // Temporary mechanism to monitor pipeliner setup time-cost; there are
448 // intermittent time where this takes in excess of 2 seconds. Why?
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700449 private long start = 0, totals = 0, count = 0;
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700450 private long cTime, dTime, eTime, hTime, hbTime;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700451 private static final long LIMIT = 500;
Thomas Vachuska174bb912015-07-16 21:27:14 -0700452
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700453 private long now() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700454 return System.currentTimeMillis();
455 }
456
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700457 private void stopWatch() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700458 long duration = System.currentTimeMillis() - start;
459 totals += duration;
460 count += 1;
461 if (duration > LIMIT) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700462 log.info("Pipeline setup took {} ms; avg {} ms; cTime={}, dTime={}, eTime={}, hTime={}, hbTime={}",
463 duration, totals / count, diff(cTime), diff(dTime), diff(eTime), diff(hTime), diff(hbTime));
Thomas Vachuska174bb912015-07-16 21:27:14 -0700464 }
465 }
466
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700467 private long diff(long bTime) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700468 long diff = bTime - start;
469 return diff < 0 ? 0 : diff;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700470 }
471
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700472 // Processing context for initializing pipeline driver behaviours.
473 private class InnerPipelineContext implements PipelinerContext {
474 @Override
475 public ServiceDirectory directory() {
476 return serviceDirectory;
alshabibaebe7752015-04-07 17:45:42 -0700477 }
alshabib2a441c62015-04-13 18:39:38 -0700478
479 @Override
480 public FlowObjectiveStore store() {
481 return flowObjectiveStore;
482 }
alshabib2a441c62015-04-13 18:39:38 -0700483 }
484
485 private class InternalStoreDelegate implements FlowObjectiveStoreDelegate {
486 @Override
487 public void notify(ObjectiveEvent event) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800488 if (event.type() == Type.ADD) {
489 log.debug("Received notification of obj event {}", event);
Saurav Das1547b3f2017-05-05 17:01:08 -0700490 Set<PendingFlowObjective> pending;
491
492 // first send all pending flows
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700493 synchronized (pendingForwards) {
494 // needs to be synchronized for queueObjective lookup
495 pending = pendingForwards.remove(event.subject());
496 }
Saurav Das423fe2b2015-12-04 10:52:59 -0800497 if (pending == null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700498 log.debug("No forwarding objectives pending for this "
499 + "obj event {}", event);
500 } else {
501 log.debug("Processing {} pending forwarding objectives for nextId {}",
502 pending.size(), event.subject());
503 pending.forEach(p -> getDevicePipeliner(p.deviceId())
504 .forward((ForwardingObjective) p.flowObjective()));
Saurav Das423fe2b2015-12-04 10:52:59 -0800505 }
506
Saurav Das1547b3f2017-05-05 17:01:08 -0700507 // now check for pending next-objectives
508 synchronized (pendingNexts) {
509 // needs to be synchronized for queueObjective lookup
510 pending = pendingNexts.remove(event.subject());
511 }
512 if (pending == null) {
513 log.debug("No next objectives pending for this "
514 + "obj event {}", event);
515 } else {
516 log.debug("Processing {} pending next objectives for nextId {}",
517 pending.size(), event.subject());
518 pending.forEach(p -> getDevicePipeliner(p.deviceId())
519 .next((NextObjective) p.flowObjective()));
520 }
alshabib2a441c62015-04-13 18:39:38 -0700521 }
alshabib2a441c62015-04-13 18:39:38 -0700522 }
523 }
524
525 /**
Saurav Das1547b3f2017-05-05 17:01:08 -0700526 * Data class used to hold a pending flow objective that could not
alshabib2a441c62015-04-13 18:39:38 -0700527 * be processed because the associated next object was not present.
Saurav Das1547b3f2017-05-05 17:01:08 -0700528 * Note that this pending flow objective could be a forwarding objective
529 * waiting for a next objective to complete execution. Or it could a
530 * next objective (with a different operation - remove, addToExisting, or
531 * removeFromExisting) waiting for a next objective with the same id to
532 * complete execution.
alshabib2a441c62015-04-13 18:39:38 -0700533 */
Saurav Das1547b3f2017-05-05 17:01:08 -0700534 private class PendingFlowObjective {
alshabib2a441c62015-04-13 18:39:38 -0700535 private final DeviceId deviceId;
Saurav Das1547b3f2017-05-05 17:01:08 -0700536 private final Objective flowObj;
alshabib2a441c62015-04-13 18:39:38 -0700537
Saurav Das1547b3f2017-05-05 17:01:08 -0700538 public PendingFlowObjective(DeviceId deviceId, Objective flowObj) {
alshabib2a441c62015-04-13 18:39:38 -0700539 this.deviceId = deviceId;
Saurav Das1547b3f2017-05-05 17:01:08 -0700540 this.flowObj = flowObj;
alshabib2a441c62015-04-13 18:39:38 -0700541 }
542
543 public DeviceId deviceId() {
544 return deviceId;
545 }
546
Saurav Das1547b3f2017-05-05 17:01:08 -0700547 public Objective flowObjective() {
548 return flowObj;
alshabib2a441c62015-04-13 18:39:38 -0700549 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800550
551 @Override
552 public int hashCode() {
Saurav Das1547b3f2017-05-05 17:01:08 -0700553 return Objects.hash(deviceId, flowObj);
Saurav Das8a0732e2015-11-20 15:27:53 -0800554 }
555
556 @Override
557 public boolean equals(final Object obj) {
558 if (this == obj) {
559 return true;
560 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700561 if (!(obj instanceof PendingFlowObjective)) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800562 return false;
563 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700564 final PendingFlowObjective other = (PendingFlowObjective) obj;
Saurav Das8a0732e2015-11-20 15:27:53 -0800565 if (this.deviceId.equals(other.deviceId) &&
Saurav Das1547b3f2017-05-05 17:01:08 -0700566 this.flowObj.equals(other.flowObj)) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800567 return true;
568 }
569 return false;
570 }
alshabibaebe7752015-04-07 17:45:42 -0700571 }
Saurav Das24431192016-03-07 19:13:00 -0800572
573 @Override
574 public List<String> getNextMappings() {
575 List<String> mappings = new ArrayList<>();
576 Map<Integer, NextGroup> allnexts = flowObjectiveStore.getAllGroups();
Saurav Das25190812016-05-27 13:54:07 -0700577 // XXX if the NextGroup after de-serialization actually stored info of the deviceId
Saurav Das24431192016-03-07 19:13:00 -0800578 // then info on any nextObj could be retrieved from one controller instance.
579 // Right now the drivers on one instance can only fetch for next-ids that came
580 // to them.
581 // Also, we still need to send the right next-id to the right driver as potentially
582 // there can be different drivers for different devices. But on that account,
583 // no instance should be decoding for another instance's nextIds.
584
585 for (Map.Entry<Integer, NextGroup> e : allnexts.entrySet()) {
586 // get the device this next Objective was sent to
587 DeviceId deviceId = nextToDevice.get(e.getKey());
588 mappings.add("NextId " + e.getKey() + ": " +
589 ((deviceId != null) ? deviceId : "nextId not in this onos instance"));
590 if (deviceId != null) {
591 // this instance of the controller sent the nextObj to a driver
592 Pipeliner pipeliner = getDevicePipeliner(deviceId);
593 List<String> nextMappings = pipeliner.getNextMappings(e.getValue());
594 if (nextMappings != null) {
595 mappings.addAll(nextMappings);
596 }
597 }
598 }
599 return mappings;
600 }
Saurav Dasb5c236e2016-06-07 10:08:06 -0700601
602 @Override
Saurav Das1547b3f2017-05-05 17:01:08 -0700603 public List<String> getPendingFlowObjectives() {
604 List<String> pendingFlowObjectives = new ArrayList<>();
Charles Chan54734712017-03-29 11:07:55 -0700605
Saurav Das1547b3f2017-05-05 17:01:08 -0700606 for (Integer nextId : pendingForwards.keySet()) {
607 Set<PendingFlowObjective> pfwd = pendingForwards.get(nextId);
Sho SHIMIZU81470a52016-08-12 17:24:55 -0700608 StringBuilder pend = new StringBuilder();
Charles Chan54734712017-03-29 11:07:55 -0700609 pend.append("NextId: ")
610 .append(nextId);
Saurav Das1547b3f2017-05-05 17:01:08 -0700611 for (PendingFlowObjective pf : pfwd) {
Charles Chan54734712017-03-29 11:07:55 -0700612 pend.append("\n FwdId: ")
Saurav Das1547b3f2017-05-05 17:01:08 -0700613 .append(String.format("%11s", pf.flowObjective().id()))
614 .append(", DeviceId: ")
615 .append(pf.deviceId())
616 .append(", Selector: ")
617 .append(((ForwardingObjective) pf.flowObjective())
618 .selector().criteria());
619 }
620 pendingFlowObjectives.add(pend.toString());
621 }
622
623 for (Integer nextId : pendingNexts.keySet()) {
624 Set<PendingFlowObjective> pnext = pendingNexts.get(nextId);
625 StringBuilder pend = new StringBuilder();
626 pend.append("NextId: ")
627 .append(nextId);
628 for (PendingFlowObjective pn : pnext) {
629 pend.append("\n NextOp: ")
630 .append(pn.flowObjective().op())
Charles Chan54734712017-03-29 11:07:55 -0700631 .append(", DeviceId: ")
632 .append(pn.deviceId())
Saurav Das1547b3f2017-05-05 17:01:08 -0700633 .append(", Treatments: ")
634 .append(((NextObjective) pn.flowObjective())
635 .next());
Saurav Dasb5c236e2016-06-07 10:08:06 -0700636 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700637 pendingFlowObjectives.add(pend.toString());
Saurav Dasb5c236e2016-06-07 10:08:06 -0700638 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700639
640 return pendingFlowObjectives;
641 }
642
643 @Override
644 public List<String> getPendingNexts() {
645 return getPendingFlowObjectives();
Saurav Dasb5c236e2016-06-07 10:08:06 -0700646 }
alshabib77b88482015-04-07 15:47:50 -0700647}