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