blob: 17b0aa75a311759cd85ce7cc4eaae063e7a52205 [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
173 private boolean queueObjective(DeviceId deviceId, ForwardingObjective fwd) {
174 if (fwd.nextId() != null &&
175 flowObjectiveStore.getNextGroup(fwd.nextId()) == null) {
176 log.warn("Queuing forwarding objective.");
177 if (pendingForwards.putIfAbsent(fwd.nextId(),
178 Sets.newHashSet(new PendingNext(deviceId, fwd))) != null) {
179 Set<PendingNext> pending = pendingForwards.get(fwd.nextId());
180 pending.add(new PendingNext(deviceId, fwd));
181 }
182 return true;
183 }
184 return false;
185 }
186
187
alshabib910aff12015-04-09 16:55:57 -0700188 private void updatePendingMap(DeviceId deviceId, Objective pending) {
189 if (pendingObjectives.putIfAbsent(deviceId, Lists.newArrayList(pending)) != null) {
190 Collection<Objective> objectives = pendingObjectives.get(deviceId);
191 objectives.add(pending);
192 }
193
194 }
195
196 // Retrieves the device pipeline behaviour from the cache.
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700197 private Pipeliner getDevicePipeliner(DeviceId deviceId) {
alshabib910aff12015-04-09 16:55:57 -0700198 Pipeliner pipeliner = pipeliners.get(deviceId);
199 checkState(pipeliner != null, NOT_INITIALIZED);
200 return pipeliner;
alshabib77b88482015-04-07 15:47:50 -0700201 }
202
alshabib2a441c62015-04-13 18:39:38 -0700203 private void setupPipelineHandler(DeviceId deviceId) {
204 if (localNode.equals(mastershipService.getMasterFor(deviceId))) {
205 // Attempt to lookup the handler in the cache
206 DriverHandler handler = driverHandlers.get(deviceId);
207 if (handler == null) {
208 try {
209 // Otherwise create it and if it has pipeline behaviour, cache it
210 handler = driverService.createHandler(deviceId);
211 if (!handler.driver().hasBehaviour(Pipeliner.class)) {
212 log.warn("Pipeline behaviour not supported for device {}",
213 deviceId);
214 return;
215 }
216 } catch (ItemNotFoundException e) {
217 log.warn("No applicable driver for device {}", deviceId);
218 return;
219 }
220 driverHandlers.put(deviceId, handler);
221 }
222
223 // Always (re)initialize the pipeline behaviour
224 Pipeliner pipeliner = handler.behaviour(Pipeliner.class);
225 pipeliner.init(deviceId, context);
226 pipeliners.putIfAbsent(deviceId, pipeliner);
227 log.info("Driver {} bound to device {}", handler.driver().name(), deviceId);
228 }
229 }
alshabibaebe7752015-04-07 17:45:42 -0700230
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700231 // Triggers driver setup when the local node becomes a device master.
alshabib77b88482015-04-07 15:47:50 -0700232 private class InnerMastershipListener implements MastershipListener {
233 @Override
234 public void event(MastershipEvent event) {
235 switch (event.type()) {
alshabib77b88482015-04-07 15:47:50 -0700236 case MASTER_CHANGED:
alshabib4313d102015-04-08 18:55:08 -0700237 if (event.roleInfo().master() != null) {
238 setupPipelineHandler(event.subject());
239 }
240 break;
241 case BACKUPS_CHANGED:
alshabib77b88482015-04-07 15:47:50 -0700242 break;
243 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700244 break;
alshabib77b88482015-04-07 15:47:50 -0700245 }
246 }
alshabibaebe7752015-04-07 17:45:42 -0700247 }
alshabib77b88482015-04-07 15:47:50 -0700248
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700249 // Triggers driver setup when a device is (re)detected.
alshabibaebe7752015-04-07 17:45:42 -0700250 private class InnerDeviceListener implements DeviceListener {
251 @Override
252 public void event(DeviceEvent event) {
253 switch (event.type()) {
254 case DEVICE_ADDED:
255 case DEVICE_AVAILABILITY_CHANGED:
alshabib4313d102015-04-08 18:55:08 -0700256 if (deviceService.isAvailable(event.subject().id())) {
257 setupPipelineHandler(event.subject().id());
alshabib910aff12015-04-09 16:55:57 -0700258 processPendingObjectives(event.subject().id());
alshabib4313d102015-04-08 18:55:08 -0700259 }
260 break;
261 case DEVICE_UPDATED:
262 break;
263 case DEVICE_REMOVED:
264 break;
265 case DEVICE_SUSPENDED:
266 break;
267 case PORT_ADDED:
268 break;
269 case PORT_UPDATED:
270 break;
271 case PORT_REMOVED:
alshabibaebe7752015-04-07 17:45:42 -0700272 break;
273 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700274 break;
alshabibaebe7752015-04-07 17:45:42 -0700275 }
alshabib77b88482015-04-07 15:47:50 -0700276 }
alshabib910aff12015-04-09 16:55:57 -0700277
278 private void processPendingObjectives(DeviceId deviceId) {
alshabib1097c972015-04-10 14:41:32 -0700279 pendingObjectives.getOrDefault(deviceId,
280 Collections.emptySet()).forEach(obj -> {
alshabib910aff12015-04-09 16:55:57 -0700281 if (obj instanceof NextObjective) {
alshabib2a441c62015-04-13 18:39:38 -0700282 next(deviceId, (NextObjective) obj);
alshabib910aff12015-04-09 16:55:57 -0700283 } else if (obj instanceof ForwardingObjective) {
alshabib2a441c62015-04-13 18:39:38 -0700284 forward(deviceId, (ForwardingObjective) obj);
alshabib910aff12015-04-09 16:55:57 -0700285 } else {
286 getDevicePipeliner(deviceId)
alshabib2a441c62015-04-13 18:39:38 -0700287 .filter((FilteringObjective) obj);
alshabib910aff12015-04-09 16:55:57 -0700288 }
289 });
290 }
alshabib77b88482015-04-07 15:47:50 -0700291 }
alshabibaebe7752015-04-07 17:45:42 -0700292
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700293 // Processing context for initializing pipeline driver behaviours.
294 private class InnerPipelineContext implements PipelinerContext {
295 @Override
296 public ServiceDirectory directory() {
297 return serviceDirectory;
alshabibaebe7752015-04-07 17:45:42 -0700298 }
alshabib2a441c62015-04-13 18:39:38 -0700299
300 @Override
301 public FlowObjectiveStore store() {
302 return flowObjectiveStore;
303 }
304
305
306 }
307
308 private class InternalStoreDelegate implements FlowObjectiveStoreDelegate {
309 @Override
310 public void notify(ObjectiveEvent event) {
311 Set<PendingNext> pending = pendingForwards.remove(event.subject());
312
313 if (pending == null) {
314 return;
315 }
316
317 log.info("Processing pending objectives {}", pending.size());
318
319 pending.forEach(p -> getDevicePipeliner(p.deviceId())
320 .forward(p.forwardingObjective()));
321
322 }
323 }
324
325 /**
326 * Data class used to hold a pending forwarding objective that could not
327 * be processed because the associated next object was not present.
328 */
329 private class PendingNext {
330 private final DeviceId deviceId;
331 private final ForwardingObjective fwd;
332
333 public PendingNext(DeviceId deviceId, ForwardingObjective fwd) {
334 this.deviceId = deviceId;
335 this.fwd = fwd;
336 }
337
338 public DeviceId deviceId() {
339 return deviceId;
340 }
341
342 public ForwardingObjective forwardingObjective() {
343 return fwd;
344 }
345
346
alshabibaebe7752015-04-07 17:45:42 -0700347 }
alshabib77b88482015-04-07 15:47:50 -0700348}