blob: c793ca47ec20401d8e10861cb5ee9e7867e4d02d [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;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080027import org.onlab.util.ItemNotFoundException;
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;
Changhoon Yoonb856b812015-08-10 03:47:19 +090050import static org.onosproject.security.AppPermission.Type.*;
51
Changhoon Yoon541ef712015-05-23 17:18:34 +090052
alshabib77b88482015-04-07 15:47:50 -070053
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070054/**
55 * Manages inventory of device drivers.
56 */
alshabib77b88482015-04-07 15:47:50 -070057@Component(immediate = true)
58@Service
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070059public class DriverManager extends DefaultDriverProvider implements DriverAdminService {
alshabib77b88482015-04-07 15:47:50 -070060
61 private final Logger log = LoggerFactory.getLogger(getClass());
62
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070063 private static final String NO_DRIVER = "Driver not found";
64 private static final String NO_DEVICE = "Device not found";
65 private static final String DEFAULT = "default";
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected DeviceService deviceService;
69
alshabib77b88482015-04-07 15:47:50 -070070 private Set<DriverProvider> providers = Sets.newConcurrentHashSet();
alshabib77b88482015-04-07 15:47:50 -070071 private Map<String, Driver> driverByKey = Maps.newConcurrentMap();
72
73 @Activate
74 protected void activate() {
75 log.info("Started");
76 }
77
78 @Deactivate
79 protected void deactivate() {
80 log.info("Stopped");
81 }
82
83
84 @Override
85 public Set<DriverProvider> getProviders() {
86 return ImmutableSet.copyOf(providers);
87 }
88
89 @Override
90 public void registerProvider(DriverProvider provider) {
91 provider.getDrivers().forEach(driver -> {
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070092 addDrivers(provider.getDrivers());
alshabib77b88482015-04-07 15:47:50 -070093 driverByKey.put(key(driver.manufacturer(),
94 driver.hwVersion(),
95 driver.swVersion()), driver);
96 });
97 providers.add(provider);
98 }
99
100 @Override
101 public void unregisterProvider(DriverProvider provider) {
102 provider.getDrivers().forEach(driver -> {
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700103 removeDrivers(provider.getDrivers());
alshabib77b88482015-04-07 15:47:50 -0700104 driverByKey.remove(key(driver.manufacturer(),
105 driver.hwVersion(),
106 driver.swVersion()));
107 });
108 providers.remove(provider);
109 }
110
111 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700112 public Set<Driver> getDrivers() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900113 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900114
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700115 ImmutableSet.Builder<Driver> builder = ImmutableSet.builder();
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700116 drivers.values().forEach(builder::add);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700117 return builder.build();
alshabib77b88482015-04-07 15:47:50 -0700118 }
119
120 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700121 public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900122 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900123
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700124 return drivers.values().stream()
125 .filter(d -> d.hasBehaviour(withBehaviour))
126 .collect(Collectors.toSet());
127 }
128
129 @Override
alshabib77b88482015-04-07 15:47:50 -0700130 public Driver getDriver(String driverName) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900131 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900132
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700133 return nullIsNotFound(drivers.get(driverName), NO_DRIVER);
alshabib77b88482015-04-07 15:47:50 -0700134 }
135
136 @Override
137 public Driver getDriver(String mfr, String hw, String sw) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900138 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900139
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700140 // First attempt a literal search.
141 Driver driver = driverByKey.get(key(mfr, hw, sw));
142 if (driver != null) {
143 return driver;
144 }
145
146 // Otherwise, sweep through the key space and attempt to match using
147 // regular expression matching.
148 Optional<Driver> optional = driverByKey.values().stream()
149 .filter(d -> matches(d, mfr, hw, sw)).findFirst();
150
151 // If no matching driver is found, return default.
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700152 return optional.isPresent() ? optional.get() : drivers.get(DEFAULT);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700153 }
154
155 // Matches the given driver using ERE matching against the given criteria.
156 private boolean matches(Driver d, String mfr, String hw, String sw) {
157 // TODO: consider pre-compiling the expressions in the future
158 return mfr.matches(d.manufacturer()) &&
159 hw.matches(d.hwVersion()) &&
160 sw.matches(d.swVersion());
alshabib77b88482015-04-07 15:47:50 -0700161 }
162
163 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700164 public Driver getDriver(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900165 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900166
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700167 Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE);
168 String driverName = device.annotations().value(DRIVER);
169 if (driverName != null) {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800170 try {
171 return getDriver(driverName);
172 } catch (ItemNotFoundException e) {
173 log.warn("Specified driver {} not found, falling back.", driverName);
174 }
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700175 }
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800176
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700177 return nullIsNotFound(getDriver(device.manufacturer(),
178 device.hwVersion(), device.swVersion()),
179 NO_DRIVER);
180 }
181
182 @Override
183 public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900184 checkPermission(DRIVER_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900185
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700186 Driver driver = getDriver(deviceId);
Thomas Vachuska80b0a802015-07-17 08:43:30 -0700187 return new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib77b88482015-04-07 15:47:50 -0700188 }
189
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700190 // Produces a composite driver key using the specified components.
alshabib77b88482015-04-07 15:47:50 -0700191 private String key(String mfr, String hw, String sw) {
192 return String.format("%s-%s-%s", mfr, hw, sw);
193 }
alshabib77b88482015-04-07 15:47:50 -0700194}