alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [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 | package org.onosproject.net.driver.impl; |
| 17 | |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 18 | import org.onlab.util.ItemNotFoundException; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 19 | import org.onosproject.net.AbstractProjectableModel; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 20 | import org.onosproject.net.Device; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
Thomas Vachuska | 164ecf6 | 2018-05-08 17:29:55 -0700 | [diff] [blame] | 22 | import org.onosproject.net.config.NetworkConfigService; |
| 23 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 24 | import org.onosproject.net.device.DeviceService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 25 | import org.onosproject.net.driver.Behaviour; |
| 26 | import org.onosproject.net.driver.DefaultDriverData; |
| 27 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 28 | import org.onosproject.net.driver.Driver; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 29 | import org.onosproject.net.driver.DriverHandler; |
Thomas Vachuska | e6a5741 | 2017-08-23 14:09:14 -0700 | [diff] [blame] | 30 | import org.onosproject.net.driver.DriverListener; |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 31 | import org.onosproject.net.driver.DriverRegistry; |
| 32 | import org.onosproject.net.driver.DriverService; |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 33 | import org.onosproject.net.pi.model.PiPipeconfId; |
| 34 | import org.onosproject.net.pi.service.PiPipeconfService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 35 | import org.osgi.service.component.annotations.Activate; |
| 36 | import org.osgi.service.component.annotations.Component; |
| 37 | import org.osgi.service.component.annotations.Deactivate; |
| 38 | import org.osgi.service.component.annotations.Reference; |
| 39 | import org.osgi.service.component.annotations.ReferenceCardinality; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
Seyeon Jeong | 1e24910 | 2020-03-10 17:41:14 -0700 | [diff] [blame^] | 43 | import java.util.HashMap; |
| 44 | import java.util.Map; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 45 | import java.util.Set; |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 46 | import java.util.stream.Collectors; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 47 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 48 | import static org.onlab.util.Tools.nullIsNotFound; |
| 49 | import static org.onosproject.net.AnnotationKeys.DRIVER; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 50 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 51 | import static org.onosproject.security.AppPermission.Type.DRIVER_READ; |
| 52 | import static org.onosproject.security.AppPermission.Type.DRIVER_WRITE; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 53 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Manages inventory of device drivers. |
| 56 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 57 | |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 58 | // Not enabled by default to allow the DriverRegistryManager to enable it only |
| 59 | // when all the required drivers are available. |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 60 | @Component(immediate = true, enabled = false, service = DriverService.class) |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 61 | public class DriverManager implements DriverService { |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 62 | |
| 63 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 64 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 65 | private static final String NO_DRIVER = "Driver not found"; |
| 66 | private static final String NO_DEVICE = "Device not found"; |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 67 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 69 | protected DriverRegistry registry; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 70 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 72 | protected DeviceService deviceService; |
| 73 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 164ecf6 | 2018-05-08 17:29:55 -0700 | [diff] [blame] | 75 | protected NetworkConfigService networkConfigService; |
| 76 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 78 | protected PiPipeconfService pipeconfService; |
| 79 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 80 | @Activate |
| 81 | protected void activate() { |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 82 | AbstractProjectableModel.setDriverService(null, this); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 83 | log.info("Started"); |
| 84 | } |
| 85 | |
| 86 | @Deactivate |
| 87 | protected void deactivate() { |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 88 | AbstractProjectableModel.setDriverService(this, null); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 89 | log.info("Stopped"); |
| 90 | } |
| 91 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 92 | @Override |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 93 | public Set<Driver> getDrivers() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 94 | checkPermission(DRIVER_READ); |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 95 | return registry.getDrivers(); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 99 | public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 100 | checkPermission(DRIVER_READ); |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 101 | return registry.getDrivers().stream() |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 102 | .filter(d -> d.hasBehaviour(withBehaviour)) |
| 103 | .collect(Collectors.toSet()); |
| 104 | } |
| 105 | |
| 106 | @Override |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 107 | public Driver getDriver(String driverName) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 108 | checkPermission(DRIVER_READ); |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 109 | return registry.getDriver(driverName); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public Driver getDriver(String mfr, String hw, String sw) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 114 | checkPermission(DRIVER_READ); |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 115 | return registry.getDriver(mfr, hw, sw); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 119 | public Driver getDriver(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 120 | checkPermission(DRIVER_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 121 | |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 122 | Driver driver; |
| 123 | |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 124 | // Special processing for devices with pipeconf. |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 125 | if (pipeconfService.ofDevice(deviceId).isPresent()) { |
Carmelo Cascone | f23340c | 2019-04-28 12:33:14 -0700 | [diff] [blame] | 126 | // No fallback for pipeconf merged drivers. |
| 127 | // Throws exception if pipeconf driver does not exist. |
| 128 | return nullIsNotFound( |
| 129 | getPipeconfMergedDriver(deviceId), |
| 130 | "Device is pipeconf-capable but a " + |
| 131 | "pipeconf-merged driver was not found"); |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 132 | } |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 133 | |
| 134 | // Primary source of driver configuration is the network config. |
Thomas Vachuska | 164ecf6 | 2018-05-08 17:29:55 -0700 | [diff] [blame] | 135 | BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class); |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 136 | driver = lookupDriver(cfg != null ? cfg.driver() : null); |
Thomas Vachuska | 164ecf6 | 2018-05-08 17:29:55 -0700 | [diff] [blame] | 137 | if (driver != null) { |
| 138 | return driver; |
| 139 | } |
| 140 | |
| 141 | // Secondary source of the driver selection is driver annotation. |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 142 | Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE); |
Thomas Vachuska | 164ecf6 | 2018-05-08 17:29:55 -0700 | [diff] [blame] | 143 | driver = lookupDriver(device.annotations().value(DRIVER)); |
| 144 | if (driver != null) { |
| 145 | return driver; |
| 146 | } |
| 147 | |
| 148 | // Tertiary source of the driver selection is the primordial information |
| 149 | // obtained from the device. |
| 150 | return nullIsNotFound(getDriver(device.manufacturer(), |
| 151 | device.hwVersion(), device.swVersion()), |
| 152 | NO_DRIVER); |
| 153 | } |
| 154 | |
Seyeon Jeong | 1e24910 | 2020-03-10 17:41:14 -0700 | [diff] [blame^] | 155 | @Override |
| 156 | public Map<DeviceId, String> getDeviceDrivers() { |
| 157 | Map<DeviceId, String> deviceDriverNameMap = new HashMap<>(); |
| 158 | deviceService.getDevices().forEach(device -> { |
| 159 | deviceDriverNameMap.put(device.id(), getDriver(device.id()).name()); |
| 160 | }); |
| 161 | return deviceDriverNameMap; |
| 162 | } |
| 163 | |
Carmelo Cascone | da0b559 | 2018-09-14 12:54:15 -0700 | [diff] [blame] | 164 | private Driver getPipeconfMergedDriver(DeviceId deviceId) { |
| 165 | PiPipeconfId pipeconfId = pipeconfService.ofDevice(deviceId).orElse(null); |
| 166 | if (pipeconfId == null) { |
| 167 | log.warn("Missing pipeconf for {}, cannot produce a pipeconf merged driver", |
| 168 | deviceId); |
| 169 | return null; |
| 170 | } |
| 171 | String mergedDriverName = pipeconfService.getMergedDriver(deviceId, pipeconfId); |
| 172 | if (mergedDriverName == null) { |
| 173 | log.warn("Unable to get pipeconf merged driver for {} and {}", |
| 174 | deviceId, pipeconfId); |
| 175 | return null; |
| 176 | } |
| 177 | try { |
| 178 | return getDriver(mergedDriverName); |
| 179 | } catch (ItemNotFoundException e) { |
| 180 | log.warn("Specified pipeconf merged driver {} for {} not found", |
| 181 | mergedDriverName, deviceId); |
| 182 | return null; |
| 183 | } |
| 184 | } |
| 185 | |
Thomas Vachuska | 164ecf6 | 2018-05-08 17:29:55 -0700 | [diff] [blame] | 186 | private Driver lookupDriver(String driverName) { |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 187 | if (driverName != null) { |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 188 | try { |
| 189 | return getDriver(driverName); |
| 190 | } catch (ItemNotFoundException e) { |
| 191 | log.warn("Specified driver {} not found, falling back.", driverName); |
| 192 | } |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 193 | } |
Thomas Vachuska | 164ecf6 | 2018-05-08 17:29:55 -0700 | [diff] [blame] | 194 | return null; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public DriverHandler createHandler(DeviceId deviceId, String... credentials) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 199 | checkPermission(DRIVER_WRITE); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 200 | Driver driver = getDriver(deviceId); |
Thomas Vachuska | 80b0a80 | 2015-07-17 08:43:30 -0700 | [diff] [blame] | 201 | return new DefaultDriverHandler(new DefaultDriverData(driver, deviceId)); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Thomas Vachuska | e6a5741 | 2017-08-23 14:09:14 -0700 | [diff] [blame] | 204 | @Override |
| 205 | public void addListener(DriverListener listener) { |
| 206 | registry.addListener(listener); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void removeListener(DriverListener listener) { |
| 211 | registry.removeListener(listener); |
| 212 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 213 | } |