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