blob: 6b84bfa8f9307f07ed46bf1c2fe5b46ea3176b48 [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
alshabib910aff12015-04-09 16:55:57 -070018import com.google.common.collect.Lists;
alshabib77b88482015-04-07 15:47:50 -070019import com.google.common.collect.Maps;
alshabib2a441c62015-04-13 18:39:38 -070020import com.google.common.collect.Sets;
alshabib77b88482015-04-07 15:47:50 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
27import org.onlab.osgi.DefaultServiceDirectory;
28import org.onlab.osgi.ServiceDirectory;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070029import org.onlab.util.ItemNotFoundException;
alshabib77b88482015-04-07 15:47:50 -070030import org.onosproject.cluster.ClusterService;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070031import org.onosproject.cluster.NodeId;
alshabib77b88482015-04-07 15:47:50 -070032import org.onosproject.mastership.MastershipEvent;
33import org.onosproject.mastership.MastershipListener;
34import org.onosproject.mastership.MastershipService;
alshabib77b88482015-04-07 15:47:50 -070035import org.onosproject.net.DeviceId;
36import 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;
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
56import java.util.Collection;
alshabib910aff12015-04-09 16:55:57 -070057import java.util.Collections;
alshabib77b88482015-04-07 15:47:50 -070058import java.util.Map;
alshabib2a441c62015-04-13 18:39:38 -070059import java.util.Set;
alshabib77b88482015-04-07 15:47:50 -070060
61import static com.google.common.base.Preconditions.checkState;
62
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
70 private final Logger log = LoggerFactory.getLogger(getClass());
71
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070072 public static final String NOT_INITIALIZED = "Driver not initialized";
73
alshabib77b88482015-04-07 15:47:50 -070074 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected DriverService driverService;
76
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected DeviceService deviceService;
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected MastershipService mastershipService;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected ClusterService clusterService;
85
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070086 // Note: The following dependencies are added on behalf of the pipeline
87 // driver behaviours to assure these services are available for their
88 // initialization.
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected FlowRuleService flowRuleService;
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected GroupService groupService;
94
alshabib2a441c62015-04-13 18:39:38 -070095 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected FlowObjectiveStore flowObjectiveStore;
97
98 private final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070099
100 private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap();
alshabib910aff12015-04-09 16:55:57 -0700101 private final Map<DeviceId, Pipeliner> pipeliners = Maps.newConcurrentMap();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700102
103 private final PipelinerContext context = new InnerPipelineContext();
104 private final MastershipListener mastershipListener = new InnerMastershipListener();
105 private final DeviceListener deviceListener = new InnerDeviceListener();
106
alshabib77b88482015-04-07 15:47:50 -0700107 protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
alshabib910aff12015-04-09 16:55:57 -0700108
109 private final Map<DeviceId, Collection<Objective>> pendingObjectives =
110 Maps.newConcurrentMap();
alshabib2a441c62015-04-13 18:39:38 -0700111
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700112 private NodeId localNode;
alshabib77b88482015-04-07 15:47:50 -0700113
alshabib2a441c62015-04-13 18:39:38 -0700114 private Map<Integer, Set<PendingNext>> pendingForwards =
115 Maps.newConcurrentMap();
116
117
alshabib77b88482015-04-07 15:47:50 -0700118 @Activate
119 protected void activate() {
alshabib2a441c62015-04-13 18:39:38 -0700120 flowObjectiveStore.setDelegate(delegate);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700121 localNode = clusterService.getLocalNode().id();
alshabib77b88482015-04-07 15:47:50 -0700122 mastershipService.addListener(mastershipListener);
alshabibaebe7752015-04-07 17:45:42 -0700123 deviceService.addListener(deviceListener);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700124 deviceService.getDevices().forEach(device -> setupPipelineHandler(device.id()));
alshabib77b88482015-04-07 15:47:50 -0700125 log.info("Started");
126 }
127
128 @Deactivate
129 protected void deactivate() {
alshabib2a441c62015-04-13 18:39:38 -0700130 flowObjectiveStore.unsetDelegate(delegate);
alshabib77b88482015-04-07 15:47:50 -0700131 mastershipService.removeListener(mastershipListener);
alshabibaebe7752015-04-07 17:45:42 -0700132 deviceService.removeListener(deviceListener);
alshabib77b88482015-04-07 15:47:50 -0700133 log.info("Stopped");
134 }
135
136 @Override
alshabib2a441c62015-04-13 18:39:38 -0700137 public void filter(DeviceId deviceId,
138 FilteringObjective filteringObjective) {
alshabib910aff12015-04-09 16:55:57 -0700139 if (deviceService.isAvailable(deviceId)) {
alshabib2a441c62015-04-13 18:39:38 -0700140 getDevicePipeliner(deviceId).filter(filteringObjective);
alshabib910aff12015-04-09 16:55:57 -0700141 } else {
alshabib2a441c62015-04-13 18:39:38 -0700142 updatePendingMap(deviceId, filteringObjective);
alshabib910aff12015-04-09 16:55:57 -0700143 }
alshabib77b88482015-04-07 15:47:50 -0700144
alshabib77b88482015-04-07 15:47:50 -0700145 }
146
147 @Override
alshabib2a441c62015-04-13 18:39:38 -0700148 public void forward(DeviceId deviceId,
149 ForwardingObjective forwardingObjective) {
150
151 if (queueObjective(deviceId, forwardingObjective)) {
152 return;
alshabib910aff12015-04-09 16:55:57 -0700153 }
alshabib2a441c62015-04-13 18:39:38 -0700154
155 if (deviceService.isAvailable(deviceId)) {
156 getDevicePipeliner(deviceId).forward(forwardingObjective);
157 } else {
158 updatePendingMap(deviceId, forwardingObjective);
159 }
160
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700161 }
162
alshabib2a441c62015-04-13 18:39:38 -0700163 @Override
164 public void next(DeviceId deviceId,
165 NextObjective nextObjective) {
166 if (deviceService.isAvailable(deviceId)) {
167 getDevicePipeliner(deviceId).next(nextObjective);
168 } else {
169 updatePendingMap(deviceId, nextObjective);
170 }
171 }
172
alshabibf6ea9e62015-04-21 17:08:26 -0700173 @Override
174 public int allocateNextId() {
175 return flowObjectiveStore.allocateNextId();
176 }
177
alshabib2a441c62015-04-13 18:39:38 -0700178 private boolean queueObjective(DeviceId deviceId, ForwardingObjective fwd) {
179 if (fwd.nextId() != null &&
180 flowObjectiveStore.getNextGroup(fwd.nextId()) == null) {
181 log.warn("Queuing forwarding objective.");
182 if (pendingForwards.putIfAbsent(fwd.nextId(),
183 Sets.newHashSet(new PendingNext(deviceId, fwd))) != null) {
184 Set<PendingNext> pending = pendingForwards.get(fwd.nextId());
185 pending.add(new PendingNext(deviceId, fwd));
186 }
187 return true;
188 }
189 return false;
190 }
191
192
alshabib910aff12015-04-09 16:55:57 -0700193 private void updatePendingMap(DeviceId deviceId, Objective pending) {
194 if (pendingObjectives.putIfAbsent(deviceId, Lists.newArrayList(pending)) != null) {
195 Collection<Objective> objectives = pendingObjectives.get(deviceId);
196 objectives.add(pending);
197 }
198
199 }
200
201 // Retrieves the device pipeline behaviour from the cache.
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700202 private Pipeliner getDevicePipeliner(DeviceId deviceId) {
alshabib910aff12015-04-09 16:55:57 -0700203 Pipeliner pipeliner = pipeliners.get(deviceId);
204 checkState(pipeliner != null, NOT_INITIALIZED);
205 return pipeliner;
alshabib77b88482015-04-07 15:47:50 -0700206 }
207
alshabib2a441c62015-04-13 18:39:38 -0700208 private void setupPipelineHandler(DeviceId deviceId) {
209 if (localNode.equals(mastershipService.getMasterFor(deviceId))) {
210 // Attempt to lookup the handler in the cache
211 DriverHandler handler = driverHandlers.get(deviceId);
212 if (handler == null) {
213 try {
214 // Otherwise create it and if it has pipeline behaviour, cache it
215 handler = driverService.createHandler(deviceId);
216 if (!handler.driver().hasBehaviour(Pipeliner.class)) {
217 log.warn("Pipeline behaviour not supported for device {}",
218 deviceId);
219 return;
220 }
221 } catch (ItemNotFoundException e) {
222 log.warn("No applicable driver for device {}", deviceId);
223 return;
224 }
225 driverHandlers.put(deviceId, handler);
226 }
227
228 // Always (re)initialize the pipeline behaviour
229 Pipeliner pipeliner = handler.behaviour(Pipeliner.class);
230 pipeliner.init(deviceId, context);
231 pipeliners.putIfAbsent(deviceId, pipeliner);
232 log.info("Driver {} bound to device {}", handler.driver().name(), deviceId);
233 }
234 }
alshabibaebe7752015-04-07 17:45:42 -0700235
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700236 // Triggers driver setup when the local node becomes a device master.
alshabib77b88482015-04-07 15:47:50 -0700237 private class InnerMastershipListener implements MastershipListener {
238 @Override
239 public void event(MastershipEvent event) {
240 switch (event.type()) {
alshabib77b88482015-04-07 15:47:50 -0700241 case MASTER_CHANGED:
alshabib4313d102015-04-08 18:55:08 -0700242 if (event.roleInfo().master() != null) {
243 setupPipelineHandler(event.subject());
244 }
245 break;
246 case BACKUPS_CHANGED:
alshabib77b88482015-04-07 15:47:50 -0700247 break;
248 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700249 break;
alshabib77b88482015-04-07 15:47:50 -0700250 }
251 }
alshabibaebe7752015-04-07 17:45:42 -0700252 }
alshabib77b88482015-04-07 15:47:50 -0700253
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700254 // Triggers driver setup when a device is (re)detected.
alshabibaebe7752015-04-07 17:45:42 -0700255 private class InnerDeviceListener implements DeviceListener {
256 @Override
257 public void event(DeviceEvent event) {
258 switch (event.type()) {
259 case DEVICE_ADDED:
260 case DEVICE_AVAILABILITY_CHANGED:
alshabib4313d102015-04-08 18:55:08 -0700261 if (deviceService.isAvailable(event.subject().id())) {
262 setupPipelineHandler(event.subject().id());
alshabib910aff12015-04-09 16:55:57 -0700263 processPendingObjectives(event.subject().id());
alshabib4313d102015-04-08 18:55:08 -0700264 }
265 break;
266 case DEVICE_UPDATED:
267 break;
268 case DEVICE_REMOVED:
269 break;
270 case DEVICE_SUSPENDED:
271 break;
272 case PORT_ADDED:
273 break;
274 case PORT_UPDATED:
275 break;
276 case PORT_REMOVED:
alshabibaebe7752015-04-07 17:45:42 -0700277 break;
278 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700279 break;
alshabibaebe7752015-04-07 17:45:42 -0700280 }
alshabib77b88482015-04-07 15:47:50 -0700281 }
alshabib910aff12015-04-09 16:55:57 -0700282
283 private void processPendingObjectives(DeviceId deviceId) {
alshabib1097c972015-04-10 14:41:32 -0700284 pendingObjectives.getOrDefault(deviceId,
285 Collections.emptySet()).forEach(obj -> {
alshabib910aff12015-04-09 16:55:57 -0700286 if (obj instanceof NextObjective) {
alshabib2a441c62015-04-13 18:39:38 -0700287 next(deviceId, (NextObjective) obj);
alshabib910aff12015-04-09 16:55:57 -0700288 } else if (obj instanceof ForwardingObjective) {
alshabib2a441c62015-04-13 18:39:38 -0700289 forward(deviceId, (ForwardingObjective) obj);
alshabib910aff12015-04-09 16:55:57 -0700290 } else {
291 getDevicePipeliner(deviceId)
alshabib2a441c62015-04-13 18:39:38 -0700292 .filter((FilteringObjective) obj);
alshabib910aff12015-04-09 16:55:57 -0700293 }
294 });
295 }
alshabib77b88482015-04-07 15:47:50 -0700296 }
alshabibaebe7752015-04-07 17:45:42 -0700297
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700298 // Processing context for initializing pipeline driver behaviours.
299 private class InnerPipelineContext implements PipelinerContext {
300 @Override
301 public ServiceDirectory directory() {
302 return serviceDirectory;
alshabibaebe7752015-04-07 17:45:42 -0700303 }
alshabib2a441c62015-04-13 18:39:38 -0700304
305 @Override
306 public FlowObjectiveStore store() {
307 return flowObjectiveStore;
308 }
309
310
311 }
312
313 private class InternalStoreDelegate implements FlowObjectiveStoreDelegate {
314 @Override
315 public void notify(ObjectiveEvent event) {
316 Set<PendingNext> pending = pendingForwards.remove(event.subject());
317
318 if (pending == null) {
319 return;
320 }
321
322 log.info("Processing pending objectives {}", pending.size());
323
324 pending.forEach(p -> getDevicePipeliner(p.deviceId())
325 .forward(p.forwardingObjective()));
326
327 }
328 }
329
330 /**
331 * Data class used to hold a pending forwarding objective that could not
332 * be processed because the associated next object was not present.
333 */
334 private class PendingNext {
335 private final DeviceId deviceId;
336 private final ForwardingObjective fwd;
337
338 public PendingNext(DeviceId deviceId, ForwardingObjective fwd) {
339 this.deviceId = deviceId;
340 this.fwd = fwd;
341 }
342
343 public DeviceId deviceId() {
344 return deviceId;
345 }
346
347 public ForwardingObjective forwardingObjective() {
348 return fwd;
349 }
350
351
alshabibaebe7752015-04-07 17:45:42 -0700352 }
alshabib77b88482015-04-07 15:47:50 -0700353}