alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 27 | import org.onosproject.net.Device; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 29 | import org.onosproject.net.device.DeviceService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 30 | import org.onosproject.net.driver.Behaviour; |
| 31 | import org.onosproject.net.driver.DefaultDriverData; |
| 32 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 33 | import org.onosproject.net.driver.Driver; |
| 34 | import org.onosproject.net.driver.DriverAdminService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 35 | import org.onosproject.net.driver.DriverHandler; |
| 36 | import org.onosproject.net.driver.DriverProvider; |
| 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
| 40 | import java.util.Map; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 41 | import java.util.Optional; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 42 | import java.util.Set; |
| 43 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 44 | import static org.onlab.util.Tools.nullIsNotFound; |
| 45 | import static org.onosproject.net.AnnotationKeys.DRIVER; |
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 | /** |
| 48 | * Manages inventory of device drivers. |
| 49 | */ |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 50 | @Component(immediate = true) |
| 51 | @Service |
| 52 | public class DriverManager implements DriverAdminService { |
| 53 | |
| 54 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 55 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 56 | private static final String NO_DRIVER = "Driver not found"; |
| 57 | private static final String NO_DEVICE = "Device not found"; |
| 58 | private static final String DEFAULT = "default"; |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected DeviceService deviceService; |
| 62 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 63 | private Set<DriverProvider> providers = Sets.newConcurrentHashSet(); |
| 64 | private Map<String, Driver> driverByName = Maps.newConcurrentMap(); |
| 65 | private Map<String, Driver> driverByKey = Maps.newConcurrentMap(); |
| 66 | |
| 67 | @Activate |
| 68 | protected void activate() { |
| 69 | log.info("Started"); |
| 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | protected void deactivate() { |
| 74 | log.info("Stopped"); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | @Override |
| 79 | public Set<DriverProvider> getProviders() { |
| 80 | return ImmutableSet.copyOf(providers); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void registerProvider(DriverProvider provider) { |
| 85 | provider.getDrivers().forEach(driver -> { |
| 86 | driverByName.put(driver.name(), driver); |
| 87 | driverByKey.put(key(driver.manufacturer(), |
| 88 | driver.hwVersion(), |
| 89 | driver.swVersion()), driver); |
| 90 | }); |
| 91 | providers.add(provider); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void unregisterProvider(DriverProvider provider) { |
| 96 | provider.getDrivers().forEach(driver -> { |
| 97 | driverByName.remove(driver.name()); |
| 98 | driverByKey.remove(key(driver.manufacturer(), |
| 99 | driver.hwVersion(), |
| 100 | driver.swVersion())); |
| 101 | }); |
| 102 | providers.remove(provider); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public Set<Driver> getDrivers(Class<? extends Behaviour>... withBehaviours) { |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 107 | ImmutableSet.Builder<Driver> builder = ImmutableSet.builder(); |
| 108 | for (Class<? extends Behaviour> behaviour : withBehaviours) { |
| 109 | driverByName.forEach((name, driver) -> { |
| 110 | if (driver.hasBehaviour(behaviour)) { |
| 111 | builder.add(driver); |
| 112 | } |
| 113 | }); |
| 114 | } |
| 115 | return builder.build(); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public Driver getDriver(String driverName) { |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 120 | return nullIsNotFound(driverByName.get(driverName), NO_DRIVER); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public Driver getDriver(String mfr, String hw, String sw) { |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 125 | // First attempt a literal search. |
| 126 | Driver driver = driverByKey.get(key(mfr, hw, sw)); |
| 127 | if (driver != null) { |
| 128 | return driver; |
| 129 | } |
| 130 | |
| 131 | // Otherwise, sweep through the key space and attempt to match using |
| 132 | // regular expression matching. |
| 133 | Optional<Driver> optional = driverByKey.values().stream() |
| 134 | .filter(d -> matches(d, mfr, hw, sw)).findFirst(); |
| 135 | |
| 136 | // If no matching driver is found, return default. |
| 137 | return optional.isPresent() ? optional.get() : driverByName.get(DEFAULT); |
| 138 | } |
| 139 | |
| 140 | // Matches the given driver using ERE matching against the given criteria. |
| 141 | private boolean matches(Driver d, String mfr, String hw, String sw) { |
| 142 | // TODO: consider pre-compiling the expressions in the future |
| 143 | return mfr.matches(d.manufacturer()) && |
| 144 | hw.matches(d.hwVersion()) && |
| 145 | sw.matches(d.swVersion()); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 149 | public Driver getDriver(DeviceId deviceId) { |
| 150 | Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE); |
| 151 | String driverName = device.annotations().value(DRIVER); |
| 152 | if (driverName != null) { |
| 153 | return getDriver(driverName); |
| 154 | } |
| 155 | return nullIsNotFound(getDriver(device.manufacturer(), |
| 156 | device.hwVersion(), device.swVersion()), |
| 157 | NO_DRIVER); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public DriverHandler createHandler(DeviceId deviceId, String... credentials) { |
| 162 | Driver driver = getDriver(deviceId); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 163 | return new DefaultDriverHandler(new DefaultDriverData(driver)); |
| 164 | } |
| 165 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 166 | private String key(String mfr, String hw, String sw) { |
| 167 | return String.format("%s-%s-%s", mfr, hw, sw); |
| 168 | } |
| 169 | |
| 170 | } |