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