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