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