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