blob: f2ece6b4b616cd8d36288eddad6566cffc998111 [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;
Carmelo Cascone0761cd32018-08-29 19:22:50 -070039import org.onosproject.net.pi.model.PiPipeconfId;
40import org.onosproject.net.pi.service.PiPipeconfService;
alshabib77b88482015-04-07 15:47:50 -070041import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
43
alshabib77b88482015-04-07 15:47:50 -070044import java.util.Set;
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070045import java.util.stream.Collectors;
alshabib77b88482015-04-07 15:47:50 -070046
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070047import static org.onlab.util.Tools.nullIsNotFound;
48import static org.onosproject.net.AnnotationKeys.DRIVER;
Changhoon Yoon541ef712015-05-23 17:18:34 +090049import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070050import static org.onosproject.security.AppPermission.Type.DRIVER_READ;
51import static org.onosproject.security.AppPermission.Type.DRIVER_WRITE;
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@Service
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070057// Not enabled by default to allow the DriverRegistryManager to enable it only
58// when all the required drivers are available.
59@Component(immediate = true, enabled = false)
60public class DriverManager implements DriverService {
alshabib77b88482015-04-07 15:47:50 -070061
62 private final Logger log = LoggerFactory.getLogger(getClass());
63
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070064 private static final String NO_DRIVER = "Driver not found";
65 private static final String NO_DEVICE = "Device not found";
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070066
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected DriverRegistry registry;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070069
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected DeviceService deviceService;
72
Thomas Vachuska164ecf62018-05-08 17:29:55 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected NetworkConfigService networkConfigService;
75
Carmelo Cascone0761cd32018-08-29 19:22:50 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected PiPipeconfService pipeconfService;
78
alshabib77b88482015-04-07 15:47:50 -070079 @Activate
80 protected void activate() {
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080081 AbstractProjectableModel.setDriverService(null, this);
alshabib77b88482015-04-07 15:47:50 -070082 log.info("Started");
83 }
84
85 @Deactivate
86 protected void deactivate() {
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080087 AbstractProjectableModel.setDriverService(this, null);
alshabib77b88482015-04-07 15:47:50 -070088 log.info("Stopped");
89 }
90
alshabib77b88482015-04-07 15:47:50 -070091 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070092 public Set<Driver> getDrivers() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090093 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070094 return registry.getDrivers();
alshabib77b88482015-04-07 15:47:50 -070095 }
96
97 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070098 public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) {
Changhoon Yoonb856b812015-08-10 03:47:19 +090099 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700100 return registry.getDrivers().stream()
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700101 .filter(d -> d.hasBehaviour(withBehaviour))
102 .collect(Collectors.toSet());
103 }
104
105 @Override
alshabib77b88482015-04-07 15:47:50 -0700106 public Driver getDriver(String driverName) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900107 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700108 return registry.getDriver(driverName);
alshabib77b88482015-04-07 15:47:50 -0700109 }
110
111 @Override
112 public Driver getDriver(String mfr, String hw, String sw) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900113 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700114 return registry.getDriver(mfr, hw, sw);
alshabib77b88482015-04-07 15:47:50 -0700115 }
116
117 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700118 public Driver getDriver(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900119 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900120
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700121 Driver driver;
122
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700123 // Primary source of driver configuration is the network config.
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700124 if (pipeconfService.ofDevice(deviceId).isPresent()) {
125 // Device has pipeconf associated, look for merged driver.
126 // Implementation of PiPipeconfService is expected to look for a
127 // base driver in network config.
128 PiPipeconfId pipeconfId = pipeconfService.ofDevice(deviceId).get();
129 String mergedDriver = pipeconfService.getMergedDriver(deviceId, pipeconfId);
130 driver = mergedDriver != null ? lookupDriver(mergedDriver) : null;
131 if (driver != null) {
132 return driver;
133 } else {
134 log.error("Merged driver for {} with pipeconf {} not found, falling back.",
135 deviceId, pipeconfId);
136 }
137 }
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700138 BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class);
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700139 driver = lookupDriver(cfg != null ? cfg.driver() : null);
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700140 if (driver != null) {
141 return driver;
142 }
143
144 // Secondary source of the driver selection is driver annotation.
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700145 Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE);
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700146 driver = lookupDriver(device.annotations().value(DRIVER));
147 if (driver != null) {
148 return driver;
149 }
150
151 // Tertiary source of the driver selection is the primordial information
152 // obtained from the device.
153 return nullIsNotFound(getDriver(device.manufacturer(),
154 device.hwVersion(), device.swVersion()),
155 NO_DRIVER);
156 }
157
158 private Driver lookupDriver(String driverName) {
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700159 if (driverName != null) {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800160 try {
161 return getDriver(driverName);
162 } catch (ItemNotFoundException e) {
163 log.warn("Specified driver {} not found, falling back.", driverName);
164 }
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700165 }
Thomas Vachuska164ecf62018-05-08 17:29:55 -0700166 return null;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700167 }
168
169 @Override
170 public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900171 checkPermission(DRIVER_WRITE);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700172 Driver driver = getDriver(deviceId);
Thomas Vachuska80b0a802015-07-17 08:43:30 -0700173 return new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib77b88482015-04-07 15:47:50 -0700174 }
175
Thomas Vachuskae6a57412017-08-23 14:09:14 -0700176 @Override
177 public void addListener(DriverListener listener) {
178 registry.addListener(listener);
179 }
180
181 @Override
182 public void removeListener(DriverListener listener) {
183 registry.removeListener(listener);
184 }
alshabib77b88482015-04-07 15:47:50 -0700185}