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