blob: 99c8ebd08b352de2db8b5657683d547a518dbdbf [file] [log] [blame]
Sanjay Se8dcfee2015-04-23 10:07:08 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Andrea Campanella34cf65c2017-04-12 13:51:32 +020019import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Michele Santuari00cc1f72016-09-08 17:05:24 +020021import com.google.common.base.Objects;
andreaeb70a942015-10-16 21:34:46 -070022import com.google.common.base.Preconditions;
Andrea Campanella34cf65c2017-04-12 13:51:32 +020023import com.google.common.collect.ImmutableList;
Sanjay Se8dcfee2015-04-23 10:07:08 +053024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -070027import org.apache.felix.scr.annotations.Modified;
28import org.apache.felix.scr.annotations.Property;
Sanjay Se8dcfee2015-04-23 10:07:08 +053029import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
31import org.onlab.packet.ChassisId;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -070032import org.onlab.util.Tools;
33import org.onosproject.cfg.ComponentConfigService;
andreaeb70a942015-10-16 21:34:46 -070034import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
Andrea Campanella32813682017-10-23 15:29:24 +020036import org.onosproject.net.Port;
37import org.onosproject.net.behaviour.PortAdmin;
Ray Milkey6c013742017-08-15 10:16:43 -070038import org.onosproject.net.config.ConfigException;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070039import org.onosproject.mastership.MastershipService;
Marc De Leenheerb0d131c2016-03-01 20:34:59 -080040import org.onosproject.net.AnnotationKeys;
andreaeb70a942015-10-16 21:34:46 -070041import org.onosproject.net.DefaultAnnotations;
Sanjay Se8dcfee2015-04-23 10:07:08 +053042import org.onosproject.net.Device;
43import org.onosproject.net.DeviceId;
44import org.onosproject.net.MastershipRole;
Saurav Dasa2d37502016-03-25 17:50:40 -070045import org.onosproject.net.PortNumber;
andreaeb70a942015-10-16 21:34:46 -070046import org.onosproject.net.SparseAnnotations;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080047import org.onosproject.net.behaviour.PortDiscovery;
andreaeb70a942015-10-16 21:34:46 -070048import org.onosproject.net.config.ConfigFactory;
49import org.onosproject.net.config.NetworkConfigEvent;
50import org.onosproject.net.config.NetworkConfigListener;
51import org.onosproject.net.config.NetworkConfigRegistry;
Andrea Campanella34cf65c2017-04-12 13:51:32 +020052import org.onosproject.net.config.basics.SubjectFactories;
Sanjay Se8dcfee2015-04-23 10:07:08 +053053import org.onosproject.net.device.DefaultDeviceDescription;
Andrea Campanella32813682017-10-23 15:29:24 +020054import org.onosproject.net.device.DefaultPortDescription;
Sanjay Se8dcfee2015-04-23 10:07:08 +053055import org.onosproject.net.device.DeviceDescription;
Andrea Campanella6c71a052016-04-22 11:56:31 -070056import org.onosproject.net.device.DeviceDescriptionDiscovery;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070057import org.onosproject.net.device.DeviceEvent;
58import org.onosproject.net.device.DeviceListener;
Sanjay Se8dcfee2015-04-23 10:07:08 +053059import org.onosproject.net.device.DeviceProvider;
60import org.onosproject.net.device.DeviceProviderRegistry;
61import org.onosproject.net.device.DeviceProviderService;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080062import org.onosproject.net.device.DeviceService;
Andrea Campanellac3627842017-04-04 18:06:54 +020063import org.onosproject.net.device.PortStatistics;
Gaurav Agrawaldab4d772017-03-29 15:15:13 +053064import org.onosproject.net.device.PortStatisticsDiscovery;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070065import org.onosproject.net.key.DeviceKey;
66import org.onosproject.net.key.DeviceKeyAdminService;
67import org.onosproject.net.key.DeviceKeyId;
Sanjay Se8dcfee2015-04-23 10:07:08 +053068import org.onosproject.net.provider.AbstractProvider;
69import org.onosproject.net.provider.ProviderId;
andreaeb70a942015-10-16 21:34:46 -070070import org.onosproject.netconf.NetconfController;
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -070071import org.onosproject.netconf.NetconfDevice;
andreaeb70a942015-10-16 21:34:46 -070072import org.onosproject.netconf.NetconfDeviceListener;
Andrea Campanella8b1cb672016-01-25 13:58:58 -080073import org.onosproject.netconf.NetconfException;
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -070074import org.onosproject.netconf.config.NetconfDeviceConfig;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -070075import org.osgi.service.component.ComponentContext;
Sanjay Se8dcfee2015-04-23 10:07:08 +053076import org.slf4j.Logger;
77
Andrea Campanella087ceb92015-12-07 09:58:34 -080078import java.io.IOException;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070079import java.net.Socket;
80import java.net.URI;
81import java.net.URISyntaxException;
82import java.util.Arrays;
Andrea Campanellac3627842017-04-04 18:06:54 +020083import java.util.Collection;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -070084import java.util.Dictionary;
Andrea Campanella34cf65c2017-04-12 13:51:32 +020085import java.util.List;
mskala832d0472017-06-09 16:31:42 +020086import java.util.Map;
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -070087import java.util.Optional;
Andrea Campanella34cf65c2017-04-12 13:51:32 +020088import java.util.Set;
Andrea Campanella32813682017-10-23 15:29:24 +020089import java.util.concurrent.CompletableFuture;
mskala832d0472017-06-09 16:31:42 +020090import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella5c999e22016-03-01 15:12:53 -080091import java.util.concurrent.ExecutorService;
92import java.util.concurrent.Executors;
helenyrwufd296b62016-06-22 17:43:02 -070093import java.util.concurrent.ScheduledExecutorService;
94import java.util.concurrent.ScheduledFuture;
95import java.util.concurrent.TimeUnit;
mskala832d0472017-06-09 16:31:42 +020096import java.util.concurrent.atomic.AtomicInteger;
andreaeb70a942015-10-16 21:34:46 -070097
Yuta HIGUCHI1624df12016-07-21 16:54:33 -070098import static java.util.concurrent.Executors.newScheduledThreadPool;
Andrea Campanella5c999e22016-03-01 15:12:53 -080099import static org.onlab.util.Tools.groupedThreads;
andreaeb70a942015-10-16 21:34:46 -0700100import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
101import static org.slf4j.LoggerFactory.getLogger;
102
Sanjay Se8dcfee2015-04-23 10:07:08 +0530103/**
andreaeb70a942015-10-16 21:34:46 -0700104 * Provider which uses an NETCONF controller to detect device.
Sanjay Se8dcfee2015-04-23 10:07:08 +0530105 */
106@Component(immediate = true)
107public class NetconfDeviceProvider extends AbstractProvider
108 implements DeviceProvider {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700109
andreaeb70a942015-10-16 21:34:46 -0700110 private final Logger log = getLogger(getClass());
Sanjay Se8dcfee2015-04-23 10:07:08 +0530111
112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
113 protected DeviceProviderRegistry providerRegistry;
114
andreaeb70a942015-10-16 21:34:46 -0700115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella101417d2015-12-11 17:58:07 -0800116 protected NetconfController controller;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530117
118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -0700119 protected NetworkConfigRegistry cfgService;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530120
Thomas Vachuskad6811712015-04-29 21:37:04 -0700121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -0700122 protected CoreService coreService;
Thomas Vachuskad6811712015-04-29 21:37:04 -0700123
Aaron Kruglikov17b4c852016-01-15 16:37:04 -0800124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700125 protected DeviceService deviceService;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -0800126
127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700128 protected DeviceKeyAdminService deviceKeyAdminService;
129
130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
131 protected MastershipService mastershipService;
132
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700133 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
134 protected ComponentConfigService componentConfigService;
135
136
Michele Santuari576f09c2016-09-28 14:20:00 +0200137 protected static final String APP_NAME = "org.onosproject.netconf";
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200138 protected static final String SCHEME_NAME = "netconf";
Andrea Campanella101417d2015-12-11 17:58:07 -0800139 private static final String DEVICE_PROVIDER_PACKAGE = "org.onosproject.netconf.provider.device";
140 private static final String UNKNOWN = "unknown";
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700141 protected static final String ISNULL = "NetconfDeviceInfo is null";
142 private static final String IPADDRESS = "ipaddress";
143 private static final String NETCONF = "netconf";
144 private static final String PORT = "port";
helenyrwufd296b62016-06-22 17:43:02 -0700145 private static final int CORE_POOL_SIZE = 10;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700146
Thomas Vachuskadb29dcf2017-03-31 11:26:19 -0700147 private static final int DEFAULT_POLL_FREQUENCY_SECONDS = 30;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700148 @Property(name = "pollFrequency", intValue = DEFAULT_POLL_FREQUENCY_SECONDS,
149 label = "Configure poll frequency for port status and statistics; " +
Thomas Vachuskadb29dcf2017-03-31 11:26:19 -0700150 "default is 30 sec")
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700151 private int pollFrequency = DEFAULT_POLL_FREQUENCY_SECONDS;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530152
mskala832d0472017-06-09 16:31:42 +0200153 private static final int DEFAULT_MAX_RETRIES = 5;
154 @Property(name = "maxRetries", intValue = DEFAULT_MAX_RETRIES,
155 label = "Configure maximum allowed number of retries for obtaining list of ports; " +
156 "default is 5 times")
157 private int maxRetries = DEFAULT_MAX_RETRIES;
158
Michal Mach26a90fa2017-06-07 11:12:46 +0200159 protected ExecutorService executor =
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700160 Executors.newFixedThreadPool(5, groupedThreads("onos/netconfdeviceprovider",
161 "device-installer-%d", log));
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700162 protected ScheduledExecutorService connectionExecutor
163 = newScheduledThreadPool(CORE_POOL_SIZE,
164 groupedThreads("onos/netconfdeviceprovider",
165 "connection-executor-%d", log));
Andrea Campanella5c999e22016-03-01 15:12:53 -0800166
Michele Santuari576f09c2016-09-28 14:20:00 +0200167 protected DeviceProviderService providerService;
andreaeb70a942015-10-16 21:34:46 -0700168 private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700169 private InternalDeviceListener deviceListener = new InternalDeviceListener();
mskala832d0472017-06-09 16:31:42 +0200170 private final Map<DeviceId, AtomicInteger> retriedPortDiscoveryMap = new ConcurrentHashMap<>();
Michele Santuari576f09c2016-09-28 14:20:00 +0200171 protected ScheduledFuture<?> scheduledTask;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530172
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200173 protected final List<ConfigFactory> factories = ImmutableList.of(
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700174 // TODO consider moving Config registration to NETCONF ctl bundle
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200175 new ConfigFactory<DeviceId, NetconfDeviceConfig>(
176 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700177 NetconfDeviceConfig.class, NetconfDeviceConfig.CONFIG_KEY) {
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200178 @Override
179 public NetconfDeviceConfig createConfig() {
180 return new NetconfDeviceConfig();
181 }
182 },
andreaeb70a942015-10-16 21:34:46 -0700183 new ConfigFactory<ApplicationId, NetconfProviderConfig>(APP_SUBJECT_FACTORY,
184 NetconfProviderConfig.class,
Palash Kalac3ffad92017-06-09 21:18:19 +0900185 "netconf_devices",
andreaeb70a942015-10-16 21:34:46 -0700186 true) {
187 @Override
188 public NetconfProviderConfig createConfig() {
189 return new NetconfProviderConfig();
190 }
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200191 });
192
Michele Santuari576f09c2016-09-28 14:20:00 +0200193 protected final NetworkConfigListener cfgListener = new InternalNetworkConfigListener();
andreaeb70a942015-10-16 21:34:46 -0700194 private ApplicationId appId;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700195 private boolean active;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530196
Sanjay Se8dcfee2015-04-23 10:07:08 +0530197
198 @Activate
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700199 public void activate(ComponentContext context) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700200 active = true;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700201 componentConfigService.registerProperties(getClass());
Sanjay Se8dcfee2015-04-23 10:07:08 +0530202 providerService = providerRegistry.register(this);
Andrea Campanella101417d2015-12-11 17:58:07 -0800203 appId = coreService.registerApplication(APP_NAME);
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200204 factories.forEach(cfgService::registerConfigFactory);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700205 cfgService.addListener(cfgListener);
andreaeb70a942015-10-16 21:34:46 -0700206 controller.addDeviceListener(innerNodeListener);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700207 deviceService.addListener(deviceListener);
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200208 translateConfig();
Andrea Campanella7d8449b2016-03-02 10:16:42 -0800209 executor.execute(NetconfDeviceProvider.this::connectDevices);
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700210 modified(context);
Thomas Vachuskad6811712015-04-29 21:37:04 -0700211 log.info("Started");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530212 }
213
andreaeb70a942015-10-16 21:34:46 -0700214
Sanjay Se8dcfee2015-04-23 10:07:08 +0530215 @Deactivate
andreaeb70a942015-10-16 21:34:46 -0700216 public void deactivate() {
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700217 componentConfigService.unregisterProperties(getClass(), false);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700218 deviceService.removeListener(deviceListener);
219 active = false;
220 controller.getNetconfDevices().forEach(id -> {
221 deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(id.toString()));
222 controller.disconnectDevice(id, true);
223 });
Andrea Campanella86294db2016-03-07 11:42:49 -0800224 controller.removeDeviceListener(innerNodeListener);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700225 deviceService.removeListener(deviceListener);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530226 providerRegistry.unregister(this);
227 providerService = null;
mskala832d0472017-06-09 16:31:42 +0200228 retriedPortDiscoveryMap.clear();
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200229 factories.forEach(cfgService::unregisterConfigFactory);
helenyrwufd296b62016-06-22 17:43:02 -0700230 scheduledTask.cancel(true);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700231 executor.shutdown();
Sanjay Seb5eebb2015-04-24 15:44:50 +0530232 log.info("Stopped");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530233 }
234
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700235
236 @Modified
237 public void modified(ComponentContext context) {
238 if (context != null) {
239 Dictionary<?, ?> properties = context.getProperties();
240 pollFrequency = Tools.getIntegerProperty(properties, "pollFrequency",
241 DEFAULT_POLL_FREQUENCY_SECONDS);
242 log.info("Configured. Poll frequency is configured to {} seconds", pollFrequency);
mskala832d0472017-06-09 16:31:42 +0200243
244 maxRetries = Tools.getIntegerProperty(properties, "maxRetries",
245 DEFAULT_MAX_RETRIES);
246 log.info("Configured. Number of retries is configured to {} times", maxRetries);
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700247 }
248 if (scheduledTask != null) {
249 scheduledTask.cancel(false);
250 }
251 scheduledTask = schedulePolling();
252 }
253
andreaeb70a942015-10-16 21:34:46 -0700254 public NetconfDeviceProvider() {
Andrea Campanella101417d2015-12-11 17:58:07 -0800255 super(new ProviderId(SCHEME_NAME, DEVICE_PROVIDER_PACKAGE));
Sanjay Se8dcfee2015-04-23 10:07:08 +0530256 }
257
helenyrwufd296b62016-06-22 17:43:02 -0700258 // Checks connection to devices in the config file
259 // every DEFAULT_POLL_FREQUENCY_SECONDS seconds.
260 private ScheduledFuture schedulePolling() {
Yuta HIGUCHIc2b82e32017-03-10 14:33:41 -0800261 return connectionExecutor.scheduleAtFixedRate(exceptionSafe(this::checkAndUpdateDevices),
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700262 pollFrequency / 10,
263 pollFrequency, TimeUnit.SECONDS);
helenyrwufd296b62016-06-22 17:43:02 -0700264 }
265
Yuta HIGUCHIc2b82e32017-03-10 14:33:41 -0800266 private Runnable exceptionSafe(Runnable runnable) {
267 return new Runnable() {
268
269 @Override
270 public void run() {
271 try {
272 runnable.run();
273 } catch (Exception e) {
274 log.error("Unhandled Exception", e);
275 }
276 }
277 };
278 }
279
Sanjay Se8dcfee2015-04-23 10:07:08 +0530280 @Override
281 public void triggerProbe(DeviceId deviceId) {
andreaeb70a942015-10-16 21:34:46 -0700282 // TODO: This will be implemented later.
Yuta HIGUCHIe4cb8cf2017-05-01 22:18:28 -0700283 log.debug("Should be triggering probe on device {}", deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530284 }
285
286 @Override
287 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700288 if (active) {
289 switch (newRole) {
290 case MASTER:
291 initiateConnection(deviceId, newRole);
292 log.debug("Accepting mastership role change to {} for device {}", newRole, deviceId);
293 break;
294 case STANDBY:
295 controller.disconnectDevice(deviceId, false);
296 providerService.receivedRoleReply(deviceId, newRole, MastershipRole.STANDBY);
297 //else no-op
298 break;
299 case NONE:
300 controller.disconnectDevice(deviceId, false);
301 providerService.receivedRoleReply(deviceId, newRole, MastershipRole.NONE);
302 break;
303 default:
304 log.error("Unimplemented Mastership state : {}", newRole);
305
306 }
307 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530308 }
309
310 @Override
311 public boolean isReachable(DeviceId deviceId) {
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -0700312
313 boolean sessionExists =
314 Optional.ofNullable(controller.getDevicesMap().get(deviceId))
315 .map(NetconfDevice::isActive)
316 .orElse(false);
317 if (sessionExists) {
318 return true;
319 }
320
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700321 //FIXME this is a workaround util device state is shared
322 // between controller instances.
323 Device device = deviceService.getDevice(deviceId);
324 String ip;
325 int port;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700326 if (device != null) {
327 ip = device.annotations().value(IPADDRESS);
328 port = Integer.parseInt(device.annotations().value(PORT));
329 } else {
330 String[] info = deviceId.toString().split(":");
331 if (info.length == 3) {
332 ip = info[1];
333 port = Integer.parseInt(info[2]);
334 } else {
335 ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0])
336 && !el.equals(info[info.length - 1]))
337 .reduce((t, u) -> t + ":" + u)
338 .get();
339 log.debug("ip v6 {}", ip);
340 port = Integer.parseInt(info[info.length - 1]);
341 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530342 }
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -0700343 // FIXME just opening TCP session probably is not the appropriate
344 // method to test reachability.
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700345 //test connection to device opening a socket to it.
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -0700346 log.debug("Testing reachability for {}:{}", ip, port);
Yuta HIGUCHI70c21472017-04-20 20:40:46 -0700347 try (Socket socket = new Socket(ip, port)) {
Yuta HIGUCHI0454d702017-03-17 10:08:38 -0700348 log.debug("rechability of {}, {}, {}", deviceId, socket.isConnected(), !socket.isClosed());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700349 return socket.isConnected() && !socket.isClosed();
350 } catch (IOException e) {
351 log.info("Device {} is not reachable", deviceId);
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -0700352 log.debug(" error details", e);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700353 return false;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700354 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530355 }
356
Saurav Dasa2d37502016-03-25 17:50:40 -0700357 @Override
358 public void changePortState(DeviceId deviceId, PortNumber portNumber,
359 boolean enable) {
Andrea Campanella32813682017-10-23 15:29:24 +0200360 Device device = deviceService.getDevice(deviceId);
361 if (mastershipService.isLocalMaster(deviceId)) {
362 if (device.is(PortAdmin.class)) {
363 PortAdmin portAdmin =
364 device.as(PortAdmin.class);
365 CompletableFuture<Boolean> modified;
366 if (enable) {
367 modified = portAdmin.enable(portNumber);
368 } else {
369 modified = portAdmin.disable(portNumber);
370 }
371 modified.thenAccept(result -> {
372 if (result) {
373 Port port = deviceService.getPort(deviceId, portNumber);
374 //rebuilding port description with admin state changed.
375 providerService.portStatusChanged(deviceId,
376 new DefaultPortDescription(portNumber, enable, false,
377 port.type(), port.portSpeed(),
378 (SparseAnnotations) port.annotations()));
379 } else {
380 log.warn("Your device {} port {} status can't be changed to {}",
381 deviceId, portNumber, enable);
382 }
383 });
384 } else {
385 log.warn("Device {} does not support Port Admin", deviceId);
386 }
387 } else {
388 log.debug("Not master but {}, not changing port state", mastershipService.getLocalRole(deviceId));
389 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700390 }
391
andreaeb70a942015-10-16 21:34:46 -0700392 private class InnerNetconfDeviceListener implements NetconfDeviceListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530393
Andrea Campanella101417d2015-12-11 17:58:07 -0800394
andreaeb70a942015-10-16 21:34:46 -0700395 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700396 public void deviceAdded(DeviceId deviceId) {
397 //no-op
398 log.debug("Netconf device {} added to Netconf subController", deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530399 }
400
401 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700402 public void deviceRemoved(DeviceId deviceId) {
403 Preconditions.checkNotNull(deviceId, ISNULL);
helenyrwufd296b62016-06-22 17:43:02 -0700404
405 if (deviceService.getDevice(deviceId) != null) {
406 providerService.deviceDisconnected(deviceId);
mskala832d0472017-06-09 16:31:42 +0200407 retriedPortDiscoveryMap.remove(deviceId);
helenyrwufd296b62016-06-22 17:43:02 -0700408 log.debug("Netconf device {} removed from Netconf subController", deviceId);
409 } else {
410 log.warn("Netconf device {} does not exist in the store, " +
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530411 "it may already have been removed", deviceId);
helenyrwufd296b62016-06-22 17:43:02 -0700412 }
andreaeb70a942015-10-16 21:34:46 -0700413 }
414 }
415
andreaeb70a942015-10-16 21:34:46 -0700416 private void connectDevices() {
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200417 Set<DeviceId> deviceSubjects =
418 cfgService.getSubjects(DeviceId.class, NetconfDeviceConfig.class);
419 deviceSubjects.forEach(deviceId -> {
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700420 connectDevice(cfgService.getConfig(deviceId, NetconfDeviceConfig.class));
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200421 });
helenyrwufd296b62016-06-22 17:43:02 -0700422 }
423
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700424
425 private void connectDevice(NetconfDeviceConfig config) {
426 if (config == null) {
427 return;
428 }
429 DeviceId deviceId = config.subject();
430 if (!deviceId.uri().getScheme().equals(SCHEME_NAME)) {
431 // not under my scheme, skipping
432 log.trace("{} not my scheme, skipping", deviceId);
433 return;
434 }
435 DeviceDescription deviceDescription = createDeviceRepresentation(deviceId, config);
436 log.debug("Connecting NETCONF device {}, on {}:{} with username {}",
437 deviceId, config.ip(), config.port(), config.username());
438 storeDeviceKey(config.sshKey(), config.username(), config.password(), deviceId);
Andrea Campanella105736e2017-11-23 12:52:43 +0100439 retriedPortDiscoveryMap.put(deviceId, new AtomicInteger(0));
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700440 if (deviceService.getDevice(deviceId) == null) {
441 providerService.deviceConnected(deviceId, deviceDescription);
442 }
443 try {
Andrea Campanella105736e2017-11-23 12:52:43 +0100444 checkAndUpdateDevice(deviceId, deviceDescription, true);
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700445 } catch (Exception e) {
446 log.error("Unhandled exception checking {}", deviceId, e);
447 }
448 }
449
Andrea Campanella105736e2017-11-23 12:52:43 +0100450 private void checkAndUpdateDevice(DeviceId deviceId, DeviceDescription deviceDescription, boolean newlyConnected) {
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530451 Device device = deviceService.getDevice(deviceId);
452 if (device == null) {
Andrea Campanella105736e2017-11-23 12:52:43 +0100453 log.debug("Device {} has not been added to store, since it's not reachable", deviceId);
454 return;
455 }
456 boolean isReachable = isReachable(deviceId);
457 if (!isReachable && deviceService.isAvailable(deviceId)) {
458 providerService.deviceDisconnected(deviceId);
459 return;
460 } else if (newlyConnected) {
461 updateDeviceDescription(deviceId, deviceDescription, device);
462 }
463 if (isReachable && deviceService.isAvailable(deviceId) &&
464 mastershipService.isLocalMaster(deviceId)) {
465 //if ports are not discovered, retry the discovery
466 if (deviceService.getPorts(deviceId).isEmpty() &&
467 retriedPortDiscoveryMap.get(deviceId).getAndIncrement() < maxRetries) {
468 discoverPorts(deviceId);
469 }
470 updatePortStatistics(device);
471 }
472 }
473
474 private void updateDeviceDescription(DeviceId deviceId, DeviceDescription deviceDescription, Device device) {
475 if (device.is(DeviceDescriptionDiscovery.class)) {
476 if (mastershipService.isLocalMaster(deviceId)) {
477 DeviceDescriptionDiscovery deviceDescriptionDiscovery =
478 device.as(DeviceDescriptionDiscovery.class);
479 DeviceDescription updatedDeviceDescription =
480 deviceDescriptionDiscovery.discoverDeviceDetails();
481 if (updatedDeviceDescription != null &&
482 !descriptionEquals(device, updatedDeviceDescription)) {
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530483 providerService.deviceConnected(
484 deviceId, new DefaultDeviceDescription(
Andrea Campanella105736e2017-11-23 12:52:43 +0100485 updatedDeviceDescription, true,
486 updatedDeviceDescription.annotations()));
487 } else if (updatedDeviceDescription == null) {
488 providerService.deviceConnected(
489 deviceId, new DefaultDeviceDescription(
490 deviceDescription, true,
491 deviceDescription.annotations()));
Konstantinos Kanonakis4d67dd82016-08-05 12:18:52 -0500492 }
helenyrwufd296b62016-06-22 17:43:02 -0700493 }
Andrea Campanella105736e2017-11-23 12:52:43 +0100494 } else {
495 log.warn("No DeviceDescriptionDiscovery behaviour for device {} " +
496 "using DefaultDeviceDescription", deviceId);
497 providerService.deviceConnected(
498 deviceId, new DefaultDeviceDescription(
499 deviceDescription, true, deviceDescription.annotations()));
helenyrwufd296b62016-06-22 17:43:02 -0700500 }
501 }
502
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530503 private void updatePortStatistics(Device device) {
504 if (device.is(PortStatisticsDiscovery.class)) {
505 PortStatisticsDiscovery d = device.as(PortStatisticsDiscovery.class);
Andrea Campanellac3627842017-04-04 18:06:54 +0200506 Collection<PortStatistics> portStatistics = d.discoverPortStatistics();
507 if (portStatistics != null) {
508 providerService.updatePortStatistics(device.id(),
509 portStatistics);
510 }
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530511 } else {
Yuta HIGUCHIe4cb8cf2017-05-01 22:18:28 -0700512 log.debug("No port statistics getter behaviour for device {}",
513 device.id());
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530514 }
515 }
516
Michele Santuari00cc1f72016-09-08 17:05:24 +0200517 private boolean descriptionEquals(Device device, DeviceDescription updatedDeviceDescription) {
Yuta HIGUCHIf381fc72017-01-03 10:39:36 -0800518 return Objects.equal(device.id().uri(), updatedDeviceDescription.deviceUri())
Michele Santuari00cc1f72016-09-08 17:05:24 +0200519 && Objects.equal(device.type(), updatedDeviceDescription.type())
520 && Objects.equal(device.manufacturer(), updatedDeviceDescription.manufacturer())
521 && Objects.equal(device.hwVersion(), updatedDeviceDescription.hwVersion())
522 && Objects.equal(device.swVersion(), updatedDeviceDescription.swVersion())
523 && Objects.equal(device.serialNumber(), updatedDeviceDescription.serialNumber())
524 && Objects.equal(device.chassisId(), updatedDeviceDescription.chassisId())
525 && Objects.equal(device.annotations(), updatedDeviceDescription.annotations());
526 }
527
helenyrwufd296b62016-06-22 17:43:02 -0700528 private void checkAndUpdateDevices() {
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200529 Set<DeviceId> deviceSubjects =
530 cfgService.getSubjects(DeviceId.class, NetconfDeviceConfig.class);
531 deviceSubjects.forEach(deviceId -> {
532 NetconfDeviceConfig config =
533 cfgService.getConfig(deviceId, NetconfDeviceConfig.class);
534 DeviceDescription deviceDescription = createDeviceRepresentation(deviceId, config);
535 storeDeviceKey(config.sshKey(), config.username(), config.password(), deviceId);
Andrea Campanella105736e2017-11-23 12:52:43 +0100536 checkAndUpdateDevice(deviceId, deviceDescription, false);
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200537 });
andreaeb70a942015-10-16 21:34:46 -0700538 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530539
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200540 private DeviceDescription createDeviceRepresentation(DeviceId deviceId, NetconfDeviceConfig config) {
541 Preconditions.checkNotNull(deviceId, ISNULL);
542 //Netconf configuration object
543 ChassisId cid = new ChassisId();
544 String ipAddress = config.ip().toString();
545 SparseAnnotations annotations = DefaultAnnotations.builder()
546 .set(IPADDRESS, ipAddress)
547 .set(PORT, String.valueOf(config.port()))
548 .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase())
549 .build();
550 return new DefaultDeviceDescription(
551 deviceId.uri(),
552 Device.Type.SWITCH,
553 UNKNOWN, UNKNOWN,
554 UNKNOWN, UNKNOWN,
555 cid, false,
556 annotations);
557 }
558
559 private void storeDeviceKey(String sshKey, String username, String password, DeviceId deviceId) {
560 if (sshKey.equals("")) {
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600561 deviceKeyAdminService.addKey(
562 DeviceKey.createDeviceKeyUsingUsernamePassword(
563 DeviceKeyId.deviceKeyId(deviceId.toString()),
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200564 null, username, password));
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600565 } else {
566 deviceKeyAdminService.addKey(
567 DeviceKey.createDeviceKeyUsingSshKey(
568 DeviceKeyId.deviceKeyId(deviceId.toString()),
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200569 null, username, password,
570 sshKey));
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600571 }
572 }
573
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700574 private void initiateConnection(DeviceId deviceId, MastershipRole newRole) {
575 try {
576 if (isReachable(deviceId)) {
577 controller.connectDevice(deviceId);
578 providerService.receivedRoleReply(deviceId, newRole, MastershipRole.MASTER);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700579 }
580 } catch (Exception e) {
581 if (deviceService.getDevice(deviceId) != null) {
582 providerService.deviceDisconnected(deviceId);
583 }
584 deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(deviceId.toString()));
Ray Milkey986a47a2018-01-25 11:38:51 -0800585 throw new IllegalStateException(new NetconfException(
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200586 "Can't connect to NETCONF device " + deviceId, e));
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700587
588 }
589 }
590
591 private void discoverPorts(DeviceId deviceId) {
592 Device device = deviceService.getDevice(deviceId);
Andrea Campanella6c71a052016-04-22 11:56:31 -0700593 //TODO remove when PortDiscovery is removed from master
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700594 if (device.is(PortDiscovery.class)) {
595 PortDiscovery portConfig = device.as(PortDiscovery.class);
596 providerService.updatePorts(deviceId,
597 portConfig.getPorts());
Andrea Campanella6c71a052016-04-22 11:56:31 -0700598 } else if (device.is(DeviceDescriptionDiscovery.class)) {
599 DeviceDescriptionDiscovery deviceDescriptionDiscovery =
600 device.as(DeviceDescriptionDiscovery.class);
601 providerService.updatePorts(deviceId,
602 deviceDescriptionDiscovery.discoverPortDetails());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700603 } else {
604 log.warn("No portGetter behaviour for device {}", deviceId);
605 }
Gaurav Agrawaldab4d772017-03-29 15:15:13 +0530606
607 // Port statistics discovery
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530608 updatePortStatistics(device);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700609 }
610
611 /**
612 * Return the DeviceId about the device containing the URI.
613 *
Andrea Campanella6c71a052016-04-22 11:56:31 -0700614 * @param ip IP address
Ray Milkeyd4334db2016-04-05 17:39:44 -0700615 * @param port port number
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700616 * @return DeviceId
617 */
618 public DeviceId getDeviceId(String ip, int port) {
619 try {
620 return DeviceId.deviceId(new URI(NETCONF, ip + ":" + port, null));
621 } catch (URISyntaxException e) {
622 throw new IllegalArgumentException("Unable to build deviceID for device "
623 + ip + ":" + port, e);
624 }
625 }
626
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200627
628 protected void translateConfig() {
629 NetconfProviderConfig cfg = cfgService.getConfig(appId, NetconfProviderConfig.class);
630 if (cfg != null) {
631 try {
632 cfg.getDevicesAddresses().forEach(addr -> {
633 DeviceId deviceId = getDeviceId(addr.ip().toString(), addr.port());
634 log.info("Translating config for device {}", deviceId);
635 if (cfgService.getConfig(deviceId, NetconfDeviceConfig.class) == null) {
636 ObjectMapper mapper = new ObjectMapper();
637 ObjectNode device = mapper.createObjectNode();
638 device.put("ip", addr.ip().toString());
639 device.put("port", addr.port());
640 device.put("username", addr.name());
641 device.put("password", addr.password());
642 device.put("sshkey", addr.sshkey());
643 cfgService.applyConfig(deviceId, NetconfDeviceConfig.class, device);
644 } else {
645 // This is a corner case where new updated config is
646 // pushed with old /app tree after an initial with the
647 // new device/ tree. Since old method will be deprecated
648 // it's ok to ignore
649 log.warn("Config for device {} already exists, ignoring", deviceId);
650 }
651
652 });
653 } catch (ConfigException e) {
654 log.error("Cannot read config error " + e);
655 }
656 }
657 }
658
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700659 /**
660 * Listener for configuration events.
661 */
andreaeb70a942015-10-16 21:34:46 -0700662 private class InternalNetworkConfigListener implements NetworkConfigListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530663
andreaeb70a942015-10-16 21:34:46 -0700664
665 @Override
666 public void event(NetworkConfigEvent event) {
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200667 if (event.configClass().equals(NetconfDeviceConfig.class)) {
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700668 executor.execute(() -> connectDevice((NetconfDeviceConfig) event.config().get()));
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200669 } else {
670 log.warn("Injecting device via this Json is deprecated, " +
671 "please put configuration under devices/ as shown in the wiki");
672 translateConfig();
673 }
674
Sanjay Se8dcfee2015-04-23 10:07:08 +0530675 }
676
andreaeb70a942015-10-16 21:34:46 -0700677 @Override
678 public boolean isRelevant(NetworkConfigEvent event) {
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200679 return (event.configClass().equals(NetconfDeviceConfig.class) ||
680 event.configClass().equals(NetconfProviderConfig.class)) &&
andreaeb70a942015-10-16 21:34:46 -0700681 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
682 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530683 }
684 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700685
686 /**
687 * Listener for core device events.
688 */
689 private class InternalDeviceListener implements DeviceListener {
690 @Override
691 public void event(DeviceEvent event) {
692 if ((event.type() == DeviceEvent.Type.DEVICE_ADDED)) {
693 executor.execute(() -> discoverPorts(event.subject().id()));
694 } else if ((event.type() == DeviceEvent.Type.DEVICE_REMOVED)) {
695 log.debug("removing device {}", event.subject().id());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700696 controller.disconnectDevice(event.subject().id(), true);
697 }
698 }
699
700 @Override
701 public boolean isRelevant(DeviceEvent event) {
702 if (mastershipService.getMasterFor(event.subject().id()) == null) {
703 return true;
704 }
Andrea Campanella968f93f2017-06-08 11:09:28 +0200705 return (SCHEME_NAME.equalsIgnoreCase(event.subject().annotations().value(AnnotationKeys.PROTOCOL)) ||
706 (SCHEME_NAME.equalsIgnoreCase(event.subject().id().uri().getScheme()))) &&
Michele Santuari576f09c2016-09-28 14:20:00 +0200707 mastershipService.isLocalMaster(event.subject().id());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700708 }
709 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530710}