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