blob: 9c7fddbcb9af387f8f2fbd340bcca143ce791b05 [file] [log] [blame]
alshabib77b88482015-04-07 15:47:50 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
18import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Maps;
20import com.google.common.collect.Sets;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabib77b88482015-04-07 15:47:50 -070026import org.apache.felix.scr.annotations.Service;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070027import org.onosproject.net.Device;
alshabib77b88482015-04-07 15:47:50 -070028import org.onosproject.net.DeviceId;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070029import org.onosproject.net.device.DeviceService;
alshabib77b88482015-04-07 15:47:50 -070030import org.onosproject.net.driver.Behaviour;
31import org.onosproject.net.driver.DefaultDriverData;
32import org.onosproject.net.driver.DefaultDriverHandler;
33import org.onosproject.net.driver.Driver;
34import org.onosproject.net.driver.DriverAdminService;
alshabib77b88482015-04-07 15:47:50 -070035import org.onosproject.net.driver.DriverHandler;
36import org.onosproject.net.driver.DriverProvider;
37import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
40import java.util.Map;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070041import java.util.Optional;
alshabib77b88482015-04-07 15:47:50 -070042import java.util.Set;
43
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070044import static org.onlab.util.Tools.nullIsNotFound;
45import static org.onosproject.net.AnnotationKeys.DRIVER;
alshabib77b88482015-04-07 15:47:50 -070046
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070047/**
48 * Manages inventory of device drivers.
49 */
alshabib77b88482015-04-07 15:47:50 -070050@Component(immediate = true)
51@Service
52public class DriverManager implements DriverAdminService {
53
54 private final Logger log = LoggerFactory.getLogger(getClass());
55
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070056 private static final String NO_DRIVER = "Driver not found";
57 private static final String NO_DEVICE = "Device not found";
58 private static final String DEFAULT = "default";
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected DeviceService deviceService;
62
alshabib77b88482015-04-07 15:47:50 -070063 private Set<DriverProvider> providers = Sets.newConcurrentHashSet();
64 private Map<String, Driver> driverByName = Maps.newConcurrentMap();
65 private Map<String, Driver> driverByKey = Maps.newConcurrentMap();
66
67 @Activate
68 protected void activate() {
69 log.info("Started");
70 }
71
72 @Deactivate
73 protected void deactivate() {
74 log.info("Stopped");
75 }
76
77
78 @Override
79 public Set<DriverProvider> getProviders() {
80 return ImmutableSet.copyOf(providers);
81 }
82
83 @Override
84 public void registerProvider(DriverProvider provider) {
85 provider.getDrivers().forEach(driver -> {
86 driverByName.put(driver.name(), driver);
87 driverByKey.put(key(driver.manufacturer(),
88 driver.hwVersion(),
89 driver.swVersion()), driver);
90 });
91 providers.add(provider);
92 }
93
94 @Override
95 public void unregisterProvider(DriverProvider provider) {
96 provider.getDrivers().forEach(driver -> {
97 driverByName.remove(driver.name());
98 driverByKey.remove(key(driver.manufacturer(),
99 driver.hwVersion(),
100 driver.swVersion()));
101 });
102 providers.remove(provider);
103 }
104
105 @Override
106 public Set<Driver> getDrivers(Class<? extends Behaviour>... withBehaviours) {
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700107 ImmutableSet.Builder<Driver> builder = ImmutableSet.builder();
108 for (Class<? extends Behaviour> behaviour : withBehaviours) {
109 driverByName.forEach((name, driver) -> {
110 if (driver.hasBehaviour(behaviour)) {
111 builder.add(driver);
112 }
113 });
114 }
115 return builder.build();
alshabib77b88482015-04-07 15:47:50 -0700116 }
117
118 @Override
119 public Driver getDriver(String driverName) {
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700120 return nullIsNotFound(driverByName.get(driverName), NO_DRIVER);
alshabib77b88482015-04-07 15:47:50 -0700121 }
122
123 @Override
124 public Driver getDriver(String mfr, String hw, String sw) {
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700125 // First attempt a literal search.
126 Driver driver = driverByKey.get(key(mfr, hw, sw));
127 if (driver != null) {
128 return driver;
129 }
130
131 // Otherwise, sweep through the key space and attempt to match using
132 // regular expression matching.
133 Optional<Driver> optional = driverByKey.values().stream()
134 .filter(d -> matches(d, mfr, hw, sw)).findFirst();
135
136 // If no matching driver is found, return default.
137 return optional.isPresent() ? optional.get() : driverByName.get(DEFAULT);
138 }
139
140 // Matches the given driver using ERE matching against the given criteria.
141 private boolean matches(Driver d, String mfr, String hw, String sw) {
142 // TODO: consider pre-compiling the expressions in the future
143 return mfr.matches(d.manufacturer()) &&
144 hw.matches(d.hwVersion()) &&
145 sw.matches(d.swVersion());
alshabib77b88482015-04-07 15:47:50 -0700146 }
147
148 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700149 public Driver getDriver(DeviceId deviceId) {
150 Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE);
151 String driverName = device.annotations().value(DRIVER);
152 if (driverName != null) {
153 return getDriver(driverName);
154 }
155 return nullIsNotFound(getDriver(device.manufacturer(),
156 device.hwVersion(), device.swVersion()),
157 NO_DRIVER);
158 }
159
160 @Override
161 public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
162 Driver driver = getDriver(deviceId);
alshabib77b88482015-04-07 15:47:50 -0700163 return new DefaultDriverHandler(new DefaultDriverData(driver));
164 }
165
alshabib77b88482015-04-07 15:47:50 -0700166 private String key(String mfr, String hw, String sw) {
167 return String.format("%s-%s-%s", mfr, hw, sw);
168 }
169
170}