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