blob: 4ba0d6ecca7e6e73d9c71303b94fdaf8e932b8cc [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
pier1b7dd122020-02-28 09:24:11 +0100309 if (pipeliner != null && pipeliner.isReady()) {
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);
pierf37ce522020-03-20 11:00:38 +0100518 Pipeliner pipeliner = pipeliners.remove(id);
519 if (pipeliner != null) {
520 pipeliner.cleanUp();
521 }
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700522 if (deviceService.isAvailable(id)) {
523 getAndInitDevicePipeliner(id);
524 }
525 }
526
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700527 // Triggers driver setup when a device is (re)detected.
alshabibaebe7752015-04-07 17:45:42 -0700528 private class InnerDeviceListener implements DeviceListener {
529 @Override
530 public void event(DeviceEvent event) {
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000531 if (devEventExecutor != null) {
alshabibaebe7752015-04-07 17:45:42 -0700532 switch (event.type()) {
533 case DEVICE_ADDED:
534 case DEVICE_AVAILABILITY_CHANGED:
Madan Jampani0174f452015-05-29 11:52:05 -0700535 log.debug("Device either added or availability changed {}",
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700536 event.subject().id());
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000537 devEventExecutor.execute(() -> {
538 if (deviceService.isAvailable(event.subject().id())) {
Madan Jampani0174f452015-05-29 11:52:05 -0700539 log.debug("Device is now available {}", event.subject().id());
Pier Ventre57a61cd2016-09-07 10:55:41 -0700540 getAndInitDevicePipeliner(event.subject().id());
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000541 } else {
Yuta HIGUCHI1fb0a8c2016-08-12 10:59:24 -0700542 log.debug("Device is no longer available {}", event.subject().id());
pierf37ce522020-03-20 11:00:38 +0100543 getDevicePipeliner(event.subject().id()).cleanUp();
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000544 }
545 });
alshabib4313d102015-04-08 18:55:08 -0700546 break;
547 case DEVICE_UPDATED:
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700548 // Invalidate pipeliner and handler caches if the driver name
549 // device annotation changed.
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000550 devEventExecutor.execute(() -> invalidatePipelinerIfNecessary(event.subject()));
alshabib4313d102015-04-08 18:55:08 -0700551 break;
552 case DEVICE_REMOVED:
Yuta HIGUCHIad0c9902016-08-23 10:37:32 -0700553 // evict Pipeliner and Handler cache, when
554 // the Device was administratively removed.
555 //
556 // System expect the user to clear all existing flows,
557 // before removing device, especially if they intend to
558 // replace driver/pipeliner assigned to the device.
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000559 devEventExecutor.execute(() -> {
pierf37ce522020-03-20 11:00:38 +0100560 driverHandlers.remove(event.subject().id());
561 Pipeliner pipeliner = pipeliners.remove(event.subject().id());
562 if (pipeliner != null) {
563 pipeliner.cleanUp();
564 }
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000565 });
alshabib4313d102015-04-08 18:55:08 -0700566 break;
Yuta HIGUCHIad0c9902016-08-23 10:37:32 -0700567 case DEVICE_SUSPENDED:
568 break;
alshabib4313d102015-04-08 18:55:08 -0700569 case PORT_ADDED:
570 break;
571 case PORT_UPDATED:
572 break;
573 case PORT_REMOVED:
alshabibaebe7752015-04-07 17:45:42 -0700574 break;
575 default:
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700576 break;
alshabibaebe7752015-04-07 17:45:42 -0700577 }
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000578 }
alshabib77b88482015-04-07 15:47:50 -0700579 }
580 }
alshabibaebe7752015-04-07 17:45:42 -0700581
Thomas Vachuskacfeff192017-08-23 15:29:34 -0700582 // Monitors driver configuration changes and invalidates the pipeliner cache entries.
583 // Note that this may leave stale entries on the device if the driver changes
584 // in manner where the new driver does not produce backward compatible flow objectives.
585 // In such cases, it is the operator's responsibility to force device re-connect.
586 private class InnerDriverListener implements DriverListener {
587 @Override
588 public void event(DriverEvent event) {
589 String driverName = event.subject().name();
590 driverHandlers.entrySet().stream()
591 .filter(e -> driverName.equals(e.getValue().driver().name()))
592 .map(Map.Entry::getKey)
593 .distinct()
594 .forEach(FlowObjectiveManager.this::invalidatePipeliner);
595 }
596 }
597
Thomas Vachuska174bb912015-07-16 21:27:14 -0700598 // Temporary mechanism to monitor pipeliner setup time-cost; there are
599 // intermittent time where this takes in excess of 2 seconds. Why?
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700600 private long start = 0, totals = 0, count = 0;
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700601 private long cTime, dTime, eTime, hTime, hbTime;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700602 private static final long LIMIT = 500;
Thomas Vachuska174bb912015-07-16 21:27:14 -0700603
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700604 private long now() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700605 return System.currentTimeMillis();
606 }
607
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700608 private void stopWatch() {
Thomas Vachuska174bb912015-07-16 21:27:14 -0700609 long duration = System.currentTimeMillis() - start;
610 totals += duration;
611 count += 1;
612 if (duration > LIMIT) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700613 log.info("Pipeline setup took {} ms; avg {} ms; cTime={}, dTime={}, eTime={}, hTime={}, hbTime={}",
614 duration, totals / count, diff(cTime), diff(dTime), diff(eTime), diff(hTime), diff(hbTime));
Thomas Vachuska174bb912015-07-16 21:27:14 -0700615 }
616 }
617
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700618 private long diff(long bTime) {
Thomas Vachuskab19bffb2015-07-22 10:56:16 -0700619 long diff = bTime - start;
620 return diff < 0 ? 0 : diff;
Thomas Vachuska9c9ff7c2015-07-20 10:38:59 -0700621 }
622
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700623 // Processing context for initializing pipeline driver behaviours.
624 private class InnerPipelineContext implements PipelinerContext {
pier8b3aef42019-03-11 15:14:02 -0700625
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700626 @Override
627 public ServiceDirectory directory() {
628 return serviceDirectory;
alshabibaebe7752015-04-07 17:45:42 -0700629 }
alshabib2a441c62015-04-13 18:39:38 -0700630
631 @Override
632 public FlowObjectiveStore store() {
633 return flowObjectiveStore;
634 }
pier8b3aef42019-03-11 15:14:02 -0700635
636 @Override
637 public int accumulatorMaxObjectives() {
638 return accumulatorMaxObjectives;
639 }
640
641 @Override
642 public int accumulatorMaxIdleMillis() {
643 return accumulatorMaxIdleMillis;
644 }
645
646 @Override
647 public int accumulatorMaxBatchMillis() {
648 return accumulatorMaxBatchMillis;
649 }
650
alshabib2a441c62015-04-13 18:39:38 -0700651 }
652
653 private class InternalStoreDelegate implements FlowObjectiveStoreDelegate {
654 @Override
655 public void notify(ObjectiveEvent event) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800656 if (event.type() == Type.ADD) {
657 log.debug("Received notification of obj event {}", event);
Saurav Das1547b3f2017-05-05 17:01:08 -0700658 Set<PendingFlowObjective> pending;
659
660 // first send all pending flows
Thomas Vachuskad27097c2016-06-14 19:10:41 -0700661 synchronized (pendingForwards) {
662 // needs to be synchronized for queueObjective lookup
663 pending = pendingForwards.remove(event.subject());
664 }
Saurav Das423fe2b2015-12-04 10:52:59 -0800665 if (pending == null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700666 log.debug("No forwarding objectives pending for this "
667 + "obj event {}", event);
668 } else {
669 log.debug("Processing {} pending forwarding objectives for nextId {}",
670 pending.size(), event.subject());
671 pending.forEach(p -> getDevicePipeliner(p.deviceId())
672 .forward((ForwardingObjective) p.flowObjective()));
Saurav Das423fe2b2015-12-04 10:52:59 -0800673 }
674
Saurav Das1547b3f2017-05-05 17:01:08 -0700675 // now check for pending next-objectives
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800676 List<PendingFlowObjective> pendNexts;
Saurav Das1547b3f2017-05-05 17:01:08 -0700677 synchronized (pendingNexts) {
678 // needs to be synchronized for queueObjective lookup
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800679 pendNexts = pendingNexts.remove(event.subject());
Saurav Das1547b3f2017-05-05 17:01:08 -0700680 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800681 if (pendNexts == null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700682 log.debug("No next objectives pending for this "
683 + "obj event {}", event);
684 } else {
685 log.debug("Processing {} pending next objectives for nextId {}",
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800686 pendNexts.size(), event.subject());
687 pendNexts.forEach(p -> getDevicePipeliner(p.deviceId())
Saurav Das1547b3f2017-05-05 17:01:08 -0700688 .next((NextObjective) p.flowObjective()));
689 }
alshabib2a441c62015-04-13 18:39:38 -0700690 }
alshabib2a441c62015-04-13 18:39:38 -0700691 }
692 }
693
694 /**
Saurav Das1547b3f2017-05-05 17:01:08 -0700695 * Data class used to hold a pending flow objective that could not
alshabib2a441c62015-04-13 18:39:38 -0700696 * be processed because the associated next object was not present.
Saurav Das1547b3f2017-05-05 17:01:08 -0700697 * Note that this pending flow objective could be a forwarding objective
698 * waiting for a next objective to complete execution. Or it could a
699 * next objective (with a different operation - remove, addToExisting, or
700 * removeFromExisting) waiting for a next objective with the same id to
701 * complete execution.
alshabib2a441c62015-04-13 18:39:38 -0700702 */
Charles Chana7903c82018-03-15 20:14:16 -0700703 protected class PendingFlowObjective {
alshabib2a441c62015-04-13 18:39:38 -0700704 private final DeviceId deviceId;
Saurav Das1547b3f2017-05-05 17:01:08 -0700705 private final Objective flowObj;
alshabib2a441c62015-04-13 18:39:38 -0700706
Charles Chana7903c82018-03-15 20:14:16 -0700707 PendingFlowObjective(DeviceId deviceId, Objective flowObj) {
alshabib2a441c62015-04-13 18:39:38 -0700708 this.deviceId = deviceId;
Saurav Das1547b3f2017-05-05 17:01:08 -0700709 this.flowObj = flowObj;
alshabib2a441c62015-04-13 18:39:38 -0700710 }
711
712 public DeviceId deviceId() {
713 return deviceId;
714 }
715
Saurav Das1547b3f2017-05-05 17:01:08 -0700716 public Objective flowObjective() {
717 return flowObj;
alshabib2a441c62015-04-13 18:39:38 -0700718 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800719
720 @Override
721 public int hashCode() {
Saurav Das1547b3f2017-05-05 17:01:08 -0700722 return Objects.hash(deviceId, flowObj);
Saurav Das8a0732e2015-11-20 15:27:53 -0800723 }
724
725 @Override
726 public boolean equals(final Object obj) {
727 if (this == obj) {
728 return true;
729 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700730 if (!(obj instanceof PendingFlowObjective)) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800731 return false;
732 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700733 final PendingFlowObjective other = (PendingFlowObjective) obj;
Charles Chana7903c82018-03-15 20:14:16 -0700734
735 return this.deviceId.equals(other.deviceId) &&
736 this.flowObj.equals(other.flowObj);
Saurav Das8a0732e2015-11-20 15:27:53 -0800737 }
alshabibaebe7752015-04-07 17:45:42 -0700738 }
Saurav Das24431192016-03-07 19:13:00 -0800739
740 @Override
741 public List<String> getNextMappings() {
742 List<String> mappings = new ArrayList<>();
743 Map<Integer, NextGroup> allnexts = flowObjectiveStore.getAllGroups();
Saurav Das25190812016-05-27 13:54:07 -0700744 // XXX if the NextGroup after de-serialization actually stored info of the deviceId
Saurav Das24431192016-03-07 19:13:00 -0800745 // then info on any nextObj could be retrieved from one controller instance.
746 // Right now the drivers on one instance can only fetch for next-ids that came
747 // to them.
748 // Also, we still need to send the right next-id to the right driver as potentially
749 // there can be different drivers for different devices. But on that account,
750 // no instance should be decoding for another instance's nextIds.
751
752 for (Map.Entry<Integer, NextGroup> e : allnexts.entrySet()) {
753 // get the device this next Objective was sent to
754 DeviceId deviceId = nextToDevice.get(e.getKey());
755 mappings.add("NextId " + e.getKey() + ": " +
756 ((deviceId != null) ? deviceId : "nextId not in this onos instance"));
757 if (deviceId != null) {
758 // this instance of the controller sent the nextObj to a driver
759 Pipeliner pipeliner = getDevicePipeliner(deviceId);
760 List<String> nextMappings = pipeliner.getNextMappings(e.getValue());
761 if (nextMappings != null) {
762 mappings.addAll(nextMappings);
763 }
764 }
765 }
766 return mappings;
767 }
Saurav Dasb5c236e2016-06-07 10:08:06 -0700768
769 @Override
Harshada Chaundkar5a198b02019-07-03 16:27:45 +0000770 public Map<Pair<Integer, DeviceId>, List<String>> getNextMappingsChain() {
771 Map<Pair<Integer, DeviceId>, List<String>> nextObjGroupMap = new HashMap<>();
772 Map<Integer, NextGroup> allnexts = flowObjectiveStore.getAllGroups();
773
774 // XXX if the NextGroup after de-serialization actually stored info of the deviceId
775 // then info on any nextObj could be retrieved from one controller instance.
776 // Right now the drivers on one instance can only fetch for next-ids that came
777 // to them.
778 // Also, we still need to send the right next-id to the right driver as potentially
779 // there can be different drivers for different devices. But on that account,
780 // no instance should be decoding for another instance's nextIds.
781
782 for (Map.Entry<Integer, NextGroup> e : allnexts.entrySet()) {
783 // get the device this next Objective was sent to
784 DeviceId deviceId = nextToDevice.get(e.getKey());
785 if (deviceId != null) {
786 // this instance of the controller sent the nextObj to a driver
787 Pipeliner pipeliner = getDevicePipeliner(deviceId);
788 List<String> nextMappings = pipeliner.getNextMappings(e.getValue());
789 if (nextMappings != null) {
790 //mappings.addAll(nextMappings);
791 nextObjGroupMap.put(Pair.of(e.getKey(), deviceId), nextMappings);
792 }
793 } else {
794 nextObjGroupMap.put(Pair.of(e.getKey(), deviceId), ImmutableList.of("nextId not in this onos instance"));
795 }
796 }
797 return nextObjGroupMap;
798 }
799
800
801 @Override
Saurav Das1547b3f2017-05-05 17:01:08 -0700802 public List<String> getPendingFlowObjectives() {
803 List<String> pendingFlowObjectives = new ArrayList<>();
Charles Chan54734712017-03-29 11:07:55 -0700804
Saurav Das1547b3f2017-05-05 17:01:08 -0700805 for (Integer nextId : pendingForwards.keySet()) {
806 Set<PendingFlowObjective> pfwd = pendingForwards.get(nextId);
Sho SHIMIZU81470a52016-08-12 17:24:55 -0700807 StringBuilder pend = new StringBuilder();
Charles Chan54734712017-03-29 11:07:55 -0700808 pend.append("NextId: ")
809 .append(nextId);
Saurav Das1547b3f2017-05-05 17:01:08 -0700810 for (PendingFlowObjective pf : pfwd) {
Charles Chan54734712017-03-29 11:07:55 -0700811 pend.append("\n FwdId: ")
Saurav Das1547b3f2017-05-05 17:01:08 -0700812 .append(String.format("%11s", pf.flowObjective().id()))
813 .append(", DeviceId: ")
814 .append(pf.deviceId())
815 .append(", Selector: ")
816 .append(((ForwardingObjective) pf.flowObjective())
817 .selector().criteria());
818 }
819 pendingFlowObjectives.add(pend.toString());
820 }
821
822 for (Integer nextId : pendingNexts.keySet()) {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800823 List<PendingFlowObjective> pnext = pendingNexts.get(nextId);
Saurav Das1547b3f2017-05-05 17:01:08 -0700824 StringBuilder pend = new StringBuilder();
825 pend.append("NextId: ")
826 .append(nextId);
827 for (PendingFlowObjective pn : pnext) {
828 pend.append("\n NextOp: ")
829 .append(pn.flowObjective().op())
Charles Chan54734712017-03-29 11:07:55 -0700830 .append(", DeviceId: ")
831 .append(pn.deviceId())
Saurav Das1547b3f2017-05-05 17:01:08 -0700832 .append(", Treatments: ")
833 .append(((NextObjective) pn.flowObjective())
834 .next());
Saurav Dasb5c236e2016-06-07 10:08:06 -0700835 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700836 pendingFlowObjectives.add(pend.toString());
Saurav Dasb5c236e2016-06-07 10:08:06 -0700837 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700838
839 return pendingFlowObjectives;
840 }
alshabib77b88482015-04-07 15:47:50 -0700841}