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