blob: 3c5c571217f3e16d4bad21e366d6c67de87e10dd [file] [log] [blame]
alshabib77b88482015-04-07 15:47:50 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
alshabib77b88482015-04-07 15:47:50 -07003 *
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 */
16package org.onosproject.net.flowobjective.impl;
17
Pier Luigi97893112018-03-05 11:09:42 +010018import com.google.common.collect.ImmutableSet;
Harshada Chaundkar5a198b02019-07-03 16:27:45 +000019import com.google.common.collect.ImmutableList;
Saurav Dasf14d9ef2017-12-05 15:00:23 -080020import com.google.common.collect.Lists;
alshabib77b88482015-04-07 15:47:50 -070021import com.google.common.collect.Maps;
Thomas Vachuskad27097c2016-06-14 19:10:41 -070022import com.google.common.collect.Sets;
Harshada Chaundkar5a198b02019-07-03 16:27:45 +000023import org.apache.commons.lang3.tuple.Pair;
alshabib77b88482015-04-07 15:47:50 -070024import org.onlab.osgi.DefaultServiceDirectory;
25import org.onlab.osgi.ServiceDirectory;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070026import org.onlab.util.ItemNotFoundException;
Yi Tseng374c5f32017-03-05 22:51:35 -080027import org.onlab.util.Tools;
28import org.onosproject.cfg.ComponentConfigService;
alshabib77b88482015-04-07 15:47:50 -070029import org.onosproject.cluster.ClusterService;
Thomas Vachuskacfeff192017-08-23 15:29:34 -070030import org.onosproject.net.Device;
alshabib77b88482015-04-07 15:47:50 -070031import org.onosproject.net.DeviceId;
Saurav Das24431192016-03-07 19:13:00 -080032import org.onosproject.net.behaviour.NextGroup;
alshabib77b88482015-04-07 15:47:50 -070033import org.onosproject.net.behaviour.Pipeliner;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070034import org.onosproject.net.behaviour.PipelinerContext;
alshabibaebe7752015-04-07 17:45:42 -070035import org.onosproject.net.device.DeviceEvent;
36import org.onosproject.net.device.DeviceListener;
alshabib77b88482015-04-07 15:47:50 -070037import org.onosproject.net.device.DeviceService;
Thomas Vachuskacfeff192017-08-23 15:29:34 -070038import org.onosproject.net.driver.DriverEvent;
alshabib77b88482015-04-07 15:47:50 -070039import org.onosproject.net.driver.DriverHandler;
Thomas Vachuskacfeff192017-08-23 15:29:34 -070040import org.onosproject.net.driver.DriverListener;
alshabib77b88482015-04-07 15:47:50 -070041import org.onosproject.net.driver.DriverService;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070042import org.onosproject.net.flow.FlowRuleService;
alshabib77b88482015-04-07 15:47:50 -070043import org.onosproject.net.flowobjective.FilteringObjective;
44import org.onosproject.net.flowobjective.FlowObjectiveService;
alshabib2a441c62015-04-13 18:39:38 -070045import org.onosproject.net.flowobjective.FlowObjectiveStore;
46import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate;
alshabib77b88482015-04-07 15:47:50 -070047import org.onosproject.net.flowobjective.ForwardingObjective;
48import org.onosproject.net.flowobjective.NextObjective;
alshabib910aff12015-04-09 16:55:57 -070049import org.onosproject.net.flowobjective.Objective;
Saurav Das1547b3f2017-05-05 17:01:08 -070050import org.onosproject.net.flowobjective.Objective.Operation;
Jonathan Hart17d00452015-04-21 17:10:00 -070051import org.onosproject.net.flowobjective.ObjectiveError;
alshabib2a441c62015-04-13 18:39:38 -070052import org.onosproject.net.flowobjective.ObjectiveEvent;
Saurav Das423fe2b2015-12-04 10:52:59 -080053import org.onosproject.net.flowobjective.ObjectiveEvent.Type;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070054import org.onosproject.net.group.GroupService;
Yi Tseng374c5f32017-03-05 22:51:35 -080055import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056import org.osgi.service.component.annotations.Activate;
57import org.osgi.service.component.annotations.Component;
58import org.osgi.service.component.annotations.Deactivate;
59import org.osgi.service.component.annotations.Modified;
60import org.osgi.service.component.annotations.Reference;
61import org.osgi.service.component.annotations.ReferenceCardinality;
alshabib77b88482015-04-07 15:47:50 -070062import org.slf4j.Logger;
63import org.slf4j.LoggerFactory;
64
Saurav Das24431192016-03-07 19:13:00 -080065import java.util.ArrayList;
Saurav Das24431192016-03-07 19:13:00 -080066import java.util.List;
alshabib77b88482015-04-07 15:47:50 -070067import java.util.Map;
Saurav Das8a0732e2015-11-20 15:27:53 -080068import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070069import java.util.Set;
Harshada Chaundkar5a198b02019-07-03 16:27:45 +000070import java.util.HashMap;
Jonathan Hart17d00452015-04-21 17:10:00 -070071import java.util.concurrent.ExecutorService;
alshabib77b88482015-04-07 15:47:50 -070072
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -070073import static com.google.common.base.Preconditions.checkNotNull;
Yi Tseng374c5f32017-03-05 22:51:35 -080074import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuska866b46a2015-04-30 00:26:55 -070075import static java.util.concurrent.Executors.newFixedThreadPool;
Ruchi Sahotae4934e12019-03-01 16:56:07 +000076import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Jonathan Hart17d00452015-04-21 17:10:00 -070077import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskacfeff192017-08-23 15:29:34 -070078import static org.onosproject.net.AnnotationKeys.DRIVER;
Ray Milkeyd04e2272018-10-16 18:20:18 -070079import static org.onosproject.net.OsgiPropertyConstants.FOM_NUM_THREADS;
80import static org.onosproject.net.OsgiPropertyConstants.FOM_NUM_THREADS_DEFAULT;
pier8b3aef42019-03-11 15:14:02 -070081import static org.onosproject.net.OsgiPropertyConstants.FOM_ACCUMULATOR_MAX_OBJECTIVES;
82import static org.onosproject.net.OsgiPropertyConstants.FOM_ACCUMULATOR_MAX_OBJECTIVES_DEFAULT;
83import static org.onosproject.net.OsgiPropertyConstants.FOM_ACCUMULATOR_MAX_IDLE_MILLIS;
84import static org.onosproject.net.OsgiPropertyConstants.FOM_ACCUMULATOR_MAX_IDLE_MILLIS_DEFAULT;
85import static org.onosproject.net.OsgiPropertyConstants.FOM_ACCUMULATOR_MAX_BATCH_MILLIS;
86import static org.onosproject.net.OsgiPropertyConstants.FOM_ACCUMULATOR_MAX_BATCH_MILLIS_DEFAULT;
Changhoon Yoon541ef712015-05-23 17:18:34 +090087import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskad27097c2016-06-14 19:10:41 -070088import static org.onosproject.security.AppPermission.Type.FLOWRULE_WRITE;
alshabib77b88482015-04-07 15:47:50 -070089
90/**
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070091 * Provides implementation of the flow objective programming service.
alshabib77b88482015-04-07 15:47:50 -070092 */
Ray Milkeyd04e2272018-10-16 18:20:18 -070093@Component(
94 enabled = false,
95 service = FlowObjectiveService.class,
96 property = {
pier8b3aef42019-03-11 15:14:02 -070097 FOM_NUM_THREADS + ":Integer=" + FOM_NUM_THREADS_DEFAULT,
98 FOM_ACCUMULATOR_MAX_OBJECTIVES + ":Integer=" + FOM_ACCUMULATOR_MAX_OBJECTIVES_DEFAULT,
99 FOM_ACCUMULATOR_MAX_IDLE_MILLIS + ":Integer=" + FOM_ACCUMULATOR_MAX_IDLE_MILLIS_DEFAULT,
100 FOM_ACCUMULATOR_MAX_BATCH_MILLIS + ":Integer=" + FOM_ACCUMULATOR_MAX_BATCH_MILLIS_DEFAULT,
Ray Milkeyd04e2272018-10-16 18:20:18 -0700101 }
102)
alshabib77b88482015-04-07 15:47:50 -0700103public class FlowObjectiveManager implements FlowObjectiveService {
104
Charles Chana7903c82018-03-15 20:14:16 -0700105 private static final int INSTALL_RETRY_ATTEMPTS = 5;
106 private static final long INSTALL_RETRY_INTERVAL = 1000; // ms
alshabib77b88482015-04-07 15:47:50 -0700107
Yi Tseng374c5f32017-03-05 22:51:35 -0800108 private static final String WORKER_PATTERN = "objective-installer-%d";
109 private static final String GROUP_THREAD_NAME = "onos/objective-installer";
Yi Tseng374c5f32017-03-05 22:51:35 -0800110
Jonathan Hart17d00452015-04-21 17:10:00 -0700111 private final Logger log = LoggerFactory.getLogger(getClass());
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700112
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700113 /** Number of worker threads. */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700114 private int numThreads = FOM_NUM_THREADS_DEFAULT;
Yi Tseng374c5f32017-03-05 22:51:35 -0800115
pier8b3aef42019-03-11 15:14:02 -0700116 // Parameters for the accumulator, each pipeline can implement
117 // its own accumulation logic. The following parameters are used
118 // to control the accumulator.
119
120 // Maximum number of objectives to accumulate before processing is triggered
121 private int accumulatorMaxObjectives = FOM_ACCUMULATOR_MAX_OBJECTIVES_DEFAULT;
122 // Maximum number of millis between objectives before processing is triggered
123 private int accumulatorMaxIdleMillis = FOM_ACCUMULATOR_MAX_IDLE_MILLIS_DEFAULT;
124 // Maximum number of millis allowed since the first objective before processing is triggered
125 private int accumulatorMaxBatchMillis = FOM_ACCUMULATOR_MAX_BATCH_MILLIS_DEFAULT;
126
127
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700128 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib77b88482015-04-07 15:47:50 -0700129 protected DriverService driverService;
130
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700131 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib77b88482015-04-07 15:47:50 -0700132 protected DeviceService deviceService;
133
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700134 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib77b88482015-04-07 15:47:50 -0700135 protected ClusterService clusterService;
136
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700137 // Note: The following dependencies are added on behalf of the pipeline
138 // driver behaviours to assure these services are available for their
139 // initialization.
Charles Chana7903c82018-03-15 20:14:16 -0700140 @SuppressWarnings("unused")
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700141 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700142 protected FlowRuleService flowRuleService;
143
Charles Chana7903c82018-03-15 20:14:16 -0700144 @SuppressWarnings("unused")
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700145 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700146 protected GroupService groupService;
147
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700148 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib2a441c62015-04-13 18:39:38 -0700149 protected FlowObjectiveStore flowObjectiveStore;
150
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700151 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tseng374c5f32017-03-05 22:51:35 -0800152 protected ComponentConfigService cfgService;
153
Charles Chana7903c82018-03-15 20:14:16 -0700154 final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700155
156 private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap();
Charles Chana7903c82018-03-15 20:14:16 -0700157 protected final Map<DeviceId, Pipeliner> pipeliners = Maps.newConcurrentMap();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700158
159 private final PipelinerContext context = new InnerPipelineContext();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700160 private final DeviceListener deviceListener = new InnerDeviceListener();
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700161 private final DriverListener driverListener = new InnerDriverListener();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700162
Charles Chana7903c82018-03-15 20:14:16 -0700163 private ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
alshabib910aff12015-04-09 16:55:57 -0700164
Saurav Das1547b3f2017-05-05 17:01:08 -0700165 // local stores for queuing fwd and next objectives that are waiting for an
166 // associated next objective execution to complete. The signal for completed
167 // execution comes from a pipeline driver, in this or another controller
168 // instance, via the DistributedFlowObjectiveStore.
Charles Chana7903c82018-03-15 20:14:16 -0700169 // TODO Making these cache and timeout the entries
170 final Map<Integer, Set<PendingFlowObjective>> pendingForwards = Maps.newConcurrentMap();
171 final Map<Integer, List<PendingFlowObjective>> pendingNexts = Maps.newConcurrentMap();
alshabib2a441c62015-04-13 18:39:38 -0700172
Saurav Das24431192016-03-07 19:13:00 -0800173 // local store to track which nextObjectives were sent to which device
174 // for debugging purposes
175 private Map<Integer, DeviceId> nextToDevice = Maps.newConcurrentMap();
176
Charles Chana7903c82018-03-15 20:14:16 -0700177 ExecutorService executorService;
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000178 protected ExecutorService devEventExecutor;
alshabib2a441c62015-04-13 18:39:38 -0700179
alshabib77b88482015-04-07 15:47:50 -0700180 @Activate
pier8b3aef42019-03-11 15:14:02 -0700181 protected void activate(ComponentContext context) {
Ray Milkey1f0764a2019-03-01 08:40:37 -0800182 cfgService.registerProperties(FlowObjectiveManager.class);
Yi Tseng374c5f32017-03-05 22:51:35 -0800183 executorService = newFixedThreadPool(numThreads,
184 groupedThreads(GROUP_THREAD_NAME, WORKER_PATTERN, log));
pier8b3aef42019-03-11 15:14:02 -0700185 modified(context);
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000186 devEventExecutor = newSingleThreadScheduledExecutor(
187 groupedThreads("onos/flowobj-dev-events", "events-%d", log));
alshabib2a441c62015-04-13 18:39:38 -0700188 flowObjectiveStore.setDelegate(delegate);
alshabibaebe7752015-04-07 17:45:42 -0700189 deviceService.addListener(deviceListener);
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700190 driverService.addListener(driverListener);
alshabib77b88482015-04-07 15:47:50 -0700191 log.info("Started");
192 }
193
194 @Deactivate
195 protected void deactivate() {
Yi Tseng374c5f32017-03-05 22:51:35 -0800196 cfgService.unregisterProperties(getClass(), false);
alshabib2a441c62015-04-13 18:39:38 -0700197 flowObjectiveStore.unsetDelegate(delegate);
alshabibaebe7752015-04-07 17:45:42 -0700198 deviceService.removeListener(deviceListener);
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700199 driverService.removeListener(driverListener);
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700200 executorService.shutdown();
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000201 devEventExecutor.shutdownNow();
202 devEventExecutor = null;
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700203 pipeliners.clear();
204 driverHandlers.clear();
Saurav Das24431192016-03-07 19:13:00 -0800205 nextToDevice.clear();
alshabib77b88482015-04-07 15:47:50 -0700206 log.info("Stopped");
207 }
208
Yi Tseng374c5f32017-03-05 22:51:35 -0800209 @Modified
210 protected void modified(ComponentContext context) {
pier8b3aef42019-03-11 15:14:02 -0700211 if (context != null) {
212 readComponentConfiguration(context);
213 }
214 }
215
216 /**
217 * Extracts properties from the component configuration context.
218 *
219 * @param context the component context
220 */
221 private void readComponentConfiguration(ComponentContext context) {
222 String propertyValue = Tools.get(context.getProperties(), FOM_NUM_THREADS);
Yi Tseng374c5f32017-03-05 22:51:35 -0800223 int newNumThreads = isNullOrEmpty(propertyValue) ? numThreads : Integer.parseInt(propertyValue);
224
225 if (newNumThreads != numThreads && newNumThreads > 0) {
226 numThreads = newNumThreads;
227 ExecutorService oldWorkerExecutor = executorService;
228 executorService = newFixedThreadPool(numThreads,
229 groupedThreads(GROUP_THREAD_NAME, WORKER_PATTERN, log));
230 if (oldWorkerExecutor != null) {
231 oldWorkerExecutor.shutdown();
232 }
233 log.info("Reconfigured number of worker threads to {}", numThreads);
234 }
pier8b3aef42019-03-11 15:14:02 -0700235
236 // Reconfiguration of the accumulator parameters is allowed
237 // Note: it will affect only pipelines going through init method
238 propertyValue = Tools.get(context.getProperties(), FOM_ACCUMULATOR_MAX_OBJECTIVES);
239 int newMaxObjs = isNullOrEmpty(propertyValue) ?
240 accumulatorMaxObjectives : Integer.parseInt(propertyValue);
241 if (newMaxObjs != accumulatorMaxObjectives && newMaxObjs > 0) {
242 accumulatorMaxObjectives = newMaxObjs;
243 log.info("Reconfigured maximum number of objectives to accumulate to {}",
244 accumulatorMaxObjectives);
245 }
246
247 propertyValue = Tools.get(context.getProperties(), FOM_ACCUMULATOR_MAX_IDLE_MILLIS);
248 int newMaxIdleMS = isNullOrEmpty(propertyValue) ?
249 accumulatorMaxIdleMillis : Integer.parseInt(propertyValue);
250 if (newMaxIdleMS != accumulatorMaxIdleMillis && newMaxIdleMS > 0) {
251 accumulatorMaxIdleMillis = newMaxIdleMS;
252 log.info("Reconfigured maximum number of millis between objectives to {}",
253 accumulatorMaxIdleMillis);
254 }
255
256 propertyValue = Tools.get(context.getProperties(), FOM_ACCUMULATOR_MAX_BATCH_MILLIS);
257 int newMaxBatchMS = isNullOrEmpty(propertyValue) ?
258 accumulatorMaxBatchMillis : Integer.parseInt(propertyValue);
259 if (newMaxBatchMS != accumulatorMaxBatchMillis && newMaxBatchMS > 0) {
260 accumulatorMaxBatchMillis = newMaxBatchMS;
261 log.info("Reconfigured maximum number of millis allowed since the first objective to {}",
262 accumulatorMaxBatchMillis);
263 }
264
Yi Tseng374c5f32017-03-05 22:51:35 -0800265 }
266
Jonathan Hart17d00452015-04-21 17:10:00 -0700267 /**
268 * Task that passes the flow objective down to the driver. The task will
269 * make a few attempts to find the appropriate driver, then eventually give
270 * up and report an error if no suitable driver could be found.
271 */
Charles Chana7903c82018-03-15 20:14:16 -0700272 class ObjectiveInstaller implements Runnable {
273 final DeviceId deviceId;
274 final Objective objective;
Jonathan Hart17d00452015-04-21 17:10:00 -0700275
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700276 private final int numAttempts;
Jonathan Hart17d00452015-04-21 17:10:00 -0700277
Charles Chana7903c82018-03-15 20:14:16 -0700278 ObjectiveInstaller(DeviceId deviceId, Objective objective) {
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700279 this(deviceId, objective, 1);
280 }
281
Charles Chana7903c82018-03-15 20:14:16 -0700282 ObjectiveInstaller(DeviceId deviceId, Objective objective, int attemps) {
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700283 this.deviceId = checkNotNull(deviceId);
284 this.objective = checkNotNull(objective);
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800285 this.numAttempts = attemps;
alshabib910aff12015-04-09 16:55:57 -0700286 }
alshabib77b88482015-04-07 15:47:50 -0700287
Jonathan Hart17d00452015-04-21 17:10:00 -0700288 @Override
289 public void run() {
290 try {
Jonathan Hart17d00452015-04-21 17:10:00 -0700291 Pipeliner pipeliner = getDevicePipeliner(deviceId);
292
pier1b7dd122020-02-28 09:24:11 +0100293 if (pipeliner != null && pipeliner.isReady()) {
Jonathan Hart17d00452015-04-21 17:10:00 -0700294 if (objective instanceof NextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700295 nextToDevice.put(objective.id(), deviceId);
Jonathan Hart17d00452015-04-21 17:10:00 -0700296 pipeliner.next((NextObjective) objective);
297 } else if (objective instanceof ForwardingObjective) {
298 pipeliner.forward((ForwardingObjective) objective);
299 } else {
300 pipeliner.filter((FilteringObjective) objective);
301 }
Andrea Campanella1f8188d2016-02-29 13:24:54 -0800302 //Attempts to check if pipeliner is null for retry attempts
Jonathan Hart17d00452015-04-21 17:10:00 -0700303 } else if (numAttempts < INSTALL_RETRY_ATTEMPTS) {
Saurav Das3d038262015-04-23 12:36:58 -0700304 Thread.sleep(INSTALL_RETRY_INTERVAL);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700305 executorService.execute(new ObjectiveInstaller(deviceId, objective, numAttempts + 1));
Jonathan Hart17d00452015-04-21 17:10:00 -0700306 } else {
307 // Otherwise we've tried a few times and failed, report an
308 // error back to the user.
309 objective.context().ifPresent(
Andrea Campanella1f8188d2016-02-29 13:24:54 -0800310 c -> c.onError(objective, ObjectiveError.NOPIPELINER));
Jonathan Hart17d00452015-04-21 17:10:00 -0700311 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700312 //Exception thrown
Jonathan Hart17d00452015-04-21 17:10:00 -0700313 } catch (Exception e) {
314 log.warn("Exception while installing flow objective", e);
315 }
316 }
317 }
318
319 @Override
320 public void filter(DeviceId deviceId, FilteringObjective filteringObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900321 checkPermission(FLOWRULE_WRITE);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700322 executorService.execute(new ObjectiveInstaller(deviceId, filteringObjective));
alshabib77b88482015-04-07 15:47:50 -0700323 }
324
325 @Override
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700326 public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900327 checkPermission(FLOWRULE_WRITE);
Yi Tseng1666b502017-05-17 11:05:18 -0700328 if (forwardingObjective.nextId() == null ||
Yi Tseng1666b502017-05-17 11:05:18 -0700329 flowObjectiveStore.getNextGroup(forwardingObjective.nextId()) != null ||
330 !queueFwdObjective(deviceId, forwardingObjective)) {
331 // fast path
332 executorService.execute(new ObjectiveInstaller(deviceId, forwardingObjective));
alshabib910aff12015-04-09 16:55:57 -0700333 }
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700334 }
335
alshabib2a441c62015-04-13 18:39:38 -0700336 @Override
Jonathan Hart17d00452015-04-21 17:10:00 -0700337 public void next(DeviceId deviceId, NextObjective nextObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900338 checkPermission(FLOWRULE_WRITE);
Yi Tseng1666b502017-05-17 11:05:18 -0700339 if (nextObjective.op() == Operation.ADD ||
Charles Chanb71e1ba2018-08-16 21:02:34 -0700340 nextObjective.op() == Operation.VERIFY ||
Yi Tseng1666b502017-05-17 11:05:18 -0700341 flowObjectiveStore.getNextGroup(nextObjective.id()) != null ||
342 !queueNextObjective(deviceId, nextObjective)) {
343 // either group exists or we are trying to create it - let it through
344 executorService.execute(new ObjectiveInstaller(deviceId, nextObjective));
Saurav Das1547b3f2017-05-05 17:01:08 -0700345 }
alshabib2a441c62015-04-13 18:39:38 -0700346 }
347
alshabibf6ea9e62015-04-21 17:08:26 -0700348 @Override
349 public int allocateNextId() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900350 checkPermission(FLOWRULE_WRITE);
alshabibf6ea9e62015-04-21 17:08:26 -0700351 return flowObjectiveStore.allocateNextId();
352 }
353
Xin Jin313708b2015-07-09 13:43:04 -0700354 @Override
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700355 public void initPolicy(String policy) {
356 }
Xin Jin313708b2015-07-09 13:43:04 -0700357
Charles Chana7903c82018-03-15 20:14:16 -0700358 boolean queueFwdObjective(DeviceId deviceId, ForwardingObjective fwd) {
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700359 boolean queued = false;
360 synchronized (pendingForwards) {
361 // double check the flow objective store, because this block could run
362 // after a notification arrives
363 if (flowObjectiveStore.getNextGroup(fwd.nextId()) == null) {
364 pendingForwards.compute(fwd.nextId(), (id, pending) -> {
Saurav Das1547b3f2017-05-05 17:01:08 -0700365 PendingFlowObjective pendfo = new PendingFlowObjective(deviceId, fwd);
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700366 if (pending == null) {
Pier Luigi97893112018-03-05 11:09:42 +0100367 return Sets.newLinkedHashSet(ImmutableSet.of(pendfo));
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700368 } else {
Saurav Das1547b3f2017-05-05 17:01:08 -0700369 pending.add(pendfo);
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700370 return pending;
371 }
372 });
373 queued = true;
374 }
375 }
376 if (queued) {
Saurav Dasc568c342018-01-25 09:49:01 -0800377 log.debug("Queued forwarding objective {} for nextId {} meant for device {}",
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700378 fwd.id(), fwd.nextId(), deviceId);
379 }
380 return queued;
alshabib2a441c62015-04-13 18:39:38 -0700381 }
382
Charles Chana7903c82018-03-15 20:14:16 -0700383 boolean queueNextObjective(DeviceId deviceId, NextObjective next) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700384 // we need to hold off on other operations till we get notified that the
385 // initial group creation has succeeded
386 boolean queued = false;
387 synchronized (pendingNexts) {
388 // double check the flow objective store, because this block could run
389 // after a notification arrives
390 if (flowObjectiveStore.getNextGroup(next.id()) == null) {
391 pendingNexts.compute(next.id(), (id, pending) -> {
392 PendingFlowObjective pendfo = new PendingFlowObjective(deviceId, next);
393 if (pending == null) {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800394 return Lists.newArrayList(pendfo);
Saurav Das1547b3f2017-05-05 17:01:08 -0700395 } else {
396 pending.add(pendfo);
397 return pending;
398 }
399 });
400 queued = true;
401 }
402 }
403 if (queued) {
Saurav Dasc568c342018-01-25 09:49:01 -0800404 log.debug("Queued next objective {} with operation {} meant for device {}",
Saurav Das1547b3f2017-05-05 17:01:08 -0700405 next.id(), next.op(), deviceId);
406 }
407 return queued;
408 }
409
Pier Ventre57a61cd2016-09-07 10:55:41 -0700410 /**
411 * Retrieves (if it exists) the device pipeline behaviour from the cache.
412 * Otherwise it warms the caches and triggers the init method of the Pipeline.
413 *
414 * @param deviceId the id of the device associated to the pipeline
415 * @return the implementation of the Pipeliner behaviour
416 */
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700417 private Pipeliner getDevicePipeliner(DeviceId deviceId) {
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700418 return pipeliners.computeIfAbsent(deviceId, this::initPipelineHandler);
alshabib77b88482015-04-07 15:47:50 -0700419 }
420
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700421 /**
Pier Ventre57a61cd2016-09-07 10:55:41 -0700422 * Retrieves (if it exists) the device pipeline behaviour from the cache and
423 * and triggers the init method of the pipeline. Otherwise (DEVICE_ADDED) it warms
424 * the caches and triggers the init method of the Pipeline. The rationale of this
425 * method is for managing the scenario of a switch that goes down for a failure
426 * and goes up after a while.
427 *
428 * @param deviceId the id of the device associated to the pipeline
429 * @return the implementation of the Pipeliner behaviour
430 */
431 private Pipeliner getAndInitDevicePipeliner(DeviceId deviceId) {
432 return pipeliners.compute(deviceId, (deviceIdValue, pipelinerValue) -> {
433 if (pipelinerValue != null) {
434 pipelinerValue.init(deviceId, context);
435 return pipelinerValue;
436 }
437 return this.initPipelineHandler(deviceId);
438 });
439 }
440
441 /**
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700442 * Creates and initialize {@link Pipeliner}.
443 * <p>
444 * Note: Expected to be called under per-Device lock.
445 * e.g., {@code pipeliners}' Map#compute family methods
446 *
447 * @param deviceId Device to initialize pipeliner
448 * @return {@link Pipeliner} instance or null
449 */
450 private Pipeliner initPipelineHandler(DeviceId deviceId) {
451 start = now();
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700452
Jonathan Hart17d00452015-04-21 17:10:00 -0700453 // Attempt to lookup the handler in the cache
454 DriverHandler handler = driverHandlers.get(deviceId);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700455 cTime = now();
456
Jonathan Hart17d00452015-04-21 17:10:00 -0700457 if (handler == null) {
458 try {
459 // Otherwise create it and if it has pipeline behaviour, cache it
460 handler = driverService.createHandler(deviceId);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700461 dTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700462 if (!handler.driver().hasBehaviour(Pipeliner.class)) {
Yuta HIGUCHIa2a4f342017-03-17 11:38:57 -0700463 log.debug("Pipeline behaviour not supported for device {}",
Jonathan Hart17d00452015-04-21 17:10:00 -0700464 deviceId);
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700465 return null;
alshabib2a441c62015-04-13 18:39:38 -0700466 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700467 } catch (ItemNotFoundException e) {
468 log.warn("No applicable driver for device {}", deviceId);
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700469 return null;
alshabib2a441c62015-04-13 18:39:38 -0700470 }
471
Jonathan Hart17d00452015-04-21 17:10:00 -0700472 driverHandlers.put(deviceId, handler);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700473 eTime = now();
alshabib2a441c62015-04-13 18:39:38 -0700474 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700475
476 // Always (re)initialize the pipeline behaviour
477 log.info("Driver {} bound to device {} ... initializing driver",
478 handler.driver().name(), deviceId);
Thomas Vachuska0121a612015-07-21 11:18:09 -0700479 hTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700480 Pipeliner pipeliner = handler.behaviour(Pipeliner.class);
Thomas Vachuska94c3cf42015-07-20 13:01:12 -0700481 hbTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700482 pipeliner.init(deviceId, context);
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700483 stopWatch();
484 return pipeliner;
alshabibaebe7752015-04-07 17:45:42 -0700485 }
alshabib77b88482015-04-07 15:47:50 -0700486
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700487 private void invalidatePipelinerIfNecessary(Device device) {
488 DriverHandler handler = driverHandlers.get(device.id());
489 if (handler != null &&
490 !Objects.equals(handler.driver().name(),
491 device.annotations().value(DRIVER))) {
492 invalidatePipeliner(device.id());
493 }
494 }
495
496 private void invalidatePipeliner(DeviceId id) {
497 log.info("Invalidating cached pipeline behaviour for {}", id);
498 driverHandlers.remove(id);
pierf37ce522020-03-20 11:00:38 +0100499 Pipeliner pipeliner = pipeliners.remove(id);
500 if (pipeliner != null) {
501 pipeliner.cleanUp();
502 }
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700503 if (deviceService.isAvailable(id)) {
504 getAndInitDevicePipeliner(id);
505 }
506 }
507
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700508 // Triggers driver setup when a device is (re)detected.
alshabibaebe7752015-04-07 17:45:42 -0700509 private class InnerDeviceListener implements DeviceListener {
510 @Override
511 public void event(DeviceEvent event) {
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000512 if (devEventExecutor != null) {
alshabibaebe7752015-04-07 17:45:42 -0700513 switch (event.type()) {
514 case DEVICE_ADDED:
515 case DEVICE_AVAILABILITY_CHANGED:
Madan Jampani0174f452015-05-29 11:52:05 -0700516 log.debug("Device either added or availability changed {}",
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700517 event.subject().id());
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000518 devEventExecutor.execute(() -> {
519 if (deviceService.isAvailable(event.subject().id())) {
Madan Jampani0174f452015-05-29 11:52:05 -0700520 log.debug("Device is now available {}", event.subject().id());
Pier Ventre57a61cd2016-09-07 10:55:41 -0700521 getAndInitDevicePipeliner(event.subject().id());
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000522 } else {
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700523 log.debug("Device is no longer available {}", event.subject().id());
pierf37ce522020-03-20 11:00:38 +0100524 getDevicePipeliner(event.subject().id()).cleanUp();
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000525 }
526 });
alshabib4313d102015-04-08 18:55:08 -0700527 break;
528 case DEVICE_UPDATED:
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700529 // Invalidate pipeliner and handler caches if the driver name
530 // device annotation changed.
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000531 devEventExecutor.execute(() -> invalidatePipelinerIfNecessary(event.subject()));
alshabib4313d102015-04-08 18:55:08 -0700532 break;
533 case DEVICE_REMOVED:
Yuta HIGUCHIad0c9902016-08-23 10:37:32 -0700534 // evict Pipeliner and Handler cache, when
535 // the Device was administratively removed.
536 //
537 // System expect the user to clear all existing flows,
538 // before removing device, especially if they intend to
539 // replace driver/pipeliner assigned to the device.
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000540 devEventExecutor.execute(() -> {
pierf37ce522020-03-20 11:00:38 +0100541 driverHandlers.remove(event.subject().id());
542 Pipeliner pipeliner = pipeliners.remove(event.subject().id());
543 if (pipeliner != null) {
544 pipeliner.cleanUp();
545 }
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000546 });
alshabib4313d102015-04-08 18:55:08 -0700547 break;
Yuta HIGUCHIad0c9902016-08-23 10:37:32 -0700548 case DEVICE_SUSPENDED:
549 break;
alshabib4313d102015-04-08 18:55:08 -0700550 case PORT_ADDED:
551 break;
552 case PORT_UPDATED:
553 break;
554 case PORT_REMOVED:
alshabibaebe7752015-04-07 17:45:42 -0700555 break;
556 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700557 break;
alshabibaebe7752015-04-07 17:45:42 -0700558 }
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000559 }
alshabib77b88482015-04-07 15:47:50 -0700560 }
561 }
alshabibaebe7752015-04-07 17:45:42 -0700562
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700563 // Monitors driver configuration changes and invalidates the pipeliner cache entries.
564 // Note that this may leave stale entries on the device if the driver changes
565 // in manner where the new driver does not produce backward compatible flow objectives.
566 // In such cases, it is the operator's responsibility to force device re-connect.
567 private class InnerDriverListener implements DriverListener {
568 @Override
569 public void event(DriverEvent event) {
570 String driverName = event.subject().name();
571 driverHandlers.entrySet().stream()
572 .filter(e -> driverName.equals(e.getValue().driver().name()))
573 .map(Map.Entry::getKey)
574 .distinct()
575 .forEach(FlowObjectiveManager.this::invalidatePipeliner);
576 }
577 }
578
Thomas Vachuska174bb912015-07-16 21:27:14 -0700579 // Temporary mechanism to monitor pipeliner setup time-cost; there are
580 // intermittent time where this takes in excess of 2 seconds. Why?
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700581 private long start = 0, totals = 0, count = 0;
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700582 private long cTime, dTime, eTime, hTime, hbTime;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700583 private static final long LIMIT = 500;
Thomas Vachuska174bb912015-07-16 21:27:14 -0700584
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700585 private long now() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700586 return System.currentTimeMillis();
587 }
588
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700589 private void stopWatch() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700590 long duration = System.currentTimeMillis() - start;
591 totals += duration;
592 count += 1;
593 if (duration > LIMIT) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700594 log.info("Pipeline setup took {} ms; avg {} ms; cTime={}, dTime={}, eTime={}, hTime={}, hbTime={}",
595 duration, totals / count, diff(cTime), diff(dTime), diff(eTime), diff(hTime), diff(hbTime));
Thomas Vachuska174bb912015-07-16 21:27:14 -0700596 }
597 }
598
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700599 private long diff(long bTime) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700600 long diff = bTime - start;
601 return diff < 0 ? 0 : diff;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700602 }
603
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700604 // Processing context for initializing pipeline driver behaviours.
605 private class InnerPipelineContext implements PipelinerContext {
pier8b3aef42019-03-11 15:14:02 -0700606
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700607 @Override
608 public ServiceDirectory directory() {
609 return serviceDirectory;
alshabibaebe7752015-04-07 17:45:42 -0700610 }
alshabib2a441c62015-04-13 18:39:38 -0700611
612 @Override
613 public FlowObjectiveStore store() {
614 return flowObjectiveStore;
615 }
pier8b3aef42019-03-11 15:14:02 -0700616
617 @Override
618 public int accumulatorMaxObjectives() {
619 return accumulatorMaxObjectives;
620 }
621
622 @Override
623 public int accumulatorMaxIdleMillis() {
624 return accumulatorMaxIdleMillis;
625 }
626
627 @Override
628 public int accumulatorMaxBatchMillis() {
629 return accumulatorMaxBatchMillis;
630 }
631
alshabib2a441c62015-04-13 18:39:38 -0700632 }
633
634 private class InternalStoreDelegate implements FlowObjectiveStoreDelegate {
635 @Override
636 public void notify(ObjectiveEvent event) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800637 if (event.type() == Type.ADD) {
638 log.debug("Received notification of obj event {}", event);
Saurav Das1547b3f2017-05-05 17:01:08 -0700639 Set<PendingFlowObjective> pending;
640
641 // first send all pending flows
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700642 synchronized (pendingForwards) {
643 // needs to be synchronized for queueObjective lookup
644 pending = pendingForwards.remove(event.subject());
645 }
Saurav Das423fe2b2015-12-04 10:52:59 -0800646 if (pending == null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700647 log.debug("No forwarding objectives pending for this "
648 + "obj event {}", event);
649 } else {
650 log.debug("Processing {} pending forwarding objectives for nextId {}",
651 pending.size(), event.subject());
652 pending.forEach(p -> getDevicePipeliner(p.deviceId())
653 .forward((ForwardingObjective) p.flowObjective()));
Saurav Das423fe2b2015-12-04 10:52:59 -0800654 }
655
Saurav Das1547b3f2017-05-05 17:01:08 -0700656 // now check for pending next-objectives
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800657 List<PendingFlowObjective> pendNexts;
Saurav Das1547b3f2017-05-05 17:01:08 -0700658 synchronized (pendingNexts) {
659 // needs to be synchronized for queueObjective lookup
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800660 pendNexts = pendingNexts.remove(event.subject());
Saurav Das1547b3f2017-05-05 17:01:08 -0700661 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800662 if (pendNexts == null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700663 log.debug("No next objectives pending for this "
664 + "obj event {}", event);
665 } else {
666 log.debug("Processing {} pending next objectives for nextId {}",
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800667 pendNexts.size(), event.subject());
668 pendNexts.forEach(p -> getDevicePipeliner(p.deviceId())
Saurav Das1547b3f2017-05-05 17:01:08 -0700669 .next((NextObjective) p.flowObjective()));
670 }
alshabib2a441c62015-04-13 18:39:38 -0700671 }
alshabib2a441c62015-04-13 18:39:38 -0700672 }
673 }
674
675 /**
Saurav Das1547b3f2017-05-05 17:01:08 -0700676 * Data class used to hold a pending flow objective that could not
alshabib2a441c62015-04-13 18:39:38 -0700677 * be processed because the associated next object was not present.
Saurav Das1547b3f2017-05-05 17:01:08 -0700678 * Note that this pending flow objective could be a forwarding objective
679 * waiting for a next objective to complete execution. Or it could a
680 * next objective (with a different operation - remove, addToExisting, or
681 * removeFromExisting) waiting for a next objective with the same id to
682 * complete execution.
alshabib2a441c62015-04-13 18:39:38 -0700683 */
Charles Chana7903c82018-03-15 20:14:16 -0700684 protected class PendingFlowObjective {
alshabib2a441c62015-04-13 18:39:38 -0700685 private final DeviceId deviceId;
Saurav Das1547b3f2017-05-05 17:01:08 -0700686 private final Objective flowObj;
alshabib2a441c62015-04-13 18:39:38 -0700687
Charles Chana7903c82018-03-15 20:14:16 -0700688 PendingFlowObjective(DeviceId deviceId, Objective flowObj) {
alshabib2a441c62015-04-13 18:39:38 -0700689 this.deviceId = deviceId;
Saurav Das1547b3f2017-05-05 17:01:08 -0700690 this.flowObj = flowObj;
alshabib2a441c62015-04-13 18:39:38 -0700691 }
692
693 public DeviceId deviceId() {
694 return deviceId;
695 }
696
Saurav Das1547b3f2017-05-05 17:01:08 -0700697 public Objective flowObjective() {
698 return flowObj;
alshabib2a441c62015-04-13 18:39:38 -0700699 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800700
701 @Override
702 public int hashCode() {
Saurav Das1547b3f2017-05-05 17:01:08 -0700703 return Objects.hash(deviceId, flowObj);
Saurav Das8a0732e2015-11-20 15:27:53 -0800704 }
705
706 @Override
707 public boolean equals(final Object obj) {
708 if (this == obj) {
709 return true;
710 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700711 if (!(obj instanceof PendingFlowObjective)) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800712 return false;
713 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700714 final PendingFlowObjective other = (PendingFlowObjective) obj;
Charles Chana7903c82018-03-15 20:14:16 -0700715
716 return this.deviceId.equals(other.deviceId) &&
717 this.flowObj.equals(other.flowObj);
Saurav Das8a0732e2015-11-20 15:27:53 -0800718 }
alshabibaebe7752015-04-07 17:45:42 -0700719 }
Saurav Das24431192016-03-07 19:13:00 -0800720
721 @Override
722 public List<String> getNextMappings() {
723 List<String> mappings = new ArrayList<>();
724 Map<Integer, NextGroup> allnexts = flowObjectiveStore.getAllGroups();
Saurav Das25190812016-05-27 13:54:07 -0700725 // XXX if the NextGroup after de-serialization actually stored info of the deviceId
Saurav Das24431192016-03-07 19:13:00 -0800726 // then info on any nextObj could be retrieved from one controller instance.
727 // Right now the drivers on one instance can only fetch for next-ids that came
728 // to them.
729 // Also, we still need to send the right next-id to the right driver as potentially
730 // there can be different drivers for different devices. But on that account,
731 // no instance should be decoding for another instance's nextIds.
732
733 for (Map.Entry<Integer, NextGroup> e : allnexts.entrySet()) {
734 // get the device this next Objective was sent to
735 DeviceId deviceId = nextToDevice.get(e.getKey());
736 mappings.add("NextId " + e.getKey() + ": " +
737 ((deviceId != null) ? deviceId : "nextId not in this onos instance"));
738 if (deviceId != null) {
739 // this instance of the controller sent the nextObj to a driver
740 Pipeliner pipeliner = getDevicePipeliner(deviceId);
741 List<String> nextMappings = pipeliner.getNextMappings(e.getValue());
742 if (nextMappings != null) {
743 mappings.addAll(nextMappings);
744 }
745 }
746 }
747 return mappings;
748 }
Saurav Dasb5c236e2016-06-07 10:08:06 -0700749
750 @Override
Harshada Chaundkar5a198b02019-07-03 16:27:45 +0000751 public Map<Pair<Integer, DeviceId>, List<String>> getNextMappingsChain() {
752 Map<Pair<Integer, DeviceId>, List<String>> nextObjGroupMap = new HashMap<>();
753 Map<Integer, NextGroup> allnexts = flowObjectiveStore.getAllGroups();
754
755 // XXX if the NextGroup after de-serialization actually stored info of the deviceId
756 // then info on any nextObj could be retrieved from one controller instance.
757 // Right now the drivers on one instance can only fetch for next-ids that came
758 // to them.
759 // Also, we still need to send the right next-id to the right driver as potentially
760 // there can be different drivers for different devices. But on that account,
761 // no instance should be decoding for another instance's nextIds.
762
763 for (Map.Entry<Integer, NextGroup> e : allnexts.entrySet()) {
764 // get the device this next Objective was sent to
765 DeviceId deviceId = nextToDevice.get(e.getKey());
766 if (deviceId != null) {
767 // this instance of the controller sent the nextObj to a driver
768 Pipeliner pipeliner = getDevicePipeliner(deviceId);
769 List<String> nextMappings = pipeliner.getNextMappings(e.getValue());
770 if (nextMappings != null) {
771 //mappings.addAll(nextMappings);
772 nextObjGroupMap.put(Pair.of(e.getKey(), deviceId), nextMappings);
773 }
774 } else {
775 nextObjGroupMap.put(Pair.of(e.getKey(), deviceId), ImmutableList.of("nextId not in this onos instance"));
776 }
777 }
778 return nextObjGroupMap;
779 }
780
781
782 @Override
Saurav Das1547b3f2017-05-05 17:01:08 -0700783 public List<String> getPendingFlowObjectives() {
784 List<String> pendingFlowObjectives = new ArrayList<>();
Charles Chan54734712017-03-29 11:07:55 -0700785
Saurav Das1547b3f2017-05-05 17:01:08 -0700786 for (Integer nextId : pendingForwards.keySet()) {
787 Set<PendingFlowObjective> pfwd = pendingForwards.get(nextId);
Sho SHIMIZU81470a52016-08-12 17:24:55 -0700788 StringBuilder pend = new StringBuilder();
Charles Chan54734712017-03-29 11:07:55 -0700789 pend.append("NextId: ")
790 .append(nextId);
Saurav Das1547b3f2017-05-05 17:01:08 -0700791 for (PendingFlowObjective pf : pfwd) {
Charles Chan54734712017-03-29 11:07:55 -0700792 pend.append("\n FwdId: ")
Saurav Das1547b3f2017-05-05 17:01:08 -0700793 .append(String.format("%11s", pf.flowObjective().id()))
794 .append(", DeviceId: ")
795 .append(pf.deviceId())
796 .append(", Selector: ")
797 .append(((ForwardingObjective) pf.flowObjective())
798 .selector().criteria());
799 }
800 pendingFlowObjectives.add(pend.toString());
801 }
802
803 for (Integer nextId : pendingNexts.keySet()) {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800804 List<PendingFlowObjective> pnext = pendingNexts.get(nextId);
Saurav Das1547b3f2017-05-05 17:01:08 -0700805 StringBuilder pend = new StringBuilder();
806 pend.append("NextId: ")
807 .append(nextId);
808 for (PendingFlowObjective pn : pnext) {
809 pend.append("\n NextOp: ")
810 .append(pn.flowObjective().op())
Charles Chan54734712017-03-29 11:07:55 -0700811 .append(", DeviceId: ")
812 .append(pn.deviceId())
Saurav Das1547b3f2017-05-05 17:01:08 -0700813 .append(", Treatments: ")
814 .append(((NextObjective) pn.flowObjective())
815 .next());
Saurav Dasb5c236e2016-06-07 10:08:06 -0700816 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700817 pendingFlowObjectives.add(pend.toString());
Saurav Dasb5c236e2016-06-07 10:08:06 -0700818 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700819
820 return pendingFlowObjectives;
821 }
alshabib77b88482015-04-07 15:47:50 -0700822}