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 | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 28 | import org.onosproject.net.device.DeviceService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 29 | import org.onosproject.net.driver.Behaviour; |
| 30 | import org.onosproject.net.driver.DefaultDriverData; |
| 31 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 32 | import org.onosproject.net.driver.Driver; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 33 | import org.onosproject.net.driver.DriverHandler; |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 34 | import org.onosproject.net.driver.DriverRegistry; |
| 35 | import org.onosproject.net.driver.DriverService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 39 | import java.util.Set; |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 40 | import java.util.stream.Collectors; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 41 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 42 | import static org.onlab.util.Tools.nullIsNotFound; |
| 43 | import static org.onosproject.net.AnnotationKeys.DRIVER; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 44 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 45 | import static org.onosproject.security.AppPermission.Type.DRIVER_READ; |
| 46 | import static org.onosproject.security.AppPermission.Type.DRIVER_WRITE; |
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 | /** |
| 49 | * Manages inventory of device drivers. |
| 50 | */ |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 51 | @Service |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 52 | // Not enabled by default to allow the DriverRegistryManager to enable it only |
| 53 | // when all the required drivers are available. |
| 54 | @Component(immediate = true, enabled = false) |
| 55 | public class DriverManager implements DriverService { |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 56 | |
| 57 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 58 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 59 | private static final String NO_DRIVER = "Driver not found"; |
| 60 | private static final String NO_DEVICE = "Device not found"; |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected DriverRegistry registry; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected DeviceService deviceService; |
| 67 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 68 | @Activate |
| 69 | protected void activate() { |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 70 | AbstractProjectableModel.setDriverService(null, this); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 71 | log.info("Started"); |
| 72 | } |
| 73 | |
| 74 | @Deactivate |
| 75 | protected void deactivate() { |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 76 | AbstractProjectableModel.setDriverService(this, null); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 77 | log.info("Stopped"); |
| 78 | } |
| 79 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 80 | @Override |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 81 | public Set<Driver> getDrivers() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 82 | checkPermission(DRIVER_READ); |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 83 | return registry.getDrivers(); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 87 | public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 88 | checkPermission(DRIVER_READ); |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 89 | return registry.getDrivers().stream() |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 90 | .filter(d -> d.hasBehaviour(withBehaviour)) |
| 91 | .collect(Collectors.toSet()); |
| 92 | } |
| 93 | |
| 94 | @Override |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 95 | public Driver getDriver(String driverName) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 96 | checkPermission(DRIVER_READ); |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 97 | return registry.getDriver(driverName); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Driver getDriver(String mfr, String hw, String sw) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 102 | checkPermission(DRIVER_READ); |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 103 | return registry.getDriver(mfr, hw, sw); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 107 | public Driver getDriver(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 108 | checkPermission(DRIVER_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 109 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 110 | Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE); |
| 111 | String driverName = device.annotations().value(DRIVER); |
| 112 | if (driverName != null) { |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 113 | try { |
| 114 | return getDriver(driverName); |
| 115 | } catch (ItemNotFoundException e) { |
| 116 | log.warn("Specified driver {} not found, falling back.", driverName); |
| 117 | } |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 118 | } |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 119 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 120 | return nullIsNotFound(getDriver(device.manufacturer(), |
| 121 | device.hwVersion(), device.swVersion()), |
| 122 | NO_DRIVER); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public DriverHandler createHandler(DeviceId deviceId, String... credentials) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 127 | checkPermission(DRIVER_WRITE); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 128 | Driver driver = getDriver(deviceId); |
Thomas Vachuska | 80b0a80 | 2015-07-17 08:43:30 -0700 | [diff] [blame] | 129 | return new DefaultDriverHandler(new DefaultDriverData(driver, deviceId)); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 130 | } |
| 131 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 132 | } |