Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package org.onosproject.provider.rest.device.impl; |
| 18 | |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 19 | import com.google.common.base.Objects; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 20 | import com.google.common.base.Preconditions; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.onlab.packet.ChassisId; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.incubator.net.config.basics.ConfigException; |
Marc De Leenheer | b0d131c | 2016-03-01 20:34:59 -0800 | [diff] [blame] | 30 | import org.onosproject.net.AnnotationKeys; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 31 | import org.onosproject.net.DefaultAnnotations; |
| 32 | import org.onosproject.net.Device; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.MastershipRole; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 35 | import org.onosproject.net.PortNumber; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 36 | import org.onosproject.net.SparseAnnotations; |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 37 | import org.onosproject.net.behaviour.PortDiscovery; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 38 | import org.onosproject.net.config.ConfigFactory; |
| 39 | import org.onosproject.net.config.NetworkConfigEvent; |
| 40 | import org.onosproject.net.config.NetworkConfigListener; |
| 41 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 42 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 43 | import org.onosproject.net.device.DeviceDescription; |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 44 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 45 | import org.onosproject.net.device.DeviceProvider; |
| 46 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 47 | import org.onosproject.net.device.DeviceProviderService; |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 48 | import org.onosproject.net.device.DeviceService; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 49 | import org.onosproject.net.provider.AbstractProvider; |
| 50 | import org.onosproject.net.provider.ProviderId; |
| 51 | import org.onosproject.protocol.rest.RestSBController; |
| 52 | import org.onosproject.protocol.rest.RestSBDevice; |
| 53 | import org.slf4j.Logger; |
| 54 | |
Andrea Campanella | c6ecc63 | 2016-03-10 17:57:06 -0800 | [diff] [blame] | 55 | import javax.ws.rs.ProcessingException; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 56 | import java.util.HashSet; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 57 | import java.util.Set; |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 58 | import java.util.concurrent.ExecutorService; |
| 59 | import java.util.concurrent.Executors; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 60 | |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 61 | import static org.onlab.util.Tools.groupedThreads; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 62 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_ADDED; |
| 63 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED; |
| 64 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 65 | import static org.slf4j.LoggerFactory.getLogger; |
| 66 | |
| 67 | /** |
| 68 | * Provider for devices that use REST as means of configuration communication. |
| 69 | */ |
| 70 | @Component(immediate = true) |
| 71 | public class RestDeviceProvider extends AbstractProvider |
| 72 | implements DeviceProvider { |
| 73 | private static final String APP_NAME = "org.onosproject.restsb"; |
| 74 | private static final String REST = "rest"; |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 75 | private static final String JSON = "json"; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 76 | private static final String PROVIDER = "org.onosproject.provider.rest.device"; |
| 77 | private static final String IPADDRESS = "ipaddress"; |
| 78 | private static final int TEST_CONNECT_TIMEOUT = 1000; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 79 | private static final String HTTPS = "https"; |
| 80 | private static final String AUTHORIZATION_PROPERTY = "authorization"; |
| 81 | private static final String BASIC_AUTH_PREFIX = "Basic "; |
| 82 | private static final String URL_SEPARATOR = "://"; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 83 | private final Logger log = getLogger(getClass()); |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected DeviceProviderRegistry providerRegistry; |
| 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected RestSBController controller; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected NetworkConfigRegistry cfgService; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected CoreService coreService; |
| 96 | |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 98 | protected DeviceService deviceService; |
Andrea Campanella | d8d92db | 2016-01-14 16:24:41 -0800 | [diff] [blame] | 99 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 100 | |
| 101 | private DeviceProviderService providerService; |
| 102 | protected static final String ISNOTNULL = "Rest device is not null"; |
| 103 | private static final String UNKNOWN = "unknown"; |
| 104 | |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 105 | private final ExecutorService executor = |
Andrea Campanella | 90f044f | 2016-03-02 09:14:57 -0800 | [diff] [blame] | 106 | Executors.newFixedThreadPool(5, groupedThreads("onos/restsbprovider", "device-installer-%d", log)); |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 107 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 108 | private final ConfigFactory factory = |
| 109 | new ConfigFactory<ApplicationId, RestProviderConfig>(APP_SUBJECT_FACTORY, |
| 110 | RestProviderConfig.class, |
Konstantinos Kanonakis | b3e9704 | 2016-11-15 11:33:07 -0600 | [diff] [blame] | 111 | "devices", |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 112 | true) { |
| 113 | @Override |
| 114 | public RestProviderConfig createConfig() { |
| 115 | return new RestProviderConfig(); |
| 116 | } |
| 117 | }; |
| 118 | private final NetworkConfigListener cfgLister = new InternalNetworkConfigListener(); |
| 119 | private ApplicationId appId; |
| 120 | |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 121 | private Set<DeviceId> addedDevices = new HashSet<>(); |
| 122 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 123 | |
| 124 | @Activate |
| 125 | public void activate() { |
| 126 | appId = coreService.registerApplication(APP_NAME); |
| 127 | providerService = providerRegistry.register(this); |
| 128 | cfgService.registerConfigFactory(factory); |
| 129 | cfgService.addListener(cfgLister); |
Andrea Campanella | 7d8449b | 2016-03-02 10:16:42 -0800 | [diff] [blame] | 130 | executor.execute(RestDeviceProvider.this::connectDevices); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 131 | log.info("Started"); |
| 132 | } |
| 133 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 134 | @Deactivate |
| 135 | public void deactivate() { |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 136 | cfgService.removeListener(cfgLister); |
| 137 | controller.getDevices().keySet().forEach(this::deviceRemoved); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 138 | providerRegistry.unregister(this); |
| 139 | providerService = null; |
| 140 | cfgService.unregisterConfigFactory(factory); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 141 | log.info("Stopped"); |
| 142 | } |
| 143 | |
| 144 | public RestDeviceProvider() { |
| 145 | super(new ProviderId(REST, PROVIDER)); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void triggerProbe(DeviceId deviceId) { |
| 150 | // TODO: This will be implemented later. |
| 151 | log.info("Triggering probe on device {}", deviceId); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
| 156 | // TODO: This will be implemented later. |
| 157 | } |
| 158 | |
| 159 | |
| 160 | @Override |
| 161 | public boolean isReachable(DeviceId deviceId) { |
| 162 | RestSBDevice restDevice = controller.getDevice(deviceId); |
| 163 | if (restDevice == null) { |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 164 | log.debug("the requested device id: " + |
| 165 | deviceId.toString() + |
| 166 | " is not associated to any REST Device"); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | return restDevice.isActive(); |
| 170 | } |
| 171 | |
| 172 | private void deviceAdded(RestSBDevice nodeId) { |
| 173 | Preconditions.checkNotNull(nodeId, ISNOTNULL); |
| 174 | DeviceId deviceId = nodeId.deviceId(); |
| 175 | ChassisId cid = new ChassisId(); |
| 176 | String ipAddress = nodeId.ip().toString(); |
| 177 | SparseAnnotations annotations = DefaultAnnotations.builder() |
Marc De Leenheer | b0d131c | 2016-03-01 20:34:59 -0800 | [diff] [blame] | 178 | .set(IPADDRESS, ipAddress) |
| 179 | .set(AnnotationKeys.PROTOCOL, REST.toUpperCase()) |
| 180 | .build(); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 181 | DeviceDescription deviceDescription = new DefaultDeviceDescription( |
| 182 | deviceId.uri(), |
| 183 | Device.Type.SWITCH, |
| 184 | UNKNOWN, UNKNOWN, |
| 185 | UNKNOWN, UNKNOWN, |
| 186 | cid, |
| 187 | annotations); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 188 | nodeId.setActive(true); |
Andrea Campanella | c6ecc63 | 2016-03-10 17:57:06 -0800 | [diff] [blame] | 189 | providerService.deviceConnected(deviceId, deviceDescription); |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 190 | checkAndUpdateDevice(deviceId); |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 191 | addedDevices.add(deviceId); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 194 | private void checkAndUpdateDevice(DeviceId deviceId) { |
| 195 | if (deviceService.getDevice(deviceId) == null) { |
| 196 | log.warn("Device {} has not been added to store, " + |
| 197 | "maybe due to a problem in connectivity", deviceId); |
| 198 | } else { |
| 199 | boolean isReachable = isReachable(deviceId); |
| 200 | if (isReachable && deviceService.isAvailable(deviceId)) { |
| 201 | Device device = deviceService.getDevice(deviceId); |
| 202 | if (device.is(DeviceDescriptionDiscovery.class)) { |
| 203 | DeviceDescriptionDiscovery deviceDescriptionDiscovery = |
| 204 | device.as(DeviceDescriptionDiscovery.class); |
| 205 | DeviceDescription updatedDeviceDescription = |
| 206 | deviceDescriptionDiscovery.discoverDeviceDetails(); |
| 207 | if (updatedDeviceDescription != null && |
| 208 | !descriptionEquals(device, updatedDeviceDescription)) { |
| 209 | providerService.deviceConnected( |
| 210 | deviceId, |
| 211 | new DefaultDeviceDescription( |
| 212 | updatedDeviceDescription, true, |
| 213 | updatedDeviceDescription.annotations())); |
| 214 | //if ports are not discovered, retry the discovery |
| 215 | if (deviceService.getPorts(deviceId).isEmpty()) { |
| 216 | discoverPorts(deviceId); |
| 217 | } |
| 218 | } |
| 219 | } else { |
| 220 | log.warn("No DeviceDescriptionDiscovery behaviour for device {}", deviceId); |
| 221 | } |
| 222 | } else if (!isReachable && deviceService.isAvailable(deviceId)) { |
| 223 | providerService.deviceDisconnected(deviceId); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | private boolean descriptionEquals(Device device, DeviceDescription updatedDeviceDescription) { |
Michele Santuari | d2c8f21 | 2017-01-09 18:23:45 +0100 | [diff] [blame^] | 229 | return Objects.equal(device.id().uri(), updatedDeviceDescription.deviceUri()) |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 230 | && Objects.equal(device.type(), updatedDeviceDescription.type()) |
| 231 | && Objects.equal(device.manufacturer(), updatedDeviceDescription.manufacturer()) |
| 232 | && Objects.equal(device.hwVersion(), updatedDeviceDescription.hwVersion()) |
| 233 | && Objects.equal(device.swVersion(), updatedDeviceDescription.swVersion()) |
| 234 | && Objects.equal(device.serialNumber(), updatedDeviceDescription.serialNumber()) |
| 235 | && Objects.equal(device.chassisId(), updatedDeviceDescription.chassisId()) |
| 236 | && Objects.equal(device.annotations(), updatedDeviceDescription.annotations()); |
| 237 | } |
| 238 | |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 239 | private void deviceRemoved(DeviceId deviceId) { |
| 240 | Preconditions.checkNotNull(deviceId, ISNOTNULL); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 241 | providerService.deviceDisconnected(deviceId); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 242 | controller.removeDevice(deviceId); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | private void connectDevices() { |
| 246 | RestProviderConfig cfg = cfgService.getConfig(appId, RestProviderConfig.class); |
| 247 | try { |
| 248 | if (cfg != null && cfg.getDevicesAddresses() != null) { |
| 249 | //Precomputing the devices to be removed |
| 250 | Set<RestSBDevice> toBeRemoved = new HashSet<>(controller.getDevices().values()); |
| 251 | toBeRemoved.removeAll(cfg.getDevicesAddresses()); |
| 252 | //Adding new devices |
| 253 | cfg.getDevicesAddresses().stream() |
Andrea Campanella | c6ecc63 | 2016-03-10 17:57:06 -0800 | [diff] [blame] | 254 | .filter(device -> { |
| 255 | device.setActive(false); |
| 256 | controller.addDevice(device); |
| 257 | return testDeviceConnection(device); |
| 258 | }) |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 259 | .forEach(device -> { |
| 260 | deviceAdded(device); |
| 261 | }); |
| 262 | //Removing devices not wanted anymore |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 263 | toBeRemoved.forEach(device -> deviceRemoved(device.deviceId())); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 264 | } |
| 265 | } catch (ConfigException e) { |
| 266 | log.error("Configuration error {}", e); |
| 267 | } |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 268 | log.debug("REST Devices {}", controller.getDevices()); |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 269 | addedDevices.clear(); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 272 | private void discoverPorts(DeviceId deviceId) { |
| 273 | Device device = deviceService.getDevice(deviceId); |
| 274 | //TODO remove when PortDiscovery is removed from master |
| 275 | if (device.is(PortDiscovery.class)) { |
| 276 | PortDiscovery portConfig = device.as(PortDiscovery.class); |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 277 | providerService.updatePorts(deviceId, portConfig.getPorts()); |
| 278 | } else { |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 279 | DeviceDescriptionDiscovery deviceDescriptionDiscovery = |
| 280 | device.as(DeviceDescriptionDiscovery.class); |
| 281 | providerService.updatePorts(deviceId, deviceDescriptionDiscovery.discoverPortDetails()); |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 285 | private boolean testDeviceConnection(RestSBDevice device) { |
| 286 | try { |
Konstantinos Kanonakis | 3cd8555 | 2016-11-17 10:11:13 -0600 | [diff] [blame] | 287 | return controller.get(device.deviceId(), "", JSON) != null; |
Andrea Campanella | c6ecc63 | 2016-03-10 17:57:06 -0800 | [diff] [blame] | 288 | } catch (ProcessingException e) { |
| 289 | log.warn("Cannot connect to device {}", device, e); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 290 | } |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 295 | @Override |
| 296 | public void event(NetworkConfigEvent event) { |
Andrea Campanella | 90f044f | 2016-03-02 09:14:57 -0800 | [diff] [blame] | 297 | executor.execute(RestDeviceProvider.this::connectDevices); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public boolean isRelevant(NetworkConfigEvent event) { |
| 302 | //TODO refactor |
| 303 | return event.configClass().equals(RestProviderConfig.class) && |
| 304 | (event.type() == CONFIG_ADDED || |
| 305 | event.type() == CONFIG_UPDATED); |
| 306 | } |
| 307 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 308 | |
| 309 | @Override |
| 310 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 311 | boolean enable) { |
| 312 | // TODO if required |
| 313 | } |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 314 | } |