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