blob: 79d486ecdc76d35fa56db446a7e3b1c14def874e [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;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070030import org.onosproject.cluster.NodeId;
alshabib77b88482015-04-07 15:47:50 -070031import org.onosproject.mastership.MastershipEvent;
32import org.onosproject.mastership.MastershipListener;
33import org.onosproject.mastership.MastershipService;
alshabib77b88482015-04-07 15:47:50 -070034import org.onosproject.net.DeviceId;
35import org.onosproject.net.behaviour.Pipeliner;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070036import org.onosproject.net.behaviour.PipelinerContext;
alshabibaebe7752015-04-07 17:45:42 -070037import org.onosproject.net.device.DeviceEvent;
38import org.onosproject.net.device.DeviceListener;
alshabib77b88482015-04-07 15:47:50 -070039import org.onosproject.net.device.DeviceService;
alshabib77b88482015-04-07 15:47:50 -070040import org.onosproject.net.driver.DriverHandler;
41import org.onosproject.net.driver.DriverService;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070042import org.onosproject.net.flow.FlowRuleService;
alshabib77b88482015-04-07 15:47:50 -070043import org.onosproject.net.flowobjective.FilteringObjective;
44import org.onosproject.net.flowobjective.FlowObjectiveService;
alshabib2a441c62015-04-13 18:39:38 -070045import org.onosproject.net.flowobjective.FlowObjectiveStore;
46import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate;
alshabib77b88482015-04-07 15:47:50 -070047import org.onosproject.net.flowobjective.ForwardingObjective;
48import org.onosproject.net.flowobjective.NextObjective;
alshabib910aff12015-04-09 16:55:57 -070049import org.onosproject.net.flowobjective.Objective;
Jonathan Hart17d00452015-04-21 17:10:00 -070050import org.onosproject.net.flowobjective.ObjectiveError;
alshabib2a441c62015-04-13 18:39:38 -070051import org.onosproject.net.flowobjective.ObjectiveEvent;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070052import org.onosproject.net.group.GroupService;
alshabib77b88482015-04-07 15:47:50 -070053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
55
alshabib77b88482015-04-07 15:47:50 -070056import java.util.Map;
alshabib2a441c62015-04-13 18:39:38 -070057import java.util.Set;
Jonathan Hart17d00452015-04-21 17:10:00 -070058import java.util.concurrent.ExecutorService;
59import java.util.concurrent.Executors;
alshabib77b88482015-04-07 15:47:50 -070060
Jonathan Hart17d00452015-04-21 17:10:00 -070061import static org.onlab.util.Tools.groupedThreads;
alshabib77b88482015-04-07 15:47:50 -070062
63/**
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070064 * Provides implementation of the flow objective programming service.
alshabib77b88482015-04-07 15:47:50 -070065 */
alshabib2a441c62015-04-13 18:39:38 -070066@Component(immediate = true)
alshabib77b88482015-04-07 15:47:50 -070067@Service
68public class FlowObjectiveManager implements FlowObjectiveService {
69
Jonathan Hart17d00452015-04-21 17:10:00 -070070 public static final int INSTALL_RETRY_ATTEMPTS = 5;
71 public static final long INSTALL_RETRY_INTERVAL = 1000; // ms
alshabib77b88482015-04-07 15:47:50 -070072
Jonathan Hart17d00452015-04-21 17:10:00 -070073 private final Logger log = LoggerFactory.getLogger(getClass());
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070074
alshabib77b88482015-04-07 15:47:50 -070075 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected DriverService driverService;
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected DeviceService deviceService;
80
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected MastershipService mastershipService;
83
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected ClusterService clusterService;
86
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070087 // Note: The following dependencies are added on behalf of the pipeline
88 // driver behaviours to assure these services are available for their
89 // initialization.
90 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 protected FlowRuleService flowRuleService;
92
93 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
94 protected GroupService groupService;
95
alshabib2a441c62015-04-13 18:39:38 -070096 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
97 protected FlowObjectiveStore flowObjectiveStore;
98
99 private final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700100
101 private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap();
alshabib910aff12015-04-09 16:55:57 -0700102 private final Map<DeviceId, Pipeliner> pipeliners = Maps.newConcurrentMap();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700103
104 private final PipelinerContext context = new InnerPipelineContext();
105 private final MastershipListener mastershipListener = new InnerMastershipListener();
106 private final DeviceListener deviceListener = new InnerDeviceListener();
107
alshabib77b88482015-04-07 15:47:50 -0700108 protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
alshabib910aff12015-04-09 16:55:57 -0700109
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700110 private NodeId localNode;
alshabib77b88482015-04-07 15:47:50 -0700111
alshabib2a441c62015-04-13 18:39:38 -0700112 private Map<Integer, Set<PendingNext>> pendingForwards =
113 Maps.newConcurrentMap();
114
Jonathan Hart17d00452015-04-21 17:10:00 -0700115 private ExecutorService executorService;
alshabib2a441c62015-04-13 18:39:38 -0700116
alshabib77b88482015-04-07 15:47:50 -0700117 @Activate
118 protected void activate() {
Jonathan Hart17d00452015-04-21 17:10:00 -0700119 executorService = Executors.newFixedThreadPool(
120 4, groupedThreads("onos/objective-installer", "%d"));
121
alshabib2a441c62015-04-13 18:39:38 -0700122 flowObjectiveStore.setDelegate(delegate);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700123 localNode = clusterService.getLocalNode().id();
alshabib77b88482015-04-07 15:47:50 -0700124 mastershipService.addListener(mastershipListener);
alshabibaebe7752015-04-07 17:45:42 -0700125 deviceService.addListener(deviceListener);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700126 deviceService.getDevices().forEach(device -> setupPipelineHandler(device.id()));
alshabib77b88482015-04-07 15:47:50 -0700127 log.info("Started");
128 }
129
130 @Deactivate
131 protected void deactivate() {
alshabib2a441c62015-04-13 18:39:38 -0700132 flowObjectiveStore.unsetDelegate(delegate);
alshabib77b88482015-04-07 15:47:50 -0700133 mastershipService.removeListener(mastershipListener);
alshabibaebe7752015-04-07 17:45:42 -0700134 deviceService.removeListener(deviceListener);
alshabib77b88482015-04-07 15:47:50 -0700135 log.info("Stopped");
136 }
137
Jonathan Hart17d00452015-04-21 17:10:00 -0700138 /**
139 * Task that passes the flow objective down to the driver. The task will
140 * make a few attempts to find the appropriate driver, then eventually give
141 * up and report an error if no suitable driver could be found.
142 */
143 private class ObjectiveInstaller implements Runnable {
144 private final DeviceId deviceId;
145 private final Objective objective;
146
147 private int numAttempts = 0;
148
149 public ObjectiveInstaller(DeviceId deviceId, Objective objective) {
150 this.deviceId = deviceId;
151 this.objective = objective;
alshabib910aff12015-04-09 16:55:57 -0700152 }
alshabib77b88482015-04-07 15:47:50 -0700153
Jonathan Hart17d00452015-04-21 17:10:00 -0700154 @Override
155 public void run() {
156 try {
157 numAttempts++;
158
159 Pipeliner pipeliner = getDevicePipeliner(deviceId);
160
161 if (pipeliner != null) {
162 if (objective instanceof NextObjective) {
163 pipeliner.next((NextObjective) objective);
164 } else if (objective instanceof ForwardingObjective) {
165 pipeliner.forward((ForwardingObjective) objective);
166 } else {
167 pipeliner.filter((FilteringObjective) objective);
168 }
169 } else if (numAttempts < INSTALL_RETRY_ATTEMPTS) {
170 Thread.currentThread().sleep(INSTALL_RETRY_INTERVAL);
171 executorService.submit(this);
172 } else {
173 // Otherwise we've tried a few times and failed, report an
174 // error back to the user.
175 objective.context().ifPresent(
176 c -> c.onError(objective, ObjectiveError.DEVICEMISSING));
177 }
178 } catch (Exception e) {
179 log.warn("Exception while installing flow objective", e);
180 }
181 }
182 }
183
184 @Override
185 public void filter(DeviceId deviceId, FilteringObjective filteringObjective) {
186 executorService.submit(new ObjectiveInstaller(deviceId, filteringObjective));
alshabib77b88482015-04-07 15:47:50 -0700187 }
188
189 @Override
alshabib2a441c62015-04-13 18:39:38 -0700190 public void forward(DeviceId deviceId,
191 ForwardingObjective forwardingObjective) {
192
193 if (queueObjective(deviceId, forwardingObjective)) {
194 return;
alshabib910aff12015-04-09 16:55:57 -0700195 }
alshabib2a441c62015-04-13 18:39:38 -0700196
Jonathan Hart17d00452015-04-21 17:10:00 -0700197 executorService.submit(new ObjectiveInstaller(deviceId, forwardingObjective));
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700198 }
199
alshabib2a441c62015-04-13 18:39:38 -0700200 @Override
Jonathan Hart17d00452015-04-21 17:10:00 -0700201 public void next(DeviceId deviceId, NextObjective nextObjective) {
202 executorService.submit(new ObjectiveInstaller(deviceId, nextObjective));
alshabib2a441c62015-04-13 18:39:38 -0700203 }
204
alshabibf6ea9e62015-04-21 17:08:26 -0700205 @Override
206 public int allocateNextId() {
207 return flowObjectiveStore.allocateNextId();
208 }
209
alshabib2a441c62015-04-13 18:39:38 -0700210 private boolean queueObjective(DeviceId deviceId, ForwardingObjective fwd) {
211 if (fwd.nextId() != null &&
212 flowObjectiveStore.getNextGroup(fwd.nextId()) == null) {
Saurav Das3ea46622015-04-22 14:01:34 -0700213 log.trace("Queuing forwarding objective for nextId {}", fwd.nextId());
alshabib2a441c62015-04-13 18:39:38 -0700214 if (pendingForwards.putIfAbsent(fwd.nextId(),
215 Sets.newHashSet(new PendingNext(deviceId, fwd))) != null) {
216 Set<PendingNext> pending = pendingForwards.get(fwd.nextId());
217 pending.add(new PendingNext(deviceId, fwd));
218 }
219 return true;
220 }
221 return false;
222 }
223
alshabib910aff12015-04-09 16:55:57 -0700224 // Retrieves the device pipeline behaviour from the cache.
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700225 private Pipeliner getDevicePipeliner(DeviceId deviceId) {
alshabib910aff12015-04-09 16:55:57 -0700226 Pipeliner pipeliner = pipeliners.get(deviceId);
alshabib910aff12015-04-09 16:55:57 -0700227 return pipeliner;
alshabib77b88482015-04-07 15:47:50 -0700228 }
229
alshabib2a441c62015-04-13 18:39:38 -0700230 private void setupPipelineHandler(DeviceId deviceId) {
Jonathan Hart17d00452015-04-21 17:10:00 -0700231 // Attempt to lookup the handler in the cache
232 DriverHandler handler = driverHandlers.get(deviceId);
233 if (handler == null) {
234 try {
235 // Otherwise create it and if it has pipeline behaviour, cache it
236 handler = driverService.createHandler(deviceId);
237 if (!handler.driver().hasBehaviour(Pipeliner.class)) {
238 log.warn("Pipeline behaviour not supported for device {}",
239 deviceId);
alshabib2a441c62015-04-13 18:39:38 -0700240 return;
241 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700242 } catch (ItemNotFoundException e) {
243 log.warn("No applicable driver for device {}", deviceId);
244 return;
alshabib2a441c62015-04-13 18:39:38 -0700245 }
246
Jonathan Hart17d00452015-04-21 17:10:00 -0700247 driverHandlers.put(deviceId, handler);
alshabib2a441c62015-04-13 18:39:38 -0700248 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700249
250 // Always (re)initialize the pipeline behaviour
251 log.info("Driver {} bound to device {} ... initializing driver",
252 handler.driver().name(), deviceId);
253 Pipeliner pipeliner = handler.behaviour(Pipeliner.class);
254 pipeliner.init(deviceId, context);
255 pipeliners.putIfAbsent(deviceId, pipeliner);
alshabib2a441c62015-04-13 18:39:38 -0700256 }
alshabibaebe7752015-04-07 17:45:42 -0700257
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700258 // Triggers driver setup when the local node becomes a device master.
alshabib77b88482015-04-07 15:47:50 -0700259 private class InnerMastershipListener implements MastershipListener {
260 @Override
261 public void event(MastershipEvent event) {
262 switch (event.type()) {
alshabib77b88482015-04-07 15:47:50 -0700263 case MASTER_CHANGED:
Jonathan Hart17d00452015-04-21 17:10:00 -0700264 log.info("mastership changed on device {}", event.subject());
265 setupPipelineHandler(event.subject());
alshabib4313d102015-04-08 18:55:08 -0700266 break;
267 case BACKUPS_CHANGED:
alshabib77b88482015-04-07 15:47:50 -0700268 break;
269 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700270 break;
alshabib77b88482015-04-07 15:47:50 -0700271 }
272 }
alshabibaebe7752015-04-07 17:45:42 -0700273 }
alshabib77b88482015-04-07 15:47:50 -0700274
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700275 // Triggers driver setup when a device is (re)detected.
alshabibaebe7752015-04-07 17:45:42 -0700276 private class InnerDeviceListener implements DeviceListener {
277 @Override
278 public void event(DeviceEvent event) {
279 switch (event.type()) {
280 case DEVICE_ADDED:
Jonathan Hart17d00452015-04-21 17:10:00 -0700281 setupPipelineHandler(event.subject().id());
282 break;
alshabibaebe7752015-04-07 17:45:42 -0700283 case DEVICE_AVAILABILITY_CHANGED:
Saurav Das3ea46622015-04-22 14:01:34 -0700284 log.info("Device either added or availability changed {}",
285 event.subject().id());
alshabib4313d102015-04-08 18:55:08 -0700286 if (deviceService.isAvailable(event.subject().id())) {
Saurav Das3ea46622015-04-22 14:01:34 -0700287 log.info("Device is now available {}", event.subject().id());
alshabib4313d102015-04-08 18:55:08 -0700288 setupPipelineHandler(event.subject().id());
289 }
290 break;
291 case DEVICE_UPDATED:
292 break;
293 case DEVICE_REMOVED:
294 break;
295 case DEVICE_SUSPENDED:
296 break;
297 case PORT_ADDED:
298 break;
299 case PORT_UPDATED:
300 break;
301 case PORT_REMOVED:
alshabibaebe7752015-04-07 17:45:42 -0700302 break;
303 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700304 break;
alshabibaebe7752015-04-07 17:45:42 -0700305 }
alshabib77b88482015-04-07 15:47:50 -0700306 }
307 }
alshabibaebe7752015-04-07 17:45:42 -0700308
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700309 // Processing context for initializing pipeline driver behaviours.
310 private class InnerPipelineContext implements PipelinerContext {
311 @Override
312 public ServiceDirectory directory() {
313 return serviceDirectory;
alshabibaebe7752015-04-07 17:45:42 -0700314 }
alshabib2a441c62015-04-13 18:39:38 -0700315
316 @Override
317 public FlowObjectiveStore store() {
318 return flowObjectiveStore;
319 }
alshabib2a441c62015-04-13 18:39:38 -0700320 }
321
322 private class InternalStoreDelegate implements FlowObjectiveStoreDelegate {
323 @Override
324 public void notify(ObjectiveEvent event) {
Saurav Das3ea46622015-04-22 14:01:34 -0700325 log.debug("Received notification of obj event {}", event);
alshabib2a441c62015-04-13 18:39:38 -0700326 Set<PendingNext> pending = pendingForwards.remove(event.subject());
327
328 if (pending == null) {
Saurav Das3ea46622015-04-22 14:01:34 -0700329 log.debug("Nothing pending for this obj event");
alshabib2a441c62015-04-13 18:39:38 -0700330 return;
331 }
332
Saurav Das3ea46622015-04-22 14:01:34 -0700333 log.debug("Processing pending forwarding objectives {}", pending.size());
alshabib2a441c62015-04-13 18:39:38 -0700334
335 pending.forEach(p -> getDevicePipeliner(p.deviceId())
336 .forward(p.forwardingObjective()));
337
338 }
339 }
340
341 /**
342 * Data class used to hold a pending forwarding objective that could not
343 * be processed because the associated next object was not present.
344 */
345 private class PendingNext {
346 private final DeviceId deviceId;
347 private final ForwardingObjective fwd;
348
349 public PendingNext(DeviceId deviceId, ForwardingObjective fwd) {
350 this.deviceId = deviceId;
351 this.fwd = fwd;
352 }
353
354 public DeviceId deviceId() {
355 return deviceId;
356 }
357
358 public ForwardingObjective forwardingObjective() {
359 return fwd;
360 }
alshabibaebe7752015-04-07 17:45:42 -0700361 }
alshabib77b88482015-04-07 15:47:50 -0700362}