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 | |
| 18 | import com.google.common.collect.Maps; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onlab.osgi.DefaultServiceDirectory; |
| 27 | import org.onlab.osgi.ServiceDirectory; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 28 | import org.onlab.util.ItemNotFoundException; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 29 | import org.onosproject.cluster.ClusterService; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 30 | import org.onosproject.core.Permission; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 31 | import org.onosproject.mastership.MastershipEvent; |
| 32 | import org.onosproject.mastership.MastershipListener; |
| 33 | import org.onosproject.mastership.MastershipService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.behaviour.Pipeliner; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 36 | import org.onosproject.net.behaviour.PipelinerContext; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 37 | import org.onosproject.net.device.DeviceEvent; |
| 38 | import org.onosproject.net.device.DeviceListener; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 39 | import org.onosproject.net.device.DeviceService; |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 40 | import org.onosproject.net.driver.DefaultDriverProviderService; |
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; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 46 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 47 | import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 48 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 49 | import org.onosproject.net.flowobjective.NextObjective; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 50 | import org.onosproject.net.flowobjective.Objective; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 51 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 52 | import org.onosproject.net.flowobjective.ObjectiveEvent; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 53 | import org.onosproject.net.group.GroupService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
| 56 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 57 | import java.util.Map; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 58 | import java.util.Set; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 59 | import java.util.concurrent.ExecutorService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 60 | |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 61 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 62 | import static org.onlab.util.Tools.groupedThreads; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 63 | import static org.onosproject.security.AppGuard.checkPermission; |
| 64 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 67 | * Provides implementation of the flow objective programming service. |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 68 | */ |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 69 | @Component(immediate = true) |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 70 | @Service |
| 71 | public class FlowObjectiveManager implements FlowObjectiveService { |
| 72 | |
Saurav Das | bd7f742 | 2015-04-23 16:31:47 -0700 | [diff] [blame] | 73 | public static final int INSTALL_RETRY_ATTEMPTS = 5; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 74 | public static final long INSTALL_RETRY_INTERVAL = 1000; // ms |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 75 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 76 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 77 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected DriverService driverService; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected DeviceService deviceService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected MastershipService mastershipService; |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected ClusterService clusterService; |
| 89 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 90 | // Note: The following dependencies are added on behalf of the pipeline |
| 91 | // driver behaviours to assure these services are available for their |
| 92 | // initialization. |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected FlowRuleService flowRuleService; |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected GroupService groupService; |
| 98 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 100 | protected FlowObjectiveStore flowObjectiveStore; |
| 101 | |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 102 | // Note: This must remain an optional dependency to allow re-install of default drivers. |
| 103 | // Note: For now disabled until we can move to OPTIONAL_UNARY dependency |
| 104 | // @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, policy = ReferencePolicy.DYNAMIC) |
| 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | protected DefaultDriverProviderService defaultDriverService; |
| 107 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 108 | private final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 109 | |
| 110 | private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap(); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 111 | private final Map<DeviceId, Pipeliner> pipeliners = Maps.newConcurrentMap(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 112 | |
| 113 | private final PipelinerContext context = new InnerPipelineContext(); |
| 114 | private final MastershipListener mastershipListener = new InnerMastershipListener(); |
| 115 | private final DeviceListener deviceListener = new InnerDeviceListener(); |
| 116 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 117 | protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 118 | |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 119 | private Map<Integer, Set<PendingNext>> pendingForwards = Maps.newConcurrentMap(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 120 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 121 | private ExecutorService executorService; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 122 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 123 | @Activate |
| 124 | protected void activate() { |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 125 | executorService = newFixedThreadPool(4, groupedThreads("onos/objective-installer", "%d")); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 126 | flowObjectiveStore.setDelegate(delegate); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 127 | mastershipService.addListener(mastershipListener); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 128 | deviceService.addListener(deviceListener); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 129 | deviceService.getDevices().forEach(device -> setupPipelineHandler(device.id())); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 130 | log.info("Started"); |
| 131 | } |
| 132 | |
| 133 | @Deactivate |
| 134 | protected void deactivate() { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 135 | flowObjectiveStore.unsetDelegate(delegate); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 136 | mastershipService.removeListener(mastershipListener); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 137 | deviceService.removeListener(deviceListener); |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 138 | executorService.shutdown(); |
| 139 | pipeliners.clear(); |
| 140 | driverHandlers.clear(); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 141 | log.info("Stopped"); |
| 142 | } |
| 143 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 144 | /** |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 145 | * Hook for binding the optional default driver providers. |
| 146 | * |
| 147 | * @param service arriving default driver provider service |
| 148 | */ |
| 149 | // Note: For now disabled until we can move to OPTIONAL_UNARY dependency |
| 150 | protected void xbindDefaultDriverService(DefaultDriverProviderService service) { |
| 151 | log.info("Detected default drivers... going active"); |
| 152 | defaultDriverService = service; |
| 153 | deviceService.getDevices().forEach(device -> setupPipelineHandler(device.id())); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Hook for unbinding the optional default driver providers. |
| 158 | * |
| 159 | * @param service departing default driver provider service |
| 160 | */ |
| 161 | // Note: For now disabled until we can move to OPTIONAL_UNARY dependency |
| 162 | protected void xunbindDefaultDriverService(DefaultDriverProviderService service) { |
| 163 | log.info("Lost default drivers... going dormant"); |
| 164 | defaultDriverService = null; |
| 165 | pipeliners.clear(); |
| 166 | driverHandlers.clear(); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /** |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 171 | * Task that passes the flow objective down to the driver. The task will |
| 172 | * make a few attempts to find the appropriate driver, then eventually give |
| 173 | * up and report an error if no suitable driver could be found. |
| 174 | */ |
| 175 | private class ObjectiveInstaller implements Runnable { |
| 176 | private final DeviceId deviceId; |
| 177 | private final Objective objective; |
| 178 | |
| 179 | private int numAttempts = 0; |
| 180 | |
| 181 | public ObjectiveInstaller(DeviceId deviceId, Objective objective) { |
| 182 | this.deviceId = deviceId; |
| 183 | this.objective = objective; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 184 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 185 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 186 | @Override |
| 187 | public void run() { |
| 188 | try { |
| 189 | numAttempts++; |
| 190 | |
| 191 | Pipeliner pipeliner = getDevicePipeliner(deviceId); |
| 192 | |
| 193 | if (pipeliner != null) { |
| 194 | if (objective instanceof NextObjective) { |
| 195 | pipeliner.next((NextObjective) objective); |
| 196 | } else if (objective instanceof ForwardingObjective) { |
| 197 | pipeliner.forward((ForwardingObjective) objective); |
| 198 | } else { |
| 199 | pipeliner.filter((FilteringObjective) objective); |
| 200 | } |
| 201 | } else if (numAttempts < INSTALL_RETRY_ATTEMPTS) { |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 202 | Thread.sleep(INSTALL_RETRY_INTERVAL); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 203 | executorService.submit(this); |
| 204 | } else { |
| 205 | // Otherwise we've tried a few times and failed, report an |
| 206 | // error back to the user. |
| 207 | objective.context().ifPresent( |
| 208 | c -> c.onError(objective, ObjectiveError.DEVICEMISSING)); |
| 209 | } |
| 210 | } catch (Exception e) { |
| 211 | log.warn("Exception while installing flow objective", e); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public void filter(DeviceId deviceId, FilteringObjective filteringObjective) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 218 | checkPermission(Permission.FLOWRULE_WRITE); |
| 219 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 220 | executorService.submit(new ObjectiveInstaller(deviceId, filteringObjective)); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 224 | public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 225 | checkPermission(Permission.FLOWRULE_WRITE); |
| 226 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 227 | if (queueObjective(deviceId, forwardingObjective)) { |
| 228 | return; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 229 | } |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 230 | executorService.submit(new ObjectiveInstaller(deviceId, forwardingObjective)); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 231 | } |
| 232 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 233 | @Override |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 234 | public void next(DeviceId deviceId, NextObjective nextObjective) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 235 | checkPermission(Permission.FLOWRULE_WRITE); |
| 236 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 237 | executorService.submit(new ObjectiveInstaller(deviceId, nextObjective)); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 238 | } |
| 239 | |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 240 | @Override |
| 241 | public int allocateNextId() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 242 | checkPermission(Permission.FLOWRULE_WRITE); |
| 243 | |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 244 | return flowObjectiveStore.allocateNextId(); |
| 245 | } |
| 246 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 247 | private boolean queueObjective(DeviceId deviceId, ForwardingObjective fwd) { |
| 248 | if (fwd.nextId() != null && |
| 249 | flowObjectiveStore.getNextGroup(fwd.nextId()) == null) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 250 | log.trace("Queuing forwarding objective for nextId {}", fwd.nextId()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 251 | if (pendingForwards.putIfAbsent(fwd.nextId(), |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 252 | Sets.newHashSet(new PendingNext(deviceId, fwd))) != null) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 253 | Set<PendingNext> pending = pendingForwards.get(fwd.nextId()); |
| 254 | pending.add(new PendingNext(deviceId, fwd)); |
| 255 | } |
| 256 | return true; |
| 257 | } |
| 258 | return false; |
| 259 | } |
| 260 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 261 | // Retrieves the device pipeline behaviour from the cache. |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 262 | private Pipeliner getDevicePipeliner(DeviceId deviceId) { |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 263 | Pipeliner pipeliner = pipeliners.get(deviceId); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 264 | return pipeliner; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 265 | } |
| 266 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 267 | private void setupPipelineHandler(DeviceId deviceId) { |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 268 | if (defaultDriverService == null) { |
| 269 | // We're not ready to go to work yet. |
| 270 | return; |
| 271 | } |
| 272 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 273 | // Attempt to lookup the handler in the cache |
| 274 | DriverHandler handler = driverHandlers.get(deviceId); |
| 275 | if (handler == null) { |
| 276 | try { |
| 277 | // Otherwise create it and if it has pipeline behaviour, cache it |
| 278 | handler = driverService.createHandler(deviceId); |
| 279 | if (!handler.driver().hasBehaviour(Pipeliner.class)) { |
| 280 | log.warn("Pipeline behaviour not supported for device {}", |
| 281 | deviceId); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 282 | return; |
| 283 | } |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 284 | } catch (ItemNotFoundException e) { |
| 285 | log.warn("No applicable driver for device {}", deviceId); |
| 286 | return; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 289 | driverHandlers.put(deviceId, handler); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 290 | } |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 291 | |
| 292 | // Always (re)initialize the pipeline behaviour |
| 293 | log.info("Driver {} bound to device {} ... initializing driver", |
| 294 | handler.driver().name(), deviceId); |
| 295 | Pipeliner pipeliner = handler.behaviour(Pipeliner.class); |
| 296 | pipeliner.init(deviceId, context); |
| 297 | pipeliners.putIfAbsent(deviceId, pipeliner); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 298 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 299 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 300 | // Triggers driver setup when the local node becomes a device master. |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 301 | private class InnerMastershipListener implements MastershipListener { |
| 302 | @Override |
| 303 | public void event(MastershipEvent event) { |
| 304 | switch (event.type()) { |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 305 | case MASTER_CHANGED: |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 306 | log.info("mastership changed on device {}", event.subject()); |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 307 | if (deviceService.isAvailable(event.subject())) { |
| 308 | setupPipelineHandler(event.subject()); |
| 309 | } |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 310 | break; |
| 311 | case BACKUPS_CHANGED: |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 312 | break; |
| 313 | default: |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 314 | break; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 317 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 318 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 319 | // Triggers driver setup when a device is (re)detected. |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 320 | private class InnerDeviceListener implements DeviceListener { |
| 321 | @Override |
| 322 | public void event(DeviceEvent event) { |
| 323 | switch (event.type()) { |
| 324 | case DEVICE_ADDED: |
| 325 | case DEVICE_AVAILABILITY_CHANGED: |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 326 | log.info("Device either added or availability changed {}", |
| 327 | event.subject().id()); |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 328 | if (deviceService.isAvailable(event.subject().id())) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 329 | log.info("Device is now available {}", event.subject().id()); |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 330 | setupPipelineHandler(event.subject().id()); |
| 331 | } |
| 332 | break; |
| 333 | case DEVICE_UPDATED: |
| 334 | break; |
| 335 | case DEVICE_REMOVED: |
| 336 | break; |
| 337 | case DEVICE_SUSPENDED: |
| 338 | break; |
| 339 | case PORT_ADDED: |
| 340 | break; |
| 341 | case PORT_UPDATED: |
| 342 | break; |
| 343 | case PORT_REMOVED: |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 344 | break; |
| 345 | default: |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 346 | break; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 347 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 350 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 351 | // Processing context for initializing pipeline driver behaviours. |
| 352 | private class InnerPipelineContext implements PipelinerContext { |
| 353 | @Override |
| 354 | public ServiceDirectory directory() { |
| 355 | return serviceDirectory; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 356 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 357 | |
| 358 | @Override |
| 359 | public FlowObjectiveStore store() { |
| 360 | return flowObjectiveStore; |
| 361 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | private class InternalStoreDelegate implements FlowObjectiveStoreDelegate { |
| 365 | @Override |
| 366 | public void notify(ObjectiveEvent event) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 367 | log.debug("Received notification of obj event {}", event); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 368 | Set<PendingNext> pending = pendingForwards.remove(event.subject()); |
| 369 | |
| 370 | if (pending == null) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 371 | log.debug("Nothing pending for this obj event"); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 372 | return; |
| 373 | } |
| 374 | |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 375 | log.debug("Processing pending forwarding objectives {}", pending.size()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 376 | |
| 377 | pending.forEach(p -> getDevicePipeliner(p.deviceId()) |
| 378 | .forward(p.forwardingObjective())); |
| 379 | |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Data class used to hold a pending forwarding objective that could not |
| 385 | * be processed because the associated next object was not present. |
| 386 | */ |
| 387 | private class PendingNext { |
| 388 | private final DeviceId deviceId; |
| 389 | private final ForwardingObjective fwd; |
| 390 | |
| 391 | public PendingNext(DeviceId deviceId, ForwardingObjective fwd) { |
| 392 | this.deviceId = deviceId; |
| 393 | this.fwd = fwd; |
| 394 | } |
| 395 | |
| 396 | public DeviceId deviceId() { |
| 397 | return deviceId; |
| 398 | } |
| 399 | |
| 400 | public ForwardingObjective forwardingObjective() { |
| 401 | return fwd; |
| 402 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 403 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 404 | } |