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