blob: 1dd8f7bd95c0b8ef811c1712e5e951fb73385013 [file] [log] [blame]
alshabib77b88482015-04-07 15:47:50 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
alshabib77b88482015-04-07 15:47:50 -07003 *
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
alshabib77b88482015-04-07 15:47:50 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070021import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabib77b88482015-04-07 15:47:50 -070023import org.apache.felix.scr.annotations.Service;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080024import org.onlab.util.ItemNotFoundException;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080025import org.onosproject.net.AbstractProjectableModel;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070026import org.onosproject.net.Device;
alshabib77b88482015-04-07 15:47:50 -070027import org.onosproject.net.DeviceId;
Thomas Vachuska164ecf62018-05-08 17:29:55 -070028import org.onosproject.net.config.NetworkConfigService;
29import org.onosproject.net.config.basics.BasicDeviceConfig;
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;
34import org.onosproject.net.driver.Driver;
alshabib77b88482015-04-07 15:47:50 -070035import org.onosproject.net.driver.DriverHandler;
Thomas Vachuskae6a57412017-08-23 14:09:14 -070036import org.onosproject.net.driver.DriverListener;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070037import org.onosproject.net.driver.DriverRegistry;
38import org.onosproject.net.driver.DriverService;
alshabib77b88482015-04-07 15:47:50 -070039import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
alshabib77b88482015-04-07 15:47:50 -070042import java.util.Set;
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070043import java.util.stream.Collectors;
alshabib77b88482015-04-07 15:47:50 -070044
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070045import static org.onlab.util.Tools.nullIsNotFound;
46import static org.onosproject.net.AnnotationKeys.DRIVER;
Changhoon Yoon541ef712015-05-23 17:18:34 +090047import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070048import static org.onosproject.security.AppPermission.Type.DRIVER_READ;
49import static org.onosproject.security.AppPermission.Type.DRIVER_WRITE;
alshabib77b88482015-04-07 15:47:50 -070050
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070051/**
52 * Manages inventory of device drivers.
53 */
alshabib77b88482015-04-07 15:47:50 -070054@Service
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070055// Not enabled by default to allow the DriverRegistryManager to enable it only
56// when all the required drivers are available.
57@Component(immediate = true, enabled = false)
58public class DriverManager implements DriverService {
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";
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070064
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected DriverRegistry registry;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070067
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected DeviceService deviceService;
70
Thomas Vachuska164ecf62018-05-08 17:29:55 -070071 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected NetworkConfigService networkConfigService;
73
alshabib77b88482015-04-07 15:47:50 -070074 @Activate
75 protected void activate() {
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080076 AbstractProjectableModel.setDriverService(null, this);
alshabib77b88482015-04-07 15:47:50 -070077 log.info("Started");
78 }
79
80 @Deactivate
81 protected void deactivate() {
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080082 AbstractProjectableModel.setDriverService(this, null);
alshabib77b88482015-04-07 15:47:50 -070083 log.info("Stopped");
84 }
85
alshabib77b88482015-04-07 15:47:50 -070086 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070087 public Set<Driver> getDrivers() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090088 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070089 return registry.getDrivers();
alshabib77b88482015-04-07 15:47:50 -070090 }
91
92 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070093 public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) {
Changhoon Yoonb856b812015-08-10 03:47:19 +090094 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070095 return registry.getDrivers().stream()
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070096 .filter(d -> d.hasBehaviour(withBehaviour))
97 .collect(Collectors.toSet());
98 }
99
100 @Override
alshabib77b88482015-04-07 15:47:50 -0700101 public Driver getDriver(String driverName) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900102 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700103 return registry.getDriver(driverName);
alshabib77b88482015-04-07 15:47:50 -0700104 }
105
106 @Override
107 public Driver getDriver(String mfr, String hw, String sw) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900108 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700109 return registry.getDriver(mfr, hw, sw);
alshabib77b88482015-04-07 15:47:50 -0700110 }
111
112 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700113 public Driver getDriver(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900114 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900115
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700116 // Primary source of driver configuration is the network config.
117 BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class);
118 Driver driver = lookupDriver(cfg != null ? cfg.driver() : null);
119 if (driver != null) {
120 return driver;
121 }
122
123 // Secondary source of the driver selection is driver annotation.
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700124 Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE);
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700125 driver = lookupDriver(device.annotations().value(DRIVER));
126 if (driver != null) {
127 return driver;
128 }
129
130 // Tertiary source of the driver selection is the primordial information
131 // obtained from the device.
132 return nullIsNotFound(getDriver(device.manufacturer(),
133 device.hwVersion(), device.swVersion()),
134 NO_DRIVER);
135 }
136
137 private Driver lookupDriver(String driverName) {
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700138 if (driverName != null) {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800139 try {
140 return getDriver(driverName);
141 } catch (ItemNotFoundException e) {
142 log.warn("Specified driver {} not found, falling back.", driverName);
143 }
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700144 }
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700145 return null;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700146 }
147
148 @Override
149 public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900150 checkPermission(DRIVER_WRITE);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700151 Driver driver = getDriver(deviceId);
Thomas Vachuska80b0a802015-07-17 08:43:30 -0700152 return new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib77b88482015-04-07 15:47:50 -0700153 }
154
Thomas Vachuskae6a57412017-08-23 14:09:14 -0700155 @Override
156 public void addListener(DriverListener listener) {
157 registry.addListener(listener);
158 }
159
160 @Override
161 public void removeListener(DriverListener listener) {
162 registry.removeListener(listener);
163 }
alshabib77b88482015-04-07 15:47:50 -0700164}