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