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