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; |
| 33 | import org.onosproject.cluster.NodeId; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.config.ConfigFactory; |
| 36 | import org.onosproject.net.config.NetworkConfigEvent; |
| 37 | import org.onosproject.net.config.NetworkConfigListener; |
| 38 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 39 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 40 | import org.onosproject.net.config.basics.SubjectFactories; |
| 41 | import org.onosproject.net.driver.Behaviour; |
| 42 | import org.onosproject.net.driver.DefaultDriver; |
| 43 | import org.onosproject.net.driver.Driver; |
| 44 | import org.onosproject.net.driver.DriverAdminService; |
| 45 | import org.onosproject.net.driver.DriverProvider; |
| 46 | import org.onosproject.net.driver.DriverService; |
| 47 | import org.onosproject.net.pi.model.PiPipeconf; |
| 48 | import org.onosproject.net.pi.model.PiPipeconfId; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 49 | import org.onosproject.net.pi.service.PiPipeconfConfig; |
| 50 | import org.onosproject.net.pi.service.PiPipeconfMappingStore; |
| 51 | import org.onosproject.net.pi.service.PiPipeconfService; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | |
| 54 | import java.util.HashMap; |
| 55 | import java.util.Map; |
| 56 | import java.util.Optional; |
| 57 | import java.util.Set; |
| 58 | import java.util.concurrent.CompletableFuture; |
| 59 | import java.util.concurrent.ConcurrentHashMap; |
| 60 | import java.util.concurrent.ExecutorService; |
| 61 | import java.util.concurrent.Executors; |
| 62 | |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 63 | import static java.lang.String.format; |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 64 | import static org.onlab.util.Tools.groupedThreads; |
| 65 | import static org.slf4j.LoggerFactory.getLogger; |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * Implementation of the PiPipeconfService. |
| 70 | */ |
| 71 | @Component(immediate = true) |
| 72 | @Service |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 73 | @Beta |
| 74 | public class PiPipeconfManager implements PiPipeconfService { |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 75 | |
| 76 | private final Logger log = getLogger(getClass()); |
| 77 | |
| 78 | private static final String DRIVER = "driver"; |
| 79 | private static final String CFG_SCHEME = "piPipeconf"; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected NetworkConfigRegistry cfgService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 85 | protected LeadershipService leadershipService; |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 88 | protected DriverService driverService; |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 91 | protected DriverAdminService driverAdminService; |
| 92 | |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected PiPipeconfMappingStore pipeconfMappingStore; |
| 95 | |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected ClusterService clusterService; |
| 98 | |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 99 | // Registered pipeconf are replicated through the app subsystem and registered on app activated events. |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 100 | protected ConcurrentHashMap<PiPipeconfId, PiPipeconf> piPipeconfs = new ConcurrentHashMap<>(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 101 | |
| 102 | protected ExecutorService executor = |
| 103 | Executors.newFixedThreadPool(5, groupedThreads("onos/pipipeconfservice", |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 104 | "pipeline-to-device-%d", log)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 105 | |
| 106 | protected final ConfigFactory factory = |
| 107 | new ConfigFactory<DeviceId, PiPipeconfConfig>( |
| 108 | SubjectFactories.DEVICE_SUBJECT_FACTORY, |
| 109 | PiPipeconfConfig.class, CFG_SCHEME) { |
| 110 | @Override |
| 111 | public PiPipeconfConfig createConfig() { |
| 112 | return new PiPipeconfConfig(); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | protected final NetworkConfigListener cfgListener = new InternalNetworkConfigListener(); |
| 117 | |
| 118 | @Activate |
| 119 | public void activate() { |
| 120 | cfgService.registerConfigFactory(factory); |
| 121 | cfgService.addListener(cfgListener); |
| 122 | cfgService.getSubjects(DeviceId.class, PiPipeconfConfig.class) |
| 123 | .forEach(this::addPipeconfFromCfg); |
| 124 | log.info("Started"); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | @Deactivate |
| 129 | public void deactivate() { |
| 130 | executor.shutdown(); |
| 131 | cfgService.removeListener(cfgListener); |
| 132 | cfgService.unregisterConfigFactory(factory); |
| 133 | piPipeconfs.clear(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 134 | cfgService = null; |
| 135 | driverAdminService = null; |
| 136 | driverService = null; |
| 137 | log.info("Stopped"); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void register(PiPipeconf pipeconf) throws IllegalStateException { |
| 142 | log.warn("Currently using local maps, needs to be moved to a distributed store"); |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 143 | if (piPipeconfs.containsKey(pipeconf.id())) { |
| 144 | throw new IllegalStateException(format("Pipeconf %s is already registered", pipeconf.id())); |
| 145 | } |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 146 | piPipeconfs.put(pipeconf.id(), pipeconf); |
Carmelo Cascone | 44daf56 | 2017-07-16 23:55:08 -0400 | [diff] [blame] | 147 | log.info("New pipeconf registered: {}", pipeconf.id()); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 151 | public void remove(PiPipeconfId pipeconfId) throws IllegalStateException { |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 152 | //TODO add mechanism to remove from device. |
| 153 | if (!piPipeconfs.containsKey(pipeconfId)) { |
| 154 | throw new IllegalStateException(format("Pipeconf %s is not registered", pipeconfId)); |
| 155 | } |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 156 | // TODO remove the binding from the distributed Store when the lifecycle of a pipeconf is defined. |
| 157 | // pipeconfMappingStore.removeBindings(pipeconfId); |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 158 | piPipeconfs.remove(pipeconfId); |
| 159 | } |
| 160 | |
| 161 | @Override |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 162 | public Iterable<PiPipeconf> getPipeconfs() { |
Andrea Campanella | 48f99fa | 2017-07-13 19:06:21 +0200 | [diff] [blame] | 163 | return piPipeconfs.values(); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Optional<PiPipeconf> getPipeconf(PiPipeconfId id) { |
| 168 | return Optional.ofNullable(piPipeconfs.get(id)); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public CompletableFuture<Boolean> bindToDevice(PiPipeconfId pipeconfId, DeviceId deviceId) { |
| 173 | CompletableFuture<Boolean> operationResult = new CompletableFuture<>(); |
| 174 | |
| 175 | executor.execute(() -> { |
| 176 | BasicDeviceConfig basicDeviceConfig = |
| 177 | cfgService.getConfig(deviceId, BasicDeviceConfig.class); |
| 178 | Driver baseDriver = driverService.getDriver(basicDeviceConfig.driver()); |
| 179 | |
| 180 | String completeDriverName = baseDriver.name() + ":" + pipeconfId; |
| 181 | PiPipeconf piPipeconf = piPipeconfs.get(pipeconfId); |
| 182 | if (piPipeconf == null) { |
| 183 | log.warn("Pipeconf {} is not present", pipeconfId); |
| 184 | operationResult.complete(false); |
| 185 | } else { |
| 186 | //if driver exists already we don't create a new one. |
| 187 | //needs to be done via exception catching due to DriverRegistry throwing it on a null return from |
| 188 | //the driver map. |
| 189 | try { |
| 190 | driverService.getDriver(completeDriverName); |
| 191 | } catch (ItemNotFoundException e) { |
| 192 | |
| 193 | log.debug("First time pipeconf {} is used with base driver {}, merging the two", |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 194 | pipeconfId, baseDriver); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 195 | //extract the behaviours from the pipipeconf. |
| 196 | Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = new HashMap<>(); |
| 197 | piPipeconf.behaviours().forEach(b -> { |
| 198 | behaviours.put(b, piPipeconf.implementation(b).get()); |
| 199 | }); |
| 200 | |
| 201 | Driver piPipeconfDriver = new DefaultDriver(completeDriverName, baseDriver.parents(), |
Andrea Campanella | a9b3c9b | 2017-07-21 14:03:15 +0200 | [diff] [blame] | 202 | baseDriver.manufacturer(), baseDriver.hwVersion(), |
| 203 | baseDriver.swVersion(), behaviours, new HashMap<>()); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 204 | //we take the base driver created with the behaviours of the PiPeconf and |
| 205 | // merge it with the base driver that was assigned to the device |
| 206 | Driver completeDriver = piPipeconfDriver.merge(baseDriver); |
| 207 | |
| 208 | //This might lead to explosion of number of providers in the core, |
| 209 | // due to 1:1:1 pipeconf:driver:provider maybe find better way |
| 210 | DriverProvider provider = new PiPipeconfDriverProviderInternal(completeDriver); |
| 211 | |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 212 | //we register to the driver susbystem the driver provider containing the merged driver |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 213 | driverAdminService.registerProvider(provider); |
| 214 | } |
| 215 | |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 216 | // Changing the configuration for the device to enforce the full driver with pipipeconf |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 217 | // and base behaviours, updating binding only first time something changes |
| 218 | NodeId leaderNodeId = leadershipService.getLeader("deploy-" + |
| 219 | deviceId.toString() + "-pipeconf"); |
| 220 | NodeId localNodeId = clusterService.getLocalNode().id(); |
| 221 | |
| 222 | if (!basicDeviceConfig.driver().equals(completeDriverName) && localNodeId.equals(leaderNodeId)) { |
| 223 | ObjectNode newCfg = (ObjectNode) basicDeviceConfig.node(); |
| 224 | newCfg = newCfg.put(DRIVER, completeDriverName); |
| 225 | ObjectMapper mapper = new ObjectMapper(); |
| 226 | JsonNode newCfgNode = mapper.convertValue(newCfg, JsonNode.class); |
| 227 | log.debug("New driver {} for device {}", completeDriverName, deviceId); |
| 228 | cfgService.applyConfig(deviceId, BasicDeviceConfig.class, newCfgNode); |
| 229 | // Completable future is needed for when this method will also apply the pipeline to the device. |
| 230 | // FIXME (maybe): the pipeline is currently applied by the general device provider. |
| 231 | // But we store here the association between device and pipeconf. |
| 232 | pipeconfMappingStore.createOrUpdateBinding(deviceId, pipeconfId); |
| 233 | } |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 234 | operationResult.complete(true); |
| 235 | } |
| 236 | }); |
| 237 | return operationResult; |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public Optional<PiPipeconfId> ofDevice(DeviceId deviceId) { |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 242 | return Optional.ofNullable(pipeconfMappingStore.getPipeconfId(deviceId)); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 245 | |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 246 | private class PiPipeconfDriverProviderInternal implements DriverProvider { |
| 247 | |
| 248 | Driver driver; |
| 249 | |
| 250 | PiPipeconfDriverProviderInternal(Driver driver) { |
| 251 | this.driver = driver; |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public Set<Driver> getDrivers() { |
| 256 | return ImmutableSet.of(driver); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | private void addPipeconfFromCfg(DeviceId deviceId) { |
| 261 | PiPipeconfConfig pipeconfConfig = |
| 262 | cfgService.getConfig(deviceId, PiPipeconfConfig.class); |
| 263 | PiPipeconfId id = pipeconfConfig.piPipeconfId(); |
| 264 | if (id.id().equals("")) { |
| 265 | log.warn("Not adding empty pipeconfId for device {}", deviceId); |
| 266 | } else { |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 267 | pipeconfMappingStore.createOrUpdateBinding(deviceId, id); |
Andrea Campanella | bc112a9 | 2017-06-26 19:06:43 +0200 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Listener for configuration events. |
| 273 | */ |
| 274 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 275 | |
| 276 | |
| 277 | @Override |
| 278 | public void event(NetworkConfigEvent event) { |
| 279 | DeviceId deviceId = (DeviceId) event.subject(); |
| 280 | addPipeconfFromCfg(deviceId); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public boolean isRelevant(NetworkConfigEvent event) { |
| 285 | return event.configClass().equals(PiPipeconfConfig.class) && |
| 286 | (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 287 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED); |
| 288 | } |
| 289 | } |
| 290 | } |