blob: 9336adb9d4d850f1117fbd03c0e7a8b00bbbf182 [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
24import java.net.URI;
25import java.net.URISyntaxException;
26import java.util.Dictionary;
27import java.util.Map;
28import java.util.Map.Entry;
29import java.util.concurrent.ConcurrentHashMap;
30import java.util.concurrent.ExecutorService;
31import java.util.concurrent.Executors;
32import java.util.concurrent.TimeUnit;
33
34import org.apache.felix.scr.annotations.Activate;
35import org.apache.felix.scr.annotations.Component;
36import org.apache.felix.scr.annotations.Deactivate;
37import org.apache.felix.scr.annotations.Modified;
38import org.apache.felix.scr.annotations.Property;
39import org.apache.felix.scr.annotations.Reference;
40import org.apache.felix.scr.annotations.ReferenceCardinality;
41import org.onlab.packet.ChassisId;
42import org.onosproject.cluster.ClusterService;
43import org.onosproject.net.Device;
44import org.onosproject.net.DeviceId;
45import org.onosproject.net.MastershipRole;
46import org.onosproject.net.device.DefaultDeviceDescription;
47import org.onosproject.net.device.DeviceDescription;
48import org.onosproject.net.device.DeviceProvider;
49import org.onosproject.net.device.DeviceProviderRegistry;
50import org.onosproject.net.device.DeviceProviderService;
51import org.onosproject.net.device.DeviceService;
52import org.onosproject.net.provider.AbstractProvider;
53import org.onosproject.net.provider.ProviderId;
54import org.onosproject.provider.netconf.device.impl.NetconfDevice.DeviceState;
55import org.osgi.service.component.ComponentContext;
56import 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)
63public class NetconfDeviceProvider extends AbstractProvider
64 implements DeviceProvider {
65
Sanjay Seb5eebb2015-04-24 15:44:50 +053066 private final Logger log = getLogger(NetconfDeviceProvider.class);
Sanjay Se8dcfee2015-04-23 10:07:08 +053067
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 Seb5eebb2015-04-24 15:44:50 +0530105 log.info("Netconf Device Provider Started");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530106 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 Seb5eebb2015-04-24 15:44:50 +0530120 log.error("Device builder did not terminate");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530121 }
122 deviceBuilder.shutdownNow();
123 netconfDeviceMap.clear();
124 providerRegistry.unregister(this);
125 providerService = null;
Sanjay Seb5eebb2015-04-24 15:44:50 +0530126 log.info("Stopped");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530127 }
128
129 @Modified
130 public void modified(ComponentContext context) {
131 if (context == null) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530132 log.info("No configuration file");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530133 return;
134 }
135 Dictionary<?, ?> properties = context.getProperties();
136 String deviceCfgValue = get(properties, "devConfigs");
Sanjay Seb5eebb2015-04-24 15:44:50 +0530137 log.info("Getting Device configuration from cfg file: "
138 + deviceCfgValue);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530139 if (!isNullOrEmpty(deviceCfgValue)) {
140 addOrRemoveDevicesConfig(deviceCfgValue);
141 } else {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530142 log.info("Device Configuration value receiviced from the property 'devConfigs': "
143 + deviceCfgValue + ", is not valid");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530144 }
145 }
146
147 private void addOrRemoveDevicesConfig(String deviceConfig) {
148 for (String deviceEntry : deviceConfig.split(",")) {
149 NetconfDevice device = processDeviceEntry(deviceEntry);
150 if (device != null) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530151 log.info("Device Detail: " + "username: "
Sanjay Se8dcfee2015-04-23 10:07:08 +0530152 + device.getUsername() + ", host: "
153 + device.getSshHost() + ", port: "
Sanjay Seb5eebb2015-04-24 15:44:50 +0530154 + device.getSshPort() + " device state: "
155 + device.getDeviceState().name());
Sanjay Se8dcfee2015-04-23 10:07:08 +0530156 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 Seb5eebb2015-04-24 15:44:50 +0530167 log.info("No content for Device Entry, so cannot proceed further.");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530168 return null;
169 }
Sanjay Seb5eebb2015-04-24 15:44:50 +0530170 log.info("Trying to convert Device Entry String: " + deviceEntry
171 + " to a Netconf Device Object");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530172 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 Seb5eebb2015-04-24 15:44:50 +0530187 log.error("Bad Configuration Data: Failed to parse host port number string: "
188 + infoSplit[1]);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530189 throw nfe;
190 }
191 String deviceState = infoSplit[2];
192 if (isNullOrEmpty(username) || isNullOrEmpty(password)
193 || isNullOrEmpty(hostIp) || hostPort == 0) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530194 log.warn("Bad Configuration Data: both user and device information parts of Configuration "
195 + deviceEntry + " should be non-nullable");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530196 } 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 Seb5eebb2015-04-24 15:44:50 +0530204 device.setDeviceState(DeviceState.INACTIVE);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530205 } else {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530206 log.warn("Device State Information can not be empty, so marking the state as INVALID");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530207 device.setDeviceState(DeviceState.INVALID);
208 }
209 } else {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530210 log.warn("The device entry do not specify state information, so marking the state as INVALID");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530211 device.setDeviceState(DeviceState.INVALID);
212 }
213 }
214 } catch (ArrayIndexOutOfBoundsException aie) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530215 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 Se8dcfee2015-04-23 10:07:08 +0530219 } catch (Exception e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530220 log.error("Error while parsing config information for the device entry: "
221 + deviceEntry, e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530222 }
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 Seb5eebb2015-04-24 15:44:50 +0530240 log.warn("BAD REQUEST: the requested device id: "
241 + deviceId.toString()
242 + " is not associated to any NETCONF Device");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530243 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 Seb5eebb2015-04-24 15:44:50 +0530266 if (createFlag) {
267 log.info("Trying to create Device Info on ONOS core");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530268 advertiseDevices();
269 } else {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530270 log.info("Trying to remove Device Info on ONOS core");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530271 removeDevices();
272 }
273 }
274
275 /**
276 * For each Netconf Device, remove the entry from the device store.
277 */
278 private void removeDevices() {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530279 if (device == null) {
280 log.warn("The Request Netconf Device is null, cannot proceed further");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530281 return;
282 }
283 try {
284 DeviceId did = getDeviceId();
Sanjay Seb5eebb2015-04-24 15:44:50 +0530285 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 Se8dcfee2015-04-23 10:07:08 +0530291 providerService.deviceDisconnected(did);
292 device.disconnect();
Sanjay Seb5eebb2015-04-24 15:44:50 +0530293 netconfDeviceMap.remove(did);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530294 delay(EVENTINTERVAL);
295 } catch (URISyntaxException uriSyntaxExcpetion) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530296 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 Se8dcfee2015-04-23 10:07:08 +0530300 }
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 Seb5eebb2015-04-24 15:44:50 +0530310 log.warn("The Request Netconf Device is null, cannot proceed further");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530311 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 Seb5eebb2015-04-24 15:44:50 +0530322 log.info("Persisting Device" + did.uri().toString());
Sanjay Se8dcfee2015-04-23 10:07:08 +0530323
324 netconfDeviceMap.put(did, device);
325 providerService.deviceConnected(did, desc);
Sanjay Seb5eebb2015-04-24 15:44:50 +0530326 log.info("Done with Device Info Creation on ONOS core. Device Info: "
327 + device.deviceInfo() + " " + did.uri().toString());
Sanjay Se8dcfee2015-04-23 10:07:08 +0530328 delay(EVENTINTERVAL);
329 } catch (URISyntaxException e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530330 log.error("Syntax Error while creating URI for the device: "
331 + device.deviceInfo()
332 + " couldn't persist the device onto the store", e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530333 } catch (Exception e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530334 log.error("Error while initializing session for the device: "
335 + device.deviceInfo(), e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530336 }
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}