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 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 22 | import com.google.common.annotations.Beta; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableSet; |
| 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.apache.felix.scr.annotations.Service; |
| 30 | import org.onlab.util.ItemNotFoundException; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 31 | import org.onosproject.cluster.ClusterService; |
| 32 | import org.onosproject.cluster.LeadershipService; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.config.ConfigFactory; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 35 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 36 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 37 | import org.onosproject.net.config.basics.SubjectFactories; |
| 38 | import org.onosproject.net.driver.Behaviour; |
| 39 | import org.onosproject.net.driver.DefaultDriver; |
| 40 | import org.onosproject.net.driver.Driver; |
| 41 | import org.onosproject.net.driver.DriverAdminService; |
| 42 | import org.onosproject.net.driver.DriverProvider; |
| 43 | import org.onosproject.net.driver.DriverService; |
| 44 | import org.onosproject.net.pi.model.PiPipeconf; |
| 45 | import org.onosproject.net.pi.model.PiPipeconfId; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 46 | import org.onosproject.net.pi.service.PiPipeconfConfig; |
| 47 | import org.onosproject.net.pi.service.PiPipeconfMappingStore; |
| 48 | import org.onosproject.net.pi.service.PiPipeconfService; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 49 | import org.slf4j.Logger; |
| 50 | |
| 51 | import java.util.HashMap; |
| 52 | import java.util.Map; |
| 53 | import java.util.Optional; |
| 54 | import java.util.Set; |
| 55 | import java.util.concurrent.CompletableFuture; |
| 56 | import java.util.concurrent.ConcurrentHashMap; |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 57 | import java.util.concurrent.ConcurrentMap; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 58 | import java.util.concurrent.ExecutorService; |
| 59 | import java.util.concurrent.Executors; |
| 60 | |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 61 | import static java.lang.String.format; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 62 | import static org.onlab.util.Tools.groupedThreads; |
| 63 | import static org.slf4j.LoggerFactory.getLogger; |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * Implementation of the PiPipeconfService. |
| 68 | */ |
| 69 | @Component(immediate = true) |
| 70 | @Service |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 71 | @Beta |
| 72 | public class PiPipeconfManager implements PiPipeconfService { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 73 | |
| 74 | private final Logger log = getLogger(getClass()); |
| 75 | |
| 76 | private static final String DRIVER = "driver"; |
| 77 | private static final String CFG_SCHEME = "piPipeconf"; |
| 78 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 79 | private static final String DRIVER_MERGE_TOPIC = |
| 80 | PiPipeconfManager.class.getSimpleName() + "-driver-merge-"; |
| 81 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected NetworkConfigRegistry cfgService; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 86 | private LeadershipService leadershipService; |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 89 | protected DriverService driverService; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected DriverAdminService driverAdminService; |
| 93 | |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 95 | private PiPipeconfMappingStore pipeconfMappingStore; |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 96 | |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 98 | protected ClusterService clusterService; |
| 99 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 100 | // Registered pipeconf are replicated through the app subsystem and |
| 101 | // registered on app activated events. Hence, there should be no need of |
| 102 | // distributing this map. |
| 103 | protected ConcurrentMap<PiPipeconfId, PiPipeconf> pipeconfs = new ConcurrentHashMap<>(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 104 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 105 | protected ExecutorService executor = Executors.newFixedThreadPool( |
| 106 | 10, groupedThreads("onos/pipeconf-manager", "%d", log)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 107 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 108 | protected final ConfigFactory configFactory = |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 109 | new ConfigFactory<DeviceId, PiPipeconfConfig>( |
| 110 | SubjectFactories.DEVICE_SUBJECT_FACTORY, |
| 111 | PiPipeconfConfig.class, CFG_SCHEME) { |
| 112 | @Override |
| 113 | public PiPipeconfConfig createConfig() { |
| 114 | return new PiPipeconfConfig(); |
| 115 | } |
| 116 | }; |
| 117 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 118 | @Activate |
| 119 | public void activate() { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 120 | cfgService.registerConfigFactory(configFactory); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 121 | log.info("Started"); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | @Deactivate |
| 126 | public void deactivate() { |
| 127 | executor.shutdown(); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 128 | cfgService.unregisterConfigFactory(configFactory); |
| 129 | pipeconfs.clear(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 130 | cfgService = null; |
| 131 | driverAdminService = null; |
| 132 | driverService = null; |
| 133 | log.info("Stopped"); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void register(PiPipeconf pipeconf) throws IllegalStateException { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 138 | if (pipeconfs.containsKey(pipeconf.id())) { |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 139 | throw new IllegalStateException(format("Pipeconf %s is already registered", pipeconf.id())); |
| 140 | } |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 141 | pipeconfs.put(pipeconf.id(), pipeconf); |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 142 | log.info("New pipeconf registered: {}", pipeconf.id()); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 146 | public void remove(PiPipeconfId pipeconfId) throws IllegalStateException { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 147 | // TODO add mechanism to remove from device. |
| 148 | if (!pipeconfs.containsKey(pipeconfId)) { |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 149 | throw new IllegalStateException(format("Pipeconf %s is not registered", pipeconfId)); |
| 150 | } |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 151 | // TODO remove the binding from the distributed Store when the lifecycle of a pipeconf is defined. |
| 152 | // pipeconfMappingStore.removeBindings(pipeconfId); |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 153 | log.info("Removing pipeconf {}", pipeconfId); |
| 154 | pipeconfs.remove(pipeconfId); |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 158 | public Iterable<PiPipeconf> getPipeconfs() { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 159 | return pipeconfs.values(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public Optional<PiPipeconf> getPipeconf(PiPipeconfId id) { |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 164 | return Optional.ofNullable(pipeconfs.get(id)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 168 | public CompletableFuture<Boolean> bindToDevice( |
| 169 | PiPipeconfId pipeconfId, DeviceId deviceId) { |
| 170 | return CompletableFuture.supplyAsync(() -> doMergeDriver( |
| 171 | deviceId, pipeconfId), executor); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public Optional<PiPipeconfId> ofDevice(DeviceId deviceId) { |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 176 | return Optional.ofNullable(pipeconfMappingStore.getPipeconfId(deviceId)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Carmelo Cascone | 96beb6f | 2018-06-27 18:07:12 +0200 | [diff] [blame^] | 179 | private boolean doMergeDriver(DeviceId deviceId, PiPipeconfId pipeconfId) { |
| 180 | // Perform the following operations: |
| 181 | // 1) ALL nodes: create and register new merged driver (pipeconf + base driver) |
| 182 | // 2) ONE node (leader): updated netcfg with new driver |
| 183 | log.warn("Starting device driver merge of {} with {}...", deviceId, pipeconfId); |
| 184 | final BasicDeviceConfig basicDeviceConfig = cfgService.getConfig( |
| 185 | deviceId, BasicDeviceConfig.class); |
| 186 | final Driver baseDriver = driverService.getDriver( |
| 187 | basicDeviceConfig.driver()); |
| 188 | final String newDriverName = baseDriver.name() + ":" + pipeconfId; |
| 189 | if (baseDriver.name().equals(newDriverName)) { |
| 190 | log.warn("Requested to merge {} driver with {} for {}, " |
| 191 | + "but current device driver is already merged", |
| 192 | baseDriver.name(), pipeconfId, deviceId); |
| 193 | return true; |
| 194 | } |
| 195 | final PiPipeconf pipeconf = pipeconfs.get(pipeconfId); |
| 196 | if (pipeconf == null) { |
| 197 | log.error("Pipeconf {} is not registered", pipeconfId); |
| 198 | return false; |
| 199 | } |
| 200 | // 1) if merged driver exists already we don't create a new one. |
| 201 | try { |
| 202 | driverService.getDriver(newDriverName); |
| 203 | log.info("Found existing merged driver {}, re-using that", newDriverName); |
| 204 | } catch (ItemNotFoundException e) { |
| 205 | log.info("Creating merged driver {}...", newDriverName); |
| 206 | createMergedDriver(pipeconf, baseDriver, newDriverName); |
| 207 | } |
| 208 | // 2) Updating device cfg to enforce the merged driver (one node only) |
| 209 | final boolean isLeader = leadershipService |
| 210 | .runForLeadership(DRIVER_MERGE_TOPIC + deviceId.toString()) |
| 211 | .leaderNodeId() |
| 212 | .equals(clusterService.getLocalNode().id()); |
| 213 | if (isLeader) { |
| 214 | // FIXME: this binding should be updated by the same entity |
| 215 | // deploying the pipeconf. |
| 216 | pipeconfMappingStore.createOrUpdateBinding(deviceId, pipeconfId); |
| 217 | if (!basicDeviceConfig.driver().equals(newDriverName)) { |
| 218 | log.info("Applying new driver {} for device {} via cfg...", |
| 219 | newDriverName, deviceId); |
| 220 | setDriverViaCfg(deviceId, newDriverName, basicDeviceConfig); |
| 221 | } |
| 222 | } |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | private void createMergedDriver(PiPipeconf pipeconf, Driver baseDriver, |
| 227 | String newDriverName) { |
| 228 | // extract the behaviours from the pipipeconf. |
| 229 | final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = |
| 230 | new HashMap<>(); |
| 231 | pipeconf.behaviours().forEach( |
| 232 | b -> behaviours.put(b, pipeconf.implementation(b).get())); |
| 233 | final Driver piPipeconfDriver = new DefaultDriver( |
| 234 | newDriverName, baseDriver.parents(), |
| 235 | baseDriver.manufacturer(), baseDriver.hwVersion(), |
| 236 | baseDriver.swVersion(), behaviours, new HashMap<>()); |
| 237 | // take the base driver created with the behaviours of the PiPeconf and |
| 238 | // merge it with the base driver that was assigned to the device |
| 239 | final Driver completeDriver = piPipeconfDriver.merge(baseDriver); |
| 240 | // This might lead to explosion of number of providers in the core, |
| 241 | // due to 1:1:1 pipeconf:driver:provider maybe find better way |
| 242 | final DriverProvider provider = new PiPipeconfDriverProviderInternal( |
| 243 | completeDriver); |
| 244 | // register the merged driver |
| 245 | driverAdminService.registerProvider(provider); |
| 246 | } |
| 247 | |
| 248 | private void setDriverViaCfg(DeviceId deviceId, String driverName, |
| 249 | BasicDeviceConfig basicDeviceConfig) { |
| 250 | ObjectNode newCfg = (ObjectNode) basicDeviceConfig.node(); |
| 251 | newCfg = newCfg.put(DRIVER, driverName); |
| 252 | ObjectMapper mapper = new ObjectMapper(); |
| 253 | JsonNode newCfgNode = mapper.convertValue(newCfg, JsonNode.class); |
| 254 | cfgService.applyConfig(deviceId, BasicDeviceConfig.class, newCfgNode); |
| 255 | } |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 256 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 257 | private class PiPipeconfDriverProviderInternal implements DriverProvider { |
| 258 | |
| 259 | Driver driver; |
| 260 | |
| 261 | PiPipeconfDriverProviderInternal(Driver driver) { |
| 262 | this.driver = driver; |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public Set<Driver> getDrivers() { |
| 267 | return ImmutableSet.of(driver); |
| 268 | } |
| 269 | } |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 270 | } |