alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | |
Pier Luigi | 9789311 | 2018-03-05 11:09:42 +0100 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 22 | import org.onlab.osgi.DefaultServiceDirectory; |
| 23 | import org.onlab.osgi.ServiceDirectory; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 24 | import org.onlab.util.ItemNotFoundException; |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 25 | import org.onlab.util.Tools; |
| 26 | import org.onosproject.cfg.ComponentConfigService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 27 | import org.onosproject.cluster.ClusterService; |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 28 | import org.onosproject.net.Device; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 30 | import org.onosproject.net.behaviour.NextGroup; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 31 | import org.onosproject.net.behaviour.Pipeliner; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 32 | import org.onosproject.net.behaviour.PipelinerContext; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 33 | import org.onosproject.net.device.DeviceEvent; |
| 34 | import org.onosproject.net.device.DeviceListener; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 35 | import org.onosproject.net.device.DeviceService; |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 36 | import org.onosproject.net.driver.DriverEvent; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 37 | import org.onosproject.net.driver.DriverHandler; |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 38 | import org.onosproject.net.driver.DriverListener; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 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; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 48 | import org.onosproject.net.flowobjective.Objective.Operation; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 49 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 50 | import org.onosproject.net.flowobjective.ObjectiveEvent; |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 51 | import org.onosproject.net.flowobjective.ObjectiveEvent.Type; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 52 | import org.onosproject.net.group.GroupService; |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 53 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 54 | import org.osgi.service.component.annotations.Activate; |
| 55 | import org.osgi.service.component.annotations.Component; |
| 56 | import org.osgi.service.component.annotations.Deactivate; |
| 57 | import org.osgi.service.component.annotations.Modified; |
| 58 | import org.osgi.service.component.annotations.Reference; |
| 59 | import org.osgi.service.component.annotations.ReferenceCardinality; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 60 | import org.slf4j.Logger; |
| 61 | import org.slf4j.LoggerFactory; |
| 62 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 63 | import java.util.ArrayList; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 64 | import java.util.List; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 65 | import java.util.Map; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 66 | import java.util.Objects; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 67 | import java.util.Set; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 68 | import java.util.concurrent.ExecutorService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 69 | |
Sho SHIMIZU | f45d85d | 2015-07-01 14:39:11 -0700 | [diff] [blame] | 70 | import static com.google.common.base.Preconditions.checkNotNull; |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 71 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 72 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 73 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 74 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 75 | import static org.onosproject.net.AnnotationKeys.DRIVER; |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 76 | import static org.onosproject.net.OsgiPropertyConstants.FOM_NUM_THREADS; |
| 77 | import static org.onosproject.net.OsgiPropertyConstants.FOM_NUM_THREADS_DEFAULT; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 78 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 79 | import static org.onosproject.security.AppPermission.Type.FLOWRULE_WRITE; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 82 | * Provides implementation of the flow objective programming service. |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 83 | */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 84 | @Component( |
| 85 | enabled = false, |
| 86 | service = FlowObjectiveService.class, |
| 87 | property = { |
Ray Milkey | 2d7bca1 | 2018-10-17 14:51:52 -0700 | [diff] [blame] | 88 | FOM_NUM_THREADS + ":Integer=" + FOM_NUM_THREADS_DEFAULT |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 89 | } |
| 90 | ) |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 91 | public class FlowObjectiveManager implements FlowObjectiveService { |
| 92 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 93 | private static final int INSTALL_RETRY_ATTEMPTS = 5; |
| 94 | private static final long INSTALL_RETRY_INTERVAL = 1000; // ms |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 95 | |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 96 | private static final String WORKER_PATTERN = "objective-installer-%d"; |
| 97 | private static final String GROUP_THREAD_NAME = "onos/objective-installer"; |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 98 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 99 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 100 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 101 | /** Number of worker threads. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 102 | private int numThreads = FOM_NUM_THREADS_DEFAULT; |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 103 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 105 | protected DriverService driverService; |
| 106 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 108 | protected DeviceService deviceService; |
| 109 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 111 | protected ClusterService clusterService; |
| 112 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 113 | // Note: The following dependencies are added on behalf of the pipeline |
| 114 | // driver behaviours to assure these services are available for their |
| 115 | // initialization. |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 116 | @SuppressWarnings("unused") |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 118 | protected FlowRuleService flowRuleService; |
| 119 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 120 | @SuppressWarnings("unused") |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 122 | protected GroupService groupService; |
| 123 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 125 | protected FlowObjectiveStore flowObjectiveStore; |
| 126 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 128 | protected ComponentConfigService cfgService; |
| 129 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 130 | final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 131 | |
| 132 | private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap(); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 133 | protected final Map<DeviceId, Pipeliner> pipeliners = Maps.newConcurrentMap(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 134 | |
| 135 | private final PipelinerContext context = new InnerPipelineContext(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 136 | private final DeviceListener deviceListener = new InnerDeviceListener(); |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 137 | private final DriverListener driverListener = new InnerDriverListener(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 138 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 139 | private ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 140 | |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 141 | // local stores for queuing fwd and next objectives that are waiting for an |
| 142 | // associated next objective execution to complete. The signal for completed |
| 143 | // execution comes from a pipeline driver, in this or another controller |
| 144 | // instance, via the DistributedFlowObjectiveStore. |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 145 | // TODO Making these cache and timeout the entries |
| 146 | final Map<Integer, Set<PendingFlowObjective>> pendingForwards = Maps.newConcurrentMap(); |
| 147 | final Map<Integer, List<PendingFlowObjective>> pendingNexts = Maps.newConcurrentMap(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 148 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 149 | // local store to track which nextObjectives were sent to which device |
| 150 | // for debugging purposes |
| 151 | private Map<Integer, DeviceId> nextToDevice = Maps.newConcurrentMap(); |
| 152 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 153 | ExecutorService executorService; |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 154 | protected ExecutorService devEventExecutor; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 155 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 156 | @Activate |
| 157 | protected void activate() { |
Ray Milkey | 1f0764a | 2019-03-01 08:40:37 -0800 | [diff] [blame] | 158 | cfgService.registerProperties(FlowObjectiveManager.class); |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 159 | executorService = newFixedThreadPool(numThreads, |
| 160 | groupedThreads(GROUP_THREAD_NAME, WORKER_PATTERN, log)); |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 161 | devEventExecutor = newSingleThreadScheduledExecutor( |
| 162 | groupedThreads("onos/flowobj-dev-events", "events-%d", log)); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 163 | flowObjectiveStore.setDelegate(delegate); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 164 | deviceService.addListener(deviceListener); |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 165 | driverService.addListener(driverListener); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 166 | log.info("Started"); |
| 167 | } |
| 168 | |
| 169 | @Deactivate |
| 170 | protected void deactivate() { |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 171 | cfgService.unregisterProperties(getClass(), false); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 172 | flowObjectiveStore.unsetDelegate(delegate); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 173 | deviceService.removeListener(deviceListener); |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 174 | driverService.removeListener(driverListener); |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 175 | executorService.shutdown(); |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 176 | devEventExecutor.shutdownNow(); |
| 177 | devEventExecutor = null; |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 178 | pipeliners.clear(); |
| 179 | driverHandlers.clear(); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 180 | nextToDevice.clear(); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 181 | log.info("Stopped"); |
| 182 | } |
| 183 | |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 184 | @Modified |
| 185 | protected void modified(ComponentContext context) { |
| 186 | String propertyValue = |
Ray Milkey | 1f0764a | 2019-03-01 08:40:37 -0800 | [diff] [blame] | 187 | Tools.get(context.getProperties(), FOM_NUM_THREADS); |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 188 | int newNumThreads = isNullOrEmpty(propertyValue) ? numThreads : Integer.parseInt(propertyValue); |
| 189 | |
| 190 | if (newNumThreads != numThreads && newNumThreads > 0) { |
| 191 | numThreads = newNumThreads; |
| 192 | ExecutorService oldWorkerExecutor = executorService; |
| 193 | executorService = newFixedThreadPool(numThreads, |
| 194 | groupedThreads(GROUP_THREAD_NAME, WORKER_PATTERN, log)); |
| 195 | if (oldWorkerExecutor != null) { |
| 196 | oldWorkerExecutor.shutdown(); |
| 197 | } |
| 198 | log.info("Reconfigured number of worker threads to {}", numThreads); |
| 199 | } |
| 200 | } |
| 201 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 202 | /** |
| 203 | * Task that passes the flow objective down to the driver. The task will |
| 204 | * make a few attempts to find the appropriate driver, then eventually give |
| 205 | * up and report an error if no suitable driver could be found. |
| 206 | */ |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 207 | class ObjectiveInstaller implements Runnable { |
| 208 | final DeviceId deviceId; |
| 209 | final Objective objective; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 210 | |
Sho SHIMIZU | f45d85d | 2015-07-01 14:39:11 -0700 | [diff] [blame] | 211 | private final int numAttempts; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 212 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 213 | ObjectiveInstaller(DeviceId deviceId, Objective objective) { |
Sho SHIMIZU | f45d85d | 2015-07-01 14:39:11 -0700 | [diff] [blame] | 214 | this(deviceId, objective, 1); |
| 215 | } |
| 216 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 217 | ObjectiveInstaller(DeviceId deviceId, Objective objective, int attemps) { |
Sho SHIMIZU | f45d85d | 2015-07-01 14:39:11 -0700 | [diff] [blame] | 218 | this.deviceId = checkNotNull(deviceId); |
| 219 | this.objective = checkNotNull(objective); |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 220 | this.numAttempts = attemps; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 221 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 222 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 223 | @Override |
| 224 | public void run() { |
| 225 | try { |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 226 | Pipeliner pipeliner = getDevicePipeliner(deviceId); |
| 227 | |
| 228 | if (pipeliner != null) { |
| 229 | if (objective instanceof NextObjective) { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 230 | nextToDevice.put(objective.id(), deviceId); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 231 | pipeliner.next((NextObjective) objective); |
| 232 | } else if (objective instanceof ForwardingObjective) { |
| 233 | pipeliner.forward((ForwardingObjective) objective); |
| 234 | } else { |
| 235 | pipeliner.filter((FilteringObjective) objective); |
| 236 | } |
Andrea Campanella | 1f8188d | 2016-02-29 13:24:54 -0800 | [diff] [blame] | 237 | //Attempts to check if pipeliner is null for retry attempts |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 238 | } else if (numAttempts < INSTALL_RETRY_ATTEMPTS) { |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 239 | Thread.sleep(INSTALL_RETRY_INTERVAL); |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 240 | executorService.execute(new ObjectiveInstaller(deviceId, objective, numAttempts + 1)); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 241 | } else { |
| 242 | // Otherwise we've tried a few times and failed, report an |
| 243 | // error back to the user. |
| 244 | objective.context().ifPresent( |
Andrea Campanella | 1f8188d | 2016-02-29 13:24:54 -0800 | [diff] [blame] | 245 | c -> c.onError(objective, ObjectiveError.NOPIPELINER)); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 246 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 247 | //Exception thrown |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 248 | } catch (Exception e) { |
| 249 | log.warn("Exception while installing flow objective", e); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public void filter(DeviceId deviceId, FilteringObjective filteringObjective) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 256 | checkPermission(FLOWRULE_WRITE); |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 257 | executorService.execute(new ObjectiveInstaller(deviceId, filteringObjective)); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Override |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 261 | public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 262 | checkPermission(FLOWRULE_WRITE); |
Yi Tseng | 1666b50 | 2017-05-17 11:05:18 -0700 | [diff] [blame] | 263 | if (forwardingObjective.nextId() == null || |
Yi Tseng | 1666b50 | 2017-05-17 11:05:18 -0700 | [diff] [blame] | 264 | flowObjectiveStore.getNextGroup(forwardingObjective.nextId()) != null || |
| 265 | !queueFwdObjective(deviceId, forwardingObjective)) { |
| 266 | // fast path |
| 267 | executorService.execute(new ObjectiveInstaller(deviceId, forwardingObjective)); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 268 | } |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 269 | } |
| 270 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 271 | @Override |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 272 | public void next(DeviceId deviceId, NextObjective nextObjective) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 273 | checkPermission(FLOWRULE_WRITE); |
Yi Tseng | 1666b50 | 2017-05-17 11:05:18 -0700 | [diff] [blame] | 274 | if (nextObjective.op() == Operation.ADD || |
Charles Chan | b71e1ba | 2018-08-16 21:02:34 -0700 | [diff] [blame] | 275 | nextObjective.op() == Operation.VERIFY || |
Yi Tseng | 1666b50 | 2017-05-17 11:05:18 -0700 | [diff] [blame] | 276 | flowObjectiveStore.getNextGroup(nextObjective.id()) != null || |
| 277 | !queueNextObjective(deviceId, nextObjective)) { |
| 278 | // either group exists or we are trying to create it - let it through |
| 279 | executorService.execute(new ObjectiveInstaller(deviceId, nextObjective)); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 280 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 281 | } |
| 282 | |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 283 | @Override |
| 284 | public int allocateNextId() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 285 | checkPermission(FLOWRULE_WRITE); |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 286 | return flowObjectiveStore.allocateNextId(); |
| 287 | } |
| 288 | |
Xin Jin | 313708b | 2015-07-09 13:43:04 -0700 | [diff] [blame] | 289 | @Override |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 290 | public void initPolicy(String policy) { |
| 291 | } |
Xin Jin | 313708b | 2015-07-09 13:43:04 -0700 | [diff] [blame] | 292 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 293 | boolean queueFwdObjective(DeviceId deviceId, ForwardingObjective fwd) { |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 294 | boolean queued = false; |
| 295 | synchronized (pendingForwards) { |
| 296 | // double check the flow objective store, because this block could run |
| 297 | // after a notification arrives |
| 298 | if (flowObjectiveStore.getNextGroup(fwd.nextId()) == null) { |
| 299 | pendingForwards.compute(fwd.nextId(), (id, pending) -> { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 300 | PendingFlowObjective pendfo = new PendingFlowObjective(deviceId, fwd); |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 301 | if (pending == null) { |
Pier Luigi | 9789311 | 2018-03-05 11:09:42 +0100 | [diff] [blame] | 302 | return Sets.newLinkedHashSet(ImmutableSet.of(pendfo)); |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 303 | } else { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 304 | pending.add(pendfo); |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 305 | return pending; |
| 306 | } |
| 307 | }); |
| 308 | queued = true; |
| 309 | } |
| 310 | } |
| 311 | if (queued) { |
Saurav Das | c568c34 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 312 | log.debug("Queued forwarding objective {} for nextId {} meant for device {}", |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 313 | fwd.id(), fwd.nextId(), deviceId); |
| 314 | } |
| 315 | return queued; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 318 | boolean queueNextObjective(DeviceId deviceId, NextObjective next) { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 319 | // we need to hold off on other operations till we get notified that the |
| 320 | // initial group creation has succeeded |
| 321 | boolean queued = false; |
| 322 | synchronized (pendingNexts) { |
| 323 | // double check the flow objective store, because this block could run |
| 324 | // after a notification arrives |
| 325 | if (flowObjectiveStore.getNextGroup(next.id()) == null) { |
| 326 | pendingNexts.compute(next.id(), (id, pending) -> { |
| 327 | PendingFlowObjective pendfo = new PendingFlowObjective(deviceId, next); |
| 328 | if (pending == null) { |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 329 | return Lists.newArrayList(pendfo); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 330 | } else { |
| 331 | pending.add(pendfo); |
| 332 | return pending; |
| 333 | } |
| 334 | }); |
| 335 | queued = true; |
| 336 | } |
| 337 | } |
| 338 | if (queued) { |
Saurav Das | c568c34 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 339 | log.debug("Queued next objective {} with operation {} meant for device {}", |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 340 | next.id(), next.op(), deviceId); |
| 341 | } |
| 342 | return queued; |
| 343 | } |
| 344 | |
Pier Ventre | 57a61cd | 2016-09-07 10:55:41 -0700 | [diff] [blame] | 345 | /** |
| 346 | * Retrieves (if it exists) the device pipeline behaviour from the cache. |
| 347 | * Otherwise it warms the caches and triggers the init method of the Pipeline. |
| 348 | * |
| 349 | * @param deviceId the id of the device associated to the pipeline |
| 350 | * @return the implementation of the Pipeliner behaviour |
| 351 | */ |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 352 | private Pipeliner getDevicePipeliner(DeviceId deviceId) { |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 353 | return pipeliners.computeIfAbsent(deviceId, this::initPipelineHandler); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 356 | /** |
Pier Ventre | 57a61cd | 2016-09-07 10:55:41 -0700 | [diff] [blame] | 357 | * Retrieves (if it exists) the device pipeline behaviour from the cache and |
| 358 | * and triggers the init method of the pipeline. Otherwise (DEVICE_ADDED) it warms |
| 359 | * the caches and triggers the init method of the Pipeline. The rationale of this |
| 360 | * method is for managing the scenario of a switch that goes down for a failure |
| 361 | * and goes up after a while. |
| 362 | * |
| 363 | * @param deviceId the id of the device associated to the pipeline |
| 364 | * @return the implementation of the Pipeliner behaviour |
| 365 | */ |
| 366 | private Pipeliner getAndInitDevicePipeliner(DeviceId deviceId) { |
| 367 | return pipeliners.compute(deviceId, (deviceIdValue, pipelinerValue) -> { |
| 368 | if (pipelinerValue != null) { |
| 369 | pipelinerValue.init(deviceId, context); |
| 370 | return pipelinerValue; |
| 371 | } |
| 372 | return this.initPipelineHandler(deviceId); |
| 373 | }); |
| 374 | } |
| 375 | |
| 376 | /** |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 377 | * Creates and initialize {@link Pipeliner}. |
| 378 | * <p> |
| 379 | * Note: Expected to be called under per-Device lock. |
| 380 | * e.g., {@code pipeliners}' Map#compute family methods |
| 381 | * |
| 382 | * @param deviceId Device to initialize pipeliner |
| 383 | * @return {@link Pipeliner} instance or null |
| 384 | */ |
| 385 | private Pipeliner initPipelineHandler(DeviceId deviceId) { |
| 386 | start = now(); |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 387 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 388 | // Attempt to lookup the handler in the cache |
| 389 | DriverHandler handler = driverHandlers.get(deviceId); |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 390 | cTime = now(); |
| 391 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 392 | if (handler == null) { |
| 393 | try { |
| 394 | // Otherwise create it and if it has pipeline behaviour, cache it |
| 395 | handler = driverService.createHandler(deviceId); |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 396 | dTime = now(); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 397 | if (!handler.driver().hasBehaviour(Pipeliner.class)) { |
Yuta HIGUCHI | a2a4f34 | 2017-03-17 11:38:57 -0700 | [diff] [blame] | 398 | log.debug("Pipeline behaviour not supported for device {}", |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 399 | deviceId); |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 400 | return null; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 401 | } |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 402 | } catch (ItemNotFoundException e) { |
| 403 | log.warn("No applicable driver for device {}", deviceId); |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 404 | return null; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 407 | driverHandlers.put(deviceId, handler); |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 408 | eTime = now(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 409 | } |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 410 | |
| 411 | // Always (re)initialize the pipeline behaviour |
| 412 | log.info("Driver {} bound to device {} ... initializing driver", |
| 413 | handler.driver().name(), deviceId); |
Thomas Vachuska | 0121a61 | 2015-07-21 11:18:09 -0700 | [diff] [blame] | 414 | hTime = now(); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 415 | Pipeliner pipeliner = handler.behaviour(Pipeliner.class); |
Thomas Vachuska | 94c3cf4 | 2015-07-20 13:01:12 -0700 | [diff] [blame] | 416 | hbTime = now(); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 417 | pipeliner.init(deviceId, context); |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 418 | stopWatch(); |
| 419 | return pipeliner; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 420 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 421 | |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 422 | private void invalidatePipelinerIfNecessary(Device device) { |
| 423 | DriverHandler handler = driverHandlers.get(device.id()); |
| 424 | if (handler != null && |
| 425 | !Objects.equals(handler.driver().name(), |
| 426 | device.annotations().value(DRIVER))) { |
| 427 | invalidatePipeliner(device.id()); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | private void invalidatePipeliner(DeviceId id) { |
| 432 | log.info("Invalidating cached pipeline behaviour for {}", id); |
| 433 | driverHandlers.remove(id); |
| 434 | pipeliners.remove(id); |
| 435 | if (deviceService.isAvailable(id)) { |
| 436 | getAndInitDevicePipeliner(id); |
| 437 | } |
| 438 | } |
| 439 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 440 | // Triggers driver setup when a device is (re)detected. |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 441 | private class InnerDeviceListener implements DeviceListener { |
| 442 | @Override |
| 443 | public void event(DeviceEvent event) { |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 444 | if (devEventExecutor != null) { |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 445 | switch (event.type()) { |
| 446 | case DEVICE_ADDED: |
| 447 | case DEVICE_AVAILABILITY_CHANGED: |
Madan Jampani | 0174f45 | 2015-05-29 11:52:05 -0700 | [diff] [blame] | 448 | log.debug("Device either added or availability changed {}", |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 449 | event.subject().id()); |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 450 | devEventExecutor.execute(() -> { |
| 451 | if (deviceService.isAvailable(event.subject().id())) { |
Madan Jampani | 0174f45 | 2015-05-29 11:52:05 -0700 | [diff] [blame] | 452 | log.debug("Device is now available {}", event.subject().id()); |
Pier Ventre | 57a61cd | 2016-09-07 10:55:41 -0700 | [diff] [blame] | 453 | getAndInitDevicePipeliner(event.subject().id()); |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 454 | } else { |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 455 | log.debug("Device is no longer available {}", event.subject().id()); |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 456 | } |
| 457 | }); |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 458 | break; |
| 459 | case DEVICE_UPDATED: |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 460 | // Invalidate pipeliner and handler caches if the driver name |
| 461 | // device annotation changed. |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 462 | devEventExecutor.execute(() -> invalidatePipelinerIfNecessary(event.subject())); |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 463 | break; |
| 464 | case DEVICE_REMOVED: |
Yuta HIGUCHI | ad0c990 | 2016-08-23 10:37:32 -0700 | [diff] [blame] | 465 | // evict Pipeliner and Handler cache, when |
| 466 | // the Device was administratively removed. |
| 467 | // |
| 468 | // System expect the user to clear all existing flows, |
| 469 | // before removing device, especially if they intend to |
| 470 | // replace driver/pipeliner assigned to the device. |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 471 | devEventExecutor.execute(() -> { |
| 472 | driverHandlers.remove(event.subject().id()); |
| 473 | pipeliners.remove(event.subject().id()); |
| 474 | }); |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 475 | break; |
Yuta HIGUCHI | ad0c990 | 2016-08-23 10:37:32 -0700 | [diff] [blame] | 476 | case DEVICE_SUSPENDED: |
| 477 | break; |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 478 | case PORT_ADDED: |
| 479 | break; |
| 480 | case PORT_UPDATED: |
| 481 | break; |
| 482 | case PORT_REMOVED: |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 483 | break; |
| 484 | default: |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 485 | break; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 486 | } |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 487 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 490 | |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 491 | // Monitors driver configuration changes and invalidates the pipeliner cache entries. |
| 492 | // Note that this may leave stale entries on the device if the driver changes |
| 493 | // in manner where the new driver does not produce backward compatible flow objectives. |
| 494 | // In such cases, it is the operator's responsibility to force device re-connect. |
| 495 | private class InnerDriverListener implements DriverListener { |
| 496 | @Override |
| 497 | public void event(DriverEvent event) { |
| 498 | String driverName = event.subject().name(); |
| 499 | driverHandlers.entrySet().stream() |
| 500 | .filter(e -> driverName.equals(e.getValue().driver().name())) |
| 501 | .map(Map.Entry::getKey) |
| 502 | .distinct() |
| 503 | .forEach(FlowObjectiveManager.this::invalidatePipeliner); |
| 504 | } |
| 505 | } |
| 506 | |
Thomas Vachuska | 174bb91 | 2015-07-16 21:27:14 -0700 | [diff] [blame] | 507 | // Temporary mechanism to monitor pipeliner setup time-cost; there are |
| 508 | // intermittent time where this takes in excess of 2 seconds. Why? |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 509 | private long start = 0, totals = 0, count = 0; |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 510 | private long cTime, dTime, eTime, hTime, hbTime; |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 511 | private static final long LIMIT = 500; |
Thomas Vachuska | 174bb91 | 2015-07-16 21:27:14 -0700 | [diff] [blame] | 512 | |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 513 | private long now() { |
Thomas Vachuska | 174bb91 | 2015-07-16 21:27:14 -0700 | [diff] [blame] | 514 | return System.currentTimeMillis(); |
| 515 | } |
| 516 | |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 517 | private void stopWatch() { |
Thomas Vachuska | 174bb91 | 2015-07-16 21:27:14 -0700 | [diff] [blame] | 518 | long duration = System.currentTimeMillis() - start; |
| 519 | totals += duration; |
| 520 | count += 1; |
| 521 | if (duration > LIMIT) { |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 522 | log.info("Pipeline setup took {} ms; avg {} ms; cTime={}, dTime={}, eTime={}, hTime={}, hbTime={}", |
| 523 | 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] | 524 | } |
| 525 | } |
| 526 | |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 527 | private long diff(long bTime) { |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 528 | long diff = bTime - start; |
| 529 | return diff < 0 ? 0 : diff; |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 532 | // Processing context for initializing pipeline driver behaviours. |
| 533 | private class InnerPipelineContext implements PipelinerContext { |
| 534 | @Override |
| 535 | public ServiceDirectory directory() { |
| 536 | return serviceDirectory; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 537 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 538 | |
| 539 | @Override |
| 540 | public FlowObjectiveStore store() { |
| 541 | return flowObjectiveStore; |
| 542 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | private class InternalStoreDelegate implements FlowObjectiveStoreDelegate { |
| 546 | @Override |
| 547 | public void notify(ObjectiveEvent event) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 548 | if (event.type() == Type.ADD) { |
| 549 | log.debug("Received notification of obj event {}", event); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 550 | Set<PendingFlowObjective> pending; |
| 551 | |
| 552 | // first send all pending flows |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 553 | synchronized (pendingForwards) { |
| 554 | // needs to be synchronized for queueObjective lookup |
| 555 | pending = pendingForwards.remove(event.subject()); |
| 556 | } |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 557 | if (pending == null) { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 558 | log.debug("No forwarding objectives pending for this " |
| 559 | + "obj event {}", event); |
| 560 | } else { |
| 561 | log.debug("Processing {} pending forwarding objectives for nextId {}", |
| 562 | pending.size(), event.subject()); |
| 563 | pending.forEach(p -> getDevicePipeliner(p.deviceId()) |
| 564 | .forward((ForwardingObjective) p.flowObjective())); |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 567 | // now check for pending next-objectives |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 568 | List<PendingFlowObjective> pendNexts; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 569 | synchronized (pendingNexts) { |
| 570 | // needs to be synchronized for queueObjective lookup |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 571 | pendNexts = pendingNexts.remove(event.subject()); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 572 | } |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 573 | if (pendNexts == null) { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 574 | log.debug("No next objectives pending for this " |
| 575 | + "obj event {}", event); |
| 576 | } else { |
| 577 | log.debug("Processing {} pending next objectives for nextId {}", |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 578 | pendNexts.size(), event.subject()); |
| 579 | pendNexts.forEach(p -> getDevicePipeliner(p.deviceId()) |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 580 | .next((NextObjective) p.flowObjective())); |
| 581 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 582 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
| 586 | /** |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 587 | * Data class used to hold a pending flow objective that could not |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 588 | * be processed because the associated next object was not present. |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 589 | * Note that this pending flow objective could be a forwarding objective |
| 590 | * waiting for a next objective to complete execution. Or it could a |
| 591 | * next objective (with a different operation - remove, addToExisting, or |
| 592 | * removeFromExisting) waiting for a next objective with the same id to |
| 593 | * complete execution. |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 594 | */ |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 595 | protected class PendingFlowObjective { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 596 | private final DeviceId deviceId; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 597 | private final Objective flowObj; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 598 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 599 | PendingFlowObjective(DeviceId deviceId, Objective flowObj) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 600 | this.deviceId = deviceId; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 601 | this.flowObj = flowObj; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | public DeviceId deviceId() { |
| 605 | return deviceId; |
| 606 | } |
| 607 | |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 608 | public Objective flowObjective() { |
| 609 | return flowObj; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 610 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 611 | |
| 612 | @Override |
| 613 | public int hashCode() { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 614 | return Objects.hash(deviceId, flowObj); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | @Override |
| 618 | public boolean equals(final Object obj) { |
| 619 | if (this == obj) { |
| 620 | return true; |
| 621 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 622 | if (!(obj instanceof PendingFlowObjective)) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 623 | return false; |
| 624 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 625 | final PendingFlowObjective other = (PendingFlowObjective) obj; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 626 | |
| 627 | return this.deviceId.equals(other.deviceId) && |
| 628 | this.flowObj.equals(other.flowObj); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 629 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 630 | } |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 631 | |
| 632 | @Override |
| 633 | public List<String> getNextMappings() { |
| 634 | List<String> mappings = new ArrayList<>(); |
| 635 | Map<Integer, NextGroup> allnexts = flowObjectiveStore.getAllGroups(); |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 636 | // 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] | 637 | // then info on any nextObj could be retrieved from one controller instance. |
| 638 | // Right now the drivers on one instance can only fetch for next-ids that came |
| 639 | // to them. |
| 640 | // Also, we still need to send the right next-id to the right driver as potentially |
| 641 | // there can be different drivers for different devices. But on that account, |
| 642 | // no instance should be decoding for another instance's nextIds. |
| 643 | |
| 644 | for (Map.Entry<Integer, NextGroup> e : allnexts.entrySet()) { |
| 645 | // get the device this next Objective was sent to |
| 646 | DeviceId deviceId = nextToDevice.get(e.getKey()); |
| 647 | mappings.add("NextId " + e.getKey() + ": " + |
| 648 | ((deviceId != null) ? deviceId : "nextId not in this onos instance")); |
| 649 | if (deviceId != null) { |
| 650 | // this instance of the controller sent the nextObj to a driver |
| 651 | Pipeliner pipeliner = getDevicePipeliner(deviceId); |
| 652 | List<String> nextMappings = pipeliner.getNextMappings(e.getValue()); |
| 653 | if (nextMappings != null) { |
| 654 | mappings.addAll(nextMappings); |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | return mappings; |
| 659 | } |
Saurav Das | b5c236e | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 660 | |
| 661 | @Override |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 662 | public List<String> getPendingFlowObjectives() { |
| 663 | List<String> pendingFlowObjectives = new ArrayList<>(); |
Charles Chan | 5473471 | 2017-03-29 11:07:55 -0700 | [diff] [blame] | 664 | |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 665 | for (Integer nextId : pendingForwards.keySet()) { |
| 666 | Set<PendingFlowObjective> pfwd = pendingForwards.get(nextId); |
Sho SHIMIZU | 81470a5 | 2016-08-12 17:24:55 -0700 | [diff] [blame] | 667 | StringBuilder pend = new StringBuilder(); |
Charles Chan | 5473471 | 2017-03-29 11:07:55 -0700 | [diff] [blame] | 668 | pend.append("NextId: ") |
| 669 | .append(nextId); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 670 | for (PendingFlowObjective pf : pfwd) { |
Charles Chan | 5473471 | 2017-03-29 11:07:55 -0700 | [diff] [blame] | 671 | pend.append("\n FwdId: ") |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 672 | .append(String.format("%11s", pf.flowObjective().id())) |
| 673 | .append(", DeviceId: ") |
| 674 | .append(pf.deviceId()) |
| 675 | .append(", Selector: ") |
| 676 | .append(((ForwardingObjective) pf.flowObjective()) |
| 677 | .selector().criteria()); |
| 678 | } |
| 679 | pendingFlowObjectives.add(pend.toString()); |
| 680 | } |
| 681 | |
| 682 | for (Integer nextId : pendingNexts.keySet()) { |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 683 | List<PendingFlowObjective> pnext = pendingNexts.get(nextId); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 684 | StringBuilder pend = new StringBuilder(); |
| 685 | pend.append("NextId: ") |
| 686 | .append(nextId); |
| 687 | for (PendingFlowObjective pn : pnext) { |
| 688 | pend.append("\n NextOp: ") |
| 689 | .append(pn.flowObjective().op()) |
Charles Chan | 5473471 | 2017-03-29 11:07:55 -0700 | [diff] [blame] | 690 | .append(", DeviceId: ") |
| 691 | .append(pn.deviceId()) |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 692 | .append(", Treatments: ") |
| 693 | .append(((NextObjective) pn.flowObjective()) |
| 694 | .next()); |
Saurav Das | b5c236e | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 695 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 696 | pendingFlowObjectives.add(pend.toString()); |
Saurav Das | b5c236e | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 697 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 698 | |
| 699 | return pendingFlowObjectives; |
| 700 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 701 | } |