alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.net.flowobjective.impl; |
| 17 | |
Pier Luigi | 9789311 | 2018-03-05 11:09:42 +0100 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Harshada Chaundkar | 5a198b0 | 2019-07-03 16:27:45 +0000 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Sets; |
Harshada Chaundkar | 5a198b0 | 2019-07-03 16:27:45 +0000 | [diff] [blame] | 23 | import org.apache.commons.lang3.tuple.Pair; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 24 | import org.onlab.osgi.DefaultServiceDirectory; |
| 25 | import org.onlab.osgi.ServiceDirectory; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 26 | import org.onlab.util.ItemNotFoundException; |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 27 | import org.onlab.util.Tools; |
| 28 | import org.onosproject.cfg.ComponentConfigService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 29 | import org.onosproject.cluster.ClusterService; |
Daniele Moro | 06aac70 | 2021-07-19 22:39:22 +0200 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 31 | import org.onosproject.net.Device; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 32 | import org.onosproject.net.DeviceId; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 33 | import org.onosproject.net.behaviour.NextGroup; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 34 | import org.onosproject.net.behaviour.Pipeliner; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 35 | import org.onosproject.net.behaviour.PipelinerContext; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 36 | import org.onosproject.net.device.DeviceEvent; |
| 37 | import org.onosproject.net.device.DeviceListener; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 38 | import org.onosproject.net.device.DeviceService; |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 39 | import org.onosproject.net.driver.DriverEvent; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 40 | import org.onosproject.net.driver.DriverHandler; |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 41 | import org.onosproject.net.driver.DriverListener; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 42 | import org.onosproject.net.driver.DriverService; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flow.FlowRuleService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 44 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 45 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 46 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 47 | import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 48 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 49 | import org.onosproject.net.flowobjective.NextObjective; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 50 | import org.onosproject.net.flowobjective.Objective; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 51 | import org.onosproject.net.flowobjective.Objective.Operation; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 52 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 53 | import org.onosproject.net.flowobjective.ObjectiveEvent; |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 54 | import org.onosproject.net.flowobjective.ObjectiveEvent.Type; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 55 | import org.onosproject.net.group.GroupService; |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 56 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 57 | import org.osgi.service.component.annotations.Activate; |
| 58 | import org.osgi.service.component.annotations.Component; |
| 59 | import org.osgi.service.component.annotations.Deactivate; |
| 60 | import org.osgi.service.component.annotations.Modified; |
| 61 | import org.osgi.service.component.annotations.Reference; |
| 62 | import org.osgi.service.component.annotations.ReferenceCardinality; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 63 | import org.slf4j.Logger; |
| 64 | import org.slf4j.LoggerFactory; |
| 65 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 66 | import java.util.ArrayList; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 67 | import java.util.List; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 68 | import java.util.Map; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 69 | import java.util.Objects; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 70 | import java.util.Set; |
Harshada Chaundkar | 5a198b0 | 2019-07-03 16:27:45 +0000 | [diff] [blame] | 71 | import java.util.HashMap; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 72 | import java.util.concurrent.ExecutorService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 73 | |
Sho SHIMIZU | f45d85d | 2015-07-01 14:39:11 -0700 | [diff] [blame] | 74 | import static com.google.common.base.Preconditions.checkNotNull; |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 75 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 76 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 77 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 78 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 79 | import static org.onosproject.net.AnnotationKeys.DRIVER; |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 80 | import static org.onosproject.net.OsgiPropertyConstants.FOM_NUM_THREADS; |
| 81 | import static org.onosproject.net.OsgiPropertyConstants.FOM_NUM_THREADS_DEFAULT; |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 82 | import static org.onosproject.net.OsgiPropertyConstants.FOM_ACCUMULATOR_MAX_OBJECTIVES; |
| 83 | import static org.onosproject.net.OsgiPropertyConstants.FOM_ACCUMULATOR_MAX_OBJECTIVES_DEFAULT; |
| 84 | import static org.onosproject.net.OsgiPropertyConstants.FOM_ACCUMULATOR_MAX_IDLE_MILLIS; |
| 85 | import static org.onosproject.net.OsgiPropertyConstants.FOM_ACCUMULATOR_MAX_IDLE_MILLIS_DEFAULT; |
| 86 | import static org.onosproject.net.OsgiPropertyConstants.FOM_ACCUMULATOR_MAX_BATCH_MILLIS; |
| 87 | import static org.onosproject.net.OsgiPropertyConstants.FOM_ACCUMULATOR_MAX_BATCH_MILLIS_DEFAULT; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 88 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 89 | import static org.onosproject.security.AppPermission.Type.FLOWRULE_WRITE; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 92 | * Provides implementation of the flow objective programming service. |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 93 | */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 94 | @Component( |
| 95 | enabled = false, |
| 96 | service = FlowObjectiveService.class, |
| 97 | property = { |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 98 | FOM_NUM_THREADS + ":Integer=" + FOM_NUM_THREADS_DEFAULT, |
| 99 | FOM_ACCUMULATOR_MAX_OBJECTIVES + ":Integer=" + FOM_ACCUMULATOR_MAX_OBJECTIVES_DEFAULT, |
| 100 | FOM_ACCUMULATOR_MAX_IDLE_MILLIS + ":Integer=" + FOM_ACCUMULATOR_MAX_IDLE_MILLIS_DEFAULT, |
| 101 | FOM_ACCUMULATOR_MAX_BATCH_MILLIS + ":Integer=" + FOM_ACCUMULATOR_MAX_BATCH_MILLIS_DEFAULT, |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 102 | } |
| 103 | ) |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 104 | public class FlowObjectiveManager implements FlowObjectiveService { |
| 105 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 106 | private static final int INSTALL_RETRY_ATTEMPTS = 5; |
| 107 | private static final long INSTALL_RETRY_INTERVAL = 1000; // ms |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 108 | |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 109 | private static final String INSTALLER_PATTERN = "installer-%d"; |
| 110 | private static final String VERIFIER_PATTERN = "verifier-%d"; |
| 111 | private static final String GROUP_THREAD_NAME = "onos/objective"; |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 112 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 113 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 114 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 115 | /** Number of worker threads. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 116 | private int numThreads = FOM_NUM_THREADS_DEFAULT; |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 117 | |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 118 | // Parameters for the accumulator, each pipeline can implement |
| 119 | // its own accumulation logic. The following parameters are used |
| 120 | // to control the accumulator. |
| 121 | |
pierventre | 13a7235 | 2020-10-27 16:08:48 +0100 | [diff] [blame] | 122 | /** Max number of objs to accumulate before processing is triggered. */ |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 123 | private int accumulatorMaxObjectives = FOM_ACCUMULATOR_MAX_OBJECTIVES_DEFAULT; |
pierventre | 13a7235 | 2020-10-27 16:08:48 +0100 | [diff] [blame] | 124 | /** Max of ms between objs before processing is triggered. */ |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 125 | private int accumulatorMaxIdleMillis = FOM_ACCUMULATOR_MAX_IDLE_MILLIS_DEFAULT; |
pierventre | 13a7235 | 2020-10-27 16:08:48 +0100 | [diff] [blame] | 126 | /** Max number of ms allowed since the first obj before processing is triggered. */ |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 127 | private int accumulatorMaxBatchMillis = FOM_ACCUMULATOR_MAX_BATCH_MILLIS_DEFAULT; |
| 128 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 130 | protected DriverService driverService; |
| 131 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 133 | protected DeviceService deviceService; |
| 134 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 136 | protected ClusterService clusterService; |
| 137 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 138 | // 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 Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 141 | @SuppressWarnings("unused") |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 142 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 143 | protected FlowRuleService flowRuleService; |
| 144 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 145 | @SuppressWarnings("unused") |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 146 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 147 | protected GroupService groupService; |
| 148 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 149 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 150 | protected FlowObjectiveStore flowObjectiveStore; |
| 151 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 152 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 153 | protected ComponentConfigService cfgService; |
| 154 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 155 | final FlowObjectiveStoreDelegate delegate = new InternalStoreDelegate(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 156 | |
| 157 | private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap(); |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 158 | protected final Map<DeviceId, Pipeliner> pipeliners = Maps.newConcurrentMap(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 159 | |
| 160 | private final PipelinerContext context = new InnerPipelineContext(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 161 | private final DeviceListener deviceListener = new InnerDeviceListener(); |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 162 | private final DriverListener driverListener = new InnerDriverListener(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 163 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 164 | private ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 165 | |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 166 | // 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 Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 170 | // 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(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 173 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 174 | // local store to track which nextObjectives were sent to which device |
| 175 | // for debugging purposes |
| 176 | private Map<Integer, DeviceId> nextToDevice = Maps.newConcurrentMap(); |
| 177 | |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 178 | ExecutorService installerExecutor; |
| 179 | ExecutorService verifierExecutor; |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 180 | protected ExecutorService devEventExecutor; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 181 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 182 | @Activate |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 183 | protected void activate(ComponentContext context) { |
Ray Milkey | 1f0764a | 2019-03-01 08:40:37 -0800 | [diff] [blame] | 184 | cfgService.registerProperties(FlowObjectiveManager.class); |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 185 | installerExecutor = newFixedThreadPool(numThreads, |
| 186 | groupedThreads(GROUP_THREAD_NAME, INSTALLER_PATTERN, log)); |
| 187 | verifierExecutor = newFixedThreadPool(numThreads, |
| 188 | groupedThreads(GROUP_THREAD_NAME, VERIFIER_PATTERN, log)); |
| 189 | |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 190 | modified(context); |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 191 | devEventExecutor = newSingleThreadScheduledExecutor( |
| 192 | groupedThreads("onos/flowobj-dev-events", "events-%d", log)); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 193 | flowObjectiveStore.setDelegate(delegate); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 194 | deviceService.addListener(deviceListener); |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 195 | driverService.addListener(driverListener); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 196 | log.info("Started"); |
| 197 | } |
| 198 | |
| 199 | @Deactivate |
| 200 | protected void deactivate() { |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 201 | cfgService.unregisterProperties(getClass(), false); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 202 | flowObjectiveStore.unsetDelegate(delegate); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 203 | deviceService.removeListener(deviceListener); |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 204 | driverService.removeListener(driverListener); |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 205 | installerExecutor.shutdown(); |
| 206 | verifierExecutor.shutdown(); |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 207 | devEventExecutor.shutdownNow(); |
| 208 | devEventExecutor = null; |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 209 | pipeliners.clear(); |
| 210 | driverHandlers.clear(); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 211 | nextToDevice.clear(); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 212 | log.info("Stopped"); |
| 213 | } |
| 214 | |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 215 | @Modified |
| 216 | protected void modified(ComponentContext context) { |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 217 | 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 | */ |
pierventre | 13a7235 | 2020-10-27 16:08:48 +0100 | [diff] [blame] | 227 | protected void readComponentConfiguration(ComponentContext context) { |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 228 | String propertyValue = Tools.get(context.getProperties(), FOM_NUM_THREADS); |
Yi Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 229 | int newNumThreads = isNullOrEmpty(propertyValue) ? numThreads : Integer.parseInt(propertyValue); |
| 230 | |
| 231 | if (newNumThreads != numThreads && newNumThreads > 0) { |
| 232 | numThreads = newNumThreads; |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 233 | 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 Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 242 | if (oldWorkerExecutor != null) { |
| 243 | oldWorkerExecutor.shutdown(); |
| 244 | } |
| 245 | log.info("Reconfigured number of worker threads to {}", numThreads); |
| 246 | } |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 247 | |
| 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 Tseng | 374c5f3 | 2017-03-05 22:51:35 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 279 | /** |
| 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 | */ |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 284 | class ObjectiveProcessor implements Runnable { |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 285 | final DeviceId deviceId; |
| 286 | final Objective objective; |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 287 | final ExecutorService executor; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 288 | |
Sho SHIMIZU | f45d85d | 2015-07-01 14:39:11 -0700 | [diff] [blame] | 289 | private final int numAttempts; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 290 | |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 291 | ObjectiveProcessor(DeviceId deviceId, Objective objective, |
| 292 | ExecutorService executorService) { |
| 293 | this(deviceId, objective, 1, executorService); |
Sho SHIMIZU | f45d85d | 2015-07-01 14:39:11 -0700 | [diff] [blame] | 294 | } |
| 295 | |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 296 | ObjectiveProcessor(DeviceId deviceId, Objective objective, int attempts, |
| 297 | ExecutorService executorService) { |
Sho SHIMIZU | f45d85d | 2015-07-01 14:39:11 -0700 | [diff] [blame] | 298 | this.deviceId = checkNotNull(deviceId); |
| 299 | this.objective = checkNotNull(objective); |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 300 | this.executor = checkNotNull(executorService); |
| 301 | this.numAttempts = attempts; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 302 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 303 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 304 | @Override |
| 305 | public void run() { |
| 306 | try { |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 307 | Pipeliner pipeliner = getDevicePipeliner(deviceId); |
| 308 | |
pier | 561c881 | 2020-05-20 22:50:30 +0200 | [diff] [blame] | 309 | if (pipeliner != null) { |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 310 | if (objective instanceof NextObjective) { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 311 | nextToDevice.put(objective.id(), deviceId); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 312 | pipeliner.next((NextObjective) objective); |
| 313 | } else if (objective instanceof ForwardingObjective) { |
| 314 | pipeliner.forward((ForwardingObjective) objective); |
| 315 | } else { |
| 316 | pipeliner.filter((FilteringObjective) objective); |
| 317 | } |
Andrea Campanella | 1f8188d | 2016-02-29 13:24:54 -0800 | [diff] [blame] | 318 | //Attempts to check if pipeliner is null for retry attempts |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 319 | } else if (numAttempts < INSTALL_RETRY_ATTEMPTS) { |
Saurav Das | 3d03826 | 2015-04-23 12:36:58 -0700 | [diff] [blame] | 320 | Thread.sleep(INSTALL_RETRY_INTERVAL); |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 321 | executor.execute(new ObjectiveProcessor(deviceId, objective, |
| 322 | numAttempts + 1, executor)); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 323 | } else { |
| 324 | // Otherwise we've tried a few times and failed, report an |
| 325 | // error back to the user. |
| 326 | objective.context().ifPresent( |
Andrea Campanella | 1f8188d | 2016-02-29 13:24:54 -0800 | [diff] [blame] | 327 | c -> c.onError(objective, ObjectiveError.NOPIPELINER)); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 328 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 329 | //Exception thrown |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 330 | } catch (Exception e) { |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 331 | log.warn("Exception while processing flow objective", e); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public void filter(DeviceId deviceId, FilteringObjective filteringObjective) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 338 | checkPermission(FLOWRULE_WRITE); |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 339 | installerExecutor.execute(new ObjectiveProcessor(deviceId, filteringObjective, installerExecutor)); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | @Override |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 343 | public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 344 | checkPermission(FLOWRULE_WRITE); |
Yi Tseng | 1666b50 | 2017-05-17 11:05:18 -0700 | [diff] [blame] | 345 | if (forwardingObjective.nextId() == null || |
Yi Tseng | 1666b50 | 2017-05-17 11:05:18 -0700 | [diff] [blame] | 346 | flowObjectiveStore.getNextGroup(forwardingObjective.nextId()) != null || |
| 347 | !queueFwdObjective(deviceId, forwardingObjective)) { |
| 348 | // fast path |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 349 | installerExecutor.execute(new ObjectiveProcessor(deviceId, forwardingObjective, installerExecutor)); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 350 | } |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 351 | } |
| 352 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 353 | @Override |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 354 | public void next(DeviceId deviceId, NextObjective nextObjective) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 355 | checkPermission(FLOWRULE_WRITE); |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 356 | 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 Tseng | 1666b50 | 2017-05-17 11:05:18 -0700 | [diff] [blame] | 360 | flowObjectiveStore.getNextGroup(nextObjective.id()) != null || |
| 361 | !queueNextObjective(deviceId, nextObjective)) { |
| 362 | // either group exists or we are trying to create it - let it through |
pier | a6b1199 | 2020-01-27 17:45:03 +0100 | [diff] [blame] | 363 | installerExecutor.execute(new ObjectiveProcessor(deviceId, nextObjective, installerExecutor)); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 364 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 365 | } |
| 366 | |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 367 | @Override |
| 368 | public int allocateNextId() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 369 | checkPermission(FLOWRULE_WRITE); |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 370 | return flowObjectiveStore.allocateNextId(); |
| 371 | } |
| 372 | |
Xin Jin | 313708b | 2015-07-09 13:43:04 -0700 | [diff] [blame] | 373 | @Override |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 374 | public void initPolicy(String policy) { |
| 375 | } |
Xin Jin | 313708b | 2015-07-09 13:43:04 -0700 | [diff] [blame] | 376 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 377 | boolean queueFwdObjective(DeviceId deviceId, ForwardingObjective fwd) { |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 378 | 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 Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 384 | PendingFlowObjective pendfo = new PendingFlowObjective(deviceId, fwd); |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 385 | if (pending == null) { |
Pier Luigi | 9789311 | 2018-03-05 11:09:42 +0100 | [diff] [blame] | 386 | return Sets.newLinkedHashSet(ImmutableSet.of(pendfo)); |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 387 | } else { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 388 | pending.add(pendfo); |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 389 | return pending; |
| 390 | } |
| 391 | }); |
| 392 | queued = true; |
| 393 | } |
| 394 | } |
| 395 | if (queued) { |
Saurav Das | c568c34 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 396 | log.debug("Queued forwarding objective {} for nextId {} meant for device {}", |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 397 | fwd.id(), fwd.nextId(), deviceId); |
| 398 | } |
| 399 | return queued; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 402 | boolean queueNextObjective(DeviceId deviceId, NextObjective next) { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 403 | // 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 Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 413 | return Lists.newArrayList(pendfo); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 414 | } else { |
| 415 | pending.add(pendfo); |
| 416 | return pending; |
| 417 | } |
| 418 | }); |
| 419 | queued = true; |
| 420 | } |
| 421 | } |
| 422 | if (queued) { |
Saurav Das | c568c34 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 423 | log.debug("Queued next objective {} with operation {} meant for device {}", |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 424 | next.id(), next.op(), deviceId); |
| 425 | } |
| 426 | return queued; |
| 427 | } |
| 428 | |
Pier Ventre | 57a61cd | 2016-09-07 10:55:41 -0700 | [diff] [blame] | 429 | /** |
| 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 Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 436 | private Pipeliner getDevicePipeliner(DeviceId deviceId) { |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 437 | return pipeliners.computeIfAbsent(deviceId, this::initPipelineHandler); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 440 | /** |
Pier Ventre | 57a61cd | 2016-09-07 10:55:41 -0700 | [diff] [blame] | 441 | * 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 HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 461 | * 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 Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 471 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 472 | // Attempt to lookup the handler in the cache |
| 473 | DriverHandler handler = driverHandlers.get(deviceId); |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 474 | cTime = now(); |
| 475 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 476 | if (handler == null) { |
| 477 | try { |
| 478 | // Otherwise create it and if it has pipeline behaviour, cache it |
| 479 | handler = driverService.createHandler(deviceId); |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 480 | dTime = now(); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 481 | if (!handler.driver().hasBehaviour(Pipeliner.class)) { |
Yuta HIGUCHI | a2a4f34 | 2017-03-17 11:38:57 -0700 | [diff] [blame] | 482 | log.debug("Pipeline behaviour not supported for device {}", |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 483 | deviceId); |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 484 | return null; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 485 | } |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 486 | } catch (ItemNotFoundException e) { |
| 487 | log.warn("No applicable driver for device {}", deviceId); |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 488 | return null; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 491 | driverHandlers.put(deviceId, handler); |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 492 | eTime = now(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 493 | } |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 494 | |
| 495 | // Always (re)initialize the pipeline behaviour |
| 496 | log.info("Driver {} bound to device {} ... initializing driver", |
| 497 | handler.driver().name(), deviceId); |
Thomas Vachuska | 0121a61 | 2015-07-21 11:18:09 -0700 | [diff] [blame] | 498 | hTime = now(); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 499 | Pipeliner pipeliner = handler.behaviour(Pipeliner.class); |
Thomas Vachuska | 94c3cf4 | 2015-07-20 13:01:12 -0700 | [diff] [blame] | 500 | hbTime = now(); |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 501 | pipeliner.init(deviceId, context); |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 502 | stopWatch(); |
| 503 | return pipeliner; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 504 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 505 | |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 506 | 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); |
pier | 561c881 | 2020-05-20 22:50:30 +0200 | [diff] [blame] | 518 | pipeliners.remove(id); |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 519 | if (deviceService.isAvailable(id)) { |
| 520 | getAndInitDevicePipeliner(id); |
| 521 | } |
| 522 | } |
| 523 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 524 | // Triggers driver setup when a device is (re)detected. |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 525 | private class InnerDeviceListener implements DeviceListener { |
| 526 | @Override |
| 527 | public void event(DeviceEvent event) { |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 528 | if (devEventExecutor != null) { |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 529 | switch (event.type()) { |
| 530 | case DEVICE_ADDED: |
| 531 | case DEVICE_AVAILABILITY_CHANGED: |
Madan Jampani | 0174f45 | 2015-05-29 11:52:05 -0700 | [diff] [blame] | 532 | log.debug("Device either added or availability changed {}", |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 533 | event.subject().id()); |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 534 | devEventExecutor.execute(() -> { |
| 535 | if (deviceService.isAvailable(event.subject().id())) { |
Madan Jampani | 0174f45 | 2015-05-29 11:52:05 -0700 | [diff] [blame] | 536 | log.debug("Device is now available {}", event.subject().id()); |
Pier Ventre | 57a61cd | 2016-09-07 10:55:41 -0700 | [diff] [blame] | 537 | getAndInitDevicePipeliner(event.subject().id()); |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 538 | } else { |
Yuta HIGUCHI | 1fb0a8c | 2016-08-12 10:59:24 -0700 | [diff] [blame] | 539 | log.debug("Device is no longer available {}", event.subject().id()); |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 540 | } |
| 541 | }); |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 542 | break; |
| 543 | case DEVICE_UPDATED: |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 544 | // Invalidate pipeliner and handler caches if the driver name |
| 545 | // device annotation changed. |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 546 | devEventExecutor.execute(() -> invalidatePipelinerIfNecessary(event.subject())); |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 547 | break; |
| 548 | case DEVICE_REMOVED: |
Yuta HIGUCHI | ad0c990 | 2016-08-23 10:37:32 -0700 | [diff] [blame] | 549 | // evict Pipeliner and Handler cache, when |
| 550 | // the Device was administratively removed. |
| 551 | // |
| 552 | // System expect the user to clear all existing flows, |
| 553 | // before removing device, especially if they intend to |
| 554 | // replace driver/pipeliner assigned to the device. |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 555 | devEventExecutor.execute(() -> { |
pier | 9ada419 | 2020-03-20 11:00:38 +0100 | [diff] [blame] | 556 | driverHandlers.remove(event.subject().id()); |
pier | 561c881 | 2020-05-20 22:50:30 +0200 | [diff] [blame] | 557 | pipeliners.remove(event.subject().id()); |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 558 | }); |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 559 | break; |
Yuta HIGUCHI | ad0c990 | 2016-08-23 10:37:32 -0700 | [diff] [blame] | 560 | case DEVICE_SUSPENDED: |
| 561 | break; |
alshabib | 4313d10 | 2015-04-08 18:55:08 -0700 | [diff] [blame] | 562 | case PORT_ADDED: |
| 563 | break; |
| 564 | case PORT_UPDATED: |
| 565 | break; |
| 566 | case PORT_REMOVED: |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 567 | break; |
| 568 | default: |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 569 | break; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 570 | } |
Ruchi Sahota | e4934e1 | 2019-03-01 16:56:07 +0000 | [diff] [blame] | 571 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 572 | } |
| 573 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 574 | |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 575 | // Monitors driver configuration changes and invalidates the pipeliner cache entries. |
| 576 | // Note that this may leave stale entries on the device if the driver changes |
| 577 | // in manner where the new driver does not produce backward compatible flow objectives. |
| 578 | // In such cases, it is the operator's responsibility to force device re-connect. |
| 579 | private class InnerDriverListener implements DriverListener { |
| 580 | @Override |
| 581 | public void event(DriverEvent event) { |
| 582 | String driverName = event.subject().name(); |
Andrea Campanella | d056102 | 2021-03-26 10:34:30 +0100 | [diff] [blame] | 583 | //we invalidate the pipeliner if the event is for the same driver or |
| 584 | // if we have the device in the cache but the driver for it changed. |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 585 | driverHandlers.entrySet().stream() |
Andrea Campanella | d056102 | 2021-03-26 10:34:30 +0100 | [diff] [blame] | 586 | .filter(e -> driverName.equals(e.getValue().driver().name()) |
| 587 | || !e.getValue().driver().name() |
| 588 | .equals(driverService.getDriver(e.getKey()).name())) |
Thomas Vachuska | cfeff19 | 2017-08-23 15:29:34 -0700 | [diff] [blame] | 589 | .map(Map.Entry::getKey) |
| 590 | .distinct() |
| 591 | .forEach(FlowObjectiveManager.this::invalidatePipeliner); |
| 592 | } |
| 593 | } |
| 594 | |
Thomas Vachuska | 174bb91 | 2015-07-16 21:27:14 -0700 | [diff] [blame] | 595 | // Temporary mechanism to monitor pipeliner setup time-cost; there are |
| 596 | // intermittent time where this takes in excess of 2 seconds. Why? |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 597 | private long start = 0, totals = 0, count = 0; |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 598 | private long cTime, dTime, eTime, hTime, hbTime; |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 599 | private static final long LIMIT = 500; |
Thomas Vachuska | 174bb91 | 2015-07-16 21:27:14 -0700 | [diff] [blame] | 600 | |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 601 | private long now() { |
Thomas Vachuska | 174bb91 | 2015-07-16 21:27:14 -0700 | [diff] [blame] | 602 | return System.currentTimeMillis(); |
| 603 | } |
| 604 | |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 605 | private void stopWatch() { |
Thomas Vachuska | 174bb91 | 2015-07-16 21:27:14 -0700 | [diff] [blame] | 606 | long duration = System.currentTimeMillis() - start; |
| 607 | totals += duration; |
| 608 | count += 1; |
| 609 | if (duration > LIMIT) { |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 610 | log.info("Pipeline setup took {} ms; avg {} ms; cTime={}, dTime={}, eTime={}, hTime={}, hbTime={}", |
| 611 | duration, totals / count, diff(cTime), diff(dTime), diff(eTime), diff(hTime), diff(hbTime)); |
Thomas Vachuska | 174bb91 | 2015-07-16 21:27:14 -0700 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 615 | private long diff(long bTime) { |
Thomas Vachuska | b19bffb | 2015-07-22 10:56:16 -0700 | [diff] [blame] | 616 | long diff = bTime - start; |
| 617 | return diff < 0 ? 0 : diff; |
Thomas Vachuska | 9c9ff7c | 2015-07-20 10:38:59 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 620 | // Processing context for initializing pipeline driver behaviours. |
| 621 | private class InnerPipelineContext implements PipelinerContext { |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 622 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 623 | @Override |
| 624 | public ServiceDirectory directory() { |
| 625 | return serviceDirectory; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 626 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 627 | |
| 628 | @Override |
| 629 | public FlowObjectiveStore store() { |
| 630 | return flowObjectiveStore; |
| 631 | } |
pier | 8b3aef4 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 632 | |
| 633 | @Override |
| 634 | public int accumulatorMaxObjectives() { |
| 635 | return accumulatorMaxObjectives; |
| 636 | } |
| 637 | |
| 638 | @Override |
| 639 | public int accumulatorMaxIdleMillis() { |
| 640 | return accumulatorMaxIdleMillis; |
| 641 | } |
| 642 | |
| 643 | @Override |
| 644 | public int accumulatorMaxBatchMillis() { |
| 645 | return accumulatorMaxBatchMillis; |
| 646 | } |
| 647 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | private class InternalStoreDelegate implements FlowObjectiveStoreDelegate { |
| 651 | @Override |
| 652 | public void notify(ObjectiveEvent event) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 653 | if (event.type() == Type.ADD) { |
| 654 | log.debug("Received notification of obj event {}", event); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 655 | Set<PendingFlowObjective> pending; |
| 656 | |
| 657 | // first send all pending flows |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 658 | synchronized (pendingForwards) { |
| 659 | // needs to be synchronized for queueObjective lookup |
| 660 | pending = pendingForwards.remove(event.subject()); |
| 661 | } |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 662 | if (pending == null) { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 663 | log.debug("No forwarding objectives pending for this " |
| 664 | + "obj event {}", event); |
| 665 | } else { |
| 666 | log.debug("Processing {} pending forwarding objectives for nextId {}", |
| 667 | pending.size(), event.subject()); |
| 668 | pending.forEach(p -> getDevicePipeliner(p.deviceId()) |
| 669 | .forward((ForwardingObjective) p.flowObjective())); |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 670 | } |
| 671 | |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 672 | // now check for pending next-objectives |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 673 | List<PendingFlowObjective> pendNexts; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 674 | synchronized (pendingNexts) { |
| 675 | // needs to be synchronized for queueObjective lookup |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 676 | pendNexts = pendingNexts.remove(event.subject()); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 677 | } |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 678 | if (pendNexts == null) { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 679 | log.debug("No next objectives pending for this " |
| 680 | + "obj event {}", event); |
| 681 | } else { |
| 682 | log.debug("Processing {} pending next objectives for nextId {}", |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 683 | pendNexts.size(), event.subject()); |
| 684 | pendNexts.forEach(p -> getDevicePipeliner(p.deviceId()) |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 685 | .next((NextObjective) p.flowObjective())); |
| 686 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 687 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | /** |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 692 | * Data class used to hold a pending flow objective that could not |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 693 | * be processed because the associated next object was not present. |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 694 | * Note that this pending flow objective could be a forwarding objective |
| 695 | * waiting for a next objective to complete execution. Or it could a |
| 696 | * next objective (with a different operation - remove, addToExisting, or |
| 697 | * removeFromExisting) waiting for a next objective with the same id to |
| 698 | * complete execution. |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 699 | */ |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 700 | protected class PendingFlowObjective { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 701 | private final DeviceId deviceId; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 702 | private final Objective flowObj; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 703 | |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 704 | PendingFlowObjective(DeviceId deviceId, Objective flowObj) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 705 | this.deviceId = deviceId; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 706 | this.flowObj = flowObj; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | public DeviceId deviceId() { |
| 710 | return deviceId; |
| 711 | } |
| 712 | |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 713 | public Objective flowObjective() { |
| 714 | return flowObj; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 715 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 716 | |
| 717 | @Override |
| 718 | public int hashCode() { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 719 | return Objects.hash(deviceId, flowObj); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | @Override |
| 723 | public boolean equals(final Object obj) { |
| 724 | if (this == obj) { |
| 725 | return true; |
| 726 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 727 | if (!(obj instanceof PendingFlowObjective)) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 728 | return false; |
| 729 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 730 | final PendingFlowObjective other = (PendingFlowObjective) obj; |
Charles Chan | a7903c8 | 2018-03-15 20:14:16 -0700 | [diff] [blame] | 731 | |
| 732 | return this.deviceId.equals(other.deviceId) && |
| 733 | this.flowObj.equals(other.flowObj); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 734 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 735 | } |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 736 | |
| 737 | @Override |
| 738 | public List<String> getNextMappings() { |
| 739 | List<String> mappings = new ArrayList<>(); |
| 740 | Map<Integer, NextGroup> allnexts = flowObjectiveStore.getAllGroups(); |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 741 | // XXX if the NextGroup after de-serialization actually stored info of the deviceId |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 742 | // then info on any nextObj could be retrieved from one controller instance. |
| 743 | // Right now the drivers on one instance can only fetch for next-ids that came |
| 744 | // to them. |
| 745 | // Also, we still need to send the right next-id to the right driver as potentially |
| 746 | // there can be different drivers for different devices. But on that account, |
| 747 | // no instance should be decoding for another instance's nextIds. |
| 748 | |
| 749 | for (Map.Entry<Integer, NextGroup> e : allnexts.entrySet()) { |
| 750 | // get the device this next Objective was sent to |
| 751 | DeviceId deviceId = nextToDevice.get(e.getKey()); |
| 752 | mappings.add("NextId " + e.getKey() + ": " + |
| 753 | ((deviceId != null) ? deviceId : "nextId not in this onos instance")); |
| 754 | if (deviceId != null) { |
| 755 | // this instance of the controller sent the nextObj to a driver |
| 756 | Pipeliner pipeliner = getDevicePipeliner(deviceId); |
| 757 | List<String> nextMappings = pipeliner.getNextMappings(e.getValue()); |
| 758 | if (nextMappings != null) { |
| 759 | mappings.addAll(nextMappings); |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | return mappings; |
| 764 | } |
Saurav Das | b5c236e | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 765 | |
| 766 | @Override |
Harshada Chaundkar | 5a198b0 | 2019-07-03 16:27:45 +0000 | [diff] [blame] | 767 | public Map<Pair<Integer, DeviceId>, List<String>> getNextMappingsChain() { |
| 768 | Map<Pair<Integer, DeviceId>, List<String>> nextObjGroupMap = new HashMap<>(); |
| 769 | Map<Integer, NextGroup> allnexts = flowObjectiveStore.getAllGroups(); |
| 770 | |
| 771 | // XXX if the NextGroup after de-serialization actually stored info of the deviceId |
| 772 | // then info on any nextObj could be retrieved from one controller instance. |
| 773 | // Right now the drivers on one instance can only fetch for next-ids that came |
| 774 | // to them. |
| 775 | // Also, we still need to send the right next-id to the right driver as potentially |
| 776 | // there can be different drivers for different devices. But on that account, |
| 777 | // no instance should be decoding for another instance's nextIds. |
| 778 | |
| 779 | for (Map.Entry<Integer, NextGroup> e : allnexts.entrySet()) { |
| 780 | // get the device this next Objective was sent to |
| 781 | DeviceId deviceId = nextToDevice.get(e.getKey()); |
| 782 | if (deviceId != null) { |
| 783 | // this instance of the controller sent the nextObj to a driver |
| 784 | Pipeliner pipeliner = getDevicePipeliner(deviceId); |
| 785 | List<String> nextMappings = pipeliner.getNextMappings(e.getValue()); |
| 786 | if (nextMappings != null) { |
| 787 | //mappings.addAll(nextMappings); |
| 788 | nextObjGroupMap.put(Pair.of(e.getKey(), deviceId), nextMappings); |
| 789 | } |
| 790 | } else { |
| 791 | nextObjGroupMap.put(Pair.of(e.getKey(), deviceId), ImmutableList.of("nextId not in this onos instance")); |
| 792 | } |
| 793 | } |
| 794 | return nextObjGroupMap; |
| 795 | } |
| 796 | |
| 797 | |
| 798 | @Override |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 799 | public List<String> getPendingFlowObjectives() { |
| 800 | List<String> pendingFlowObjectives = new ArrayList<>(); |
Charles Chan | 5473471 | 2017-03-29 11:07:55 -0700 | [diff] [blame] | 801 | |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 802 | for (Integer nextId : pendingForwards.keySet()) { |
| 803 | Set<PendingFlowObjective> pfwd = pendingForwards.get(nextId); |
Sho SHIMIZU | 81470a5 | 2016-08-12 17:24:55 -0700 | [diff] [blame] | 804 | StringBuilder pend = new StringBuilder(); |
Charles Chan | 5473471 | 2017-03-29 11:07:55 -0700 | [diff] [blame] | 805 | pend.append("NextId: ") |
| 806 | .append(nextId); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 807 | for (PendingFlowObjective pf : pfwd) { |
Charles Chan | 5473471 | 2017-03-29 11:07:55 -0700 | [diff] [blame] | 808 | pend.append("\n FwdId: ") |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 809 | .append(String.format("%11s", pf.flowObjective().id())) |
| 810 | .append(", DeviceId: ") |
| 811 | .append(pf.deviceId()) |
| 812 | .append(", Selector: ") |
| 813 | .append(((ForwardingObjective) pf.flowObjective()) |
| 814 | .selector().criteria()); |
| 815 | } |
| 816 | pendingFlowObjectives.add(pend.toString()); |
| 817 | } |
| 818 | |
| 819 | for (Integer nextId : pendingNexts.keySet()) { |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 820 | List<PendingFlowObjective> pnext = pendingNexts.get(nextId); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 821 | StringBuilder pend = new StringBuilder(); |
| 822 | pend.append("NextId: ") |
| 823 | .append(nextId); |
| 824 | for (PendingFlowObjective pn : pnext) { |
| 825 | pend.append("\n NextOp: ") |
| 826 | .append(pn.flowObjective().op()) |
Charles Chan | 5473471 | 2017-03-29 11:07:55 -0700 | [diff] [blame] | 827 | .append(", DeviceId: ") |
| 828 | .append(pn.deviceId()) |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 829 | .append(", Treatments: ") |
| 830 | .append(((NextObjective) pn.flowObjective()) |
| 831 | .next()); |
Saurav Das | b5c236e | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 832 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 833 | pendingFlowObjectives.add(pend.toString()); |
Saurav Das | b5c236e | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 834 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 835 | |
| 836 | return pendingFlowObjectives; |
| 837 | } |
Daniele Moro | 06aac70 | 2021-07-19 22:39:22 +0200 | [diff] [blame] | 838 | |
| 839 | @Override |
| 840 | public void purgeAll(DeviceId deviceId, ApplicationId appId) { |
| 841 | synchronized (pendingForwards) { |
| 842 | List<Integer> emptyPendingForwards = Lists.newArrayList(); |
| 843 | pendingForwards.forEach((nextId, pendingObjectives) -> { |
| 844 | pendingObjectives.removeIf(pendingFlowObjective -> pendingFlowObjective.deviceId().equals(deviceId)); |
| 845 | if (pendingObjectives.isEmpty()) { |
| 846 | emptyPendingForwards.add(nextId); |
| 847 | } |
| 848 | }); |
| 849 | emptyPendingForwards.forEach(pendingForwards::remove); |
| 850 | } |
| 851 | synchronized (pendingNexts) { |
| 852 | List<Integer> emptyPendingNexts = Lists.newArrayList(); |
| 853 | pendingNexts.forEach((nextId, pendingObjectives) -> { |
| 854 | pendingObjectives.removeIf(pendingFlowObjective -> pendingFlowObjective.deviceId().equals(deviceId)); |
| 855 | if (pendingObjectives.isEmpty()) { |
| 856 | emptyPendingNexts.add(nextId); |
| 857 | } |
| 858 | }); |
| 859 | emptyPendingNexts.forEach(pendingNexts::remove); |
| 860 | } |
| 861 | Pipeliner pipeliner = getDevicePipeliner(deviceId); |
| 862 | if (pipeliner != null) { |
| 863 | pipeliner.purgeAll(appId); |
| 864 | } else { |
| 865 | log.warn("Skip purgeAll, pipeliner not ready!"); |
| 866 | } |
| 867 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 868 | } |