blob: dc75d84badcea3b17bc768aac2fc5f5929bf2d6d [file] [log] [blame]
Sanjay Se8dcfee2015-04-23 10:07:08 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sanjay Se8dcfee2015-04-23 10:07:08 +05303 *
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 */
andreaeb70a942015-10-16 21:34:46 -070016
Sanjay Se8dcfee2015-04-23 10:07:08 +053017package org.onosproject.provider.netconf.device.impl;
18
Michele Santuari00cc1f72016-09-08 17:05:24 +020019import com.google.common.base.Objects;
andreaeb70a942015-10-16 21:34:46 -070020import com.google.common.base.Preconditions;
Sanjay Se8dcfee2015-04-23 10:07:08 +053021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
Sanjay Se8dcfee2015-04-23 10:07:08 +053024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.onlab.packet.ChassisId;
andreaeb70a942015-10-16 21:34:46 -070027import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
29import org.onosproject.incubator.net.config.basics.ConfigException;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070030import org.onosproject.mastership.MastershipService;
Marc De Leenheerb0d131c2016-03-01 20:34:59 -080031import org.onosproject.net.AnnotationKeys;
andreaeb70a942015-10-16 21:34:46 -070032import org.onosproject.net.DefaultAnnotations;
Sanjay Se8dcfee2015-04-23 10:07:08 +053033import org.onosproject.net.Device;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.MastershipRole;
Saurav Dasa2d37502016-03-25 17:50:40 -070036import org.onosproject.net.PortNumber;
andreaeb70a942015-10-16 21:34:46 -070037import org.onosproject.net.SparseAnnotations;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080038import org.onosproject.net.behaviour.PortDiscovery;
andreaeb70a942015-10-16 21:34:46 -070039import org.onosproject.net.config.ConfigFactory;
40import org.onosproject.net.config.NetworkConfigEvent;
41import org.onosproject.net.config.NetworkConfigListener;
42import org.onosproject.net.config.NetworkConfigRegistry;
Sanjay Se8dcfee2015-04-23 10:07:08 +053043import org.onosproject.net.device.DefaultDeviceDescription;
44import org.onosproject.net.device.DeviceDescription;
Andrea Campanella6c71a052016-04-22 11:56:31 -070045import org.onosproject.net.device.DeviceDescriptionDiscovery;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070046import org.onosproject.net.device.DeviceEvent;
47import org.onosproject.net.device.DeviceListener;
Sanjay Se8dcfee2015-04-23 10:07:08 +053048import org.onosproject.net.device.DeviceProvider;
49import org.onosproject.net.device.DeviceProviderRegistry;
50import org.onosproject.net.device.DeviceProviderService;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080051import org.onosproject.net.device.DeviceService;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070052import org.onosproject.net.key.DeviceKey;
53import org.onosproject.net.key.DeviceKeyAdminService;
54import org.onosproject.net.key.DeviceKeyId;
Sanjay Se8dcfee2015-04-23 10:07:08 +053055import org.onosproject.net.provider.AbstractProvider;
56import org.onosproject.net.provider.ProviderId;
andreaeb70a942015-10-16 21:34:46 -070057import org.onosproject.netconf.NetconfController;
andreaeb70a942015-10-16 21:34:46 -070058import org.onosproject.netconf.NetconfDeviceListener;
Andrea Campanella8b1cb672016-01-25 13:58:58 -080059import org.onosproject.netconf.NetconfException;
Sanjay Se8dcfee2015-04-23 10:07:08 +053060import org.slf4j.Logger;
61
Andrea Campanella087ceb92015-12-07 09:58:34 -080062import java.io.IOException;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070063import java.net.Socket;
64import java.net.URI;
65import java.net.URISyntaxException;
66import java.util.Arrays;
Andrea Campanella5c999e22016-03-01 15:12:53 -080067import java.util.concurrent.ExecutorService;
68import java.util.concurrent.Executors;
helenyrwufd296b62016-06-22 17:43:02 -070069import java.util.concurrent.ScheduledExecutorService;
70import java.util.concurrent.ScheduledFuture;
71import java.util.concurrent.TimeUnit;
andreaeb70a942015-10-16 21:34:46 -070072
Yuta HIGUCHI1624df12016-07-21 16:54:33 -070073import static java.util.concurrent.Executors.newScheduledThreadPool;
Andrea Campanella5c999e22016-03-01 15:12:53 -080074import static org.onlab.util.Tools.groupedThreads;
andreaeb70a942015-10-16 21:34:46 -070075import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
76import static org.slf4j.LoggerFactory.getLogger;
77
Sanjay Se8dcfee2015-04-23 10:07:08 +053078/**
andreaeb70a942015-10-16 21:34:46 -070079 * Provider which uses an NETCONF controller to detect device.
Sanjay Se8dcfee2015-04-23 10:07:08 +053080 */
81@Component(immediate = true)
82public class NetconfDeviceProvider extends AbstractProvider
83 implements DeviceProvider {
Andrea Campanella7e6200a2016-03-21 09:48:40 -070084
andreaeb70a942015-10-16 21:34:46 -070085 private final Logger log = getLogger(getClass());
Sanjay Se8dcfee2015-04-23 10:07:08 +053086
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
88 protected DeviceProviderRegistry providerRegistry;
89
andreaeb70a942015-10-16 21:34:46 -070090 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella101417d2015-12-11 17:58:07 -080091 protected NetconfController controller;
Sanjay Se8dcfee2015-04-23 10:07:08 +053092
93 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -070094 protected NetworkConfigRegistry cfgService;
Sanjay Se8dcfee2015-04-23 10:07:08 +053095
Thomas Vachuskad6811712015-04-29 21:37:04 -070096 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -070097 protected CoreService coreService;
Thomas Vachuskad6811712015-04-29 21:37:04 -070098
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080099 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700100 protected DeviceService deviceService;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -0800101
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700103 protected DeviceKeyAdminService deviceKeyAdminService;
104
105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected MastershipService mastershipService;
107
Michele Santuari576f09c2016-09-28 14:20:00 +0200108 protected static final String APP_NAME = "org.onosproject.netconf";
Andrea Campanella101417d2015-12-11 17:58:07 -0800109 private static final String SCHEME_NAME = "netconf";
110 private static final String DEVICE_PROVIDER_PACKAGE = "org.onosproject.netconf.provider.device";
111 private static final String UNKNOWN = "unknown";
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700112 protected static final String ISNULL = "NetconfDeviceInfo is null";
113 private static final String IPADDRESS = "ipaddress";
114 private static final String NETCONF = "netconf";
115 private static final String PORT = "port";
helenyrwufd296b62016-06-22 17:43:02 -0700116 private static final int CORE_POOL_SIZE = 10;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700117 //FIXME eventually a property
118 private static final int ISREACHABLE_TIMEOUT = 2000;
helenyrwufd296b62016-06-22 17:43:02 -0700119 private static final int DEFAULT_POLL_FREQUENCY_SECONDS = 30;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530120
Michele Santuari576f09c2016-09-28 14:20:00 +0200121 protected final ExecutorService executor =
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700122 Executors.newFixedThreadPool(5, groupedThreads("onos/netconfdeviceprovider",
123 "device-installer-%d", log));
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700124 protected ScheduledExecutorService connectionExecutor
125 = newScheduledThreadPool(CORE_POOL_SIZE,
126 groupedThreads("onos/netconfdeviceprovider",
127 "connection-executor-%d", log));
Andrea Campanella5c999e22016-03-01 15:12:53 -0800128
Michele Santuari576f09c2016-09-28 14:20:00 +0200129 protected DeviceProviderService providerService;
andreaeb70a942015-10-16 21:34:46 -0700130 private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700131 private InternalDeviceListener deviceListener = new InternalDeviceListener();
Michele Santuari576f09c2016-09-28 14:20:00 +0200132 protected ScheduledFuture<?> scheduledTask;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530133
andreaeb70a942015-10-16 21:34:46 -0700134 private final ConfigFactory factory =
135 new ConfigFactory<ApplicationId, NetconfProviderConfig>(APP_SUBJECT_FACTORY,
136 NetconfProviderConfig.class,
137 "devices",
138 true) {
139 @Override
140 public NetconfProviderConfig createConfig() {
141 return new NetconfProviderConfig();
142 }
143 };
Michele Santuari576f09c2016-09-28 14:20:00 +0200144 protected final NetworkConfigListener cfgListener = new InternalNetworkConfigListener();
andreaeb70a942015-10-16 21:34:46 -0700145 private ApplicationId appId;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700146 private boolean active;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530147
Sanjay Se8dcfee2015-04-23 10:07:08 +0530148
149 @Activate
andreaeb70a942015-10-16 21:34:46 -0700150 public void activate() {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700151 active = true;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530152 providerService = providerRegistry.register(this);
Andrea Campanella101417d2015-12-11 17:58:07 -0800153 appId = coreService.registerApplication(APP_NAME);
andreaeb70a942015-10-16 21:34:46 -0700154 cfgService.registerConfigFactory(factory);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700155 cfgService.addListener(cfgListener);
andreaeb70a942015-10-16 21:34:46 -0700156 controller.addDeviceListener(innerNodeListener);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700157 deviceService.addListener(deviceListener);
Andrea Campanella7d8449b2016-03-02 10:16:42 -0800158 executor.execute(NetconfDeviceProvider.this::connectDevices);
helenyrwufd296b62016-06-22 17:43:02 -0700159 scheduledTask = schedulePolling();
Thomas Vachuskad6811712015-04-29 21:37:04 -0700160 log.info("Started");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530161 }
162
andreaeb70a942015-10-16 21:34:46 -0700163
Sanjay Se8dcfee2015-04-23 10:07:08 +0530164 @Deactivate
andreaeb70a942015-10-16 21:34:46 -0700165 public void deactivate() {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700166 deviceService.removeListener(deviceListener);
167 active = false;
168 controller.getNetconfDevices().forEach(id -> {
169 deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(id.toString()));
170 controller.disconnectDevice(id, true);
171 });
Andrea Campanella86294db2016-03-07 11:42:49 -0800172 controller.removeDeviceListener(innerNodeListener);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700173 deviceService.removeListener(deviceListener);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530174 providerRegistry.unregister(this);
175 providerService = null;
andreaeb70a942015-10-16 21:34:46 -0700176 cfgService.unregisterConfigFactory(factory);
helenyrwufd296b62016-06-22 17:43:02 -0700177 scheduledTask.cancel(true);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700178 executor.shutdown();
Sanjay Seb5eebb2015-04-24 15:44:50 +0530179 log.info("Stopped");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530180 }
181
andreaeb70a942015-10-16 21:34:46 -0700182 public NetconfDeviceProvider() {
Andrea Campanella101417d2015-12-11 17:58:07 -0800183 super(new ProviderId(SCHEME_NAME, DEVICE_PROVIDER_PACKAGE));
Sanjay Se8dcfee2015-04-23 10:07:08 +0530184 }
185
helenyrwufd296b62016-06-22 17:43:02 -0700186 // Checks connection to devices in the config file
187 // every DEFAULT_POLL_FREQUENCY_SECONDS seconds.
188 private ScheduledFuture schedulePolling() {
189 return connectionExecutor.scheduleAtFixedRate(this::checkAndUpdateDevices,
190 DEFAULT_POLL_FREQUENCY_SECONDS / 10,
191 DEFAULT_POLL_FREQUENCY_SECONDS,
192 TimeUnit.SECONDS);
193 }
194
Sanjay Se8dcfee2015-04-23 10:07:08 +0530195 @Override
196 public void triggerProbe(DeviceId deviceId) {
andreaeb70a942015-10-16 21:34:46 -0700197 // TODO: This will be implemented later.
198 log.info("Triggering probe on device {}", deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530199 }
200
201 @Override
202 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700203 if (active) {
204 switch (newRole) {
205 case MASTER:
206 initiateConnection(deviceId, newRole);
207 log.debug("Accepting mastership role change to {} for device {}", newRole, deviceId);
208 break;
209 case STANDBY:
210 controller.disconnectDevice(deviceId, false);
211 providerService.receivedRoleReply(deviceId, newRole, MastershipRole.STANDBY);
212 //else no-op
213 break;
214 case NONE:
215 controller.disconnectDevice(deviceId, false);
216 providerService.receivedRoleReply(deviceId, newRole, MastershipRole.NONE);
217 break;
218 default:
219 log.error("Unimplemented Mastership state : {}", newRole);
220
221 }
222 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530223 }
224
225 @Override
226 public boolean isReachable(DeviceId deviceId) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700227 //FIXME this is a workaround util device state is shared
228 // between controller instances.
229 Device device = deviceService.getDevice(deviceId);
230 String ip;
231 int port;
232 Socket socket = null;
233 if (device != null) {
234 ip = device.annotations().value(IPADDRESS);
235 port = Integer.parseInt(device.annotations().value(PORT));
236 } else {
237 String[] info = deviceId.toString().split(":");
238 if (info.length == 3) {
239 ip = info[1];
240 port = Integer.parseInt(info[2]);
241 } else {
242 ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0])
243 && !el.equals(info[info.length - 1]))
244 .reduce((t, u) -> t + ":" + u)
245 .get();
246 log.debug("ip v6 {}", ip);
247 port = Integer.parseInt(info[info.length - 1]);
248 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530249 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700250 //test connection to device opening a socket to it.
251 try {
252 socket = new Socket(ip, port);
253 log.debug("rechability of {}, {}, {}", deviceId, socket.isConnected() && !socket.isClosed());
254 return socket.isConnected() && !socket.isClosed();
255 } catch (IOException e) {
256 log.info("Device {} is not reachable", deviceId);
257 return false;
258 } finally {
259 if (socket != null) {
260 try {
261 socket.close();
262 } catch (IOException e) {
263 log.debug("Test Socket failed {} ", deviceId);
264 return false;
265 }
266 }
267 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530268 }
269
Saurav Dasa2d37502016-03-25 17:50:40 -0700270 @Override
271 public void changePortState(DeviceId deviceId, PortNumber portNumber,
272 boolean enable) {
273 // TODO if required
274 }
275
andreaeb70a942015-10-16 21:34:46 -0700276 private class InnerNetconfDeviceListener implements NetconfDeviceListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530277
Andrea Campanella101417d2015-12-11 17:58:07 -0800278
andreaeb70a942015-10-16 21:34:46 -0700279 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700280 public void deviceAdded(DeviceId deviceId) {
281 //no-op
282 log.debug("Netconf device {} added to Netconf subController", deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530283 }
284
285 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700286 public void deviceRemoved(DeviceId deviceId) {
287 Preconditions.checkNotNull(deviceId, ISNULL);
helenyrwufd296b62016-06-22 17:43:02 -0700288
289 if (deviceService.getDevice(deviceId) != null) {
290 providerService.deviceDisconnected(deviceId);
291 log.debug("Netconf device {} removed from Netconf subController", deviceId);
292 } else {
293 log.warn("Netconf device {} does not exist in the store, " +
294 "it may already have been removed", deviceId);
295 }
andreaeb70a942015-10-16 21:34:46 -0700296 }
297 }
298
andreaeb70a942015-10-16 21:34:46 -0700299 private void connectDevices() {
300 NetconfProviderConfig cfg = cfgService.getConfig(appId, NetconfProviderConfig.class);
301 if (cfg != null) {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530302 try {
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700303 cfg.getDevicesAddresses().forEach(addr -> {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700304 DeviceId deviceId = getDeviceId(addr.ip().toString(), addr.port());
305 Preconditions.checkNotNull(deviceId, ISNULL);
306 //Netconf configuration object
307 ChassisId cid = new ChassisId();
308 String ipAddress = addr.ip().toString();
309 SparseAnnotations annotations = DefaultAnnotations.builder()
310 .set(IPADDRESS, ipAddress)
311 .set(PORT, String.valueOf(addr.port()))
312 .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase())
313 .build();
314 DeviceDescription deviceDescription = new DefaultDeviceDescription(
315 deviceId.uri(),
316 Device.Type.SWITCH,
317 UNKNOWN, UNKNOWN,
318 UNKNOWN, UNKNOWN,
helenyrwufd296b62016-06-22 17:43:02 -0700319 cid, false,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700320 annotations);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600321 storeDeviceKey(addr, deviceId);
322
helenyrwufd296b62016-06-22 17:43:02 -0700323 if (deviceService.getDevice(deviceId) == null) {
324 providerService.deviceConnected(deviceId, deviceDescription);
325 }
326 checkAndUpdateDevice(deviceId, deviceDescription);
327 });
328 } catch (ConfigException e) {
329 log.error("Cannot read config error " + e);
330 }
331 }
332 }
333
334 private void checkAndUpdateDevice(DeviceId deviceId, DeviceDescription deviceDescription) {
335 if (deviceService.getDevice(deviceId) == null) {
336 log.warn("Device {} has not been added to store, " +
337 "maybe due to a problem in connectivity", deviceId);
338 } else {
339 boolean isReachable = isReachable(deviceId);
340 if (isReachable && !deviceService.isAvailable(deviceId)) {
Konstantinos Kanonakis4d67dd82016-08-05 12:18:52 -0500341 Device device = deviceService.getDevice(deviceId);
Konstantinos Kanonakis4d67dd82016-08-05 12:18:52 -0500342 if (device.is(DeviceDescriptionDiscovery.class)) {
343 if (mastershipService.isLocalMaster(deviceId)) {
344 DeviceDescriptionDiscovery deviceDescriptionDiscovery =
345 device.as(DeviceDescriptionDiscovery.class);
Michele Santuari00cc1f72016-09-08 17:05:24 +0200346 DeviceDescription updatedDeviceDescription = deviceDescriptionDiscovery.discoverDeviceDetails();
347 if (updatedDeviceDescription != null &&
348 !descriptionEquals(device, updatedDeviceDescription)) {
349 providerService.deviceConnected(
350 deviceId, new DefaultDeviceDescription(
351 updatedDeviceDescription, true, updatedDeviceDescription.annotations()));
Michele Santuari576f09c2016-09-28 14:20:00 +0200352 } else if (updatedDeviceDescription == null) {
353 providerService.deviceConnected(
354 deviceId, new DefaultDeviceDescription(
355 deviceDescription, true, deviceDescription.annotations()));
Michele Santuari00cc1f72016-09-08 17:05:24 +0200356 }
357 //if ports are not discovered, retry the discovery
358 if (deviceService.getPorts(deviceId).isEmpty()) {
359 discoverPorts(deviceId);
360 }
Konstantinos Kanonakis4d67dd82016-08-05 12:18:52 -0500361 }
362 } else {
Michele Santuari576f09c2016-09-28 14:20:00 +0200363 log.warn("No DeviceDescriptionDiscovery behaviour for device {} " +
364 "using DefaultDeviceDescription", deviceId);
365 providerService.deviceConnected(
366 deviceId, new DefaultDeviceDescription(
367 deviceDescription, true, deviceDescription.annotations()));
Konstantinos Kanonakis4d67dd82016-08-05 12:18:52 -0500368 }
helenyrwufd296b62016-06-22 17:43:02 -0700369 } else if (!isReachable && deviceService.isAvailable(deviceId)) {
370 providerService.deviceDisconnected(deviceId);
371 }
372 }
373 }
374
Michele Santuari00cc1f72016-09-08 17:05:24 +0200375 private boolean descriptionEquals(Device device, DeviceDescription updatedDeviceDescription) {
Yuta HIGUCHIf381fc72017-01-03 10:39:36 -0800376 return Objects.equal(device.id().uri(), updatedDeviceDescription.deviceUri())
Michele Santuari00cc1f72016-09-08 17:05:24 +0200377 && Objects.equal(device.type(), updatedDeviceDescription.type())
378 && Objects.equal(device.manufacturer(), updatedDeviceDescription.manufacturer())
379 && Objects.equal(device.hwVersion(), updatedDeviceDescription.hwVersion())
380 && Objects.equal(device.swVersion(), updatedDeviceDescription.swVersion())
381 && Objects.equal(device.serialNumber(), updatedDeviceDescription.serialNumber())
382 && Objects.equal(device.chassisId(), updatedDeviceDescription.chassisId())
383 && Objects.equal(device.annotations(), updatedDeviceDescription.annotations());
384 }
385
helenyrwufd296b62016-06-22 17:43:02 -0700386 private void checkAndUpdateDevices() {
387 NetconfProviderConfig cfg = cfgService.getConfig(appId, NetconfProviderConfig.class);
388 if (cfg != null) {
389 log.info("Checking connection to devices in configuration");
390 try {
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700391 cfg.getDevicesAddresses().forEach(addr -> {
helenyrwufd296b62016-06-22 17:43:02 -0700392 DeviceId deviceId = getDeviceId(addr.ip().toString(), addr.port());
393 Preconditions.checkNotNull(deviceId, ISNULL);
394 //Netconf configuration object
395 ChassisId cid = new ChassisId();
396 String ipAddress = addr.ip().toString();
397 SparseAnnotations annotations = DefaultAnnotations.builder()
398 .set(IPADDRESS, ipAddress)
399 .set(PORT, String.valueOf(addr.port()))
400 .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase())
401 .build();
402 DeviceDescription deviceDescription = new DefaultDeviceDescription(
403 deviceId.uri(),
404 Device.Type.SWITCH,
405 UNKNOWN, UNKNOWN,
406 UNKNOWN, UNKNOWN,
407 cid, false,
408 annotations);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600409 storeDeviceKey(addr, deviceId);
helenyrwufd296b62016-06-22 17:43:02 -0700410 checkAndUpdateDevice(deviceId, deviceDescription);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700411 });
andreaeb70a942015-10-16 21:34:46 -0700412 } catch (ConfigException e) {
413 log.error("Cannot read config error " + e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530414 }
415 }
andreaeb70a942015-10-16 21:34:46 -0700416 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530417
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600418 private void storeDeviceKey(NetconfProviderConfig.NetconfDeviceAddress addr, DeviceId deviceId) {
419 if (addr.sshkey().equals("")) {
420 deviceKeyAdminService.addKey(
421 DeviceKey.createDeviceKeyUsingUsernamePassword(
422 DeviceKeyId.deviceKeyId(deviceId.toString()),
423 null, addr.name(), addr.password()));
424 } else {
425 deviceKeyAdminService.addKey(
426 DeviceKey.createDeviceKeyUsingSshKey(
427 DeviceKeyId.deviceKeyId(deviceId.toString()),
428 null, addr.name(), addr.password(), addr.sshkey()));
429 }
430 }
431
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700432 private void initiateConnection(DeviceId deviceId, MastershipRole newRole) {
433 try {
434 if (isReachable(deviceId)) {
435 controller.connectDevice(deviceId);
436 providerService.receivedRoleReply(deviceId, newRole, MastershipRole.MASTER);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700437 }
438 } catch (Exception e) {
439 if (deviceService.getDevice(deviceId) != null) {
440 providerService.deviceDisconnected(deviceId);
441 }
442 deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(deviceId.toString()));
443 throw new RuntimeException(new NetconfException(
444 "Can't connect to NETCONF " + "device on " + deviceId + ":" + deviceId, e));
445
446 }
447 }
448
449 private void discoverPorts(DeviceId deviceId) {
450 Device device = deviceService.getDevice(deviceId);
Andrea Campanella6c71a052016-04-22 11:56:31 -0700451 //TODO remove when PortDiscovery is removed from master
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700452 if (device.is(PortDiscovery.class)) {
453 PortDiscovery portConfig = device.as(PortDiscovery.class);
454 providerService.updatePorts(deviceId,
455 portConfig.getPorts());
Andrea Campanella6c71a052016-04-22 11:56:31 -0700456 } else if (device.is(DeviceDescriptionDiscovery.class)) {
457 DeviceDescriptionDiscovery deviceDescriptionDiscovery =
458 device.as(DeviceDescriptionDiscovery.class);
459 providerService.updatePorts(deviceId,
460 deviceDescriptionDiscovery.discoverPortDetails());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700461 } else {
462 log.warn("No portGetter behaviour for device {}", deviceId);
463 }
464 }
465
466 /**
467 * Return the DeviceId about the device containing the URI.
468 *
Andrea Campanella6c71a052016-04-22 11:56:31 -0700469 * @param ip IP address
Ray Milkeyd4334db2016-04-05 17:39:44 -0700470 * @param port port number
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700471 * @return DeviceId
472 */
473 public DeviceId getDeviceId(String ip, int port) {
474 try {
475 return DeviceId.deviceId(new URI(NETCONF, ip + ":" + port, null));
476 } catch (URISyntaxException e) {
477 throw new IllegalArgumentException("Unable to build deviceID for device "
478 + ip + ":" + port, e);
479 }
480 }
481
482 /**
483 * Listener for configuration events.
484 */
andreaeb70a942015-10-16 21:34:46 -0700485 private class InternalNetworkConfigListener implements NetworkConfigListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530486
andreaeb70a942015-10-16 21:34:46 -0700487
488 @Override
489 public void event(NetworkConfigEvent event) {
Andrea Campanella90f044f2016-03-02 09:14:57 -0800490 executor.execute(NetconfDeviceProvider.this::connectDevices);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530491 }
492
andreaeb70a942015-10-16 21:34:46 -0700493 @Override
494 public boolean isRelevant(NetworkConfigEvent event) {
andreaeb70a942015-10-16 21:34:46 -0700495 return event.configClass().equals(NetconfProviderConfig.class) &&
496 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
497 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530498 }
499 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700500
501 /**
502 * Listener for core device events.
503 */
504 private class InternalDeviceListener implements DeviceListener {
505 @Override
506 public void event(DeviceEvent event) {
507 if ((event.type() == DeviceEvent.Type.DEVICE_ADDED)) {
508 executor.execute(() -> discoverPorts(event.subject().id()));
509 } else if ((event.type() == DeviceEvent.Type.DEVICE_REMOVED)) {
510 log.debug("removing device {}", event.subject().id());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700511 controller.disconnectDevice(event.subject().id(), true);
512 }
513 }
514
515 @Override
516 public boolean isRelevant(DeviceEvent event) {
517 if (mastershipService.getMasterFor(event.subject().id()) == null) {
518 return true;
519 }
520 return event.subject().annotations().value(AnnotationKeys.PROTOCOL)
521 .equals(SCHEME_NAME.toUpperCase()) &&
Michele Santuari576f09c2016-09-28 14:20:00 +0200522 mastershipService.isLocalMaster(event.subject().id());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700523 }
524 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530525}