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