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; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 30 | import org.onosproject.cluster.NodeId; |
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; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 40 | import org.onosproject.net.driver.DriverHandler; |
| 41 | import org.onosproject.net.driver.DriverService; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 42 | import org.onosproject.net.flow.FlowRuleService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 44 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 45 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 46 | import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 47 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 48 | import org.onosproject.net.flowobjective.NextObjective; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 49 | import org.onosproject.net.flowobjective.Objective; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 50 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 51 | import org.onosproject.net.flowobjective.ObjectiveEvent; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 52 | import org.onosproject.net.group.GroupService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | import org.slf4j.LoggerFactory; |
| 55 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 56 | import java.util.Map; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 57 | import java.util.Set; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 58 | import java.util.concurrent.ExecutorService; |
| 59 | import java.util.concurrent.Executors; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 60 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 61 | import static org.onlab.util.Tools.groupedThreads; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 64 | * Provides implementation of the flow objective programming service. |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 65 | */ |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 66 | @Component(immediate = true) |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 67 | @Service |
| 68 | public class FlowObjectiveManager implements FlowObjectiveService { |
| 69 | |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame^] | 70 | public static final int INSTALL_RETRY_ATTEMPTS = 10; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 71 | public static final long INSTALL_RETRY_INTERVAL = 1000; // ms |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 72 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 73 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 74 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 75 | @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 Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 87 | // 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 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected FlowObjectiveStore flowObjectiveStore; |
| 98 | |
| 99 | private final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 100 | |
| 101 | private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap(); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 102 | private final Map<DeviceId, Pipeliner> pipeliners = Maps.newConcurrentMap(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 103 | |
| 104 | private final PipelinerContext context = new InnerPipelineContext(); |
| 105 | private final MastershipListener mastershipListener = new InnerMastershipListener(); |
| 106 | private final DeviceListener deviceListener = new InnerDeviceListener(); |
| 107 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 108 | protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 109 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 110 | private NodeId localNode; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 111 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 112 | private Map<Integer, Set<PendingNext>> pendingForwards = |
| 113 | Maps.newConcurrentMap(); |
| 114 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 115 | private ExecutorService executorService; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 116 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 117 | @Activate |
| 118 | protected void activate() { |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 119 | executorService = Executors.newFixedThreadPool( |
| 120 | 4, groupedThreads("onos/objective-installer", "%d")); |
| 121 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 122 | flowObjectiveStore.setDelegate(delegate); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 123 | localNode = clusterService.getLocalNode().id(); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 124 | mastershipService.addListener(mastershipListener); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 125 | deviceService.addListener(deviceListener); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 126 | deviceService.getDevices().forEach(device -> setupPipelineHandler(device.id())); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 127 | log.info("Started"); |
| 128 | } |
| 129 | |
| 130 | @Deactivate |
| 131 | protected void deactivate() { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 132 | flowObjectiveStore.unsetDelegate(delegate); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 133 | mastershipService.removeListener(mastershipListener); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 134 | deviceService.removeListener(deviceListener); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 135 | log.info("Stopped"); |
| 136 | } |
| 137 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 138 | /** |
| 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; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 152 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 153 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 154 | @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) { |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame^] | 170 | Thread.sleep(INSTALL_RETRY_INTERVAL); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 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)); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 190 | public void forward(DeviceId deviceId, |
| 191 | ForwardingObjective forwardingObjective) { |
| 192 | |
| 193 | if (queueObjective(deviceId, forwardingObjective)) { |
| 194 | return; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 195 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 196 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 197 | executorService.submit(new ObjectiveInstaller(deviceId, forwardingObjective)); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 198 | } |
| 199 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 200 | @Override |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 201 | public void next(DeviceId deviceId, NextObjective nextObjective) { |
| 202 | executorService.submit(new ObjectiveInstaller(deviceId, nextObjective)); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 203 | } |
| 204 | |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 205 | @Override |
| 206 | public int allocateNextId() { |
| 207 | return flowObjectiveStore.allocateNextId(); |
| 208 | } |
| 209 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 210 | private boolean queueObjective(DeviceId deviceId, ForwardingObjective fwd) { |
| 211 | if (fwd.nextId() != null && |
| 212 | flowObjectiveStore.getNextGroup(fwd.nextId()) == null) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 213 | log.trace("Queuing forwarding objective for nextId {}", fwd.nextId()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 214 | 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 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 224 | // Retrieves the device pipeline behaviour from the cache. |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 225 | private Pipeliner getDevicePipeliner(DeviceId deviceId) { |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 226 | Pipeliner pipeliner = pipeliners.get(deviceId); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 227 | return pipeliner; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 228 | } |
| 229 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 230 | private void setupPipelineHandler(DeviceId deviceId) { |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 231 | // 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); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 240 | return; |
| 241 | } |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 242 | } catch (ItemNotFoundException e) { |
| 243 | log.warn("No applicable driver for device {}", deviceId); |
| 244 | return; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 247 | driverHandlers.put(deviceId, handler); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 248 | } |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 249 | |
| 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); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 256 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 257 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 258 | // Triggers driver setup when the local node becomes a device master. |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 259 | private class InnerMastershipListener implements MastershipListener { |
| 260 | @Override |
| 261 | public void event(MastershipEvent event) { |
| 262 | switch (event.type()) { |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 263 | case MASTER_CHANGED: |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 264 | log.info("mastership changed on device {}", event.subject()); |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame^] | 265 | if (deviceService.isAvailable(event.subject())) { |
| 266 | setupPipelineHandler(event.subject()); |
| 267 | } |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 268 | break; |
| 269 | case BACKUPS_CHANGED: |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 270 | break; |
| 271 | default: |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 272 | break; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 275 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 276 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 277 | // Triggers driver setup when a device is (re)detected. |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 278 | private class InnerDeviceListener implements DeviceListener { |
| 279 | @Override |
| 280 | public void event(DeviceEvent event) { |
| 281 | switch (event.type()) { |
| 282 | case DEVICE_ADDED: |
| 283 | case DEVICE_AVAILABILITY_CHANGED: |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 284 | log.info("Device either added or availability changed {}", |
| 285 | event.subject().id()); |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 286 | if (deviceService.isAvailable(event.subject().id())) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 287 | log.info("Device is now available {}", event.subject().id()); |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 288 | 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: |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 302 | break; |
| 303 | default: |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 304 | break; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 305 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 308 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 309 | // Processing context for initializing pipeline driver behaviours. |
| 310 | private class InnerPipelineContext implements PipelinerContext { |
| 311 | @Override |
| 312 | public ServiceDirectory directory() { |
| 313 | return serviceDirectory; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 314 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 315 | |
| 316 | @Override |
| 317 | public FlowObjectiveStore store() { |
| 318 | return flowObjectiveStore; |
| 319 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | private class InternalStoreDelegate implements FlowObjectiveStoreDelegate { |
| 323 | @Override |
| 324 | public void notify(ObjectiveEvent event) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 325 | log.debug("Received notification of obj event {}", event); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 326 | Set<PendingNext> pending = pendingForwards.remove(event.subject()); |
| 327 | |
| 328 | if (pending == null) { |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 329 | log.debug("Nothing pending for this obj event"); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 330 | return; |
| 331 | } |
| 332 | |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 333 | log.debug("Processing pending forwarding objectives {}", pending.size()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 334 | |
| 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 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 361 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 362 | } |