blob: 53bf30a10a77c45e0e3a3724e7793d5730cfb200 [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;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070027import org.onosproject.net.Device;
alshabib77b88482015-04-07 15:47:50 -070028import org.onosproject.net.DeviceId;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070029import org.onosproject.net.device.DeviceService;
alshabib77b88482015-04-07 15:47:50 -070030import org.onosproject.net.driver.Behaviour;
31import org.onosproject.net.driver.DefaultDriverData;
32import org.onosproject.net.driver.DefaultDriverHandler;
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070033import org.onosproject.net.driver.DefaultDriverProvider;
alshabib77b88482015-04-07 15:47:50 -070034import org.onosproject.net.driver.Driver;
35import org.onosproject.net.driver.DriverAdminService;
alshabib77b88482015-04-07 15:47:50 -070036import org.onosproject.net.driver.DriverHandler;
37import org.onosproject.net.driver.DriverProvider;
38import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
41import java.util.Map;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070042import java.util.Optional;
alshabib77b88482015-04-07 15:47:50 -070043import java.util.Set;
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070044import java.util.stream.Collectors;
alshabib77b88482015-04-07 15:47:50 -070045
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070046import static org.onlab.util.Tools.nullIsNotFound;
47import static org.onosproject.net.AnnotationKeys.DRIVER;
Changhoon Yoon541ef712015-05-23 17:18:34 +090048import static org.onosproject.security.AppGuard.checkPermission;
Changhoon Yoonb856b812015-08-10 03:47:19 +090049import static org.onosproject.security.AppPermission.Type.*;
50
Changhoon Yoon541ef712015-05-23 17:18:34 +090051
alshabib77b88482015-04-07 15:47:50 -070052
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070053/**
54 * Manages inventory of device drivers.
55 */
alshabib77b88482015-04-07 15:47:50 -070056@Component(immediate = true)
57@Service
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070058public class DriverManager extends DefaultDriverProvider implements DriverAdminService {
alshabib77b88482015-04-07 15:47:50 -070059
60 private final Logger log = LoggerFactory.getLogger(getClass());
61
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070062 private static final String NO_DRIVER = "Driver not found";
63 private static final String NO_DEVICE = "Device not found";
64 private static final String DEFAULT = "default";
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected DeviceService deviceService;
68
alshabib77b88482015-04-07 15:47:50 -070069 private Set<DriverProvider> providers = Sets.newConcurrentHashSet();
alshabib77b88482015-04-07 15:47:50 -070070 private Map<String, Driver> driverByKey = Maps.newConcurrentMap();
71
72 @Activate
73 protected void activate() {
74 log.info("Started");
75 }
76
77 @Deactivate
78 protected void deactivate() {
79 log.info("Stopped");
80 }
81
82
83 @Override
84 public Set<DriverProvider> getProviders() {
85 return ImmutableSet.copyOf(providers);
86 }
87
88 @Override
89 public void registerProvider(DriverProvider provider) {
90 provider.getDrivers().forEach(driver -> {
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070091 addDrivers(provider.getDrivers());
alshabib77b88482015-04-07 15:47:50 -070092 driverByKey.put(key(driver.manufacturer(),
93 driver.hwVersion(),
94 driver.swVersion()), driver);
95 });
96 providers.add(provider);
97 }
98
99 @Override
100 public void unregisterProvider(DriverProvider provider) {
101 provider.getDrivers().forEach(driver -> {
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700102 removeDrivers(provider.getDrivers());
alshabib77b88482015-04-07 15:47:50 -0700103 driverByKey.remove(key(driver.manufacturer(),
104 driver.hwVersion(),
105 driver.swVersion()));
106 });
107 providers.remove(provider);
108 }
109
110 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700111 public Set<Driver> getDrivers() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900112 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900113
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700114 ImmutableSet.Builder<Driver> builder = ImmutableSet.builder();
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700115 drivers.values().forEach(builder::add);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700116 return builder.build();
alshabib77b88482015-04-07 15:47:50 -0700117 }
118
119 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700120 public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900121 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900122
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700123 return drivers.values().stream()
124 .filter(d -> d.hasBehaviour(withBehaviour))
125 .collect(Collectors.toSet());
126 }
127
128 @Override
alshabib77b88482015-04-07 15:47:50 -0700129 public Driver getDriver(String driverName) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900130 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900131
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700132 return nullIsNotFound(drivers.get(driverName), NO_DRIVER);
alshabib77b88482015-04-07 15:47:50 -0700133 }
134
135 @Override
136 public Driver getDriver(String mfr, String hw, String sw) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900137 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900138
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700139 // 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.
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700151 return optional.isPresent() ? optional.get() : drivers.get(DEFAULT);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700152 }
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());
alshabib77b88482015-04-07 15:47:50 -0700160 }
161
162 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700163 public Driver getDriver(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900164 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900165
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700166 Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE);
167 String driverName = device.annotations().value(DRIVER);
168 if (driverName != null) {
169 return getDriver(driverName);
170 }
171 return nullIsNotFound(getDriver(device.manufacturer(),
172 device.hwVersion(), device.swVersion()),
173 NO_DRIVER);
174 }
175
176 @Override
177 public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900178 checkPermission(DRIVER_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900179
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700180 Driver driver = getDriver(deviceId);
Thomas Vachuska80b0a802015-07-17 08:43:30 -0700181 return new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib77b88482015-04-07 15:47:50 -0700182 }
183
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700184 // Produces a composite driver key using the specified components.
alshabib77b88482015-04-07 15:47:50 -0700185 private String key(String mfr, String hw, String sw) {
186 return String.format("%s-%s-%s", mfr, hw, sw);
187 }
alshabib77b88482015-04-07 15:47:50 -0700188}