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 | */ |
| 16 | package org.onosproject.provider.netconf.device.impl; |
| 17 | |
| 18 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 19 | import static org.onlab.util.Tools.delay; |
| 20 | import static org.onlab.util.Tools.get; |
| 21 | import static org.onlab.util.Tools.groupedThreads; |
| 22 | import static org.slf4j.LoggerFactory.getLogger; |
| 23 | |
| 24 | import java.net.URI; |
| 25 | import java.net.URISyntaxException; |
| 26 | import java.util.Dictionary; |
| 27 | import java.util.Map; |
| 28 | import java.util.Map.Entry; |
| 29 | import java.util.concurrent.ConcurrentHashMap; |
| 30 | import java.util.concurrent.ExecutorService; |
| 31 | import java.util.concurrent.Executors; |
| 32 | import java.util.concurrent.TimeUnit; |
| 33 | |
| 34 | import org.apache.felix.scr.annotations.Activate; |
| 35 | import org.apache.felix.scr.annotations.Component; |
| 36 | import org.apache.felix.scr.annotations.Deactivate; |
| 37 | import org.apache.felix.scr.annotations.Modified; |
| 38 | import org.apache.felix.scr.annotations.Property; |
| 39 | import org.apache.felix.scr.annotations.Reference; |
| 40 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 41 | import org.onlab.packet.ChassisId; |
| 42 | import org.onosproject.cluster.ClusterService; |
| 43 | import org.onosproject.net.Device; |
| 44 | import org.onosproject.net.DeviceId; |
| 45 | import org.onosproject.net.MastershipRole; |
| 46 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 47 | import org.onosproject.net.device.DeviceDescription; |
| 48 | import org.onosproject.net.device.DeviceProvider; |
| 49 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 50 | import org.onosproject.net.device.DeviceProviderService; |
| 51 | import org.onosproject.net.device.DeviceService; |
| 52 | import org.onosproject.net.provider.AbstractProvider; |
| 53 | import org.onosproject.net.provider.ProviderId; |
| 54 | import org.onosproject.provider.netconf.device.impl.NetconfDevice.DeviceState; |
| 55 | import org.osgi.service.component.ComponentContext; |
| 56 | import org.slf4j.Logger; |
| 57 | |
| 58 | /** |
| 59 | * Provider which will try to fetch the details of NETCONF devices from the core |
| 60 | * and run a capability discovery on each of the device. |
| 61 | */ |
| 62 | @Component(immediate = true) |
| 63 | public class NetconfDeviceProvider extends AbstractProvider |
| 64 | implements DeviceProvider { |
| 65 | |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 66 | private final Logger log = getLogger(NetconfDeviceProvider.class); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 67 | |
| 68 | private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<DeviceId, NetconfDevice>(); |
| 69 | |
| 70 | private DeviceProviderService providerService; |
| 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected DeviceProviderRegistry providerRegistry; |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected DeviceService deviceService; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected ClusterService clusterService; |
| 80 | |
| 81 | private ExecutorService deviceBuilder = Executors |
| 82 | .newFixedThreadPool(1, |
| 83 | groupedThreads("onos/netconf", "device-creator")); |
| 84 | |
| 85 | // Delay between events in ms. |
| 86 | private static final int EVENTINTERVAL = 5; |
| 87 | |
| 88 | private static final String SCHEME = "netconf"; |
| 89 | |
| 90 | @Property(name = "devConfigs", value = "", label = "Instance-specific configurations") |
| 91 | private String devConfigs = null; |
| 92 | |
| 93 | @Property(name = "devPasswords", value = "", label = "Instace-specific password") |
| 94 | private String devPasswords = null; |
| 95 | |
| 96 | /** |
| 97 | * Creates a provider with the supplier identifier. |
| 98 | */ |
| 99 | public NetconfDeviceProvider() { |
| 100 | super(new ProviderId("netconf", "org.onosproject.provider.netconf")); |
| 101 | } |
| 102 | |
| 103 | @Activate |
| 104 | public void activate(ComponentContext context) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 105 | log.info("Netconf Device Provider Started"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 106 | providerService = providerRegistry.register(this); |
| 107 | modified(context); |
| 108 | } |
| 109 | |
| 110 | @Deactivate |
| 111 | public void deactivate(ComponentContext context) { |
| 112 | try { |
| 113 | for (Entry<DeviceId, NetconfDevice> deviceEntry : netconfDeviceMap |
| 114 | .entrySet()) { |
| 115 | deviceBuilder.submit(new DeviceCreator(deviceEntry.getValue(), |
| 116 | false)); |
| 117 | } |
| 118 | deviceBuilder.awaitTermination(1000, TimeUnit.MILLISECONDS); |
| 119 | } catch (InterruptedException e) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 120 | log.error("Device builder did not terminate"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 121 | } |
| 122 | deviceBuilder.shutdownNow(); |
| 123 | netconfDeviceMap.clear(); |
| 124 | providerRegistry.unregister(this); |
| 125 | providerService = null; |
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 | |
| 129 | @Modified |
| 130 | public void modified(ComponentContext context) { |
| 131 | if (context == null) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 132 | log.info("No configuration file"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | Dictionary<?, ?> properties = context.getProperties(); |
| 136 | String deviceCfgValue = get(properties, "devConfigs"); |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 137 | log.info("Getting Device configuration from cfg file: " |
| 138 | + deviceCfgValue); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 139 | if (!isNullOrEmpty(deviceCfgValue)) { |
| 140 | addOrRemoveDevicesConfig(deviceCfgValue); |
| 141 | } else { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 142 | log.info("Device Configuration value receiviced from the property 'devConfigs': " |
| 143 | + deviceCfgValue + ", is not valid"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | private void addOrRemoveDevicesConfig(String deviceConfig) { |
| 148 | for (String deviceEntry : deviceConfig.split(",")) { |
| 149 | NetconfDevice device = processDeviceEntry(deviceEntry); |
| 150 | if (device != null) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 151 | log.info("Device Detail: " + "username: " |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 152 | + device.getUsername() + ", host: " |
| 153 | + device.getSshHost() + ", port: " |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 154 | + device.getSshPort() + " device state: " |
| 155 | + device.getDeviceState().name()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 156 | if (device.isActive()) { |
| 157 | deviceBuilder.submit(new DeviceCreator(device, true)); |
| 158 | } else { |
| 159 | deviceBuilder.submit(new DeviceCreator(device, false)); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private NetconfDevice processDeviceEntry(String deviceEntry) { |
| 166 | if (deviceEntry == null) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 167 | log.info("No content for Device Entry, so cannot proceed further."); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 168 | return null; |
| 169 | } |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 170 | log.info("Trying to convert Device Entry String: " + deviceEntry |
| 171 | + " to a Netconf Device Object"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 172 | NetconfDevice device = null; |
| 173 | try { |
| 174 | String userInfo = deviceEntry.substring(0, deviceEntry |
| 175 | .lastIndexOf('@')); |
| 176 | String hostInfo = deviceEntry.substring(deviceEntry |
| 177 | .lastIndexOf('@') + 1); |
| 178 | String[] infoSplit = userInfo.split(":"); |
| 179 | String username = infoSplit[0]; |
| 180 | String password = infoSplit[1]; |
| 181 | infoSplit = hostInfo.split(":"); |
| 182 | String hostIp = infoSplit[0]; |
| 183 | Integer hostPort; |
| 184 | try { |
| 185 | hostPort = Integer.parseInt(infoSplit[1]); |
| 186 | } catch (NumberFormatException nfe) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 187 | log.error("Bad Configuration Data: Failed to parse host port number string: " |
| 188 | + infoSplit[1]); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 189 | throw nfe; |
| 190 | } |
| 191 | String deviceState = infoSplit[2]; |
| 192 | if (isNullOrEmpty(username) || isNullOrEmpty(password) |
| 193 | || isNullOrEmpty(hostIp) || hostPort == 0) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 194 | log.warn("Bad Configuration Data: both user and device information parts of Configuration " |
| 195 | + deviceEntry + " should be non-nullable"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 196 | } else { |
| 197 | device = new NetconfDevice(hostIp, hostPort, username, password); |
| 198 | if (!isNullOrEmpty(deviceState)) { |
| 199 | if (deviceState.toUpperCase().equals(DeviceState.ACTIVE |
| 200 | .name())) { |
| 201 | device.setDeviceState(DeviceState.ACTIVE); |
| 202 | } else if (deviceState.toUpperCase() |
| 203 | .equals(DeviceState.INACTIVE.name())) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 204 | device.setDeviceState(DeviceState.INACTIVE); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 205 | } else { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 206 | log.warn("Device State Information can not be empty, so marking the state as INVALID"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 207 | device.setDeviceState(DeviceState.INVALID); |
| 208 | } |
| 209 | } else { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 210 | log.warn("The device entry do not specify state information, so marking the state as INVALID"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 211 | device.setDeviceState(DeviceState.INVALID); |
| 212 | } |
| 213 | } |
| 214 | } catch (ArrayIndexOutOfBoundsException aie) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 215 | log.error("Error while reading config infromation from the config file: " |
| 216 | + "The user, host and device state infomation should be " |
| 217 | + "in the order 'userInfo@hostInfo:deviceState'" |
| 218 | + deviceEntry, aie); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 219 | } catch (Exception e) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 220 | log.error("Error while parsing config information for the device entry: " |
| 221 | + deviceEntry, e); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 222 | } |
| 223 | return device; |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void triggerProbe(DeviceId deviceId) { |
| 228 | // TODO Auto-generated method stub |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
| 233 | |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public boolean isReachable(DeviceId deviceId) { |
| 238 | NetconfDevice netconfDevice = netconfDeviceMap.get(deviceId); |
| 239 | if (netconfDevice == null) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 240 | log.warn("BAD REQUEST: the requested device id: " |
| 241 | + deviceId.toString() |
| 242 | + " is not associated to any NETCONF Device"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 243 | return false; |
| 244 | } |
| 245 | return netconfDevice.isReachable(); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * This class is intended to add or remove Configured Netconf Devices. |
| 250 | * Functionality relies on 'createFlag' and 'NetconfDevice' content. The |
| 251 | * functionality runs as a thread and dependening on the 'createFlag' value |
| 252 | * it will create or remove Device entry from the core. |
| 253 | */ |
| 254 | private class DeviceCreator implements Runnable { |
| 255 | |
| 256 | private NetconfDevice device; |
| 257 | private boolean createFlag; |
| 258 | |
| 259 | public DeviceCreator(NetconfDevice device, boolean createFlag) { |
| 260 | this.device = device; |
| 261 | this.createFlag = createFlag; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void run() { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 266 | if (createFlag) { |
| 267 | log.info("Trying to create Device Info on ONOS core"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 268 | advertiseDevices(); |
| 269 | } else { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 270 | log.info("Trying to remove Device Info on ONOS core"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 271 | removeDevices(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * For each Netconf Device, remove the entry from the device store. |
| 277 | */ |
| 278 | private void removeDevices() { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 279 | if (device == null) { |
| 280 | log.warn("The Request Netconf Device is null, cannot proceed further"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 281 | return; |
| 282 | } |
| 283 | try { |
| 284 | DeviceId did = getDeviceId(); |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 285 | if (!netconfDeviceMap.containsKey(did)) { |
| 286 | log.error("BAD Request: 'Currently device is not discovered, " |
| 287 | + "so cannot remove/disconnect the device: " |
| 288 | + device.deviceInfo() + "'"); |
| 289 | return; |
| 290 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 291 | providerService.deviceDisconnected(did); |
| 292 | device.disconnect(); |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 293 | netconfDeviceMap.remove(did); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 294 | delay(EVENTINTERVAL); |
| 295 | } catch (URISyntaxException uriSyntaxExcpetion) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 296 | log.error("Syntax Error while creating URI for the device: " |
| 297 | + device.deviceInfo() |
| 298 | + " couldn't remove the device from the store", |
| 299 | uriSyntaxExcpetion); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Initialize Netconf Device object, and notify core saying device |
| 305 | * connected. |
| 306 | */ |
| 307 | private void advertiseDevices() { |
| 308 | try { |
| 309 | if (device == null) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 310 | log.warn("The Request Netconf Device is null, cannot proceed further"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 311 | return; |
| 312 | } |
| 313 | device.init(); |
| 314 | DeviceId did = getDeviceId(); |
| 315 | ChassisId cid = new ChassisId(); |
| 316 | DeviceDescription desc = new DefaultDeviceDescription( |
| 317 | did.uri(), |
| 318 | Device.Type.OTHER, |
| 319 | "", "", |
| 320 | "", "", |
| 321 | cid); |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 322 | log.info("Persisting Device" + did.uri().toString()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 323 | |
| 324 | netconfDeviceMap.put(did, device); |
| 325 | providerService.deviceConnected(did, desc); |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 326 | log.info("Done with Device Info Creation on ONOS core. Device Info: " |
| 327 | + device.deviceInfo() + " " + did.uri().toString()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 328 | delay(EVENTINTERVAL); |
| 329 | } catch (URISyntaxException e) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 330 | log.error("Syntax Error while creating URI for the device: " |
| 331 | + device.deviceInfo() |
| 332 | + " couldn't persist the device onto the store", e); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 333 | } catch (Exception e) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 334 | log.error("Error while initializing session for the device: " |
| 335 | + device.deviceInfo(), e); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * This will build a device id for the device. |
| 341 | */ |
| 342 | private DeviceId getDeviceId() throws URISyntaxException { |
| 343 | String additionalSSP = new StringBuilder(device.getUsername()) |
| 344 | .append("@").append(device.getSshHost()).append(":") |
| 345 | .append(device.getSshPort()).toString(); |
| 346 | DeviceId did = DeviceId.deviceId(new URI(SCHEME, additionalSSP, |
| 347 | null)); |
| 348 | return did; |
| 349 | } |
| 350 | } |
| 351 | } |