blob: 31032b317a30b22e2f693a021aef5a808d14ca83 [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
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700122 // Special processing for devices with pipeconf.
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700123 if (pipeconfService.ofDevice(deviceId).isPresent()) {
Carmelo Casconef23340c2019-04-28 12:33:14 -0700124 // No fallback for pipeconf merged drivers.
125 // Throws exception if pipeconf driver does not exist.
126 return nullIsNotFound(
127 getPipeconfMergedDriver(deviceId),
128 "Device is pipeconf-capable but a " +
129 "pipeconf-merged driver was not found");
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700130 }
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700131
132 // Primary source of driver configuration is the network config.
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700133 BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class);
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700134 driver = lookupDriver(cfg != null ? cfg.driver() : null);
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700135 if (driver != null) {
136 return driver;
137 }
138
139 // Secondary source of the driver selection is driver annotation.
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700140 Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE);
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700141 driver = lookupDriver(device.annotations().value(DRIVER));
142 if (driver != null) {
143 return driver;
144 }
145
146 // Tertiary source of the driver selection is the primordial information
147 // obtained from the device.
148 return nullIsNotFound(getDriver(device.manufacturer(),
149 device.hwVersion(), device.swVersion()),
150 NO_DRIVER);
151 }
152
Carmelo Casconeda0b5592018-09-14 12:54:15 -0700153 private Driver getPipeconfMergedDriver(DeviceId deviceId) {
154 PiPipeconfId pipeconfId = pipeconfService.ofDevice(deviceId).orElse(null);
155 if (pipeconfId == null) {
156 log.warn("Missing pipeconf for {}, cannot produce a pipeconf merged driver",
157 deviceId);
158 return null;
159 }
160 String mergedDriverName = pipeconfService.getMergedDriver(deviceId, pipeconfId);
161 if (mergedDriverName == null) {
162 log.warn("Unable to get pipeconf merged driver for {} and {}",
163 deviceId, pipeconfId);
164 return null;
165 }
166 try {
167 return getDriver(mergedDriverName);
168 } catch (ItemNotFoundException e) {
169 log.warn("Specified pipeconf merged driver {} for {} not found",
170 mergedDriverName, deviceId);
171 return null;
172 }
173 }
174
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700175 private Driver lookupDriver(String driverName) {
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700176 if (driverName != null) {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800177 try {
178 return getDriver(driverName);
179 } catch (ItemNotFoundException e) {
180 log.warn("Specified driver {} not found, falling back.", driverName);
181 }
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700182 }
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700183 return null;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700184 }
185
186 @Override
187 public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900188 checkPermission(DRIVER_WRITE);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700189 Driver driver = getDriver(deviceId);
Thomas Vachuska80b0a802015-07-17 08:43:30 -0700190 return new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib77b88482015-04-07 15:47:50 -0700191 }
192
Thomas Vachuskae6a57412017-08-23 14:09:14 -0700193 @Override
194 public void addListener(DriverListener listener) {
195 registry.addListener(listener);
196 }
197
198 @Override
199 public void removeListener(DriverListener listener) {
200 registry.removeListener(listener);
201 }
alshabib77b88482015-04-07 15:47:50 -0700202}