blob: c96b6ef208c117d1a91506782d057566504e3c4a [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 Vachuskaca88bb72015-04-08 19:38:02 -070028import org.onosproject.net.device.DeviceService;
alshabib77b88482015-04-07 15:47:50 -070029import org.onosproject.net.driver.Behaviour;
30import org.onosproject.net.driver.DefaultDriverData;
31import org.onosproject.net.driver.DefaultDriverHandler;
32import org.onosproject.net.driver.Driver;
alshabib77b88482015-04-07 15:47:50 -070033import org.onosproject.net.driver.DriverHandler;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070034import org.onosproject.net.driver.DriverRegistry;
35import org.onosproject.net.driver.DriverService;
alshabib77b88482015-04-07 15:47:50 -070036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
alshabib77b88482015-04-07 15:47:50 -070039import java.util.Set;
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070040import java.util.stream.Collectors;
alshabib77b88482015-04-07 15:47:50 -070041
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070042import static org.onlab.util.Tools.nullIsNotFound;
43import static org.onosproject.net.AnnotationKeys.DRIVER;
Changhoon Yoon541ef712015-05-23 17:18:34 +090044import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070045import static org.onosproject.security.AppPermission.Type.DRIVER_READ;
46import static org.onosproject.security.AppPermission.Type.DRIVER_WRITE;
alshabib77b88482015-04-07 15:47:50 -070047
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070048/**
49 * Manages inventory of device drivers.
50 */
alshabib77b88482015-04-07 15:47:50 -070051@Service
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070052// Not enabled by default to allow the DriverRegistryManager to enable it only
53// when all the required drivers are available.
54@Component(immediate = true, enabled = false)
55public class DriverManager implements DriverService {
alshabib77b88482015-04-07 15:47:50 -070056
57 private final Logger log = LoggerFactory.getLogger(getClass());
58
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070059 private static final String NO_DRIVER = "Driver not found";
60 private static final String NO_DEVICE = "Device not found";
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070061
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected DriverRegistry registry;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070064
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected DeviceService deviceService;
67
alshabib77b88482015-04-07 15:47:50 -070068 @Activate
69 protected void activate() {
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080070 AbstractProjectableModel.setDriverService(null, this);
alshabib77b88482015-04-07 15:47:50 -070071 log.info("Started");
72 }
73
74 @Deactivate
75 protected void deactivate() {
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080076 AbstractProjectableModel.setDriverService(this, null);
alshabib77b88482015-04-07 15:47:50 -070077 log.info("Stopped");
78 }
79
alshabib77b88482015-04-07 15:47:50 -070080 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070081 public Set<Driver> getDrivers() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090082 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070083 return registry.getDrivers();
alshabib77b88482015-04-07 15:47:50 -070084 }
85
86 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070087 public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) {
Changhoon Yoonb856b812015-08-10 03:47:19 +090088 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070089 return registry.getDrivers().stream()
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070090 .filter(d -> d.hasBehaviour(withBehaviour))
91 .collect(Collectors.toSet());
92 }
93
94 @Override
alshabib77b88482015-04-07 15:47:50 -070095 public Driver getDriver(String driverName) {
Changhoon Yoonb856b812015-08-10 03:47:19 +090096 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070097 return registry.getDriver(driverName);
alshabib77b88482015-04-07 15:47:50 -070098 }
99
100 @Override
101 public Driver getDriver(String mfr, String hw, String sw) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900102 checkPermission(DRIVER_READ);
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700103 return registry.getDriver(mfr, hw, sw);
alshabib77b88482015-04-07 15:47:50 -0700104 }
105
106 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700107 public Driver getDriver(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900108 checkPermission(DRIVER_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900109
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700110 Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE);
111 String driverName = device.annotations().value(DRIVER);
112 if (driverName != null) {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800113 try {
114 return getDriver(driverName);
115 } catch (ItemNotFoundException e) {
116 log.warn("Specified driver {} not found, falling back.", driverName);
117 }
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700118 }
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800119
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700120 return nullIsNotFound(getDriver(device.manufacturer(),
121 device.hwVersion(), device.swVersion()),
122 NO_DRIVER);
123 }
124
125 @Override
126 public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900127 checkPermission(DRIVER_WRITE);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700128 Driver driver = getDriver(deviceId);
Thomas Vachuska80b0a802015-07-17 08:43:30 -0700129 return new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib77b88482015-04-07 15:47:50 -0700130 }
131
alshabib77b88482015-04-07 15:47:50 -0700132}