blob: 43040da10ebe71fc1ac954fc141cbc995fc66a9d [file] [log] [blame]
alshabib77b88482015-04-07 15:47:50 -07001/*
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 */
16package org.onosproject.net.driver.impl;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Maps;
20import com.google.common.collect.Sets;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabib77b88482015-04-07 15:47:50 -070026import org.apache.felix.scr.annotations.Service;
Changhoon Yoon541ef712015-05-23 17:18:34 +090027import org.onosproject.core.Permission;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070028import org.onosproject.net.Device;
alshabib77b88482015-04-07 15:47:50 -070029import org.onosproject.net.DeviceId;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070030import org.onosproject.net.device.DeviceService;
alshabib77b88482015-04-07 15:47:50 -070031import org.onosproject.net.driver.Behaviour;
32import org.onosproject.net.driver.DefaultDriverData;
33import org.onosproject.net.driver.DefaultDriverHandler;
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070034import org.onosproject.net.driver.DefaultDriverProvider;
alshabib77b88482015-04-07 15:47:50 -070035import org.onosproject.net.driver.Driver;
36import org.onosproject.net.driver.DriverAdminService;
alshabib77b88482015-04-07 15:47:50 -070037import org.onosproject.net.driver.DriverHandler;
38import org.onosproject.net.driver.DriverProvider;
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
42import java.util.Map;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070043import java.util.Optional;
alshabib77b88482015-04-07 15:47:50 -070044import java.util.Set;
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070045import java.util.stream.Collectors;
alshabib77b88482015-04-07 15:47:50 -070046
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070047import static org.onlab.util.Tools.nullIsNotFound;
48import static org.onosproject.net.AnnotationKeys.DRIVER;
Changhoon Yoon541ef712015-05-23 17:18:34 +090049import static org.onosproject.security.AppGuard.checkPermission;
50
alshabib77b88482015-04-07 15:47:50 -070051
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070052/**
53 * Manages inventory of device drivers.
54 */
alshabib77b88482015-04-07 15:47:50 -070055@Component(immediate = true)
56@Service
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070057public class DriverManager extends DefaultDriverProvider implements DriverAdminService {
alshabib77b88482015-04-07 15:47:50 -070058
59 private final Logger log = LoggerFactory.getLogger(getClass());
60
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070061 private static final String NO_DRIVER = "Driver not found";
62 private static final String NO_DEVICE = "Device not found";
63 private static final String DEFAULT = "default";
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected DeviceService deviceService;
67
alshabib77b88482015-04-07 15:47:50 -070068 private Set<DriverProvider> providers = Sets.newConcurrentHashSet();
alshabib77b88482015-04-07 15:47:50 -070069 private Map<String, Driver> driverByKey = Maps.newConcurrentMap();
70
71 @Activate
72 protected void activate() {
73 log.info("Started");
74 }
75
76 @Deactivate
77 protected void deactivate() {
78 log.info("Stopped");
79 }
80
81
82 @Override
83 public Set<DriverProvider> getProviders() {
84 return ImmutableSet.copyOf(providers);
85 }
86
87 @Override
88 public void registerProvider(DriverProvider provider) {
89 provider.getDrivers().forEach(driver -> {
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070090 addDrivers(provider.getDrivers());
alshabib77b88482015-04-07 15:47:50 -070091 driverByKey.put(key(driver.manufacturer(),
92 driver.hwVersion(),
93 driver.swVersion()), driver);
94 });
95 providers.add(provider);
96 }
97
98 @Override
99 public void unregisterProvider(DriverProvider provider) {
100 provider.getDrivers().forEach(driver -> {
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700101 removeDrivers(provider.getDrivers());
alshabib77b88482015-04-07 15:47:50 -0700102 driverByKey.remove(key(driver.manufacturer(),
103 driver.hwVersion(),
104 driver.swVersion()));
105 });
106 providers.remove(provider);
107 }
108
109 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700110 public Set<Driver> getDrivers() {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900111 checkPermission(Permission.DRIVER_READ);
112
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700113 ImmutableSet.Builder<Driver> builder = ImmutableSet.builder();
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700114 drivers.values().forEach(builder::add);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700115 return builder.build();
alshabib77b88482015-04-07 15:47:50 -0700116 }
117
118 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700119 public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900120 checkPermission(Permission.DRIVER_READ);
121
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700122 return drivers.values().stream()
123 .filter(d -> d.hasBehaviour(withBehaviour))
124 .collect(Collectors.toSet());
125 }
126
127 @Override
alshabib77b88482015-04-07 15:47:50 -0700128 public Driver getDriver(String driverName) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900129 checkPermission(Permission.DRIVER_READ);
130
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700131 return nullIsNotFound(drivers.get(driverName), NO_DRIVER);
alshabib77b88482015-04-07 15:47:50 -0700132 }
133
134 @Override
135 public Driver getDriver(String mfr, String hw, String sw) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900136 checkPermission(Permission.DRIVER_READ);
137
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700138 // First attempt a literal search.
139 Driver driver = driverByKey.get(key(mfr, hw, sw));
140 if (driver != null) {
141 return driver;
142 }
143
144 // Otherwise, sweep through the key space and attempt to match using
145 // regular expression matching.
146 Optional<Driver> optional = driverByKey.values().stream()
147 .filter(d -> matches(d, mfr, hw, sw)).findFirst();
148
149 // If no matching driver is found, return default.
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700150 return optional.isPresent() ? optional.get() : drivers.get(DEFAULT);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700151 }
152
153 // Matches the given driver using ERE matching against the given criteria.
154 private boolean matches(Driver d, String mfr, String hw, String sw) {
155 // TODO: consider pre-compiling the expressions in the future
156 return mfr.matches(d.manufacturer()) &&
157 hw.matches(d.hwVersion()) &&
158 sw.matches(d.swVersion());
alshabib77b88482015-04-07 15:47:50 -0700159 }
160
161 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700162 public Driver getDriver(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900163 checkPermission(Permission.DRIVER_READ);
164
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700165 Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE);
166 String driverName = device.annotations().value(DRIVER);
167 if (driverName != null) {
168 return getDriver(driverName);
169 }
170 return nullIsNotFound(getDriver(device.manufacturer(),
171 device.hwVersion(), device.swVersion()),
172 NO_DRIVER);
173 }
174
175 @Override
176 public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900177 checkPermission(Permission.DRIVER_WRITE);
178
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700179 Driver driver = getDriver(deviceId);
alshabib77b88482015-04-07 15:47:50 -0700180 return new DefaultDriverHandler(new DefaultDriverData(driver));
181 }
182
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700183 // Produces a composite driver key using the specified components.
alshabib77b88482015-04-07 15:47:50 -0700184 private String key(String mfr, String hw, String sw) {
185 return String.format("%s-%s-%s", mfr, hw, sw);
186 }
alshabib77b88482015-04-07 15:47:50 -0700187}