blob: 18147223321c1874a8429211edaab141aa084d1e [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
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080018import org.onlab.util.ItemNotFoundException;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080019import org.onosproject.net.AbstractProjectableModel;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070020import org.onosproject.net.Device;
alshabib77b88482015-04-07 15:47:50 -070021import org.onosproject.net.DeviceId;
Thomas Vachuska164ecf62018-05-08 17:29:55 -070022import org.onosproject.net.config.NetworkConfigService;
23import org.onosproject.net.config.basics.BasicDeviceConfig;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070024import org.onosproject.net.device.DeviceService;
alshabib77b88482015-04-07 15:47:50 -070025import org.onosproject.net.driver.Behaviour;
26import org.onosproject.net.driver.DefaultDriverData;
27import org.onosproject.net.driver.DefaultDriverHandler;
28import org.onosproject.net.driver.Driver;
alshabib77b88482015-04-07 15:47:50 -070029import org.onosproject.net.driver.DriverHandler;
Thomas Vachuskae6a57412017-08-23 14:09:14 -070030import org.onosproject.net.driver.DriverListener;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070031import org.onosproject.net.driver.DriverRegistry;
32import org.onosproject.net.driver.DriverService;
Carmelo Cascone0761cd32018-08-29 19:22:50 -070033import org.onosproject.net.pi.model.PiPipeconfId;
34import org.onosproject.net.pi.service.PiPipeconfService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070035import org.osgi.service.component.annotations.Activate;
36import org.osgi.service.component.annotations.Component;
37import org.osgi.service.component.annotations.Deactivate;
38import org.osgi.service.component.annotations.Reference;
39import org.osgi.service.component.annotations.ReferenceCardinality;
alshabib77b88482015-04-07 15:47:50 -070040import org.slf4j.Logger;
41import org.slf4j.LoggerFactory;
42
Seyeon Jeong1e249102020-03-10 17:41:14 -070043import java.util.HashMap;
44import java.util.Map;
alshabib77b88482015-04-07 15:47:50 -070045import java.util.Set;
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070046import java.util.stream.Collectors;
alshabib77b88482015-04-07 15:47:50 -070047
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070048import static org.onlab.util.Tools.nullIsNotFound;
49import static org.onosproject.net.AnnotationKeys.DRIVER;
Changhoon Yoon541ef712015-05-23 17:18:34 +090050import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070051import static org.onosproject.security.AppPermission.Type.DRIVER_READ;
52import static org.onosproject.security.AppPermission.Type.DRIVER_WRITE;
alshabib77b88482015-04-07 15:47:50 -070053
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070054/**
55 * Manages inventory of device drivers.
56 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070057
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070058// Not enabled by default to allow the DriverRegistryManager to enable it only
59// when all the required drivers are available.
Ray Milkeyd84f89b2018-08-17 14:54:17 -070060@Component(immediate = true, enabled = false, service = DriverService.class)
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070061public class DriverManager implements DriverService {
alshabib77b88482015-04-07 15:47:50 -070062
63 private final Logger log = LoggerFactory.getLogger(getClass());
64
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070065 private static final String NO_DRIVER = "Driver not found";
66 private static final String NO_DEVICE = "Device not found";
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070067
Ray Milkeyd84f89b2018-08-17 14:54:17 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070069 protected DriverRegistry registry;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070070
Ray Milkeyd84f89b2018-08-17 14:54:17 -070071 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070072 protected DeviceService deviceService;
73
Ray Milkeyd84f89b2018-08-17 14:54:17 -070074 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska164ecf62018-05-08 17:29:55 -070075 protected NetworkConfigService networkConfigService;
76
Ray Milkeyd84f89b2018-08-17 14:54:17 -070077 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Carmelo Cascone0761cd32018-08-29 19:22:50 -070078 protected PiPipeconfService pipeconfService;
79
alshabib77b88482015-04-07 15:47:50 -070080 @Activate
81 protected void activate() {
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080082 AbstractProjectableModel.setDriverService(null, this);
alshabib77b88482015-04-07 15:47:50 -070083 log.info("Started");
84 }
85
86 @Deactivate
87 protected void deactivate() {
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080088 AbstractProjectableModel.setDriverService(this, null);
alshabib77b88482015-04-07 15:47:50 -070089 log.info("Stopped");
90 }
91
alshabib77b88482015-04-07 15:47:50 -070092 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070093 public Set<Driver> getDrivers() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090094 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070095 return registry.getDrivers();
alshabib77b88482015-04-07 15:47:50 -070096 }
97
98 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070099 public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900100 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700101 return registry.getDrivers().stream()
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700102 .filter(d -> d.hasBehaviour(withBehaviour))
103 .collect(Collectors.toSet());
104 }
105
106 @Override
alshabib77b88482015-04-07 15:47:50 -0700107 public Driver getDriver(String driverName) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900108 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700109 return registry.getDriver(driverName);
alshabib77b88482015-04-07 15:47:50 -0700110 }
111
112 @Override
113 public Driver getDriver(String mfr, String hw, String sw) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900114 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700115 return registry.getDriver(mfr, hw, sw);
alshabib77b88482015-04-07 15:47:50 -0700116 }
117
118 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700119 public Driver getDriver(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900120 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900121
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700122 Driver driver;
123
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700124 // Special processing for devices with pipeconf.
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700125 if (pipeconfService.ofDevice(deviceId).isPresent()) {
Carmelo Casconef23340c2019-04-28 12:33:14 -0700126 // No fallback for pipeconf merged drivers.
127 // Throws exception if pipeconf driver does not exist.
128 return nullIsNotFound(
129 getPipeconfMergedDriver(deviceId),
130 "Device is pipeconf-capable but a " +
131 "pipeconf-merged driver was not found");
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700132 }
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700133
134 // Primary source of driver configuration is the network config.
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700135 BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class);
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700136 driver = lookupDriver(cfg != null ? cfg.driver() : null);
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700137 if (driver != null) {
138 return driver;
139 }
140
141 // Secondary source of the driver selection is driver annotation.
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700142 Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE);
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700143 driver = lookupDriver(device.annotations().value(DRIVER));
144 if (driver != null) {
145 return driver;
146 }
147
148 // Tertiary source of the driver selection is the primordial information
149 // obtained from the device.
150 return nullIsNotFound(getDriver(device.manufacturer(),
151 device.hwVersion(), device.swVersion()),
152 NO_DRIVER);
153 }
154
Seyeon Jeong1e249102020-03-10 17:41:14 -0700155 @Override
156 public Map<DeviceId, String> getDeviceDrivers() {
157 Map<DeviceId, String> deviceDriverNameMap = new HashMap<>();
158 deviceService.getDevices().forEach(device -> {
159 deviceDriverNameMap.put(device.id(), getDriver(device.id()).name());
160 });
161 return deviceDriverNameMap;
162 }
163
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700164 private Driver getPipeconfMergedDriver(DeviceId deviceId) {
165 PiPipeconfId pipeconfId = pipeconfService.ofDevice(deviceId).orElse(null);
166 if (pipeconfId == null) {
167 log.warn("Missing pipeconf for {}, cannot produce a pipeconf merged driver",
168 deviceId);
169 return null;
170 }
171 String mergedDriverName = pipeconfService.getMergedDriver(deviceId, pipeconfId);
172 if (mergedDriverName == null) {
173 log.warn("Unable to get pipeconf merged driver for {} and {}",
174 deviceId, pipeconfId);
175 return null;
176 }
177 try {
178 return getDriver(mergedDriverName);
179 } catch (ItemNotFoundException e) {
180 log.warn("Specified pipeconf merged driver {} for {} not found",
181 mergedDriverName, deviceId);
182 return null;
183 }
184 }
185
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700186 private Driver lookupDriver(String driverName) {
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700187 if (driverName != null) {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800188 try {
189 return getDriver(driverName);
190 } catch (ItemNotFoundException e) {
191 log.warn("Specified driver {} not found, falling back.", driverName);
192 }
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700193 }
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700194 return null;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700195 }
196
197 @Override
198 public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900199 checkPermission(DRIVER_WRITE);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700200 Driver driver = getDriver(deviceId);
Thomas Vachuska80b0a802015-07-17 08:43:30 -0700201 return new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib77b88482015-04-07 15:47:50 -0700202 }
203
Thomas Vachuskae6a57412017-08-23 14:09:14 -0700204 @Override
205 public void addListener(DriverListener listener) {
206 registry.addListener(listener);
207 }
208
209 @Override
210 public void removeListener(DriverListener listener) {
211 registry.removeListener(listener);
212 }
alshabib77b88482015-04-07 15:47:50 -0700213}