blob: 001fe58ae4c4f3543beb898ec9132a3ddfda9dd2 [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
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;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070049import static org.onosproject.security.AppPermission.Type.DRIVER_READ;
50import static org.onosproject.security.AppPermission.Type.DRIVER_WRITE;
alshabib77b88482015-04-07 15:47:50 -070051
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070052/**
53 * Manages inventory of device drivers.
54 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070055
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070056// Not enabled by default to allow the DriverRegistryManager to enable it only
57// when all the required drivers are available.
Ray Milkeyd84f89b2018-08-17 14:54:17 -070058@Component(immediate = true, enabled = false, service = DriverService.class)
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070059public class DriverManager implements DriverService {
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";
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070065
Ray Milkeyd84f89b2018-08-17 14:54:17 -070066 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070067 protected DriverRegistry registry;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070068
Ray Milkeyd84f89b2018-08-17 14:54:17 -070069 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070070 protected DeviceService deviceService;
71
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska164ecf62018-05-08 17:29:55 -070073 protected NetworkConfigService networkConfigService;
74
Ray Milkeyd84f89b2018-08-17 14:54:17 -070075 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Carmelo Cascone0761cd32018-08-29 19:22:50 -070076 protected PiPipeconfService pipeconfService;
77
alshabib77b88482015-04-07 15:47:50 -070078 @Activate
79 protected void activate() {
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080080 AbstractProjectableModel.setDriverService(null, this);
alshabib77b88482015-04-07 15:47:50 -070081 log.info("Started");
82 }
83
84 @Deactivate
85 protected void deactivate() {
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080086 AbstractProjectableModel.setDriverService(this, null);
alshabib77b88482015-04-07 15:47:50 -070087 log.info("Stopped");
88 }
89
alshabib77b88482015-04-07 15:47:50 -070090 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070091 public Set<Driver> getDrivers() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090092 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070093 return registry.getDrivers();
alshabib77b88482015-04-07 15:47:50 -070094 }
95
96 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070097 public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) {
Changhoon Yoonb856b812015-08-10 03:47:19 +090098 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070099 return registry.getDrivers().stream()
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700100 .filter(d -> d.hasBehaviour(withBehaviour))
101 .collect(Collectors.toSet());
102 }
103
104 @Override
alshabib77b88482015-04-07 15:47:50 -0700105 public Driver getDriver(String driverName) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900106 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700107 return registry.getDriver(driverName);
alshabib77b88482015-04-07 15:47:50 -0700108 }
109
110 @Override
111 public Driver getDriver(String mfr, String hw, String sw) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900112 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700113 return registry.getDriver(mfr, hw, sw);
alshabib77b88482015-04-07 15:47:50 -0700114 }
115
116 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700117 public Driver getDriver(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900118 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900119
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700120 Driver driver;
121
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700122 // Primary source of driver configuration is the network config.
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700123 if (pipeconfService.ofDevice(deviceId).isPresent()) {
124 // Device has pipeconf associated, look for merged driver.
125 // Implementation of PiPipeconfService is expected to look for a
126 // base driver in network config.
127 PiPipeconfId pipeconfId = pipeconfService.ofDevice(deviceId).get();
128 String mergedDriver = pipeconfService.getMergedDriver(deviceId, pipeconfId);
129 driver = mergedDriver != null ? lookupDriver(mergedDriver) : null;
130 if (driver != null) {
131 return driver;
132 } else {
133 log.error("Merged driver for {} with pipeconf {} not found, falling back.",
134 deviceId, pipeconfId);
135 }
136 }
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700137 BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class);
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700138 driver = lookupDriver(cfg != null ? cfg.driver() : null);
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700139 if (driver != null) {
140 return driver;
141 }
142
143 // Secondary source of the driver selection is driver annotation.
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700144 Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE);
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700145 driver = lookupDriver(device.annotations().value(DRIVER));
146 if (driver != null) {
147 return driver;
148 }
149
150 // Tertiary source of the driver selection is the primordial information
151 // obtained from the device.
152 return nullIsNotFound(getDriver(device.manufacturer(),
153 device.hwVersion(), device.swVersion()),
154 NO_DRIVER);
155 }
156
157 private Driver lookupDriver(String driverName) {
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700158 if (driverName != null) {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800159 try {
160 return getDriver(driverName);
161 } catch (ItemNotFoundException e) {
162 log.warn("Specified driver {} not found, falling back.", driverName);
163 }
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700164 }
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700165 return null;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700166 }
167
168 @Override
169 public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900170 checkPermission(DRIVER_WRITE);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700171 Driver driver = getDriver(deviceId);
Thomas Vachuska80b0a802015-07-17 08:43:30 -0700172 return new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib77b88482015-04-07 15:47:50 -0700173 }
174
Thomas Vachuskae6a57412017-08-23 14:09:14 -0700175 @Override
176 public void addListener(DriverListener listener) {
177 registry.addListener(listener);
178 }
179
180 @Override
181 public void removeListener(DriverListener listener) {
182 registry.removeListener(listener);
183 }
alshabib77b88482015-04-07 15:47:50 -0700184}