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