blob: 9e334ad853e11d69ea815b36501362ff26e7c121 [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
pier7b7e8ac2020-01-27 17:45:03 +0100108 private static final String INSTALLER_PATTERN = "installer-%d";
109 private static final String VERIFIER_PATTERN = "verifier-%d";
110 private static final String GROUP_THREAD_NAME = "onos/objective";
Yi Tseng374c5f32017-03-05 22:51:35 -0800111
Jonathan Hart17d00452015-04-21 17:10:00 -0700112 private final Logger log = LoggerFactory.getLogger(getClass());
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700113
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700114 /** Number of worker threads. */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700115 private int numThreads = FOM_NUM_THREADS_DEFAULT;
Yi Tseng374c5f32017-03-05 22:51:35 -0800116
pier8b3aef42019-03-11 15:14:02 -0700117 // Parameters for the accumulator, each pipeline can implement
118 // its own accumulation logic. The following parameters are used
119 // to control the accumulator.
120
121 // Maximum number of objectives to accumulate before processing is triggered
122 private int accumulatorMaxObjectives = FOM_ACCUMULATOR_MAX_OBJECTIVES_DEFAULT;
123 // Maximum number of millis between objectives before processing is triggered
124 private int accumulatorMaxIdleMillis = FOM_ACCUMULATOR_MAX_IDLE_MILLIS_DEFAULT;
125 // Maximum number of millis allowed since the first objective before processing is triggered
126 private int accumulatorMaxBatchMillis = FOM_ACCUMULATOR_MAX_BATCH_MILLIS_DEFAULT;
127
128
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700129 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib77b88482015-04-07 15:47:50 -0700130 protected DriverService driverService;
131
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700132 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib77b88482015-04-07 15:47:50 -0700133 protected DeviceService deviceService;
134
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700135 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib77b88482015-04-07 15:47:50 -0700136 protected ClusterService clusterService;
137
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700138 // Note: The following dependencies are added on behalf of the pipeline
139 // driver behaviours to assure these services are available for their
140 // initialization.
Charles Chana7903c82018-03-15 20:14:16 -0700141 @SuppressWarnings("unused")
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700142 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700143 protected FlowRuleService flowRuleService;
144
Charles Chana7903c82018-03-15 20:14:16 -0700145 @SuppressWarnings("unused")
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700146 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700147 protected GroupService groupService;
148
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700149 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabib2a441c62015-04-13 18:39:38 -0700150 protected FlowObjectiveStore flowObjectiveStore;
151
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700152 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tseng374c5f32017-03-05 22:51:35 -0800153 protected ComponentConfigService cfgService;
154
Charles Chana7903c82018-03-15 20:14:16 -0700155 final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700156
157 private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap();
Charles Chana7903c82018-03-15 20:14:16 -0700158 protected final Map<DeviceId, Pipeliner> pipeliners = Maps.newConcurrentMap();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700159
160 private final PipelinerContext context = new InnerPipelineContext();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700161 private final DeviceListener deviceListener = new InnerDeviceListener();
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700162 private final DriverListener driverListener = new InnerDriverListener();
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700163
Charles Chana7903c82018-03-15 20:14:16 -0700164 private ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
alshabib910aff12015-04-09 16:55:57 -0700165
Saurav Das1547b3f2017-05-05 17:01:08 -0700166 // local stores for queuing fwd and next objectives that are waiting for an
167 // associated next objective execution to complete. The signal for completed
168 // execution comes from a pipeline driver, in this or another controller
169 // instance, via the DistributedFlowObjectiveStore.
Charles Chana7903c82018-03-15 20:14:16 -0700170 // TODO Making these cache and timeout the entries
171 final Map<Integer, Set<PendingFlowObjective>> pendingForwards = Maps.newConcurrentMap();
172 final Map<Integer, List<PendingFlowObjective>> pendingNexts = Maps.newConcurrentMap();
alshabib2a441c62015-04-13 18:39:38 -0700173
Saurav Das24431192016-03-07 19:13:00 -0800174 // local store to track which nextObjectives were sent to which device
175 // for debugging purposes
176 private Map<Integer, DeviceId> nextToDevice = Maps.newConcurrentMap();
177
pier7b7e8ac2020-01-27 17:45:03 +0100178 ExecutorService installerExecutor;
179 ExecutorService verifierExecutor;
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000180 protected ExecutorService devEventExecutor;
alshabib2a441c62015-04-13 18:39:38 -0700181
alshabib77b88482015-04-07 15:47:50 -0700182 @Activate
pier8b3aef42019-03-11 15:14:02 -0700183 protected void activate(ComponentContext context) {
Ray Milkey1f0764a2019-03-01 08:40:37 -0800184 cfgService.registerProperties(FlowObjectiveManager.class);
pier7b7e8ac2020-01-27 17:45:03 +0100185 installerExecutor = newFixedThreadPool(numThreads,
186 groupedThreads(GROUP_THREAD_NAME, INSTALLER_PATTERN, log));
187 verifierExecutor = newFixedThreadPool(numThreads,
188 groupedThreads(GROUP_THREAD_NAME, VERIFIER_PATTERN, log));
189
pier8b3aef42019-03-11 15:14:02 -0700190 modified(context);
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000191 devEventExecutor = newSingleThreadScheduledExecutor(
192 groupedThreads("onos/flowobj-dev-events", "events-%d", log));
alshabib2a441c62015-04-13 18:39:38 -0700193 flowObjectiveStore.setDelegate(delegate);
alshabibaebe7752015-04-07 17:45:42 -0700194 deviceService.addListener(deviceListener);
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700195 driverService.addListener(driverListener);
alshabib77b88482015-04-07 15:47:50 -0700196 log.info("Started");
197 }
198
199 @Deactivate
200 protected void deactivate() {
Yi Tseng374c5f32017-03-05 22:51:35 -0800201 cfgService.unregisterProperties(getClass(), false);
alshabib2a441c62015-04-13 18:39:38 -0700202 flowObjectiveStore.unsetDelegate(delegate);
alshabibaebe7752015-04-07 17:45:42 -0700203 deviceService.removeListener(deviceListener);
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700204 driverService.removeListener(driverListener);
pier7b7e8ac2020-01-27 17:45:03 +0100205 installerExecutor.shutdown();
206 verifierExecutor.shutdown();
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000207 devEventExecutor.shutdownNow();
208 devEventExecutor = null;
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700209 pipeliners.clear();
210 driverHandlers.clear();
Saurav Das24431192016-03-07 19:13:00 -0800211 nextToDevice.clear();
alshabib77b88482015-04-07 15:47:50 -0700212 log.info("Stopped");
213 }
214
Yi Tseng374c5f32017-03-05 22:51:35 -0800215 @Modified
216 protected void modified(ComponentContext context) {
pier8b3aef42019-03-11 15:14:02 -0700217 if (context != null) {
218 readComponentConfiguration(context);
219 }
220 }
221
222 /**
223 * Extracts properties from the component configuration context.
224 *
225 * @param context the component context
226 */
227 private void readComponentConfiguration(ComponentContext context) {
228 String propertyValue = Tools.get(context.getProperties(), FOM_NUM_THREADS);
Yi Tseng374c5f32017-03-05 22:51:35 -0800229 int newNumThreads = isNullOrEmpty(propertyValue) ? numThreads : Integer.parseInt(propertyValue);
230
231 if (newNumThreads != numThreads && newNumThreads > 0) {
232 numThreads = newNumThreads;
pier7b7e8ac2020-01-27 17:45:03 +0100233 ExecutorService oldWorkerExecutor = installerExecutor;
234 installerExecutor = newFixedThreadPool(numThreads,
235 groupedThreads(GROUP_THREAD_NAME, INSTALLER_PATTERN, log));
236 if (oldWorkerExecutor != null) {
237 oldWorkerExecutor.shutdown();
238 }
239 oldWorkerExecutor = verifierExecutor;
240 verifierExecutor = newFixedThreadPool(numThreads,
241 groupedThreads(GROUP_THREAD_NAME, VERIFIER_PATTERN, log));
Yi Tseng374c5f32017-03-05 22:51:35 -0800242 if (oldWorkerExecutor != null) {
243 oldWorkerExecutor.shutdown();
244 }
245 log.info("Reconfigured number of worker threads to {}", numThreads);
246 }
pier8b3aef42019-03-11 15:14:02 -0700247
248 // Reconfiguration of the accumulator parameters is allowed
249 // Note: it will affect only pipelines going through init method
250 propertyValue = Tools.get(context.getProperties(), FOM_ACCUMULATOR_MAX_OBJECTIVES);
251 int newMaxObjs = isNullOrEmpty(propertyValue) ?
252 accumulatorMaxObjectives : Integer.parseInt(propertyValue);
253 if (newMaxObjs != accumulatorMaxObjectives && newMaxObjs > 0) {
254 accumulatorMaxObjectives = newMaxObjs;
255 log.info("Reconfigured maximum number of objectives to accumulate to {}",
256 accumulatorMaxObjectives);
257 }
258
259 propertyValue = Tools.get(context.getProperties(), FOM_ACCUMULATOR_MAX_IDLE_MILLIS);
260 int newMaxIdleMS = isNullOrEmpty(propertyValue) ?
261 accumulatorMaxIdleMillis : Integer.parseInt(propertyValue);
262 if (newMaxIdleMS != accumulatorMaxIdleMillis && newMaxIdleMS > 0) {
263 accumulatorMaxIdleMillis = newMaxIdleMS;
264 log.info("Reconfigured maximum number of millis between objectives to {}",
265 accumulatorMaxIdleMillis);
266 }
267
268 propertyValue = Tools.get(context.getProperties(), FOM_ACCUMULATOR_MAX_BATCH_MILLIS);
269 int newMaxBatchMS = isNullOrEmpty(propertyValue) ?
270 accumulatorMaxBatchMillis : Integer.parseInt(propertyValue);
271 if (newMaxBatchMS != accumulatorMaxBatchMillis && newMaxBatchMS > 0) {
272 accumulatorMaxBatchMillis = newMaxBatchMS;
273 log.info("Reconfigured maximum number of millis allowed since the first objective to {}",
274 accumulatorMaxBatchMillis);
275 }
276
Yi Tseng374c5f32017-03-05 22:51:35 -0800277 }
278
Jonathan Hart17d00452015-04-21 17:10:00 -0700279 /**
280 * Task that passes the flow objective down to the driver. The task will
281 * make a few attempts to find the appropriate driver, then eventually give
282 * up and report an error if no suitable driver could be found.
283 */
pier7b7e8ac2020-01-27 17:45:03 +0100284 class ObjectiveProcessor implements Runnable {
Charles Chana7903c82018-03-15 20:14:16 -0700285 final DeviceId deviceId;
286 final Objective objective;
pier7b7e8ac2020-01-27 17:45:03 +0100287 final ExecutorService executor;
Jonathan Hart17d00452015-04-21 17:10:00 -0700288
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700289 private final int numAttempts;
Jonathan Hart17d00452015-04-21 17:10:00 -0700290
pier7b7e8ac2020-01-27 17:45:03 +0100291 ObjectiveProcessor(DeviceId deviceId, Objective objective,
292 ExecutorService executorService) {
293 this(deviceId, objective, 1, executorService);
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700294 }
295
pier7b7e8ac2020-01-27 17:45:03 +0100296 ObjectiveProcessor(DeviceId deviceId, Objective objective, int attempts,
297 ExecutorService executorService) {
Sho SHIMIZUf45d85d2015-07-01 14:39:11 -0700298 this.deviceId = checkNotNull(deviceId);
299 this.objective = checkNotNull(objective);
pier7b7e8ac2020-01-27 17:45:03 +0100300 this.executor = checkNotNull(executorService);
301 this.numAttempts = attempts;
alshabib910aff12015-04-09 16:55:57 -0700302 }
alshabib77b88482015-04-07 15:47:50 -0700303
Jonathan Hart17d00452015-04-21 17:10:00 -0700304 @Override
305 public void run() {
306 try {
Jonathan Hart17d00452015-04-21 17:10:00 -0700307 Pipeliner pipeliner = getDevicePipeliner(deviceId);
308
pier2d8ecd12020-05-20 22:50:30 +0200309 if (pipeliner != null) {
Jonathan Hart17d00452015-04-21 17:10:00 -0700310 if (objective instanceof NextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700311 nextToDevice.put(objective.id(), deviceId);
Jonathan Hart17d00452015-04-21 17:10:00 -0700312 pipeliner.next((NextObjective) objective);
313 } else if (objective instanceof ForwardingObjective) {
314 pipeliner.forward((ForwardingObjective) objective);
315 } else {
316 pipeliner.filter((FilteringObjective) objective);
317 }
Andrea Campanella1f8188d2016-02-29 13:24:54 -0800318 //Attempts to check if pipeliner is null for retry attempts
Jonathan Hart17d00452015-04-21 17:10:00 -0700319 } else if (numAttempts < INSTALL_RETRY_ATTEMPTS) {
Saurav Das3d038262015-04-23 12:36:58 -0700320 Thread.sleep(INSTALL_RETRY_INTERVAL);
pier7b7e8ac2020-01-27 17:45:03 +0100321 executor.execute(new ObjectiveProcessor(deviceId, objective,
322 numAttempts + 1, executor));
Jonathan Hart17d00452015-04-21 17:10:00 -0700323 } else {
324 // Otherwise we've tried a few times and failed, report an
325 // error back to the user.
326 objective.context().ifPresent(
Andrea Campanella1f8188d2016-02-29 13:24:54 -0800327 c -> c.onError(objective, ObjectiveError.NOPIPELINER));
Jonathan Hart17d00452015-04-21 17:10:00 -0700328 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700329 //Exception thrown
Jonathan Hart17d00452015-04-21 17:10:00 -0700330 } catch (Exception e) {
pier7b7e8ac2020-01-27 17:45:03 +0100331 log.warn("Exception while processing flow objective", e);
Jonathan Hart17d00452015-04-21 17:10:00 -0700332 }
333 }
334 }
335
336 @Override
337 public void filter(DeviceId deviceId, FilteringObjective filteringObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900338 checkPermission(FLOWRULE_WRITE);
pier7b7e8ac2020-01-27 17:45:03 +0100339 installerExecutor.execute(new ObjectiveProcessor(deviceId, filteringObjective, installerExecutor));
alshabib77b88482015-04-07 15:47:50 -0700340 }
341
342 @Override
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700343 public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900344 checkPermission(FLOWRULE_WRITE);
Yi Tseng1666b502017-05-17 11:05:18 -0700345 if (forwardingObjective.nextId() == null ||
Yi Tseng1666b502017-05-17 11:05:18 -0700346 flowObjectiveStore.getNextGroup(forwardingObjective.nextId()) != null ||
347 !queueFwdObjective(deviceId, forwardingObjective)) {
348 // fast path
pier7b7e8ac2020-01-27 17:45:03 +0100349 installerExecutor.execute(new ObjectiveProcessor(deviceId, forwardingObjective, installerExecutor));
alshabib910aff12015-04-09 16:55:57 -0700350 }
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700351 }
352
alshabib2a441c62015-04-13 18:39:38 -0700353 @Override
Jonathan Hart17d00452015-04-21 17:10:00 -0700354 public void next(DeviceId deviceId, NextObjective nextObjective) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900355 checkPermission(FLOWRULE_WRITE);
pier7b7e8ac2020-01-27 17:45:03 +0100356 if (nextObjective.op() == Operation.VERIFY) {
357 // Verify does not need to wait
358 verifierExecutor.execute(new ObjectiveProcessor(deviceId, nextObjective, verifierExecutor));
359 } else if (nextObjective.op() == Operation.ADD ||
Yi Tseng1666b502017-05-17 11:05:18 -0700360 flowObjectiveStore.getNextGroup(nextObjective.id()) != null ||
361 !queueNextObjective(deviceId, nextObjective)) {
362 // either group exists or we are trying to create it - let it through
pier7b7e8ac2020-01-27 17:45:03 +0100363 installerExecutor.execute(new ObjectiveProcessor(deviceId, nextObjective, installerExecutor));
Saurav Das1547b3f2017-05-05 17:01:08 -0700364 }
alshabib2a441c62015-04-13 18:39:38 -0700365 }
366
alshabibf6ea9e62015-04-21 17:08:26 -0700367 @Override
368 public int allocateNextId() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900369 checkPermission(FLOWRULE_WRITE);
alshabibf6ea9e62015-04-21 17:08:26 -0700370 return flowObjectiveStore.allocateNextId();
371 }
372
Xin Jin313708b2015-07-09 13:43:04 -0700373 @Override
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700374 public void initPolicy(String policy) {
375 }
Xin Jin313708b2015-07-09 13:43:04 -0700376
Charles Chana7903c82018-03-15 20:14:16 -0700377 boolean queueFwdObjective(DeviceId deviceId, ForwardingObjective fwd) {
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700378 boolean queued = false;
379 synchronized (pendingForwards) {
380 // double check the flow objective store, because this block could run
381 // after a notification arrives
382 if (flowObjectiveStore.getNextGroup(fwd.nextId()) == null) {
383 pendingForwards.compute(fwd.nextId(), (id, pending) -> {
Saurav Das1547b3f2017-05-05 17:01:08 -0700384 PendingFlowObjective pendfo = new PendingFlowObjective(deviceId, fwd);
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700385 if (pending == null) {
Pier Luigi97893112018-03-05 11:09:42 +0100386 return Sets.newLinkedHashSet(ImmutableSet.of(pendfo));
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700387 } else {
Saurav Das1547b3f2017-05-05 17:01:08 -0700388 pending.add(pendfo);
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700389 return pending;
390 }
391 });
392 queued = true;
393 }
394 }
395 if (queued) {
Saurav Dasc568c342018-01-25 09:49:01 -0800396 log.debug("Queued forwarding objective {} for nextId {} meant for device {}",
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700397 fwd.id(), fwd.nextId(), deviceId);
398 }
399 return queued;
alshabib2a441c62015-04-13 18:39:38 -0700400 }
401
Charles Chana7903c82018-03-15 20:14:16 -0700402 boolean queueNextObjective(DeviceId deviceId, NextObjective next) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700403 // we need to hold off on other operations till we get notified that the
404 // initial group creation has succeeded
405 boolean queued = false;
406 synchronized (pendingNexts) {
407 // double check the flow objective store, because this block could run
408 // after a notification arrives
409 if (flowObjectiveStore.getNextGroup(next.id()) == null) {
410 pendingNexts.compute(next.id(), (id, pending) -> {
411 PendingFlowObjective pendfo = new PendingFlowObjective(deviceId, next);
412 if (pending == null) {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800413 return Lists.newArrayList(pendfo);
Saurav Das1547b3f2017-05-05 17:01:08 -0700414 } else {
415 pending.add(pendfo);
416 return pending;
417 }
418 });
419 queued = true;
420 }
421 }
422 if (queued) {
Saurav Dasc568c342018-01-25 09:49:01 -0800423 log.debug("Queued next objective {} with operation {} meant for device {}",
Saurav Das1547b3f2017-05-05 17:01:08 -0700424 next.id(), next.op(), deviceId);
425 }
426 return queued;
427 }
428
Pier Ventre57a61cd2016-09-07 10:55:41 -0700429 /**
430 * Retrieves (if it exists) the device pipeline behaviour from the cache.
431 * Otherwise it warms the caches and triggers the init method of the Pipeline.
432 *
433 * @param deviceId the id of the device associated to the pipeline
434 * @return the implementation of the Pipeliner behaviour
435 */
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700436 private Pipeliner getDevicePipeliner(DeviceId deviceId) {
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700437 return pipeliners.computeIfAbsent(deviceId, this::initPipelineHandler);
alshabib77b88482015-04-07 15:47:50 -0700438 }
439
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700440 /**
Pier Ventre57a61cd2016-09-07 10:55:41 -0700441 * Retrieves (if it exists) the device pipeline behaviour from the cache and
442 * and triggers the init method of the pipeline. Otherwise (DEVICE_ADDED) it warms
443 * the caches and triggers the init method of the Pipeline. The rationale of this
444 * method is for managing the scenario of a switch that goes down for a failure
445 * and goes up after a while.
446 *
447 * @param deviceId the id of the device associated to the pipeline
448 * @return the implementation of the Pipeliner behaviour
449 */
450 private Pipeliner getAndInitDevicePipeliner(DeviceId deviceId) {
451 return pipeliners.compute(deviceId, (deviceIdValue, pipelinerValue) -> {
452 if (pipelinerValue != null) {
453 pipelinerValue.init(deviceId, context);
454 return pipelinerValue;
455 }
456 return this.initPipelineHandler(deviceId);
457 });
458 }
459
460 /**
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700461 * Creates and initialize {@link Pipeliner}.
462 * <p>
463 * Note: Expected to be called under per-Device lock.
464 * e.g., {@code pipeliners}' Map#compute family methods
465 *
466 * @param deviceId Device to initialize pipeliner
467 * @return {@link Pipeliner} instance or null
468 */
469 private Pipeliner initPipelineHandler(DeviceId deviceId) {
470 start = now();
Thomas Vachuska866b46a2015-04-30 00:26:55 -0700471
Jonathan Hart17d00452015-04-21 17:10:00 -0700472 // Attempt to lookup the handler in the cache
473 DriverHandler handler = driverHandlers.get(deviceId);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700474 cTime = now();
475
Jonathan Hart17d00452015-04-21 17:10:00 -0700476 if (handler == null) {
477 try {
478 // Otherwise create it and if it has pipeline behaviour, cache it
479 handler = driverService.createHandler(deviceId);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700480 dTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700481 if (!handler.driver().hasBehaviour(Pipeliner.class)) {
Yuta HIGUCHIa2a4f342017-03-17 11:38:57 -0700482 log.debug("Pipeline behaviour not supported for device {}",
Jonathan Hart17d00452015-04-21 17:10:00 -0700483 deviceId);
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700484 return null;
alshabib2a441c62015-04-13 18:39:38 -0700485 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700486 } catch (ItemNotFoundException e) {
487 log.warn("No applicable driver for device {}", deviceId);
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700488 return null;
alshabib2a441c62015-04-13 18:39:38 -0700489 }
490
Jonathan Hart17d00452015-04-21 17:10:00 -0700491 driverHandlers.put(deviceId, handler);
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700492 eTime = now();
alshabib2a441c62015-04-13 18:39:38 -0700493 }
Jonathan Hart17d00452015-04-21 17:10:00 -0700494
495 // Always (re)initialize the pipeline behaviour
496 log.info("Driver {} bound to device {} ... initializing driver",
497 handler.driver().name(), deviceId);
Thomas Vachuska0121a612015-07-21 11:18:09 -0700498 hTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700499 Pipeliner pipeliner = handler.behaviour(Pipeliner.class);
Thomas Vachuska94c3cf42015-07-20 13:01:12 -0700500 hbTime = now();
Jonathan Hart17d00452015-04-21 17:10:00 -0700501 pipeliner.init(deviceId, context);
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700502 stopWatch();
503 return pipeliner;
alshabibaebe7752015-04-07 17:45:42 -0700504 }
alshabib77b88482015-04-07 15:47:50 -0700505
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700506 private void invalidatePipelinerIfNecessary(Device device) {
507 DriverHandler handler = driverHandlers.get(device.id());
508 if (handler != null &&
509 !Objects.equals(handler.driver().name(),
510 device.annotations().value(DRIVER))) {
511 invalidatePipeliner(device.id());
512 }
513 }
514
515 private void invalidatePipeliner(DeviceId id) {
516 log.info("Invalidating cached pipeline behaviour for {}", id);
517 driverHandlers.remove(id);
pier2d8ecd12020-05-20 22:50:30 +0200518 pipeliners.remove(id);
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700519 if (deviceService.isAvailable(id)) {
520 getAndInitDevicePipeliner(id);
521 }
522 }
523
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700524 // Triggers driver setup when a device is (re)detected.
alshabibaebe7752015-04-07 17:45:42 -0700525 private class InnerDeviceListener implements DeviceListener {
526 @Override
527 public void event(DeviceEvent event) {
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000528 if (devEventExecutor != null) {
alshabibaebe7752015-04-07 17:45:42 -0700529 switch (event.type()) {
530 case DEVICE_ADDED:
531 case DEVICE_AVAILABILITY_CHANGED:
Madan Jampani0174f452015-05-29 11:52:05 -0700532 log.debug("Device either added or availability changed {}",
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700533 event.subject().id());
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000534 devEventExecutor.execute(() -> {
535 if (deviceService.isAvailable(event.subject().id())) {
Madan Jampani0174f452015-05-29 11:52:05 -0700536 log.debug("Device is now available {}", event.subject().id());
Pier Ventre57a61cd2016-09-07 10:55:41 -0700537 getAndInitDevicePipeliner(event.subject().id());
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000538 } else {
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700539 log.debug("Device is no longer available {}", event.subject().id());
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000540 }
541 });
alshabib4313d102015-04-08 18:55:08 -0700542 break;
543 case DEVICE_UPDATED:
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700544 // Invalidate pipeliner and handler caches if the driver name
545 // device annotation changed.
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000546 devEventExecutor.execute(() -> invalidatePipelinerIfNecessary(event.subject()));
alshabib4313d102015-04-08 18:55:08 -0700547 break;
548 case DEVICE_REMOVED:
Yuta HIGUCHIad0c9902016-08-23 10:37:32 -0700549 // evict Pipeliner and Handler cache, when
550 // the Device was administratively removed.
551 //
552 // System expect the user to clear all existing flows,
553 // before removing device, especially if they intend to
554 // replace driver/pipeliner assigned to the device.
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000555 devEventExecutor.execute(() -> {
pierf37ce522020-03-20 11:00:38 +0100556 driverHandlers.remove(event.subject().id());
pier2d8ecd12020-05-20 22:50:30 +0200557 pipeliners.remove(event.subject().id());
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000558 });
alshabib4313d102015-04-08 18:55:08 -0700559 break;
Yuta HIGUCHIad0c9902016-08-23 10:37:32 -0700560 case DEVICE_SUSPENDED:
561 break;
alshabib4313d102015-04-08 18:55:08 -0700562 case PORT_ADDED:
563 break;
564 case PORT_UPDATED:
565 break;
566 case PORT_REMOVED:
alshabibaebe7752015-04-07 17:45:42 -0700567 break;
568 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700569 break;
alshabibaebe7752015-04-07 17:45:42 -0700570 }
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000571 }
alshabib77b88482015-04-07 15:47:50 -0700572 }
573 }
alshabibaebe7752015-04-07 17:45:42 -0700574
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700575 // Monitors driver configuration changes and invalidates the pipeliner cache entries.
576 // Note that this may leave stale entries on the device if the driver changes
577 // in manner where the new driver does not produce backward compatible flow objectives.
578 // In such cases, it is the operator's responsibility to force device re-connect.
579 private class InnerDriverListener implements DriverListener {
580 @Override
581 public void event(DriverEvent event) {
582 String driverName = event.subject().name();
583 driverHandlers.entrySet().stream()
584 .filter(e -> driverName.equals(e.getValue().driver().name()))
585 .map(Map.Entry::getKey)
586 .distinct()
587 .forEach(FlowObjectiveManager.this::invalidatePipeliner);
588 }
589 }
590
Thomas Vachuska174bb912015-07-16 21:27:14 -0700591 // Temporary mechanism to monitor pipeliner setup time-cost; there are
592 // intermittent time where this takes in excess of 2 seconds. Why?
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700593 private long start = 0, totals = 0, count = 0;
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700594 private long cTime, dTime, eTime, hTime, hbTime;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700595 private static final long LIMIT = 500;
Thomas Vachuska174bb912015-07-16 21:27:14 -0700596
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700597 private long now() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700598 return System.currentTimeMillis();
599 }
600
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700601 private void stopWatch() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700602 long duration = System.currentTimeMillis() - start;
603 totals += duration;
604 count += 1;
605 if (duration > LIMIT) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700606 log.info("Pipeline setup took {} ms; avg {} ms; cTime={}, dTime={}, eTime={}, hTime={}, hbTime={}",
607 duration, totals / count, diff(cTime), diff(dTime), diff(eTime), diff(hTime), diff(hbTime));
Thomas Vachuska174bb912015-07-16 21:27:14 -0700608 }
609 }
610
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700611 private long diff(long bTime) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700612 long diff = bTime - start;
613 return diff < 0 ? 0 : diff;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700614 }
615
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700616 // Processing context for initializing pipeline driver behaviours.
617 private class InnerPipelineContext implements PipelinerContext {
pier8b3aef42019-03-11 15:14:02 -0700618
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700619 @Override
620 public ServiceDirectory directory() {
621 return serviceDirectory;
alshabibaebe7752015-04-07 17:45:42 -0700622 }
alshabib2a441c62015-04-13 18:39:38 -0700623
624 @Override
625 public FlowObjectiveStore store() {
626 return flowObjectiveStore;
627 }
pier8b3aef42019-03-11 15:14:02 -0700628
629 @Override
630 public int accumulatorMaxObjectives() {
631 return accumulatorMaxObjectives;
632 }
633
634 @Override
635 public int accumulatorMaxIdleMillis() {
636 return accumulatorMaxIdleMillis;
637 }
638
639 @Override
640 public int accumulatorMaxBatchMillis() {
641 return accumulatorMaxBatchMillis;
642 }
643
alshabib2a441c62015-04-13 18:39:38 -0700644 }
645
646 private class InternalStoreDelegate implements FlowObjectiveStoreDelegate {
647 @Override
648 public void notify(ObjectiveEvent event) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800649 if (event.type() == Type.ADD) {
650 log.debug("Received notification of obj event {}", event);
Saurav Das1547b3f2017-05-05 17:01:08 -0700651 Set<PendingFlowObjective> pending;
652
653 // first send all pending flows
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700654 synchronized (pendingForwards) {
655 // needs to be synchronized for queueObjective lookup
656 pending = pendingForwards.remove(event.subject());
657 }
Saurav Das423fe2b2015-12-04 10:52:59 -0800658 if (pending == null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700659 log.debug("No forwarding objectives pending for this "
660 + "obj event {}", event);
661 } else {
662 log.debug("Processing {} pending forwarding objectives for nextId {}",
663 pending.size(), event.subject());
664 pending.forEach(p -> getDevicePipeliner(p.deviceId())
665 .forward((ForwardingObjective) p.flowObjective()));
Saurav Das423fe2b2015-12-04 10:52:59 -0800666 }
667
Saurav Das1547b3f2017-05-05 17:01:08 -0700668 // now check for pending next-objectives
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800669 List<PendingFlowObjective> pendNexts;
Saurav Das1547b3f2017-05-05 17:01:08 -0700670 synchronized (pendingNexts) {
671 // needs to be synchronized for queueObjective lookup
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800672 pendNexts = pendingNexts.remove(event.subject());
Saurav Das1547b3f2017-05-05 17:01:08 -0700673 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800674 if (pendNexts == null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700675 log.debug("No next objectives pending for this "
676 + "obj event {}", event);
677 } else {
678 log.debug("Processing {} pending next objectives for nextId {}",
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800679 pendNexts.size(), event.subject());
680 pendNexts.forEach(p -> getDevicePipeliner(p.deviceId())
Saurav Das1547b3f2017-05-05 17:01:08 -0700681 .next((NextObjective) p.flowObjective()));
682 }
alshabib2a441c62015-04-13 18:39:38 -0700683 }
alshabib2a441c62015-04-13 18:39:38 -0700684 }
685 }
686
687 /**
Saurav Das1547b3f2017-05-05 17:01:08 -0700688 * Data class used to hold a pending flow objective that could not
alshabib2a441c62015-04-13 18:39:38 -0700689 * be processed because the associated next object was not present.
Saurav Das1547b3f2017-05-05 17:01:08 -0700690 * Note that this pending flow objective could be a forwarding objective
691 * waiting for a next objective to complete execution. Or it could a
692 * next objective (with a different operation - remove, addToExisting, or
693 * removeFromExisting) waiting for a next objective with the same id to
694 * complete execution.
alshabib2a441c62015-04-13 18:39:38 -0700695 */
Charles Chana7903c82018-03-15 20:14:16 -0700696 protected class PendingFlowObjective {
alshabib2a441c62015-04-13 18:39:38 -0700697 private final DeviceId deviceId;
Saurav Das1547b3f2017-05-05 17:01:08 -0700698 private final Objective flowObj;
alshabib2a441c62015-04-13 18:39:38 -0700699
Charles Chana7903c82018-03-15 20:14:16 -0700700 PendingFlowObjective(DeviceId deviceId, Objective flowObj) {
alshabib2a441c62015-04-13 18:39:38 -0700701 this.deviceId = deviceId;
Saurav Das1547b3f2017-05-05 17:01:08 -0700702 this.flowObj = flowObj;
alshabib2a441c62015-04-13 18:39:38 -0700703 }
704
705 public DeviceId deviceId() {
706 return deviceId;
707 }
708
Saurav Das1547b3f2017-05-05 17:01:08 -0700709 public Objective flowObjective() {
710 return flowObj;
alshabib2a441c62015-04-13 18:39:38 -0700711 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800712
713 @Override
714 public int hashCode() {
Saurav Das1547b3f2017-05-05 17:01:08 -0700715 return Objects.hash(deviceId, flowObj);
Saurav Das8a0732e2015-11-20 15:27:53 -0800716 }
717
718 @Override
719 public boolean equals(final Object obj) {
720 if (this == obj) {
721 return true;
722 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700723 if (!(obj instanceof PendingFlowObjective)) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800724 return false;
725 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700726 final PendingFlowObjective other = (PendingFlowObjective) obj;
Charles Chana7903c82018-03-15 20:14:16 -0700727
728 return this.deviceId.equals(other.deviceId) &&
729 this.flowObj.equals(other.flowObj);
Saurav Das8a0732e2015-11-20 15:27:53 -0800730 }
alshabibaebe7752015-04-07 17:45:42 -0700731 }
Saurav Das24431192016-03-07 19:13:00 -0800732
733 @Override
734 public List<String> getNextMappings() {
735 List<String> mappings = new ArrayList<>();
736 Map<Integer, NextGroup> allnexts = flowObjectiveStore.getAllGroups();
Saurav Das25190812016-05-27 13:54:07 -0700737 // XXX if the NextGroup after de-serialization actually stored info of the deviceId
Saurav Das24431192016-03-07 19:13:00 -0800738 // then info on any nextObj could be retrieved from one controller instance.
739 // Right now the drivers on one instance can only fetch for next-ids that came
740 // to them.
741 // Also, we still need to send the right next-id to the right driver as potentially
742 // there can be different drivers for different devices. But on that account,
743 // no instance should be decoding for another instance's nextIds.
744
745 for (Map.Entry<Integer, NextGroup> e : allnexts.entrySet()) {
746 // get the device this next Objective was sent to
747 DeviceId deviceId = nextToDevice.get(e.getKey());
748 mappings.add("NextId " + e.getKey() + ": " +
749 ((deviceId != null) ? deviceId : "nextId not in this onos instance"));
750 if (deviceId != null) {
751 // this instance of the controller sent the nextObj to a driver
752 Pipeliner pipeliner = getDevicePipeliner(deviceId);
753 List<String> nextMappings = pipeliner.getNextMappings(e.getValue());
754 if (nextMappings != null) {
755 mappings.addAll(nextMappings);
756 }
757 }
758 }
759 return mappings;
760 }
Saurav Dasb5c236e2016-06-07 10:08:06 -0700761
762 @Override
Harshada Chaundkar5a198b02019-07-03 16:27:45 +0000763 public Map<Pair<Integer, DeviceId>, List<String>> getNextMappingsChain() {
764 Map<Pair<Integer, DeviceId>, List<String>> nextObjGroupMap = new HashMap<>();
765 Map<Integer, NextGroup> allnexts = flowObjectiveStore.getAllGroups();
766
767 // XXX if the NextGroup after de-serialization actually stored info of the deviceId
768 // then info on any nextObj could be retrieved from one controller instance.
769 // Right now the drivers on one instance can only fetch for next-ids that came
770 // to them.
771 // Also, we still need to send the right next-id to the right driver as potentially
772 // there can be different drivers for different devices. But on that account,
773 // no instance should be decoding for another instance's nextIds.
774
775 for (Map.Entry<Integer, NextGroup> e : allnexts.entrySet()) {
776 // get the device this next Objective was sent to
777 DeviceId deviceId = nextToDevice.get(e.getKey());
778 if (deviceId != null) {
779 // this instance of the controller sent the nextObj to a driver
780 Pipeliner pipeliner = getDevicePipeliner(deviceId);
781 List<String> nextMappings = pipeliner.getNextMappings(e.getValue());
782 if (nextMappings != null) {
783 //mappings.addAll(nextMappings);
784 nextObjGroupMap.put(Pair.of(e.getKey(), deviceId), nextMappings);
785 }
786 } else {
787 nextObjGroupMap.put(Pair.of(e.getKey(), deviceId), ImmutableList.of("nextId not in this onos instance"));
788 }
789 }
790 return nextObjGroupMap;
791 }
792
793
794 @Override
Saurav Das1547b3f2017-05-05 17:01:08 -0700795 public List<String> getPendingFlowObjectives() {
796 List<String> pendingFlowObjectives = new ArrayList<>();
Charles Chan54734712017-03-29 11:07:55 -0700797
Saurav Das1547b3f2017-05-05 17:01:08 -0700798 for (Integer nextId : pendingForwards.keySet()) {
799 Set<PendingFlowObjective> pfwd = pendingForwards.get(nextId);
Sho SHIMIZU81470a52016-08-12 17:24:55 -0700800 StringBuilder pend = new StringBuilder();
Charles Chan54734712017-03-29 11:07:55 -0700801 pend.append("NextId: ")
802 .append(nextId);
Saurav Das1547b3f2017-05-05 17:01:08 -0700803 for (PendingFlowObjective pf : pfwd) {
Charles Chan54734712017-03-29 11:07:55 -0700804 pend.append("\n FwdId: ")
Saurav Das1547b3f2017-05-05 17:01:08 -0700805 .append(String.format("%11s", pf.flowObjective().id()))
806 .append(", DeviceId: ")
807 .append(pf.deviceId())
808 .append(", Selector: ")
809 .append(((ForwardingObjective) pf.flowObjective())
810 .selector().criteria());
811 }
812 pendingFlowObjectives.add(pend.toString());
813 }
814
815 for (Integer nextId : pendingNexts.keySet()) {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800816 List<PendingFlowObjective> pnext = pendingNexts.get(nextId);
Saurav Das1547b3f2017-05-05 17:01:08 -0700817 StringBuilder pend = new StringBuilder();
818 pend.append("NextId: ")
819 .append(nextId);
820 for (PendingFlowObjective pn : pnext) {
821 pend.append("\n NextOp: ")
822 .append(pn.flowObjective().op())
Charles Chan54734712017-03-29 11:07:55 -0700823 .append(", DeviceId: ")
824 .append(pn.deviceId())
Saurav Das1547b3f2017-05-05 17:01:08 -0700825 .append(", Treatments: ")
826 .append(((NextObjective) pn.flowObjective())
827 .next());
Saurav Dasb5c236e2016-06-07 10:08:06 -0700828 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700829 pendingFlowObjectives.add(pend.toString());
Saurav Dasb5c236e2016-06-07 10:08:06 -0700830 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700831
832 return pendingFlowObjectives;
833 }
alshabib77b88482015-04-07 15:47:50 -0700834}