Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -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 | package org.onosproject.provider.tl1.device.impl; |
| 17 | |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onlab.packet.ChassisId; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.incubator.net.config.basics.ConfigException; |
| 28 | import org.onosproject.net.AnnotationKeys; |
| 29 | import org.onosproject.net.DefaultAnnotations; |
| 30 | import org.onosproject.net.Device; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.MastershipRole; |
| 33 | import org.onosproject.net.PortNumber; |
| 34 | import org.onosproject.net.SparseAnnotations; |
| 35 | import org.onosproject.net.config.ConfigFactory; |
| 36 | import org.onosproject.net.config.NetworkConfigEvent; |
| 37 | import org.onosproject.net.config.NetworkConfigListener; |
| 38 | import org.onosproject.net.config.NetworkConfigRegistry; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 39 | import org.onosproject.net.config.basics.SubjectFactories; |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 40 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 41 | import org.onosproject.net.device.DeviceAdminService; |
| 42 | import org.onosproject.net.device.DeviceDescription; |
| 43 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 44 | import org.onosproject.net.device.DeviceProvider; |
| 45 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 46 | import org.onosproject.net.device.DeviceProviderService; |
| 47 | import org.onosproject.net.device.DeviceService; |
| 48 | import org.onosproject.net.provider.AbstractProvider; |
| 49 | import org.onosproject.net.provider.ProviderId; |
Yuta HIGUCHI | c635835 | 2017-06-23 11:56:27 -0700 | [diff] [blame] | 50 | import org.onosproject.tl1.DefaultTl1Device; |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 51 | import org.onosproject.tl1.Tl1Controller; |
| 52 | import org.onosproject.tl1.Tl1Device; |
| 53 | import org.onosproject.tl1.Tl1Listener; |
Yuta HIGUCHI | c635835 | 2017-06-23 11:56:27 -0700 | [diff] [blame] | 54 | import org.onosproject.tl1.device.Tl1DeviceConfig; |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 55 | import org.slf4j.Logger; |
| 56 | |
| 57 | import java.io.IOException; |
| 58 | import java.net.InetSocketAddress; |
| 59 | import java.net.Socket; |
| 60 | import java.net.URI; |
| 61 | import java.net.URISyntaxException; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 62 | import java.util.List; |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 63 | import java.util.NoSuchElementException; |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 64 | import java.util.Set; |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 65 | |
| 66 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 67 | import static org.slf4j.LoggerFactory.getLogger; |
| 68 | |
| 69 | /** |
| 70 | * Device provider for TL1 devices. |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 71 | * <p> |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 72 | * Sits between ONOS provider service and the TL1 controller. |
| 73 | * Relies on network config subsystem to know about devices. |
| 74 | */ |
| 75 | @Component(immediate = true) |
| 76 | public class Tl1DeviceProvider extends AbstractProvider implements DeviceProvider { |
| 77 | private static final String APP_NAME = "org.onosproject.tl1"; |
Yuta HIGUCHI | c635835 | 2017-06-23 11:56:27 -0700 | [diff] [blame] | 78 | /** |
| 79 | * @deprecated in 1.11.0. Use {@link Tl1DeviceConfig#TL1} instead |
| 80 | */ |
| 81 | @Deprecated |
| 82 | protected static final String TL1 = Tl1DeviceConfig.TL1; |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 83 | private static final String PROVIDER = "org.onosproject.provider.tl1.device"; |
| 84 | private static final String UNKNOWN = "unknown"; |
| 85 | private static final int REACHABILITY_TIMEOUT = 2000; // in milliseconds |
| 86 | |
| 87 | private final Logger log = getLogger(getClass()); |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected NetworkConfigRegistry cfgRegistry; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected CoreService coreService; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected DeviceProviderRegistry providerRegistry; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected DeviceAdminService deviceAdminService; |
| 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected DeviceService deviceService; |
| 103 | |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected Tl1Controller controller; |
| 106 | |
| 107 | private ApplicationId appId; |
| 108 | private NetworkConfigListener cfgListener = new InnerConfigListener(); |
| 109 | private Tl1Listener tl1Listener = new InnerTl1Listener(); |
| 110 | private DeviceProviderService providerService; |
| 111 | |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 112 | private final List<ConfigFactory> factories = ImmutableList.of( |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 113 | new ConfigFactory<ApplicationId, Tl1ProviderConfig>(APP_SUBJECT_FACTORY, |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 114 | Tl1ProviderConfig.class, |
| 115 | "tl1_devices", |
| 116 | true) { |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 117 | @Override |
| 118 | public Tl1ProviderConfig createConfig() { |
| 119 | return new Tl1ProviderConfig(); |
| 120 | } |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 121 | }, |
| 122 | new ConfigFactory<DeviceId, Tl1DeviceConfig>(SubjectFactories.DEVICE_SUBJECT_FACTORY, |
| 123 | Tl1DeviceConfig.class, |
Yuta HIGUCHI | c635835 | 2017-06-23 11:56:27 -0700 | [diff] [blame] | 124 | Tl1DeviceConfig.TL1) { |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 125 | @Override |
| 126 | public Tl1DeviceConfig createConfig() { |
| 127 | return new Tl1DeviceConfig(); |
| 128 | } |
| 129 | }); |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 130 | |
| 131 | @Activate |
| 132 | public void activate() { |
| 133 | appId = coreService.registerApplication(APP_NAME); |
| 134 | providerService = providerRegistry.register(this); |
| 135 | cfgRegistry.addListener(cfgListener); |
| 136 | controller.addListener(tl1Listener); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 137 | factories.forEach(cfgRegistry::registerConfigFactory); |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 138 | registerDevices(); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 139 | connectDevices(); |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 140 | log.info("Started"); |
| 141 | } |
| 142 | |
| 143 | @Deactivate |
| 144 | public void deactivate() { |
| 145 | controller.removeListener(tl1Listener); |
| 146 | cfgRegistry.removeListener(cfgListener); |
| 147 | controller.getDeviceIds().forEach(deviceId -> { |
| 148 | controller.removeDevice(deviceId); |
| 149 | deviceAdminService.removeDevice(deviceId); |
| 150 | }); |
| 151 | providerRegistry.unregister(this); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 152 | factories.forEach(cfgRegistry::unregisterConfigFactory); |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 153 | providerService = null; |
| 154 | log.info("Stopped"); |
| 155 | } |
| 156 | |
| 157 | public Tl1DeviceProvider() { |
Yuta HIGUCHI | c635835 | 2017-06-23 11:56:27 -0700 | [diff] [blame] | 158 | super(new ProviderId(Tl1DeviceConfig.TL1, PROVIDER)); |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Yuta HIGUCHI | 08057cb | 2017-03-06 10:07:06 -0800 | [diff] [blame] | 161 | @Override |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 162 | public void triggerProbe(DeviceId deviceId) { |
| 163 | // TODO |
| 164 | } |
| 165 | |
Yuta HIGUCHI | 08057cb | 2017-03-06 10:07:06 -0800 | [diff] [blame] | 166 | @Override |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 167 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
| 168 | switch (newRole) { |
| 169 | case MASTER: |
| 170 | controller.connectDevice(deviceId); |
| 171 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.MASTER); |
| 172 | log.debug("Accepting mastership role change to {} for device {}", newRole, deviceId); |
| 173 | break; |
| 174 | case STANDBY: |
| 175 | controller.disconnectDevice(deviceId); |
| 176 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.STANDBY); |
| 177 | break; |
| 178 | case NONE: |
| 179 | controller.disconnectDevice(deviceId); |
| 180 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.NONE); |
| 181 | break; |
| 182 | default: |
| 183 | log.error("Invalid mastership state: {}", newRole); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // Assumes device is registered in TL1 controller. |
Yuta HIGUCHI | 08057cb | 2017-03-06 10:07:06 -0800 | [diff] [blame] | 188 | @Override |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 189 | public boolean isReachable(DeviceId deviceId) { |
| 190 | try { |
| 191 | // First check if device is already connected. |
| 192 | // If not, try to open a socket. |
| 193 | Tl1Device device = controller.getDevice(deviceId).get(); |
| 194 | if (device.isConnected()) { |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | Socket socket = new Socket(); |
| 199 | socket.connect(new InetSocketAddress(device.ip().toInetAddress(), device.port()), REACHABILITY_TIMEOUT); |
| 200 | socket.close(); |
| 201 | return true; |
| 202 | } catch (NoSuchElementException | IOException | IllegalArgumentException e) { |
| 203 | log.error("Cannot reach device {}", deviceId, e); |
| 204 | return false; |
| 205 | } |
| 206 | } |
| 207 | |
Yuta HIGUCHI | 08057cb | 2017-03-06 10:07:06 -0800 | [diff] [blame] | 208 | @Override |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 209 | public void changePortState(DeviceId deviceId, PortNumber portNumber, boolean enable) { |
| 210 | // TODO |
| 211 | } |
| 212 | |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 213 | //Old method to register devices provided via net-cfg under apps/tl1/ tree |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 214 | void registerDevices() { |
| 215 | Tl1ProviderConfig cfg = cfgRegistry.getConfig(appId, Tl1ProviderConfig.class); |
| 216 | |
| 217 | if (cfg == null) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | try { |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 222 | cfg.readDevices().forEach(this::connectDevice); |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 223 | } catch (ConfigException e) { |
| 224 | log.error("Cannot parse network configuration", e); |
| 225 | } |
| 226 | } |
| 227 | |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 228 | //Method to register devices provided via net-cfg under devices/ tree |
| 229 | private void connectDevices() { |
| 230 | Set<DeviceId> deviceSubjects = |
| 231 | cfgRegistry.getSubjects(DeviceId.class, Tl1DeviceConfig.class); |
| 232 | deviceSubjects.forEach(deviceId -> { |
| 233 | Tl1DeviceConfig config = |
| 234 | cfgRegistry.getConfig(deviceId, Tl1DeviceConfig.class); |
| 235 | connectDevice(new DefaultTl1Device(config.ip(), config.port(), config.username(), |
| 236 | config.password())); |
| 237 | }); |
| 238 | } |
| 239 | |
| 240 | // Register a device in the core and in the TL1 controller. |
| 241 | private void connectDevice(Tl1Device device) { |
| 242 | try { |
| 243 | // Add device to TL1 controller |
| 244 | DeviceId deviceId = DeviceId.deviceId( |
Yuta HIGUCHI | c635835 | 2017-06-23 11:56:27 -0700 | [diff] [blame] | 245 | new URI(Tl1DeviceConfig.TL1, device.ip() + ":" + device.port(), null)); |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 246 | |
| 247 | if (controller.addDevice(deviceId, device)) { |
| 248 | SparseAnnotations ann = DefaultAnnotations.builder() |
Yuta HIGUCHI | c635835 | 2017-06-23 11:56:27 -0700 | [diff] [blame] | 249 | .set(AnnotationKeys.PROTOCOL, Tl1DeviceConfig.TL1.toUpperCase()) |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 250 | .build(); |
| 251 | // Register device in the core with default parameters and mark it as unavailable |
| 252 | DeviceDescription dd = new DefaultDeviceDescription(deviceId.uri(), |
| 253 | Device.Type.SWITCH, |
| 254 | UNKNOWN, UNKNOWN, |
| 255 | UNKNOWN, UNKNOWN, |
| 256 | new ChassisId(), |
| 257 | false, ann); |
| 258 | providerService.deviceConnected(deviceId, dd); |
| 259 | } |
| 260 | } catch (URISyntaxException e) { |
| 261 | log.error("Skipping device {}", device, e); |
| 262 | } |
| 263 | } |
| 264 | |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 265 | /** |
| 266 | * Tries to update the device and port descriptions through the {@code DeviceDescriptionDiscovery} behaviour. |
| 267 | * |
| 268 | * @param deviceId the device |
| 269 | */ |
| 270 | void updateDevice(DeviceId deviceId) { |
| 271 | Device device = deviceService.getDevice(deviceId); |
| 272 | |
| 273 | if (!device.is(DeviceDescriptionDiscovery.class)) { |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | try { |
| 278 | // Update device description |
| 279 | DeviceDescriptionDiscovery discovery = device.as(DeviceDescriptionDiscovery.class); |
| 280 | DeviceDescription dd = discovery.discoverDeviceDetails(); |
| 281 | if (dd == null) { |
| 282 | return; |
| 283 | } |
| 284 | providerService.deviceConnected(deviceId, |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 285 | new DefaultDeviceDescription(dd, true, dd.annotations())); |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 286 | // Update ports |
| 287 | providerService.updatePorts(deviceId, discovery.discoverPortDetails()); |
| 288 | } catch (IllegalStateException | IllegalArgumentException e) { |
| 289 | log.error("Cannot update device description {}", deviceId, e); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Listener for network configuration events. |
| 295 | */ |
| 296 | private class InnerConfigListener implements NetworkConfigListener { |
Yuta HIGUCHI | 08057cb | 2017-03-06 10:07:06 -0800 | [diff] [blame] | 297 | @Override |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 298 | public void event(NetworkConfigEvent event) { |
| 299 | if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) { |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 300 | if (event.configClass().equals(Tl1DeviceConfig.class)) { |
| 301 | connectDevices(); |
| 302 | } else { |
| 303 | log.warn("Injecting device via this Json is deprecated, " + |
| 304 | "please put configuration under devices/"); |
| 305 | registerDevices(); |
| 306 | } |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 307 | } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) { |
| 308 | // TODO: calculate delta |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 309 | if (event.configClass().equals(Tl1DeviceConfig.class)) { |
| 310 | connectDevices(); |
| 311 | } else { |
| 312 | log.warn("Injecting device via this Json is deprecated, " + |
| 313 | "please put configuration under devices/"); |
| 314 | registerDevices(); |
| 315 | } |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 316 | } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) { |
| 317 | controller.getDeviceIds().forEach(deviceId -> { |
| 318 | controller.removeDevice(deviceId); |
| 319 | deviceAdminService.removeDevice(deviceId); |
| 320 | }); |
| 321 | } |
| 322 | } |
| 323 | |
Yuta HIGUCHI | 08057cb | 2017-03-06 10:07:06 -0800 | [diff] [blame] | 324 | @Override |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 325 | public boolean isRelevant(NetworkConfigEvent event) { |
Andrea Campanella | 59b549d | 2017-04-14 21:58:16 +0200 | [diff] [blame] | 326 | return (event.configClass().equals(Tl1DeviceConfig.class) || |
| 327 | event.configClass().equals(Tl1ProviderConfig.class)) && |
Marc De Leenheer | 57a5af0 | 2016-12-02 20:54:41 -0800 | [diff] [blame] | 328 | (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 329 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED || |
| 330 | event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Listener for TL1 events. |
| 336 | */ |
| 337 | private class InnerTl1Listener implements Tl1Listener { |
| 338 | @Override |
| 339 | public void deviceConnected(DeviceId deviceId) { |
| 340 | updateDevice(deviceId); |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public void deviceDisconnected(DeviceId deviceId) { |
| 345 | providerService.deviceDisconnected(deviceId); |
| 346 | } |
| 347 | } |
| 348 | } |