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 | |
Pier Ventre | 57a61cd | 2016-09-07 10:55:41 -0700 | [diff] [blame] | 256 | /** |
| 257 | * Retrieves (if it exists) the device pipeline behaviour from the cache. |
| 258 | * Otherwise it warms the caches and triggers the init method of the Pipeline. |
| 259 | * |
| 260 | * @param deviceId the id of the device associated to the pipeline |
| 261 | * @return the implementation of the Pipeliner behaviour |
| 262 | */ |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 263 | private Pipeliner getDevicePipeliner(DeviceId deviceId) { |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 264 | return pipeliners.computeIfAbsent(deviceId, this::initPipelineHandler); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 267 | /** |
Pier Ventre | 57a61cd | 2016-09-07 10:55:41 -0700 | [diff] [blame] | 268 | * Retrieves (if it exists) the device pipeline behaviour from the cache and |
| 269 | * and triggers the init method of the pipeline. Otherwise (DEVICE_ADDED) it warms |
| 270 | * the caches and triggers the init method of the Pipeline. The rationale of this |
| 271 | * method is for managing the scenario of a switch that goes down for a failure |
| 272 | * and goes up after a while. |
| 273 | * |
| 274 | * @param deviceId the id of the device associated to the pipeline |
| 275 | * @return the implementation of the Pipeliner behaviour |
| 276 | */ |
| 277 | private Pipeliner getAndInitDevicePipeliner(DeviceId deviceId) { |
| 278 | return pipeliners.compute(deviceId, (deviceIdValue, pipelinerValue) -> { |
| 279 | if (pipelinerValue != null) { |
| 280 | pipelinerValue.init(deviceId, context); |
| 281 | return pipelinerValue; |
| 282 | } |
| 283 | return this.initPipelineHandler(deviceId); |
| 284 | }); |
| 285 | } |
| 286 | |
| 287 | /** |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 288 | * Creates and initialize {@link Pipeliner}. |
| 289 | * <p> |
| 290 | * Note: Expected to be called under per-Device lock. |
| 291 | * e.g., {@code pipeliners}' Map#compute family methods |
| 292 | * |
| 293 | * @param deviceId Device to initialize pipeliner |
| 294 | * @return {@link Pipeliner} instance or null |
| 295 | */ |
| 296 | private Pipeliner initPipelineHandler(DeviceId deviceId) { |
| 297 | start = now(); |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 298 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 299 | // Attempt to lookup the handler in the cache |
| 300 | DriverHandler handler = driverHandlers.get(deviceId); |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 301 | cTime = now(); |
| 302 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 303 | if (handler == null) { |
| 304 | try { |
| 305 | // Otherwise create it and if it has pipeline behaviour, cache it |
| 306 | handler = driverService.createHandler(deviceId); |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 307 | dTime = now(); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 308 | if (!handler.driver().hasBehaviour(Pipeliner.class)) { |
| 309 | log.warn("Pipeline behaviour not supported for device {}", |
| 310 | deviceId); |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 311 | return null; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 312 | } |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 313 | } catch (ItemNotFoundException e) { |
| 314 | log.warn("No applicable driver for device {}", deviceId); |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 315 | return null; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 318 | driverHandlers.put(deviceId, handler); |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 319 | eTime = now(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 320 | } |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 321 | |
| 322 | // Always (re)initialize the pipeline behaviour |
| 323 | log.info("Driver {} bound to device {} ... initializing driver", |
| 324 | handler.driver().name(), deviceId); |
Thomas Vachuska | 0121a61 | 2015-07-21 11:18:09 -0700 | [diff] [blame] | 325 | hTime = now(); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 326 | Pipeliner pipeliner = handler.behaviour(Pipeliner.class); |
Thomas Vachuska | 94c3cf4 | 2015-07-20 13:01:12 -0700 | [diff] [blame] | 327 | hbTime = now(); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 328 | pipeliner.init(deviceId, context); |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 329 | stopWatch(); |
| 330 | return pipeliner; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 331 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 332 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 333 | // Triggers driver setup when a device is (re)detected. |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 334 | private class InnerDeviceListener implements DeviceListener { |
| 335 | @Override |
| 336 | public void event(DeviceEvent event) { |
| 337 | switch (event.type()) { |
| 338 | case DEVICE_ADDED: |
| 339 | case DEVICE_AVAILABILITY_CHANGED: |
Madan Jampani | 0174f45 | 2015-05-29 11:52:05 -0700 | [diff] [blame] | 340 | log.debug("Device either added or availability changed {}", |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 341 | event.subject().id()); |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 342 | if (deviceService.isAvailable(event.subject().id())) { |
Madan Jampani | 0174f45 | 2015-05-29 11:52:05 -0700 | [diff] [blame] | 343 | log.debug("Device is now available {}", event.subject().id()); |
Pier Ventre | 57a61cd | 2016-09-07 10:55:41 -0700 | [diff] [blame] | 344 | getAndInitDevicePipeliner(event.subject().id()); |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 345 | } else { |
| 346 | log.debug("Device is no longer available {}", event.subject().id()); |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 347 | } |
| 348 | break; |
| 349 | case DEVICE_UPDATED: |
| 350 | break; |
| 351 | case DEVICE_REMOVED: |
Yuta HIGUCHI | ad0c990 | 2016-08-23 10:37:32 -0700 | [diff] [blame] | 352 | // evict Pipeliner and Handler cache, when |
| 353 | // the Device was administratively removed. |
| 354 | // |
| 355 | // System expect the user to clear all existing flows, |
| 356 | // before removing device, especially if they intend to |
| 357 | // replace driver/pipeliner assigned to the device. |
| 358 | driverHandlers.remove(event.subject().id()); |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 359 | pipeliners.remove(event.subject().id()); |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 360 | break; |
Yuta HIGUCHI | ad0c990 | 2016-08-23 10:37:32 -0700 | [diff] [blame] | 361 | case DEVICE_SUSPENDED: |
| 362 | break; |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 363 | case PORT_ADDED: |
| 364 | break; |
| 365 | case PORT_UPDATED: |
| 366 | break; |
| 367 | case PORT_REMOVED: |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 368 | break; |
| 369 | default: |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 370 | break; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 371 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 374 | |
Thomas Vachuska | 174bb91 | 2015-07-16 21:27:14 -0700 | [diff] [blame] | 375 | // Temporary mechanism to monitor pipeliner setup time-cost; there are |
| 376 | // intermittent time where this takes in excess of 2 seconds. Why? |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 377 | private long start = 0, totals = 0, count = 0; |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 378 | private long cTime, dTime, eTime, hTime, hbTime; |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 379 | private static final long LIMIT = 500; |
Thomas Vachuska | 174bb91 | 2015-07-16 21:27:14 -0700 | [diff] [blame] | 380 | |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 381 | private long now() { |
Thomas Vachuska | 174bb91 | 2015-07-16 21:27:14 -0700 | [diff] [blame] | 382 | return System.currentTimeMillis(); |
| 383 | } |
| 384 | |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 385 | private void stopWatch() { |
Thomas Vachuska | 174bb91 | 2015-07-16 21:27:14 -0700 | [diff] [blame] | 386 | long duration = System.currentTimeMillis() - start; |
| 387 | totals += duration; |
| 388 | count += 1; |
| 389 | if (duration > LIMIT) { |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 390 | log.info("Pipeline setup took {} ms; avg {} ms; cTime={}, dTime={}, eTime={}, hTime={}, hbTime={}", |
| 391 | 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] | 392 | } |
| 393 | } |
| 394 | |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 395 | private long diff(long bTime) { |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 396 | long diff = bTime - start; |
| 397 | return diff < 0 ? 0 : diff; |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 400 | // Processing context for initializing pipeline driver behaviours. |
| 401 | private class InnerPipelineContext implements PipelinerContext { |
| 402 | @Override |
| 403 | public ServiceDirectory directory() { |
| 404 | return serviceDirectory; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 405 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 406 | |
| 407 | @Override |
| 408 | public FlowObjectiveStore store() { |
| 409 | return flowObjectiveStore; |
| 410 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | private class InternalStoreDelegate implements FlowObjectiveStoreDelegate { |
| 414 | @Override |
| 415 | public void notify(ObjectiveEvent event) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 416 | if (event.type() == Type.ADD) { |
| 417 | log.debug("Received notification of obj event {}", event); |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 418 | Set<PendingNext> pending; |
| 419 | synchronized (pendingForwards) { |
| 420 | // needs to be synchronized for queueObjective lookup |
| 421 | pending = pendingForwards.remove(event.subject()); |
| 422 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 423 | |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 424 | if (pending == null) { |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 425 | log.debug("Nothing pending for this obj event {}", event); |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 426 | return; |
| 427 | } |
| 428 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 429 | log.debug("Processing {} pending forwarding objectives for nextId {}", |
| 430 | pending.size(), event.subject()); |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 431 | pending.forEach(p -> getDevicePipeliner(p.deviceId()) |
| 432 | .forward(p.forwardingObjective())); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 433 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Data class used to hold a pending forwarding objective that could not |
| 439 | * be processed because the associated next object was not present. |
| 440 | */ |
| 441 | private class PendingNext { |
| 442 | private final DeviceId deviceId; |
| 443 | private final ForwardingObjective fwd; |
| 444 | |
| 445 | public PendingNext(DeviceId deviceId, ForwardingObjective fwd) { |
| 446 | this.deviceId = deviceId; |
| 447 | this.fwd = fwd; |
| 448 | } |
| 449 | |
| 450 | public DeviceId deviceId() { |
| 451 | return deviceId; |
| 452 | } |
| 453 | |
| 454 | public ForwardingObjective forwardingObjective() { |
| 455 | return fwd; |
| 456 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 457 | |
| 458 | @Override |
| 459 | public int hashCode() { |
| 460 | return Objects.hash(deviceId, fwd); |
| 461 | } |
| 462 | |
| 463 | @Override |
| 464 | public boolean equals(final Object obj) { |
| 465 | if (this == obj) { |
| 466 | return true; |
| 467 | } |
| 468 | if (!(obj instanceof PendingNext)) { |
| 469 | return false; |
| 470 | } |
| 471 | final PendingNext other = (PendingNext) obj; |
| 472 | if (this.deviceId.equals(other.deviceId) && |
| 473 | this.fwd.equals(other.fwd)) { |
| 474 | return true; |
| 475 | } |
| 476 | return false; |
| 477 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 478 | } |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 479 | |
| 480 | @Override |
| 481 | public List<String> getNextMappings() { |
| 482 | List<String> mappings = new ArrayList<>(); |
| 483 | Map<Integer, NextGroup> allnexts = flowObjectiveStore.getAllGroups(); |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 484 | // 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] | 485 | // then info on any nextObj could be retrieved from one controller instance. |
| 486 | // Right now the drivers on one instance can only fetch for next-ids that came |
| 487 | // to them. |
| 488 | // Also, we still need to send the right next-id to the right driver as potentially |
| 489 | // there can be different drivers for different devices. But on that account, |
| 490 | // no instance should be decoding for another instance's nextIds. |
| 491 | |
| 492 | for (Map.Entry<Integer, NextGroup> e : allnexts.entrySet()) { |
| 493 | // get the device this next Objective was sent to |
| 494 | DeviceId deviceId = nextToDevice.get(e.getKey()); |
| 495 | mappings.add("NextId " + e.getKey() + ": " + |
| 496 | ((deviceId != null) ? deviceId : "nextId not in this onos instance")); |
| 497 | if (deviceId != null) { |
| 498 | // this instance of the controller sent the nextObj to a driver |
| 499 | Pipeliner pipeliner = getDevicePipeliner(deviceId); |
| 500 | List<String> nextMappings = pipeliner.getNextMappings(e.getValue()); |
| 501 | if (nextMappings != null) { |
| 502 | mappings.addAll(nextMappings); |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | return mappings; |
| 507 | } |
Saurav Das | b5c236e | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 508 | |
| 509 | @Override |
| 510 | public List<String> getPendingNexts() { |
| 511 | List<String> pendingNexts = new ArrayList<>(); |
| 512 | for (Integer nextId : pendingForwards.keySet()) { |
| 513 | Set<PendingNext> pnext = pendingForwards.get(nextId); |
Sho SHIMIZU | 81470a5 | 2016-08-12 17:24:55 -0700 | [diff] [blame] | 514 | StringBuilder pend = new StringBuilder(); |
Saurav Das | b5c236e | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 515 | pend.append("Next Id: ").append(Integer.toString(nextId)) |
| 516 | .append(" :: "); |
| 517 | for (PendingNext pn : pnext) { |
| 518 | pend.append(Integer.toString(pn.forwardingObjective().id())) |
| 519 | .append(" "); |
| 520 | } |
| 521 | pendingNexts.add(pend.toString()); |
| 522 | } |
| 523 | return pendingNexts; |
| 524 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 525 | } |