Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [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 | |
| 17 | package org.onosproject.net.pi.impl; |
| 18 | |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 19 | import com.google.common.annotations.Beta; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
| 22 | import com.google.common.util.concurrent.Striped; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 23 | import org.onlab.util.ItemNotFoundException; |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 24 | import org.onlab.util.SharedExecutors; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 26 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 27 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 28 | import org.onosproject.net.device.PortStatisticsDiscovery; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 29 | import org.onosproject.net.driver.Behaviour; |
| 30 | import org.onosproject.net.driver.DefaultDriver; |
| 31 | import org.onosproject.net.driver.Driver; |
| 32 | import org.onosproject.net.driver.DriverAdminService; |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 33 | import org.onosproject.net.driver.DriverEvent; |
| 34 | import org.onosproject.net.driver.DriverListener; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 35 | import org.onosproject.net.driver.DriverProvider; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 36 | import org.onosproject.net.pi.model.PiPipeconf; |
| 37 | import org.onosproject.net.pi.model.PiPipeconfId; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 38 | import org.onosproject.net.pi.service.PiPipeconfMappingStore; |
| 39 | import org.onosproject.net.pi.service.PiPipeconfService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 40 | import org.osgi.service.component.annotations.Activate; |
| 41 | import org.osgi.service.component.annotations.Component; |
| 42 | import org.osgi.service.component.annotations.Deactivate; |
| 43 | import org.osgi.service.component.annotations.Reference; |
| 44 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 45 | import org.slf4j.Logger; |
| 46 | |
| 47 | import java.util.HashMap; |
| 48 | import java.util.Map; |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 49 | import java.util.Objects; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 50 | import java.util.Optional; |
| 51 | import java.util.Set; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 52 | import java.util.concurrent.ConcurrentHashMap; |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 53 | import java.util.concurrent.ConcurrentMap; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 54 | import java.util.concurrent.ExecutorService; |
| 55 | import java.util.concurrent.Executors; |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 56 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 57 | import java.util.concurrent.locks.Lock; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 58 | |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 59 | import static java.lang.String.format; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 60 | import static org.onlab.util.Tools.groupedThreads; |
| 61 | import static org.slf4j.LoggerFactory.getLogger; |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Implementation of the PiPipeconfService. |
| 66 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 67 | @Component(immediate = true, service = PiPipeconfService.class) |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 68 | @Beta |
| 69 | public class PiPipeconfManager implements PiPipeconfService { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 70 | |
| 71 | private final Logger log = getLogger(getClass()); |
| 72 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 73 | private static final String MERGED_DRIVER_SEPARATOR = ":"; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 74 | |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 75 | private static final int MISSING_DRIVER_WATCHDOG_INTERVAL = 5; // Seconds. |
| 76 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 78 | protected NetworkConfigRegistry cfgService; |
| 79 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 81 | protected DriverAdminService driverAdminService; |
| 82 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 84 | private PiPipeconfMappingStore pipeconfMappingStore; |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 85 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 86 | // Registered pipeconf are replicated through the app subsystem and |
| 87 | // registered on app activated events. Hence, there should be no need of |
| 88 | // distributing this map. |
| 89 | protected ConcurrentMap<PiPipeconfId, PiPipeconf> pipeconfs = new ConcurrentHashMap<>(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 90 | |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 91 | private final DriverListener driverListener = new InternalDriverListener(); |
| 92 | private final Set<String> missingMergedDrivers = Sets.newCopyOnWriteArraySet(); |
| 93 | private final Striped<Lock> locks = Striped.lock(20); |
| 94 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 95 | protected ExecutorService executor = Executors.newFixedThreadPool( |
| 96 | 10, groupedThreads("onos/pipeconf-manager", "%d", log)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 97 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 98 | @Activate |
| 99 | public void activate() { |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 100 | driverAdminService.addListener(driverListener); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 101 | checkMissingMergedDrivers(); |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 102 | if (!missingMergedDrivers.isEmpty()) { |
| 103 | // Missing drivers should be created upon detecting registration |
| 104 | // events of a new pipeconf or a base driver. If, for any reason, we |
| 105 | // miss such event, here's a watchdog task. |
| 106 | SharedExecutors.getPoolThreadExecutor() |
| 107 | .execute(this::missingDriversWatchdogTask); |
| 108 | } |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 109 | log.info("Started"); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | @Deactivate |
| 114 | public void deactivate() { |
| 115 | executor.shutdown(); |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 116 | driverAdminService.removeListener(driverListener); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 117 | pipeconfs.clear(); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 118 | missingMergedDrivers.clear(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 119 | cfgService = null; |
| 120 | driverAdminService = null; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 121 | log.info("Stopped"); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void register(PiPipeconf pipeconf) throws IllegalStateException { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 126 | if (pipeconfs.containsKey(pipeconf.id())) { |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 127 | throw new IllegalStateException(format("Pipeconf %s is already registered", pipeconf.id())); |
| 128 | } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 129 | pipeconfs.put(pipeconf.id(), pipeconf); |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 130 | log.info("New pipeconf registered: {}", pipeconf.id()); |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 131 | executor.execute(() -> attemptMergeAll(pipeconf.id())); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 135 | public void remove(PiPipeconfId pipeconfId) throws IllegalStateException { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 136 | // TODO add mechanism to remove from device. |
| 137 | if (!pipeconfs.containsKey(pipeconfId)) { |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 138 | throw new IllegalStateException(format("Pipeconf %s is not registered", pipeconfId)); |
| 139 | } |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 140 | // TODO remove the binding from the distributed Store when the lifecycle of a pipeconf is defined. |
| 141 | // pipeconfMappingStore.removeBindings(pipeconfId); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 142 | log.info("Removing pipeconf {}", pipeconfId); |
| 143 | pipeconfs.remove(pipeconfId); |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 147 | public Iterable<PiPipeconf> getPipeconfs() { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 148 | return pipeconfs.values(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Optional<PiPipeconf> getPipeconf(PiPipeconfId id) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 153 | return Optional.ofNullable(pipeconfs.get(id)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 157 | public Optional<PiPipeconf> getPipeconf(DeviceId deviceId) { |
| 158 | if (pipeconfMappingStore.getPipeconfId(deviceId) == null) { |
| 159 | return Optional.empty(); |
| 160 | } else { |
| 161 | return Optional.ofNullable(pipeconfs.get( |
| 162 | pipeconfMappingStore.getPipeconfId(deviceId))); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | @Override |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 167 | public void bindToDevice(PiPipeconfId pipeconfId, DeviceId deviceId) { |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 168 | PiPipeconfId existingPipeconfId = pipeconfMappingStore.getPipeconfId(deviceId); |
| 169 | if (existingPipeconfId != null && !existingPipeconfId.equals(pipeconfId)) { |
| 170 | log.error("Cannot set binding for {} to {} as one already exists ({})", |
| 171 | deviceId, pipeconfId, existingPipeconfId); |
| 172 | return; |
| 173 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 174 | pipeconfMappingStore.createOrUpdateBinding(deviceId, pipeconfId); |
| 175 | } |
| 176 | |
| 177 | @Override |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 178 | public String getMergedDriver(DeviceId deviceId, PiPipeconfId pipeconfId) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 179 | log.debug("Starting device driver merge of {} with {}...", deviceId, pipeconfId); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 180 | final BasicDeviceConfig basicDeviceConfig = cfgService.getConfig( |
| 181 | deviceId, BasicDeviceConfig.class); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 182 | if (basicDeviceConfig == null) { |
| 183 | log.warn("Unable to get basic device config for {}, " + |
Tian Jian | 855b108 | 2018-10-23 16:22:47 +0800 | [diff] [blame] | 184 | "aborting pipeconf driver merge", deviceId); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 185 | return null; |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 186 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 187 | String baseDriverName = basicDeviceConfig.driver(); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 188 | if (baseDriverName == null) { |
| 189 | log.warn("Missing driver from basic device config for {}, " + |
| 190 | "cannot produce merged driver", deviceId); |
| 191 | return null; |
| 192 | } |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 193 | if (isMergedDriverName(baseDriverName)) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 194 | // The config already has driver name that is a merged one. We still |
| 195 | // need to make sure an instance of that merged driver is present in |
| 196 | // this node. |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 197 | log.debug("Base driver of {} ({}) is a merged one", |
| 198 | deviceId, baseDriverName); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 199 | baseDriverName = getBaseDriverNameFromMerged(baseDriverName); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 200 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 201 | |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 202 | return doMergeDriver(baseDriverName, pipeconfId); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public Optional<PiPipeconfId> ofDevice(DeviceId deviceId) { |
| 207 | return Optional.ofNullable(pipeconfMappingStore.getPipeconfId(deviceId)); |
| 208 | } |
| 209 | |
| 210 | private String doMergeDriver(String baseDriverName, PiPipeconfId pipeconfId) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 211 | final String newDriverName = mergedDriverName(baseDriverName, pipeconfId); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 212 | // Serialize per newDriverName, avoid creating duplicates. |
| 213 | locks.get(newDriverName).lock(); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 214 | try { |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 215 | // If merged driver exists already we don't create a new one. |
| 216 | if (getDriver(newDriverName) != null) { |
| 217 | return newDriverName; |
| 218 | } |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 219 | log.debug("Creating merged driver {}...", newDriverName); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 220 | final Driver mergedDriver = buildMergedDriver( |
| 221 | pipeconfId, baseDriverName, newDriverName); |
| 222 | if (mergedDriver == null) { |
| 223 | // Error logged by buildMergedDriver |
| 224 | return null; |
| 225 | } |
| 226 | registerMergedDriver(mergedDriver); |
| 227 | if (missingMergedDrivers.remove(newDriverName)) { |
| 228 | log.info("There are still {} missing merged drivers", |
| 229 | missingMergedDrivers.size()); |
| 230 | } |
| 231 | return newDriverName; |
| 232 | } finally { |
| 233 | locks.get(newDriverName).unlock(); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 234 | } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 237 | private String mergedDriverSuffix(PiPipeconfId pipeconfId) { |
| 238 | return MERGED_DRIVER_SEPARATOR + pipeconfId.id(); |
| 239 | } |
| 240 | |
| 241 | private String mergedDriverName(String baseDriverName, PiPipeconfId pipeconfId) { |
| 242 | return baseDriverName + mergedDriverSuffix(pipeconfId); |
| 243 | } |
| 244 | |
| 245 | private String getBaseDriverNameFromMerged(String mergedDriverName) { |
| 246 | final String[] pieces = mergedDriverName.split(MERGED_DRIVER_SEPARATOR); |
| 247 | if (pieces.length != 2) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 248 | return null; |
| 249 | } |
| 250 | return pieces[0]; |
| 251 | } |
| 252 | |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 253 | private PiPipeconfId getPipeconfIdFromMerged(String mergedDriverName) { |
| 254 | final String[] pieces = mergedDriverName.split(MERGED_DRIVER_SEPARATOR); |
| 255 | if (pieces.length != 2) { |
| 256 | return null; |
| 257 | } |
| 258 | return new PiPipeconfId(pieces[1]); |
| 259 | } |
| 260 | |
| 261 | private boolean isMergedDriverName(String driverName) { |
| 262 | final String[] pieces = driverName.split(MERGED_DRIVER_SEPARATOR); |
| 263 | return pieces.length == 2; |
| 264 | } |
| 265 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 266 | private Driver buildMergedDriver(PiPipeconfId pipeconfId, String baseDriverName, |
| 267 | String newDriverName) { |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 268 | final Driver baseDriver = getDriver(baseDriverName); |
| 269 | if (baseDriver == null) { |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 270 | log.error("Base driver {} not found, cannot build a merged one", |
| 271 | baseDriverName); |
| 272 | return null; |
| 273 | } |
| 274 | |
| 275 | final PiPipeconf pipeconf = pipeconfs.get(pipeconfId); |
| 276 | if (pipeconf == null) { |
| 277 | log.error("Pipeconf {} is not registered, cannot build a merged driver", |
| 278 | pipeconfId); |
| 279 | return null; |
| 280 | } |
| 281 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 282 | // extract the behaviours from the pipipeconf. |
| 283 | final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = |
| 284 | new HashMap<>(); |
| 285 | pipeconf.behaviours().forEach( |
| 286 | b -> behaviours.put(b, pipeconf.implementation(b).get())); |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame^] | 287 | |
| 288 | // FIXME: remove this check when stratum_bmv2 will be open source and we |
| 289 | // will no longer need to read port counters from the p4 program. Here |
| 290 | // we ignore the PortStatisticsDiscovery behaviour from the pipeconf if |
| 291 | // the base driver (e.g. stratum with gnmi) already has it. But in |
| 292 | // general, we should give higher priority to pipeconf behaviours. |
| 293 | if (baseDriver.hasBehaviour(PortStatisticsDiscovery.class) |
| 294 | && behaviours.remove(PortStatisticsDiscovery.class) != null) { |
| 295 | log.warn("Ignoring {} behaviour from pipeconf {}, but using " + |
| 296 | "the one provided by {} driver...", |
| 297 | PortStatisticsDiscovery.class.getSimpleName(), pipeconfId, |
| 298 | baseDriver.name()); |
| 299 | } |
| 300 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 301 | final Driver piPipeconfDriver = new DefaultDriver( |
| 302 | newDriverName, baseDriver.parents(), |
| 303 | baseDriver.manufacturer(), baseDriver.hwVersion(), |
| 304 | baseDriver.swVersion(), behaviours, new HashMap<>()); |
| 305 | // take the base driver created with the behaviours of the PiPeconf and |
| 306 | // merge it with the base driver that was assigned to the device |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 307 | return piPipeconfDriver.merge(baseDriver); |
| 308 | } |
| 309 | |
| 310 | private void registerMergedDriver(Driver driver) { |
| 311 | final DriverProvider provider = new InternalDriverProvider(driver); |
| 312 | if (driverAdminService.getProviders().contains(provider)) { |
| 313 | // A provider for this driver already exist. |
| 314 | return; |
| 315 | } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 316 | driverAdminService.registerProvider(provider); |
| 317 | } |
| 318 | |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 319 | private Driver getDriver(String name) { |
| 320 | try { |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 321 | return driverAdminService.getDriver(name); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 322 | } catch (ItemNotFoundException e) { |
| 323 | return null; |
| 324 | } |
| 325 | } |
| 326 | |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 327 | private boolean driverExists(String name) { |
| 328 | return getDriver(name) != null; |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 331 | private void checkMissingMergedDriver(DeviceId deviceId) { |
| 332 | final PiPipeconfId pipeconfId = pipeconfMappingStore.getPipeconfId(deviceId); |
| 333 | final BasicDeviceConfig cfg = cfgService.getConfig(deviceId, BasicDeviceConfig.class); |
| 334 | |
| 335 | if (pipeconfId == null) { |
| 336 | // No pipeconf associated. |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | if (cfg == null || cfg.driver() == null) { |
| 341 | log.warn("Missing basic device config or driver key in netcfg for " + |
| 342 | "{}, which is odd since it has a " + |
| 343 | "pipeconf associated ({})", |
| 344 | deviceId, pipeconfId); |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | final String baseDriverName = cfg.driver(); |
| 349 | final String mergedDriverName = mergedDriverName(baseDriverName, pipeconfId); |
| 350 | |
| 351 | if (driverExists(mergedDriverName) || |
| 352 | missingMergedDrivers.contains(mergedDriverName)) { |
| 353 | // Not missing, or already aware of it missing. |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | log.info("Detected missing merged driver: {}", mergedDriverName); |
| 358 | missingMergedDrivers.add(mergedDriverName); |
| 359 | // Attempt building the driver now if all pieces are present. |
| 360 | // If not, either a driver or pipeconf event will re-trigger |
| 361 | // the process. |
| 362 | attemptDriverMerge(mergedDriverName); |
| 363 | } |
| 364 | |
| 365 | private void attemptDriverMerge(String mergedDriverName) { |
| 366 | final String baseDriverName = getBaseDriverNameFromMerged(mergedDriverName); |
| 367 | final PiPipeconfId pipeconfId = getPipeconfIdFromMerged(mergedDriverName); |
| 368 | if (driverExists(baseDriverName) && pipeconfs.containsKey(pipeconfId)) { |
| 369 | doMergeDriver(baseDriverName, pipeconfId); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | private void missingDriversWatchdogTask() { |
| 374 | while (true) { |
| 375 | // Most probably all missing drivers will be created before the |
| 376 | // watchdog interval, so wait before starting... |
| 377 | try { |
| 378 | TimeUnit.SECONDS.sleep(MISSING_DRIVER_WATCHDOG_INTERVAL); |
| 379 | } catch (InterruptedException e) { |
| 380 | log.warn("Interrupted! There are still {} missing merged drivers", |
| 381 | missingMergedDrivers.size()); |
| 382 | } |
| 383 | if (missingMergedDrivers.isEmpty()) { |
| 384 | log.info("There are no more missing merged drivers!"); |
| 385 | return; |
| 386 | } |
| 387 | log.info("Detected {} missing merged drivers, attempt merge...", |
| 388 | missingMergedDrivers.size()); |
| 389 | missingMergedDrivers.forEach(this::attemptDriverMerge); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | private void checkMissingMergedDrivers() { |
| 394 | cfgService.getSubjects(DeviceId.class, BasicDeviceConfig.class) |
| 395 | .forEach(this::checkMissingMergedDriver); |
| 396 | } |
| 397 | |
| 398 | private void attemptMergeAll(String baseDriverName) { |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 399 | missingMergedDrivers.stream() |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 400 | .filter(missingDriver -> { |
| 401 | // Filter missing merged drivers using this base driver. |
| 402 | final String xx = getBaseDriverNameFromMerged(missingDriver); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 403 | return xx != null && xx.equals(baseDriverName); |
| 404 | }) |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 405 | .forEach(this::attemptDriverMerge); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 408 | private void attemptMergeAll(PiPipeconfId pipeconfId) { |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 409 | missingMergedDrivers.stream() |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 410 | .filter(missingDriver -> { |
| 411 | // Filter missing merged drivers using this pipeconf. |
| 412 | final PiPipeconfId xx = getPipeconfIdFromMerged(missingDriver); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 413 | return xx != null && xx.equals(pipeconfId); |
| 414 | }) |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 415 | .forEach(this::attemptDriverMerge); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | private class InternalDriverListener implements DriverListener { |
| 419 | |
| 420 | @Override |
| 421 | public void event(DriverEvent event) { |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 422 | executor.execute(() -> attemptMergeAll(event.subject().name())); |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public boolean isRelevant(DriverEvent event) { |
| 427 | return event.type() == DriverEvent.Type.DRIVER_ENHANCED; |
| 428 | } |
| 429 | } |
| 430 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 431 | /** |
| 432 | * Internal driver provider used to register merged pipeconf drivers in the |
| 433 | * core. |
| 434 | */ |
| 435 | private class InternalDriverProvider implements DriverProvider { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 436 | |
| 437 | Driver driver; |
| 438 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 439 | InternalDriverProvider(Driver driver) { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 440 | this.driver = driver; |
| 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public Set<Driver> getDrivers() { |
| 445 | return ImmutableSet.of(driver); |
| 446 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 447 | |
| 448 | @Override |
| 449 | public boolean equals(Object o) { |
| 450 | if (this == o) { |
| 451 | return true; |
| 452 | } |
| 453 | if (o == null || getClass() != o.getClass()) { |
| 454 | return false; |
| 455 | } |
| 456 | InternalDriverProvider that = (InternalDriverProvider) o; |
Carmelo Cascone | da60a61 | 2018-08-24 00:01:34 -0700 | [diff] [blame] | 457 | return Objects.equals(driver.name(), that.driver.name()); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | @Override |
| 461 | public int hashCode() { |
| 462 | return Objects.hashCode(driver.name()); |
| 463 | } |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 464 | } |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 465 | } |