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