alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Service; |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 27 | import org.onlab.util.ItemNotFoundException; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 28 | import org.onosproject.net.AbstractProjectableModel; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 29 | import org.onosproject.net.Device; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 31 | import org.onosproject.net.device.DeviceService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 32 | import org.onosproject.net.driver.Behaviour; |
| 33 | import org.onosproject.net.driver.DefaultDriverData; |
| 34 | import org.onosproject.net.driver.DefaultDriverHandler; |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 35 | import org.onosproject.net.driver.DefaultDriverProvider; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 36 | import org.onosproject.net.driver.Driver; |
| 37 | import org.onosproject.net.driver.DriverAdminService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 38 | import org.onosproject.net.driver.DriverHandler; |
| 39 | import org.onosproject.net.driver.DriverProvider; |
| 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
| 43 | import java.util.Map; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 44 | import java.util.Optional; |
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; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 51 | import static org.onosproject.security.AppPermission.Type.*; |
| 52 | |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 53 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 54 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Manages inventory of device drivers. |
| 57 | */ |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 58 | @Component(immediate = true) |
| 59 | @Service |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 60 | public class DriverManager extends DefaultDriverProvider implements DriverAdminService { |
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"; |
| 66 | private static final String DEFAULT = "default"; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected DeviceService deviceService; |
| 70 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 71 | private Set<DriverProvider> providers = Sets.newConcurrentHashSet(); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 72 | private Map<String, Driver> driverByKey = Maps.newConcurrentMap(); |
| 73 | |
| 74 | @Activate |
| 75 | protected void activate() { |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 76 | AbstractProjectableModel.setDriverService(null, this); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 77 | log.info("Started"); |
| 78 | } |
| 79 | |
| 80 | @Deactivate |
| 81 | protected void deactivate() { |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 82 | AbstractProjectableModel.setDriverService(this, null); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 83 | log.info("Stopped"); |
| 84 | } |
| 85 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 86 | @Override |
| 87 | public Set<DriverProvider> getProviders() { |
| 88 | return ImmutableSet.copyOf(providers); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void registerProvider(DriverProvider provider) { |
| 93 | provider.getDrivers().forEach(driver -> { |
Thomas Vachuska | bb062ec | 2016-01-25 16:49:47 -0800 | [diff] [blame] | 94 | Driver d = addDriver(driver); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 95 | driverByKey.put(key(driver.manufacturer(), |
| 96 | driver.hwVersion(), |
Thomas Vachuska | bb062ec | 2016-01-25 16:49:47 -0800 | [diff] [blame] | 97 | driver.swVersion()), d); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 98 | }); |
| 99 | providers.add(provider); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void unregisterProvider(DriverProvider provider) { |
| 104 | provider.getDrivers().forEach(driver -> { |
Thomas Vachuska | bb062ec | 2016-01-25 16:49:47 -0800 | [diff] [blame] | 105 | removeDriver(driver); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 106 | driverByKey.remove(key(driver.manufacturer(), |
| 107 | driver.hwVersion(), |
| 108 | driver.swVersion())); |
| 109 | }); |
| 110 | providers.remove(provider); |
| 111 | } |
| 112 | |
| 113 | @Override |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 114 | public Set<Driver> getDrivers() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 115 | checkPermission(DRIVER_READ); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 116 | ImmutableSet.Builder<Driver> builder = ImmutableSet.builder(); |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 117 | drivers.values().forEach(builder::add); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 118 | return builder.build(); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 122 | public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 123 | checkPermission(DRIVER_READ); |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 124 | return drivers.values().stream() |
| 125 | .filter(d -> d.hasBehaviour(withBehaviour)) |
| 126 | .collect(Collectors.toSet()); |
| 127 | } |
| 128 | |
| 129 | @Override |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 130 | public Driver getDriver(String driverName) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 131 | checkPermission(DRIVER_READ); |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 132 | return nullIsNotFound(drivers.get(driverName), NO_DRIVER); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public Driver getDriver(String mfr, String hw, String sw) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 137 | checkPermission(DRIVER_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 138 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 139 | // First attempt a literal search. |
| 140 | Driver driver = driverByKey.get(key(mfr, hw, sw)); |
| 141 | if (driver != null) { |
| 142 | return driver; |
| 143 | } |
| 144 | |
| 145 | // Otherwise, sweep through the key space and attempt to match using |
| 146 | // regular expression matching. |
| 147 | Optional<Driver> optional = driverByKey.values().stream() |
| 148 | .filter(d -> matches(d, mfr, hw, sw)).findFirst(); |
| 149 | |
| 150 | // If no matching driver is found, return default. |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 151 | return optional.orElse(drivers.get(DEFAULT)); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // Matches the given driver using ERE matching against the given criteria. |
| 155 | private boolean matches(Driver d, String mfr, String hw, String sw) { |
| 156 | // TODO: consider pre-compiling the expressions in the future |
| 157 | return mfr.matches(d.manufacturer()) && |
| 158 | hw.matches(d.hwVersion()) && |
| 159 | sw.matches(d.swVersion()); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 163 | public Driver getDriver(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 164 | checkPermission(DRIVER_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 165 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 166 | Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE); |
| 167 | String driverName = device.annotations().value(DRIVER); |
| 168 | if (driverName != null) { |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 169 | try { |
| 170 | return getDriver(driverName); |
| 171 | } catch (ItemNotFoundException e) { |
| 172 | log.warn("Specified driver {} not found, falling back.", driverName); |
| 173 | } |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 174 | } |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 175 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 176 | return nullIsNotFound(getDriver(device.manufacturer(), |
| 177 | device.hwVersion(), device.swVersion()), |
| 178 | NO_DRIVER); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public DriverHandler createHandler(DeviceId deviceId, String... credentials) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 183 | checkPermission(DRIVER_WRITE); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 184 | Driver driver = getDriver(deviceId); |
Thomas Vachuska | 80b0a80 | 2015-07-17 08:43:30 -0700 | [diff] [blame] | 185 | return new DefaultDriverHandler(new DefaultDriverData(driver, deviceId)); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 188 | // Produces a composite driver key using the specified components. |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 189 | private String key(String mfr, String hw, String sw) { |
| 190 | return String.format("%s-%s-%s", mfr, hw, sw); |
| 191 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 192 | } |