blob: 9a8c1bdfaa3a0039290fcf1f08ba289f61c424a4 [file] [log] [blame]
Sanjay Se8dcfee2015-04-23 10:07:08 +05301/*
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 */
16package org.onosproject.provider.netconf.device.impl;
17
18import static com.google.common.base.Strings.isNullOrEmpty;
19import static org.onlab.util.Tools.delay;
20import static org.onlab.util.Tools.get;
21import static org.onlab.util.Tools.groupedThreads;
22import static org.slf4j.LoggerFactory.getLogger;
23
Sanjay S59f86fd2015-05-20 12:23:48 +053024import java.io.IOException;
25import java.net.SocketTimeoutException;
Sanjay Se8dcfee2015-04-23 10:07:08 +053026import java.net.URI;
27import java.net.URISyntaxException;
28import java.util.Dictionary;
29import java.util.Map;
30import java.util.Map.Entry;
31import java.util.concurrent.ConcurrentHashMap;
32import java.util.concurrent.ExecutorService;
33import java.util.concurrent.Executors;
34import java.util.concurrent.TimeUnit;
35
36import org.apache.felix.scr.annotations.Activate;
37import org.apache.felix.scr.annotations.Component;
38import org.apache.felix.scr.annotations.Deactivate;
39import org.apache.felix.scr.annotations.Modified;
40import org.apache.felix.scr.annotations.Property;
41import org.apache.felix.scr.annotations.Reference;
42import org.apache.felix.scr.annotations.ReferenceCardinality;
43import org.onlab.packet.ChassisId;
Thomas Vachuskad6811712015-04-29 21:37:04 -070044import org.onosproject.cfg.ComponentConfigService;
Sanjay Se8dcfee2015-04-23 10:07:08 +053045import org.onosproject.cluster.ClusterService;
46import org.onosproject.net.Device;
47import org.onosproject.net.DeviceId;
48import org.onosproject.net.MastershipRole;
49import org.onosproject.net.device.DefaultDeviceDescription;
50import org.onosproject.net.device.DeviceDescription;
51import org.onosproject.net.device.DeviceProvider;
52import org.onosproject.net.device.DeviceProviderRegistry;
53import org.onosproject.net.device.DeviceProviderService;
54import org.onosproject.net.device.DeviceService;
55import org.onosproject.net.provider.AbstractProvider;
56import org.onosproject.net.provider.ProviderId;
57import org.onosproject.provider.netconf.device.impl.NetconfDevice.DeviceState;
58import org.osgi.service.component.ComponentContext;
59import org.slf4j.Logger;
60
61/**
62 * Provider which will try to fetch the details of NETCONF devices from the core
63 * and run a capability discovery on each of the device.
64 */
65@Component(immediate = true)
66public class NetconfDeviceProvider extends AbstractProvider
67 implements DeviceProvider {
68
Sanjay Seb5eebb2015-04-24 15:44:50 +053069 private final Logger log = getLogger(NetconfDeviceProvider.class);
Sanjay Se8dcfee2015-04-23 10:07:08 +053070
Sanjay S0d4093f2015-05-06 14:55:04 +053071 protected Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<DeviceId, NetconfDevice>();
Sanjay Se8dcfee2015-04-23 10:07:08 +053072
73 private DeviceProviderService providerService;
74
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected DeviceProviderRegistry providerRegistry;
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected DeviceService deviceService;
80
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected ClusterService clusterService;
83
Thomas Vachuskad6811712015-04-29 21:37:04 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected ComponentConfigService cfgService;
86
Sanjay Se8dcfee2015-04-23 10:07:08 +053087 private ExecutorService deviceBuilder = Executors
Thomas Vachuskad6811712015-04-29 21:37:04 -070088 .newFixedThreadPool(1, groupedThreads("onos/netconf", "device-creator"));
Sanjay Se8dcfee2015-04-23 10:07:08 +053089
90 // Delay between events in ms.
91 private static final int EVENTINTERVAL = 5;
92
93 private static final String SCHEME = "netconf";
94
95 @Property(name = "devConfigs", value = "", label = "Instance-specific configurations")
96 private String devConfigs = null;
97
Thomas Vachuskad6811712015-04-29 21:37:04 -070098 @Property(name = "devPasswords", value = "", label = "Instance-specific password")
Sanjay Se8dcfee2015-04-23 10:07:08 +053099 private String devPasswords = null;
100
101 /**
102 * Creates a provider with the supplier identifier.
103 */
104 public NetconfDeviceProvider() {
105 super(new ProviderId("netconf", "org.onosproject.provider.netconf"));
106 }
107
108 @Activate
109 public void activate(ComponentContext context) {
Thomas Vachuskad6811712015-04-29 21:37:04 -0700110 cfgService.registerProperties(getClass());
Sanjay Se8dcfee2015-04-23 10:07:08 +0530111 providerService = providerRegistry.register(this);
112 modified(context);
Thomas Vachuskad6811712015-04-29 21:37:04 -0700113 log.info("Started");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530114 }
115
116 @Deactivate
117 public void deactivate(ComponentContext context) {
Thomas Vachuskad6811712015-04-29 21:37:04 -0700118 cfgService.unregisterProperties(getClass(), false);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530119 try {
120 for (Entry<DeviceId, NetconfDevice> deviceEntry : netconfDeviceMap
121 .entrySet()) {
122 deviceBuilder.submit(new DeviceCreator(deviceEntry.getValue(),
123 false));
124 }
125 deviceBuilder.awaitTermination(1000, TimeUnit.MILLISECONDS);
126 } catch (InterruptedException e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530127 log.error("Device builder did not terminate");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530128 }
129 deviceBuilder.shutdownNow();
130 netconfDeviceMap.clear();
131 providerRegistry.unregister(this);
132 providerService = null;
Sanjay Seb5eebb2015-04-24 15:44:50 +0530133 log.info("Stopped");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530134 }
135
136 @Modified
137 public void modified(ComponentContext context) {
138 if (context == null) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530139 log.info("No configuration file");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530140 return;
141 }
142 Dictionary<?, ?> properties = context.getProperties();
143 String deviceCfgValue = get(properties, "devConfigs");
Thomas Vachuskad6811712015-04-29 21:37:04 -0700144 log.info("Settings: devConfigs={}", deviceCfgValue);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530145 if (!isNullOrEmpty(deviceCfgValue)) {
146 addOrRemoveDevicesConfig(deviceCfgValue);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530147 }
148 }
149
150 private void addOrRemoveDevicesConfig(String deviceConfig) {
151 for (String deviceEntry : deviceConfig.split(",")) {
152 NetconfDevice device = processDeviceEntry(deviceEntry);
153 if (device != null) {
Thomas Vachuskad6811712015-04-29 21:37:04 -0700154 log.info("Device Detail: username: {}, host={}, port={}, state={}",
155 device.getUsername(), device.getSshHost(),
156 device.getSshPort(), device.getDeviceState().name());
Sanjay Se8dcfee2015-04-23 10:07:08 +0530157 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) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530168 log.info("No content for Device Entry, so cannot proceed further.");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530169 return null;
170 }
Sanjay Seb5eebb2015-04-24 15:44:50 +0530171 log.info("Trying to convert Device Entry String: " + deviceEntry
172 + " to a Netconf Device Object");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530173 NetconfDevice device = null;
174 try {
175 String userInfo = deviceEntry.substring(0, deviceEntry
176 .lastIndexOf('@'));
177 String hostInfo = deviceEntry.substring(deviceEntry
178 .lastIndexOf('@') + 1);
179 String[] infoSplit = userInfo.split(":");
180 String username = infoSplit[0];
181 String password = infoSplit[1];
182 infoSplit = hostInfo.split(":");
183 String hostIp = infoSplit[0];
184 Integer hostPort;
185 try {
186 hostPort = Integer.parseInt(infoSplit[1]);
187 } catch (NumberFormatException nfe) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530188 log.error("Bad Configuration Data: Failed to parse host port number string: "
189 + infoSplit[1]);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530190 throw nfe;
191 }
192 String deviceState = infoSplit[2];
193 if (isNullOrEmpty(username) || isNullOrEmpty(password)
194 || isNullOrEmpty(hostIp) || hostPort == 0) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530195 log.warn("Bad Configuration Data: both user and device information parts of Configuration "
196 + deviceEntry + " should be non-nullable");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530197 } else {
198 device = new NetconfDevice(hostIp, hostPort, username, password);
199 if (!isNullOrEmpty(deviceState)) {
200 if (deviceState.toUpperCase().equals(DeviceState.ACTIVE
201 .name())) {
202 device.setDeviceState(DeviceState.ACTIVE);
203 } else if (deviceState.toUpperCase()
204 .equals(DeviceState.INACTIVE.name())) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530205 device.setDeviceState(DeviceState.INACTIVE);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530206 } else {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530207 log.warn("Device State Information can not be empty, so marking the state as INVALID");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530208 device.setDeviceState(DeviceState.INVALID);
209 }
210 } else {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530211 log.warn("The device entry do not specify state information, so marking the state as INVALID");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530212 device.setDeviceState(DeviceState.INVALID);
213 }
214 }
215 } catch (ArrayIndexOutOfBoundsException aie) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530216 log.error("Error while reading config infromation from the config file: "
217 + "The user, host and device state infomation should be "
218 + "in the order 'userInfo@hostInfo:deviceState'"
219 + deviceEntry, aie);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530220 } catch (Exception e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530221 log.error("Error while parsing config information for the device entry: "
222 + deviceEntry, e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530223 }
224 return device;
225 }
226
227 @Override
228 public void triggerProbe(DeviceId deviceId) {
229 // TODO Auto-generated method stub
230 }
231
232 @Override
233 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
234
235 }
236
237 @Override
238 public boolean isReachable(DeviceId deviceId) {
239 NetconfDevice netconfDevice = netconfDeviceMap.get(deviceId);
240 if (netconfDevice == null) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530241 log.warn("BAD REQUEST: the requested device id: "
242 + deviceId.toString()
243 + " is not associated to any NETCONF Device");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530244 return false;
245 }
246 return netconfDevice.isReachable();
247 }
248
249 /**
250 * This class is intended to add or remove Configured Netconf Devices.
251 * Functionality relies on 'createFlag' and 'NetconfDevice' content. The
252 * functionality runs as a thread and dependening on the 'createFlag' value
253 * it will create or remove Device entry from the core.
254 */
255 private class DeviceCreator implements Runnable {
256
257 private NetconfDevice device;
258 private boolean createFlag;
259
260 public DeviceCreator(NetconfDevice device, boolean createFlag) {
261 this.device = device;
262 this.createFlag = createFlag;
263 }
264
265 @Override
266 public void run() {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530267 if (createFlag) {
268 log.info("Trying to create Device Info on ONOS core");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530269 advertiseDevices();
270 } else {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530271 log.info("Trying to remove Device Info on ONOS core");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530272 removeDevices();
273 }
274 }
275
276 /**
277 * For each Netconf Device, remove the entry from the device store.
278 */
279 private void removeDevices() {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530280 if (device == null) {
281 log.warn("The Request Netconf Device is null, cannot proceed further");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530282 return;
283 }
284 try {
285 DeviceId did = getDeviceId();
Sanjay Seb5eebb2015-04-24 15:44:50 +0530286 if (!netconfDeviceMap.containsKey(did)) {
287 log.error("BAD Request: 'Currently device is not discovered, "
288 + "so cannot remove/disconnect the device: "
289 + device.deviceInfo() + "'");
290 return;
291 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530292 providerService.deviceDisconnected(did);
293 device.disconnect();
Sanjay Seb5eebb2015-04-24 15:44:50 +0530294 netconfDeviceMap.remove(did);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530295 delay(EVENTINTERVAL);
296 } catch (URISyntaxException uriSyntaxExcpetion) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530297 log.error("Syntax Error while creating URI for the device: "
298 + device.deviceInfo()
299 + " couldn't remove the device from the store",
300 uriSyntaxExcpetion);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530301 }
302 }
303
304 /**
305 * Initialize Netconf Device object, and notify core saying device
306 * connected.
307 */
308 private void advertiseDevices() {
309 try {
310 if (device == null) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530311 log.warn("The Request Netconf Device is null, cannot proceed further");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530312 return;
313 }
314 device.init();
315 DeviceId did = getDeviceId();
316 ChassisId cid = new ChassisId();
317 DeviceDescription desc = new DefaultDeviceDescription(
318 did.uri(),
319 Device.Type.OTHER,
320 "", "",
321 "", "",
322 cid);
Sanjay Seb5eebb2015-04-24 15:44:50 +0530323 log.info("Persisting Device" + did.uri().toString());
Sanjay Se8dcfee2015-04-23 10:07:08 +0530324
325 netconfDeviceMap.put(did, device);
326 providerService.deviceConnected(did, desc);
Sanjay Seb5eebb2015-04-24 15:44:50 +0530327 log.info("Done with Device Info Creation on ONOS core. Device Info: "
328 + device.deviceInfo() + " " + did.uri().toString());
Sanjay Se8dcfee2015-04-23 10:07:08 +0530329 delay(EVENTINTERVAL);
330 } catch (URISyntaxException e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530331 log.error("Syntax Error while creating URI for the device: "
332 + device.deviceInfo()
333 + " couldn't persist the device onto the store", e);
Sanjay S59f86fd2015-05-20 12:23:48 +0530334 } catch (SocketTimeoutException e) {
335 log.error("Error while setting connection for the device: "
336 + device.deviceInfo(), e);
337 } catch (IOException e) {
338 log.error("Error while setting connection for the device: "
339 + device.deviceInfo(), e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530340 } catch (Exception e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530341 log.error("Error while initializing session for the device: "
342 + device.deviceInfo(), e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530343 }
344 }
345
346 /**
347 * This will build a device id for the device.
348 */
349 private DeviceId getDeviceId() throws URISyntaxException {
350 String additionalSSP = new StringBuilder(device.getUsername())
351 .append("@").append(device.getSshHost()).append(":")
352 .append(device.getSshPort()).toString();
353 DeviceId did = DeviceId.deviceId(new URI(SCHEME, additionalSSP,
354 null));
355 return did;
356 }
357 }
358}