blob: 5f7e2e2264d645b3e7a4c51cb83c07f1be17ee3c [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;
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070033import org.onosproject.net.driver.DefaultDriverProvider;
alshabib77b88482015-04-07 15:47:50 -070034import org.onosproject.net.driver.Driver;
35import org.onosproject.net.driver.DriverAdminService;
alshabib77b88482015-04-07 15:47:50 -070036import org.onosproject.net.driver.DriverHandler;
37import org.onosproject.net.driver.DriverProvider;
38import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
41import java.util.Map;
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070042import java.util.Optional;
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;
alshabib77b88482015-04-07 15:47:50 -070048
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070049/**
50 * Manages inventory of device drivers.
51 */
alshabib77b88482015-04-07 15:47:50 -070052@Component(immediate = true)
53@Service
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070054public class DriverManager extends DefaultDriverProvider implements DriverAdminService {
alshabib77b88482015-04-07 15:47:50 -070055
56 private final Logger log = LoggerFactory.getLogger(getClass());
57
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070058 private static final String NO_DRIVER = "Driver not found";
59 private static final String NO_DEVICE = "Device not found";
60 private static final String DEFAULT = "default";
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected DeviceService deviceService;
64
alshabib77b88482015-04-07 15:47:50 -070065 private Set<DriverProvider> providers = Sets.newConcurrentHashSet();
alshabib77b88482015-04-07 15:47:50 -070066 private Map<String, Driver> driverByKey = Maps.newConcurrentMap();
67
68 @Activate
69 protected void activate() {
70 log.info("Started");
71 }
72
73 @Deactivate
74 protected void deactivate() {
75 log.info("Stopped");
76 }
77
78
79 @Override
80 public Set<DriverProvider> getProviders() {
81 return ImmutableSet.copyOf(providers);
82 }
83
84 @Override
85 public void registerProvider(DriverProvider provider) {
86 provider.getDrivers().forEach(driver -> {
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070087 addDrivers(provider.getDrivers());
alshabib77b88482015-04-07 15:47:50 -070088 driverByKey.put(key(driver.manufacturer(),
89 driver.hwVersion(),
90 driver.swVersion()), driver);
91 });
92 providers.add(provider);
93 }
94
95 @Override
96 public void unregisterProvider(DriverProvider provider) {
97 provider.getDrivers().forEach(driver -> {
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070098 removeDrivers(provider.getDrivers());
alshabib77b88482015-04-07 15:47:50 -070099 driverByKey.remove(key(driver.manufacturer(),
100 driver.hwVersion(),
101 driver.swVersion()));
102 });
103 providers.remove(provider);
104 }
105
106 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700107 public Set<Driver> getDrivers() {
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700108 ImmutableSet.Builder<Driver> builder = ImmutableSet.builder();
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700109 drivers.values().forEach(builder::add);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700110 return builder.build();
alshabib77b88482015-04-07 15:47:50 -0700111 }
112
113 @Override
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700114 public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) {
115 return drivers.values().stream()
116 .filter(d -> d.hasBehaviour(withBehaviour))
117 .collect(Collectors.toSet());
118 }
119
120 @Override
alshabib77b88482015-04-07 15:47:50 -0700121 public Driver getDriver(String driverName) {
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700122 return nullIsNotFound(drivers.get(driverName), NO_DRIVER);
alshabib77b88482015-04-07 15:47:50 -0700123 }
124
125 @Override
126 public Driver getDriver(String mfr, String hw, String sw) {
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700127 // First attempt a literal search.
128 Driver driver = driverByKey.get(key(mfr, hw, sw));
129 if (driver != null) {
130 return driver;
131 }
132
133 // Otherwise, sweep through the key space and attempt to match using
134 // regular expression matching.
135 Optional<Driver> optional = driverByKey.values().stream()
136 .filter(d -> matches(d, mfr, hw, sw)).findFirst();
137
138 // If no matching driver is found, return default.
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700139 return optional.isPresent() ? optional.get() : drivers.get(DEFAULT);
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700140 }
141
142 // Matches the given driver using ERE matching against the given criteria.
143 private boolean matches(Driver d, String mfr, String hw, String sw) {
144 // TODO: consider pre-compiling the expressions in the future
145 return mfr.matches(d.manufacturer()) &&
146 hw.matches(d.hwVersion()) &&
147 sw.matches(d.swVersion());
alshabib77b88482015-04-07 15:47:50 -0700148 }
149
150 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700151 public Driver getDriver(DeviceId deviceId) {
152 Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE);
153 String driverName = device.annotations().value(DRIVER);
154 if (driverName != null) {
155 return getDriver(driverName);
156 }
157 return nullIsNotFound(getDriver(device.manufacturer(),
158 device.hwVersion(), device.swVersion()),
159 NO_DRIVER);
160 }
161
162 @Override
163 public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
164 Driver driver = getDriver(deviceId);
alshabib77b88482015-04-07 15:47:50 -0700165 return new DefaultDriverHandler(new DefaultDriverData(driver));
166 }
167
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700168 // Produces a composite driver key using the specified components.
alshabib77b88482015-04-07 15:47:50 -0700169 private String key(String mfr, String hw, String sw) {
170 return String.format("%s-%s-%s", mfr, hw, sw);
171 }
172
173}