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