Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 16 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 17 | package org.onosproject.provider.netconf.device.impl; |
| 18 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.onlab.packet.ChassisId; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.incubator.net.config.basics.ConfigException; |
Marc De Leenheer | b0d131c | 2016-03-01 20:34:59 -0800 | [diff] [blame] | 29 | import org.onosproject.net.AnnotationKeys; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 30 | import org.onosproject.net.DefaultAnnotations; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 31 | import org.onosproject.net.Device; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.MastershipRole; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 34 | import org.onosproject.net.SparseAnnotations; |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 35 | import org.onosproject.net.behaviour.PortDiscovery; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 36 | import org.onosproject.net.config.ConfigFactory; |
| 37 | import org.onosproject.net.config.NetworkConfigEvent; |
| 38 | import org.onosproject.net.config.NetworkConfigListener; |
| 39 | import org.onosproject.net.config.NetworkConfigRegistry; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 40 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 41 | import org.onosproject.net.device.DeviceDescription; |
| 42 | import org.onosproject.net.device.DeviceProvider; |
| 43 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 44 | import org.onosproject.net.device.DeviceProviderService; |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 45 | import org.onosproject.net.device.DeviceService; |
| 46 | import org.onosproject.net.driver.DriverService; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 47 | import org.onosproject.net.provider.AbstractProvider; |
| 48 | import org.onosproject.net.provider.ProviderId; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 49 | import org.onosproject.netconf.NetconfController; |
| 50 | import org.onosproject.netconf.NetconfDevice; |
| 51 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 52 | import org.onosproject.netconf.NetconfDeviceListener; |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 53 | import org.onosproject.netconf.NetconfException; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 56 | import java.io.IOException; |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 57 | import java.util.concurrent.ExecutorService; |
| 58 | import java.util.concurrent.Executors; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 59 | |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 60 | import static org.onlab.util.Tools.groupedThreads; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 61 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 62 | import static org.slf4j.LoggerFactory.getLogger; |
| 63 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 64 | /** |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 65 | * Provider which uses an NETCONF controller to detect device. |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 66 | */ |
| 67 | @Component(immediate = true) |
| 68 | public class NetconfDeviceProvider extends AbstractProvider |
| 69 | implements DeviceProvider { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 70 | private final Logger log = getLogger(getClass()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected DeviceProviderRegistry providerRegistry; |
| 74 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 76 | protected NetconfController controller; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 79 | protected NetworkConfigRegistry cfgService; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 80 | |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 82 | protected CoreService coreService; |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 83 | |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected DriverService driverService; |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected DeviceService deviceService; |
| 89 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 90 | private static final String APP_NAME = "org.onosproject.netconf"; |
| 91 | private static final String SCHEME_NAME = "netconf"; |
| 92 | private static final String DEVICE_PROVIDER_PACKAGE = "org.onosproject.netconf.provider.device"; |
| 93 | private static final String UNKNOWN = "unknown"; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 94 | |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 95 | private final ExecutorService executor = |
Andrea Campanella | 90f044f | 2016-03-02 09:14:57 -0800 | [diff] [blame] | 96 | Executors.newFixedThreadPool(5, groupedThreads("onos/netconfdeviceprovider", "device-installer-%d", log)); |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 97 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 98 | private DeviceProviderService providerService; |
| 99 | private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener(); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 100 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 101 | private final ConfigFactory factory = |
| 102 | new ConfigFactory<ApplicationId, NetconfProviderConfig>(APP_SUBJECT_FACTORY, |
| 103 | NetconfProviderConfig.class, |
| 104 | "devices", |
| 105 | true) { |
| 106 | @Override |
| 107 | public NetconfProviderConfig createConfig() { |
| 108 | return new NetconfProviderConfig(); |
| 109 | } |
| 110 | }; |
| 111 | private final NetworkConfigListener cfgLister = new InternalNetworkConfigListener(); |
| 112 | private ApplicationId appId; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 113 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 114 | |
| 115 | @Activate |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 116 | public void activate() { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 117 | providerService = providerRegistry.register(this); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 118 | appId = coreService.registerApplication(APP_NAME); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 119 | cfgService.registerConfigFactory(factory); |
| 120 | cfgService.addListener(cfgLister); |
| 121 | controller.addDeviceListener(innerNodeListener); |
Andrea Campanella | 7d8449b | 2016-03-02 10:16:42 -0800 | [diff] [blame] | 122 | executor.execute(NetconfDeviceProvider.this::connectDevices); |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 123 | log.info("Started"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 124 | } |
| 125 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 126 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 127 | @Deactivate |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 128 | public void deactivate() { |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame^] | 129 | controller.removeDeviceListener(innerNodeListener); |
| 130 | controller.getNetconfDevices().forEach(id -> |
| 131 | controller.removeDevice(controller.getDevicesMap().get(id) |
| 132 | .getDeviceInfo())); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 133 | providerRegistry.unregister(this); |
| 134 | providerService = null; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 135 | cfgService.unregisterConfigFactory(factory); |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 136 | log.info("Stopped"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 137 | } |
| 138 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 139 | public NetconfDeviceProvider() { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 140 | super(new ProviderId(SCHEME_NAME, DEVICE_PROVIDER_PACKAGE)); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void triggerProbe(DeviceId deviceId) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 145 | // TODO: This will be implemented later. |
| 146 | log.info("Triggering probe on device {}", deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 151 | // TODO: This will be implemented later. |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public boolean isReachable(DeviceId deviceId) { |
Andrea Campanella | 8e290c5 | 2016-01-12 15:54:50 -0800 | [diff] [blame] | 156 | NetconfDevice netconfDevice = controller.getNetconfDevice(deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 157 | if (netconfDevice == null) { |
Andrea Campanella | d264b49 | 2016-03-01 09:46:06 -0800 | [diff] [blame] | 158 | log.debug("Requested device id: {} is not associated to any " + |
| 159 | "NETCONF Device", deviceId.toString()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 160 | return false; |
| 161 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 162 | return netconfDevice.isActive(); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 163 | } |
| 164 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 165 | private class InnerNetconfDeviceListener implements NetconfDeviceListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 166 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 167 | private static final String IPADDRESS = "ipaddress"; |
| 168 | protected static final String ISNULL = "NetconfDeviceInfo is null"; |
| 169 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 170 | @Override |
| 171 | public void deviceAdded(NetconfDeviceInfo nodeId) { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 172 | Preconditions.checkNotNull(nodeId, ISNULL); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 173 | DeviceId deviceId = nodeId.getDeviceId(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 174 | //Netconf configuration object |
| 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, SCHEME_NAME.toUpperCase()) |
| 180 | .build(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 181 | DeviceDescription deviceDescription = new DefaultDeviceDescription( |
| 182 | deviceId.uri(), |
| 183 | Device.Type.SWITCH, |
| 184 | UNKNOWN, UNKNOWN, |
| 185 | UNKNOWN, UNKNOWN, |
| 186 | cid, |
| 187 | annotations); |
| 188 | providerService.deviceConnected(deviceId, deviceDescription); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 192 | public void deviceRemoved(NetconfDeviceInfo nodeId) { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 193 | Preconditions.checkNotNull(nodeId, ISNULL); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 194 | DeviceId deviceId = nodeId.getDeviceId(); |
| 195 | providerService.deviceDisconnected(deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 196 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 200 | private void connectDevices() { |
| 201 | NetconfProviderConfig cfg = cfgService.getConfig(appId, NetconfProviderConfig.class); |
| 202 | if (cfg != null) { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 203 | try { |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 204 | cfg.getDevicesAddresses().stream() |
| 205 | .forEach(addr -> { |
| 206 | try { |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 207 | NetconfDeviceInfo netconf = new NetconfDeviceInfo(addr.name(), |
| 208 | addr.password(), |
| 209 | addr.ip(), |
| 210 | addr.port()); |
| 211 | controller.connectDevice(netconf); |
| 212 | Device device = deviceService.getDevice(netconf.getDeviceId()); |
| 213 | if (device.is(PortDiscovery.class)) { |
| 214 | PortDiscovery portConfig = device.as(PortDiscovery.class); |
| 215 | if (portConfig != null) { |
| 216 | providerService.updatePorts(netconf.getDeviceId(), |
| 217 | portConfig.getPorts()); |
| 218 | } |
| 219 | } else { |
| 220 | log.warn("No portGetter behaviour for device {}", netconf.getDeviceId()); |
| 221 | } |
| 222 | |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 223 | } catch (IOException e) { |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 224 | throw new RuntimeException( |
| 225 | new NetconfException( |
| 226 | "Can't connect to NETCONF " + |
| 227 | "device on " + addr.ip() + |
| 228 | ":" + addr.port(), e)); |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | ); |
| 232 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 233 | } catch (ConfigException e) { |
| 234 | log.error("Cannot read config error " + e); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 235 | } |
| 236 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 237 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 238 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 239 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 240 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 241 | |
| 242 | @Override |
| 243 | public void event(NetworkConfigEvent event) { |
Andrea Campanella | 90f044f | 2016-03-02 09:14:57 -0800 | [diff] [blame] | 244 | executor.execute(NetconfDeviceProvider.this::connectDevices); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 245 | } |
| 246 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 247 | @Override |
| 248 | public boolean isRelevant(NetworkConfigEvent event) { |
| 249 | //TODO refactor |
| 250 | return event.configClass().equals(NetconfProviderConfig.class) && |
| 251 | (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 252 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | } |