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; |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 20 | import com.google.common.base.Objects; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.apache.felix.scr.annotations.Service; |
| 28 | import org.onlab.util.ItemNotFoundException; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 29 | import org.onosproject.cluster.ClusterService; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.config.ConfigFactory; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 32 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 33 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 34 | import org.onosproject.net.config.basics.SubjectFactories; |
| 35 | import org.onosproject.net.driver.Behaviour; |
| 36 | import org.onosproject.net.driver.DefaultDriver; |
| 37 | import org.onosproject.net.driver.Driver; |
| 38 | import org.onosproject.net.driver.DriverAdminService; |
| 39 | import org.onosproject.net.driver.DriverProvider; |
| 40 | import org.onosproject.net.driver.DriverService; |
| 41 | import org.onosproject.net.pi.model.PiPipeconf; |
| 42 | import org.onosproject.net.pi.model.PiPipeconfId; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 43 | import org.onosproject.net.pi.service.PiPipeconfConfig; |
| 44 | import org.onosproject.net.pi.service.PiPipeconfMappingStore; |
| 45 | import org.onosproject.net.pi.service.PiPipeconfService; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | |
| 48 | import java.util.HashMap; |
| 49 | import java.util.Map; |
| 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; |
| 56 | |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 57 | import static java.lang.String.format; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 58 | import static org.onlab.util.Tools.groupedThreads; |
| 59 | import static org.slf4j.LoggerFactory.getLogger; |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Implementation of the PiPipeconfService. |
| 64 | */ |
| 65 | @Component(immediate = true) |
| 66 | @Service |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 67 | @Beta |
| 68 | public class PiPipeconfManager implements PiPipeconfService { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 69 | |
| 70 | private final Logger log = getLogger(getClass()); |
| 71 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 72 | private static final String MERGED_DRIVER_SEPARATOR = ":"; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 73 | private static final String DRIVER = "driver"; |
| 74 | private static final String CFG_SCHEME = "piPipeconf"; |
| 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected NetworkConfigRegistry cfgService; |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected DriverService driverService; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected DriverAdminService driverAdminService; |
| 84 | |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 86 | private PiPipeconfMappingStore pipeconfMappingStore; |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 87 | |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected ClusterService clusterService; |
| 90 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 91 | // Registered pipeconf are replicated through the app subsystem and |
| 92 | // registered on app activated events. Hence, there should be no need of |
| 93 | // distributing this map. |
| 94 | protected ConcurrentMap<PiPipeconfId, PiPipeconf> pipeconfs = new ConcurrentHashMap<>(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 95 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 96 | protected ExecutorService executor = Executors.newFixedThreadPool( |
| 97 | 10, groupedThreads("onos/pipeconf-manager", "%d", log)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 98 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 99 | protected final ConfigFactory configFactory = |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 100 | new ConfigFactory<DeviceId, PiPipeconfConfig>( |
| 101 | SubjectFactories.DEVICE_SUBJECT_FACTORY, |
| 102 | PiPipeconfConfig.class, CFG_SCHEME) { |
| 103 | @Override |
| 104 | public PiPipeconfConfig createConfig() { |
| 105 | return new PiPipeconfConfig(); |
| 106 | } |
| 107 | }; |
| 108 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 109 | @Activate |
| 110 | public void activate() { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 111 | cfgService.registerConfigFactory(configFactory); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 112 | log.info("Started"); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | @Deactivate |
| 117 | public void deactivate() { |
| 118 | executor.shutdown(); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 119 | cfgService.unregisterConfigFactory(configFactory); |
| 120 | pipeconfs.clear(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 121 | cfgService = null; |
| 122 | driverAdminService = null; |
| 123 | driverService = null; |
| 124 | log.info("Stopped"); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void register(PiPipeconf pipeconf) throws IllegalStateException { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 129 | if (pipeconfs.containsKey(pipeconf.id())) { |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 130 | throw new IllegalStateException(format("Pipeconf %s is already registered", pipeconf.id())); |
| 131 | } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 132 | pipeconfs.put(pipeconf.id(), pipeconf); |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 133 | log.info("New pipeconf registered: {}", pipeconf.id()); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 137 | public void remove(PiPipeconfId pipeconfId) throws IllegalStateException { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 138 | // TODO add mechanism to remove from device. |
| 139 | if (!pipeconfs.containsKey(pipeconfId)) { |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 140 | throw new IllegalStateException(format("Pipeconf %s is not registered", pipeconfId)); |
| 141 | } |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 142 | // TODO remove the binding from the distributed Store when the lifecycle of a pipeconf is defined. |
| 143 | // pipeconfMappingStore.removeBindings(pipeconfId); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 144 | log.info("Removing pipeconf {}", pipeconfId); |
| 145 | pipeconfs.remove(pipeconfId); |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 149 | public Iterable<PiPipeconf> getPipeconfs() { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 150 | return pipeconfs.values(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Optional<PiPipeconf> getPipeconf(PiPipeconfId id) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 155 | return Optional.ofNullable(pipeconfs.get(id)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 159 | public void bindToDevice(PiPipeconfId pipeconfId, DeviceId deviceId) { |
| 160 | pipeconfMappingStore.createOrUpdateBinding(deviceId, pipeconfId); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public String mergeDriver(DeviceId deviceId, PiPipeconfId pipeconfId) { |
| 165 | return doMergeDriver(deviceId, pipeconfId); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public Optional<PiPipeconfId> ofDevice(DeviceId deviceId) { |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 170 | return Optional.ofNullable(pipeconfMappingStore.getPipeconfId(deviceId)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 173 | private String doMergeDriver(DeviceId deviceId, PiPipeconfId pipeconfId) { |
| 174 | log.debug("Starting device driver merge of {} with {}...", deviceId, pipeconfId); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 175 | final BasicDeviceConfig basicDeviceConfig = cfgService.getConfig( |
| 176 | deviceId, BasicDeviceConfig.class); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 177 | if (basicDeviceConfig == null) { |
| 178 | log.warn("Unable to get basic device config for {}, " + |
| 179 | "aborting pipeconf driver merge"); |
| 180 | return null; |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 181 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 182 | String baseDriverName = basicDeviceConfig.driver(); |
| 183 | if (baseDriverName.endsWith(mergedDriverSuffix(pipeconfId))) { |
| 184 | // The config already has driver name that is a merged one. We still |
| 185 | // need to make sure an instance of that merged driver is present in |
| 186 | // this node. |
| 187 | log.debug("Base driver of {} ({}) has been already merged with {}", |
| 188 | deviceId, baseDriverName, pipeconfId); |
| 189 | baseDriverName = getBaseDriverNameFromMerged(baseDriverName); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 190 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 191 | |
| 192 | final String newDriverName = mergedDriverName(baseDriverName, pipeconfId); |
| 193 | // If merged driver exists already we don't create a new one. |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 194 | try { |
| 195 | driverService.getDriver(newDriverName); |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 196 | return newDriverName; |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 197 | } catch (ItemNotFoundException e) { |
| 198 | log.info("Creating merged driver {}...", newDriverName); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 199 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 200 | final Driver mergedDriver = buildMergedDriver( |
| 201 | pipeconfId, baseDriverName, newDriverName); |
| 202 | if (mergedDriver == null) { |
| 203 | // Error logged by buildMergedDriver |
| 204 | return null; |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 205 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 206 | registerMergedDriver(mergedDriver); |
| 207 | return newDriverName; |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 210 | private String mergedDriverSuffix(PiPipeconfId pipeconfId) { |
| 211 | return MERGED_DRIVER_SEPARATOR + pipeconfId.id(); |
| 212 | } |
| 213 | |
| 214 | private String mergedDriverName(String baseDriverName, PiPipeconfId pipeconfId) { |
| 215 | return baseDriverName + mergedDriverSuffix(pipeconfId); |
| 216 | } |
| 217 | |
| 218 | private String getBaseDriverNameFromMerged(String mergedDriverName) { |
| 219 | final String[] pieces = mergedDriverName.split(MERGED_DRIVER_SEPARATOR); |
| 220 | if (pieces.length != 2) { |
| 221 | log.error("Unrecognized merged driver name format '{}', cannot " + |
| 222 | "extract base driver name", mergedDriverName); |
| 223 | return null; |
| 224 | } |
| 225 | return pieces[0]; |
| 226 | } |
| 227 | |
| 228 | private Driver buildMergedDriver(PiPipeconfId pipeconfId, String baseDriverName, |
| 229 | String newDriverName) { |
| 230 | final Driver baseDriver; |
| 231 | try { |
| 232 | baseDriver = driverService.getDriver(baseDriverName); |
| 233 | } catch (ItemNotFoundException e) { |
| 234 | log.error("Base driver {} not found, cannot build a merged one", |
| 235 | baseDriverName); |
| 236 | return null; |
| 237 | } |
| 238 | |
| 239 | final PiPipeconf pipeconf = pipeconfs.get(pipeconfId); |
| 240 | if (pipeconf == null) { |
| 241 | log.error("Pipeconf {} is not registered, cannot build a merged driver", |
| 242 | pipeconfId); |
| 243 | return null; |
| 244 | } |
| 245 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 246 | // extract the behaviours from the pipipeconf. |
| 247 | final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = |
| 248 | new HashMap<>(); |
| 249 | pipeconf.behaviours().forEach( |
| 250 | b -> behaviours.put(b, pipeconf.implementation(b).get())); |
| 251 | final Driver piPipeconfDriver = new DefaultDriver( |
| 252 | newDriverName, baseDriver.parents(), |
| 253 | baseDriver.manufacturer(), baseDriver.hwVersion(), |
| 254 | baseDriver.swVersion(), behaviours, new HashMap<>()); |
| 255 | // take the base driver created with the behaviours of the PiPeconf and |
| 256 | // merge it with the base driver that was assigned to the device |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 257 | return piPipeconfDriver.merge(baseDriver); |
| 258 | } |
| 259 | |
| 260 | private void registerMergedDriver(Driver driver) { |
| 261 | final DriverProvider provider = new InternalDriverProvider(driver); |
| 262 | if (driverAdminService.getProviders().contains(provider)) { |
| 263 | // A provider for this driver already exist. |
| 264 | return; |
| 265 | } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame] | 266 | driverAdminService.registerProvider(provider); |
| 267 | } |
| 268 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 269 | /** |
| 270 | * Internal driver provider used to register merged pipeconf drivers in the |
| 271 | * core. |
| 272 | */ |
| 273 | private class InternalDriverProvider implements DriverProvider { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 274 | |
| 275 | Driver driver; |
| 276 | |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 277 | InternalDriverProvider(Driver driver) { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 278 | this.driver = driver; |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public Set<Driver> getDrivers() { |
| 283 | return ImmutableSet.of(driver); |
| 284 | } |
Carmelo Cascone | 158b8c4 | 2018-07-04 19:42:37 +0200 | [diff] [blame^] | 285 | |
| 286 | @Override |
| 287 | public boolean equals(Object o) { |
| 288 | if (this == o) { |
| 289 | return true; |
| 290 | } |
| 291 | if (o == null || getClass() != o.getClass()) { |
| 292 | return false; |
| 293 | } |
| 294 | InternalDriverProvider that = (InternalDriverProvider) o; |
| 295 | return Objects.equal(driver.name(), that.driver.name()); |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public int hashCode() { |
| 300 | return Objects.hashCode(driver.name()); |
| 301 | } |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 302 | } |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 303 | } |