alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.flowobjective.impl; |
| 17 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 18 | import com.google.common.collect.Lists; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 20 | import com.google.common.util.concurrent.Futures; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.osgi.DefaultServiceDirectory; |
| 28 | import org.onlab.osgi.ServiceDirectory; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 29 | import org.onlab.util.ItemNotFoundException; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 30 | import org.onosproject.cluster.ClusterService; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 31 | import org.onosproject.cluster.NodeId; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 32 | import org.onosproject.mastership.MastershipEvent; |
| 33 | import org.onosproject.mastership.MastershipListener; |
| 34 | import org.onosproject.mastership.MastershipService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.behaviour.Pipeliner; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 37 | import org.onosproject.net.behaviour.PipelinerContext; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 38 | import org.onosproject.net.device.DeviceEvent; |
| 39 | import org.onosproject.net.device.DeviceListener; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 40 | import org.onosproject.net.device.DeviceService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 41 | import org.onosproject.net.driver.DriverHandler; |
| 42 | import org.onosproject.net.driver.DriverService; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flow.FlowRuleService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 44 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 45 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 46 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 47 | import org.onosproject.net.flowobjective.NextObjective; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 48 | import org.onosproject.net.flowobjective.Objective; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 49 | import org.onosproject.net.group.GroupService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | import org.slf4j.LoggerFactory; |
| 52 | |
| 53 | import java.util.Collection; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 54 | import java.util.Collections; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 55 | import java.util.Map; |
| 56 | import java.util.concurrent.Future; |
| 57 | |
| 58 | import static com.google.common.base.Preconditions.checkState; |
| 59 | |
| 60 | /** |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 61 | * Provides implementation of the flow objective programming service. |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 62 | */ |
| 63 | @Component(immediate = true) |
| 64 | @Service |
| 65 | public class FlowObjectiveManager implements FlowObjectiveService { |
| 66 | |
| 67 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 68 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 69 | public static final String NOT_INITIALIZED = "Driver not initialized"; |
| 70 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected DriverService driverService; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected DeviceService deviceService; |
| 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected MastershipService mastershipService; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected ClusterService clusterService; |
| 82 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 83 | // Note: The following dependencies are added on behalf of the pipeline |
| 84 | // driver behaviours to assure these services are available for their |
| 85 | // initialization. |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected FlowRuleService flowRuleService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected GroupService groupService; |
| 91 | |
| 92 | |
| 93 | private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap(); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 94 | private final Map<DeviceId, Pipeliner> pipeliners = Maps.newConcurrentMap(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 95 | |
| 96 | private final PipelinerContext context = new InnerPipelineContext(); |
| 97 | private final MastershipListener mastershipListener = new InnerMastershipListener(); |
| 98 | private final DeviceListener deviceListener = new InnerDeviceListener(); |
| 99 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 100 | protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 101 | |
| 102 | private final Map<DeviceId, Collection<Objective>> pendingObjectives = |
| 103 | Maps.newConcurrentMap(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 104 | private NodeId localNode; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 105 | |
| 106 | @Activate |
| 107 | protected void activate() { |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 108 | localNode = clusterService.getLocalNode().id(); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 109 | mastershipService.addListener(mastershipListener); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 110 | deviceService.addListener(deviceListener); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 111 | deviceService.getDevices().forEach(device -> setupPipelineHandler(device.id())); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 112 | log.info("Started"); |
| 113 | } |
| 114 | |
| 115 | @Deactivate |
| 116 | protected void deactivate() { |
| 117 | mastershipService.removeListener(mastershipListener); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 118 | deviceService.removeListener(deviceListener); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 119 | log.info("Stopped"); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public Future<Boolean> filter(DeviceId deviceId, |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 124 | Collection<FilteringObjective> filteringObjectives) { |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 125 | if (deviceService.isAvailable(deviceId)) { |
| 126 | return getDevicePipeliner(deviceId).filter(filteringObjectives); |
| 127 | } else { |
| 128 | filteringObjectives.forEach(obj -> updatePendingMap(deviceId, obj)); |
| 129 | } |
| 130 | return Futures.immediateFuture(true); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 131 | } |
| 132 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 133 | |
| 134 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public Future<Boolean> forward(DeviceId deviceId, |
| 137 | Collection<ForwardingObjective> forwardingObjectives) { |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 138 | if (deviceService.isAvailable(deviceId)) { |
| 139 | return getDevicePipeliner(deviceId).forward(forwardingObjectives); |
| 140 | } else { |
| 141 | forwardingObjectives.forEach(obj -> updatePendingMap(deviceId, obj)); |
| 142 | } |
| 143 | return Futures.immediateFuture(true); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Future<Boolean> next(DeviceId deviceId, |
| 148 | Collection<NextObjective> nextObjectives) { |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 149 | if (deviceService.isAvailable(deviceId)) { |
| 150 | return getDevicePipeliner(deviceId).next(nextObjectives); |
| 151 | } else { |
| 152 | nextObjectives.forEach(obj -> updatePendingMap(deviceId, obj)); |
| 153 | } |
| 154 | return Futures.immediateFuture(true); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 155 | } |
| 156 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 157 | private void updatePendingMap(DeviceId deviceId, Objective pending) { |
| 158 | if (pendingObjectives.putIfAbsent(deviceId, Lists.newArrayList(pending)) != null) { |
| 159 | Collection<Objective> objectives = pendingObjectives.get(deviceId); |
| 160 | objectives.add(pending); |
| 161 | } |
| 162 | |
| 163 | } |
| 164 | |
| 165 | // Retrieves the device pipeline behaviour from the cache. |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 166 | private Pipeliner getDevicePipeliner(DeviceId deviceId) { |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 167 | Pipeliner pipeliner = pipeliners.get(deviceId); |
| 168 | checkState(pipeliner != null, NOT_INITIALIZED); |
| 169 | return pipeliner; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 170 | } |
| 171 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 172 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 173 | // Triggers driver setup when the local node becomes a device master. |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 174 | private class InnerMastershipListener implements MastershipListener { |
| 175 | @Override |
| 176 | public void event(MastershipEvent event) { |
| 177 | switch (event.type()) { |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 178 | case MASTER_CHANGED: |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 179 | if (event.roleInfo().master() != null) { |
| 180 | setupPipelineHandler(event.subject()); |
| 181 | } |
| 182 | break; |
| 183 | case BACKUPS_CHANGED: |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 184 | break; |
| 185 | default: |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 186 | break; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 189 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 190 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 191 | // Triggers driver setup when a device is (re)detected. |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 192 | private class InnerDeviceListener implements DeviceListener { |
| 193 | @Override |
| 194 | public void event(DeviceEvent event) { |
| 195 | switch (event.type()) { |
| 196 | case DEVICE_ADDED: |
| 197 | case DEVICE_AVAILABILITY_CHANGED: |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 198 | if (deviceService.isAvailable(event.subject().id())) { |
| 199 | setupPipelineHandler(event.subject().id()); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 200 | processPendingObjectives(event.subject().id()); |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 201 | } |
| 202 | break; |
| 203 | case DEVICE_UPDATED: |
| 204 | break; |
| 205 | case DEVICE_REMOVED: |
| 206 | break; |
| 207 | case DEVICE_SUSPENDED: |
| 208 | break; |
| 209 | case PORT_ADDED: |
| 210 | break; |
| 211 | case PORT_UPDATED: |
| 212 | break; |
| 213 | case PORT_REMOVED: |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 214 | break; |
| 215 | default: |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 216 | break; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 217 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 218 | } |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 219 | |
| 220 | private void processPendingObjectives(DeviceId deviceId) { |
| 221 | pendingObjectives.get(deviceId).forEach(obj -> { |
| 222 | if (obj instanceof NextObjective) { |
| 223 | getDevicePipeliner(deviceId) |
| 224 | .next(Collections.singletonList((NextObjective) obj)); |
| 225 | } else if (obj instanceof ForwardingObjective) { |
| 226 | getDevicePipeliner(deviceId) |
| 227 | .forward(Collections.singletonList((ForwardingObjective) obj)); |
| 228 | } else { |
| 229 | getDevicePipeliner(deviceId) |
| 230 | .filter(Collections.singletonList((FilteringObjective) obj)); |
| 231 | } |
| 232 | }); |
| 233 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 234 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 235 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 236 | private void setupPipelineHandler(DeviceId deviceId) { |
| 237 | if (localNode.equals(mastershipService.getMasterFor(deviceId))) { |
| 238 | // Attempt to lookup the handler in the cache |
| 239 | DriverHandler handler = driverHandlers.get(deviceId); |
| 240 | if (handler == null) { |
| 241 | try { |
| 242 | // Otherwise create it and if it has pipeline behaviour, cache it |
| 243 | handler = driverService.createHandler(deviceId); |
| 244 | if (!handler.driver().hasBehaviour(Pipeliner.class)) { |
| 245 | log.warn("Pipeline behaviour not supported for device {}", |
| 246 | deviceId); |
| 247 | return; |
| 248 | } |
| 249 | } catch (ItemNotFoundException e) { |
| 250 | log.warn("No applicable driver for device {}", deviceId); |
| 251 | return; |
| 252 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 253 | driverHandlers.put(deviceId, handler); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 254 | } |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 255 | |
| 256 | // Always (re)initialize the pipeline behaviour |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame^] | 257 | Pipeliner pipeliner = handler.behaviour(Pipeliner.class); |
| 258 | pipeliner.init(deviceId, context); |
| 259 | pipeliners.putIfAbsent(deviceId, pipeliner); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 260 | log.info("Driver {} bound to device {}", handler.driver().name(), deviceId); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 264 | // Processing context for initializing pipeline driver behaviours. |
| 265 | private class InnerPipelineContext implements PipelinerContext { |
| 266 | @Override |
| 267 | public ServiceDirectory directory() { |
| 268 | return serviceDirectory; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 269 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 270 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 271 | } |