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 | |
| 66 | private static final Logger log = getLogger(NetconfDeviceProvider.class); |
| 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) { |
| 105 | NetconfDeviceProvider.log.info("Netconf Device Provider Started"); |
| 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) { |
| 120 | NetconfDeviceProvider.log.error("Device builder did not terminate"); |
| 121 | } |
| 122 | deviceBuilder.shutdownNow(); |
| 123 | netconfDeviceMap.clear(); |
| 124 | providerRegistry.unregister(this); |
| 125 | providerService = null; |
| 126 | NetconfDeviceProvider.log.info("Stopped"); |
| 127 | } |
| 128 | |
| 129 | @Modified |
| 130 | public void modified(ComponentContext context) { |
| 131 | if (context == null) { |
| 132 | NetconfDeviceProvider.log.info("No configuration file"); |
| 133 | return; |
| 134 | } |
| 135 | Dictionary<?, ?> properties = context.getProperties(); |
| 136 | String deviceCfgValue = get(properties, "devConfigs"); |
| 137 | NetconfDeviceProvider.log |
| 138 | .info("Getting Device configuration from cfg file: " |
| 139 | + deviceCfgValue); |
| 140 | if (!isNullOrEmpty(deviceCfgValue)) { |
| 141 | addOrRemoveDevicesConfig(deviceCfgValue); |
| 142 | } else { |
| 143 | NetconfDeviceProvider.log |
| 144 | .info("Device Configuration value receiviced from the property 'devConfigs': " |
| 145 | + deviceCfgValue + ", is not valid"); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | private void addOrRemoveDevicesConfig(String deviceConfig) { |
| 150 | for (String deviceEntry : deviceConfig.split(",")) { |
| 151 | NetconfDevice device = processDeviceEntry(deviceEntry); |
| 152 | if (device != null) { |
| 153 | NetconfDeviceProvider.log.info("Device Detail: " + "username: " |
| 154 | + device.getUsername() + ", host: " |
| 155 | + device.getSshHost() + ", port: " |
| 156 | + device.getSshPort()); |
| 157 | if (device.isActive()) { |
| 158 | deviceBuilder.submit(new DeviceCreator(device, true)); |
| 159 | } else { |
| 160 | deviceBuilder.submit(new DeviceCreator(device, false)); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private NetconfDevice processDeviceEntry(String deviceEntry) { |
| 167 | if (deviceEntry == null) { |
| 168 | NetconfDeviceProvider.log |
| 169 | .info("No content for Device Entry, so cannot proceed further."); |
| 170 | return null; |
| 171 | } |
| 172 | NetconfDeviceProvider.log |
| 173 | .info("Trying to convert Device Entry String: " + deviceEntry |
| 174 | + " to a Netconf Device Object"); |
| 175 | NetconfDevice device = null; |
| 176 | try { |
| 177 | String userInfo = deviceEntry.substring(0, deviceEntry |
| 178 | .lastIndexOf('@')); |
| 179 | String hostInfo = deviceEntry.substring(deviceEntry |
| 180 | .lastIndexOf('@') + 1); |
| 181 | String[] infoSplit = userInfo.split(":"); |
| 182 | String username = infoSplit[0]; |
| 183 | String password = infoSplit[1]; |
| 184 | infoSplit = hostInfo.split(":"); |
| 185 | String hostIp = infoSplit[0]; |
| 186 | Integer hostPort; |
| 187 | try { |
| 188 | hostPort = Integer.parseInt(infoSplit[1]); |
| 189 | } catch (NumberFormatException nfe) { |
| 190 | NetconfDeviceProvider.log |
| 191 | .error("Bad Configuration Data: Failed to parse host port number string: " |
| 192 | + infoSplit[1]); |
| 193 | throw nfe; |
| 194 | } |
| 195 | String deviceState = infoSplit[2]; |
| 196 | if (isNullOrEmpty(username) || isNullOrEmpty(password) |
| 197 | || isNullOrEmpty(hostIp) || hostPort == 0) { |
| 198 | NetconfDeviceProvider.log |
| 199 | .warn("Bad Configuration Data: both user and device information parts of Configuration " |
| 200 | + deviceEntry + " should be non-nullable"); |
| 201 | } else { |
| 202 | device = new NetconfDevice(hostIp, hostPort, username, password); |
| 203 | if (!isNullOrEmpty(deviceState)) { |
| 204 | if (deviceState.toUpperCase().equals(DeviceState.ACTIVE |
| 205 | .name())) { |
| 206 | device.setDeviceState(DeviceState.ACTIVE); |
| 207 | } else if (deviceState.toUpperCase() |
| 208 | .equals(DeviceState.INACTIVE.name())) { |
| 209 | device.setDeviceState(DeviceState.ACTIVE); |
| 210 | } else { |
| 211 | NetconfDeviceProvider.log |
| 212 | .warn("Device State Information can not be empty, so marking the state as INVALID"); |
| 213 | device.setDeviceState(DeviceState.INVALID); |
| 214 | } |
| 215 | } else { |
| 216 | NetconfDeviceProvider.log |
| 217 | .warn("The device entry do not specify state information, so marking the state as INVALID"); |
| 218 | device.setDeviceState(DeviceState.INVALID); |
| 219 | } |
| 220 | } |
| 221 | } catch (ArrayIndexOutOfBoundsException aie) { |
| 222 | NetconfDeviceProvider.log |
| 223 | .error("Error while reading config infromation from the config file: " |
| 224 | + "The user, host and device state infomation should be " |
| 225 | + "in the order 'userInfo@hostInfo:deviceState'" |
| 226 | + deviceEntry, aie); |
| 227 | } catch (Exception e) { |
| 228 | NetconfDeviceProvider.log |
| 229 | .error("Error while parsing config information for the device entry: " |
| 230 | + deviceEntry, e); |
| 231 | } |
| 232 | return device; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public void triggerProbe(DeviceId deviceId) { |
| 237 | // TODO Auto-generated method stub |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
| 242 | |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public boolean isReachable(DeviceId deviceId) { |
| 247 | NetconfDevice netconfDevice = netconfDeviceMap.get(deviceId); |
| 248 | if (netconfDevice == null) { |
| 249 | NetconfDeviceProvider.log |
| 250 | .warn("BAD REQUEST: the requested device id: " |
| 251 | + deviceId.toString() |
| 252 | + " is not associated to any NETCONF Device"); |
| 253 | return false; |
| 254 | } |
| 255 | return netconfDevice.isReachable(); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * This class is intended to add or remove Configured Netconf Devices. |
| 260 | * Functionality relies on 'createFlag' and 'NetconfDevice' content. The |
| 261 | * functionality runs as a thread and dependening on the 'createFlag' value |
| 262 | * it will create or remove Device entry from the core. |
| 263 | */ |
| 264 | private class DeviceCreator implements Runnable { |
| 265 | |
| 266 | private NetconfDevice device; |
| 267 | private boolean createFlag; |
| 268 | |
| 269 | public DeviceCreator(NetconfDevice device, boolean createFlag) { |
| 270 | this.device = device; |
| 271 | this.createFlag = createFlag; |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public void run() { |
| 276 | if (createFlag && (device.getDeviceState() == DeviceState.ACTIVE)) { |
| 277 | NetconfDeviceProvider.log |
| 278 | .info("Trying to create Device Info on ONOS core"); |
| 279 | advertiseDevices(); |
| 280 | } else { |
| 281 | NetconfDeviceProvider.log |
| 282 | .info("Trying to remove Device Info on ONOS core"); |
| 283 | removeDevices(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * For each Netconf Device, remove the entry from the device store. |
| 289 | */ |
| 290 | private void removeDevices() { |
| 291 | if (!device.isReachable()) { |
| 292 | log.error("BAD Request: 'Currently device is not discovered, so cannot remove/disconnect the device: " |
| 293 | + device.deviceInfo() + "'"); |
| 294 | return; |
| 295 | } |
| 296 | try { |
| 297 | DeviceId did = getDeviceId(); |
| 298 | providerService.deviceDisconnected(did); |
| 299 | device.disconnect(); |
| 300 | delay(EVENTINTERVAL); |
| 301 | } catch (URISyntaxException uriSyntaxExcpetion) { |
| 302 | NetconfDeviceProvider.log |
| 303 | .error("Syntax Error while creating URI for the device: " |
| 304 | + device.deviceInfo() |
| 305 | + " couldn't remove the device from the store", |
| 306 | uriSyntaxExcpetion); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Initialize Netconf Device object, and notify core saying device |
| 312 | * connected. |
| 313 | */ |
| 314 | private void advertiseDevices() { |
| 315 | try { |
| 316 | if (device == null) { |
| 317 | NetconfDeviceProvider.log |
| 318 | .warn("The Request Netconf Device is null, cannot proceed further"); |
| 319 | return; |
| 320 | } |
| 321 | device.init(); |
| 322 | DeviceId did = getDeviceId(); |
| 323 | ChassisId cid = new ChassisId(); |
| 324 | DeviceDescription desc = new DefaultDeviceDescription( |
| 325 | did.uri(), |
| 326 | Device.Type.OTHER, |
| 327 | "", "", |
| 328 | "", "", |
| 329 | cid); |
| 330 | if (NetconfDeviceProvider.log.isDebugEnabled()) { |
| 331 | NetconfDeviceProvider.log.debug("Persisting Device" |
| 332 | + did.uri().toString()); |
| 333 | } |
| 334 | |
| 335 | netconfDeviceMap.put(did, device); |
| 336 | providerService.deviceConnected(did, desc); |
| 337 | if (NetconfDeviceProvider.log.isDebugEnabled()) { |
| 338 | NetconfDeviceProvider.log |
| 339 | .debug("Done with Device Info Creation on ONOS core. Device Info: " |
| 340 | + device.deviceInfo() |
| 341 | + " " |
| 342 | + did.uri().toString()); |
| 343 | } |
| 344 | delay(EVENTINTERVAL); |
| 345 | } catch (URISyntaxException e) { |
| 346 | NetconfDeviceProvider.log |
| 347 | .error("Syntax Error while creating URI for the device: " |
| 348 | + device.deviceInfo() |
| 349 | + " couldn't persist the device onto the store", |
| 350 | e); |
| 351 | } catch (Exception e) { |
| 352 | NetconfDeviceProvider.log |
| 353 | .error("Error while initializing session for the device: " |
| 354 | + device.deviceInfo(), e); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * This will build a device id for the device. |
| 360 | */ |
| 361 | private DeviceId getDeviceId() throws URISyntaxException { |
| 362 | String additionalSSP = new StringBuilder(device.getUsername()) |
| 363 | .append("@").append(device.getSshHost()).append(":") |
| 364 | .append(device.getSshPort()).toString(); |
| 365 | DeviceId did = DeviceId.deviceId(new URI(SCHEME, additionalSSP, |
| 366 | null)); |
| 367 | return did; |
| 368 | } |
| 369 | } |
| 370 | } |