blob: 02c629ebb664640c7275c8a702679be307fb9bd8 [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
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;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -070024import org.apache.felix.scr.annotations.Modified;
25import org.apache.felix.scr.annotations.Property;
Sanjay Se8dcfee2015-04-23 10:07:08 +053026import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
28import org.onlab.packet.ChassisId;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -070029import org.onlab.util.Tools;
30import org.onosproject.cfg.ComponentConfigService;
andreaeb70a942015-10-16 21:34:46 -070031import org.onosproject.core.ApplicationId;
32import org.onosproject.core.CoreService;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070033import org.onosproject.mastership.MastershipService;
Marc De Leenheerb0d131c2016-03-01 20:34:59 -080034import org.onosproject.net.AnnotationKeys;
andreaeb70a942015-10-16 21:34:46 -070035import org.onosproject.net.DefaultAnnotations;
Sanjay Se8dcfee2015-04-23 10:07:08 +053036import org.onosproject.net.Device;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.MastershipRole;
Ray Milkey02710432018-02-13 17:08:28 -080039import org.onosproject.net.Port;
Saurav Dasa2d37502016-03-25 17:50:40 -070040import org.onosproject.net.PortNumber;
andreaeb70a942015-10-16 21:34:46 -070041import org.onosproject.net.SparseAnnotations;
Ray Milkey02710432018-02-13 17:08:28 -080042import org.onosproject.net.behaviour.PortAdmin;
andreaeb70a942015-10-16 21:34:46 -070043import org.onosproject.net.config.ConfigFactory;
44import org.onosproject.net.config.NetworkConfigEvent;
45import org.onosproject.net.config.NetworkConfigListener;
46import org.onosproject.net.config.NetworkConfigRegistry;
Andrea Campanella34cf65c2017-04-12 13:51:32 +020047import org.onosproject.net.config.basics.SubjectFactories;
Sanjay Se8dcfee2015-04-23 10:07:08 +053048import org.onosproject.net.device.DefaultDeviceDescription;
Andrea Campanella32813682017-10-23 15:29:24 +020049import org.onosproject.net.device.DefaultPortDescription;
Sanjay Se8dcfee2015-04-23 10:07:08 +053050import org.onosproject.net.device.DeviceDescription;
Andrea Campanella6c71a052016-04-22 11:56:31 -070051import org.onosproject.net.device.DeviceDescriptionDiscovery;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070052import org.onosproject.net.device.DeviceEvent;
53import org.onosproject.net.device.DeviceListener;
Sanjay Se8dcfee2015-04-23 10:07:08 +053054import org.onosproject.net.device.DeviceProvider;
55import org.onosproject.net.device.DeviceProviderRegistry;
56import org.onosproject.net.device.DeviceProviderService;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080057import org.onosproject.net.device.DeviceService;
Andrea Campanellac3627842017-04-04 18:06:54 +020058import org.onosproject.net.device.PortStatistics;
Gaurav Agrawaldab4d772017-03-29 15:15:13 +053059import org.onosproject.net.device.PortStatisticsDiscovery;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070060import org.onosproject.net.key.DeviceKey;
61import org.onosproject.net.key.DeviceKeyAdminService;
62import org.onosproject.net.key.DeviceKeyId;
Sanjay Se8dcfee2015-04-23 10:07:08 +053063import org.onosproject.net.provider.AbstractProvider;
64import org.onosproject.net.provider.ProviderId;
andreaeb70a942015-10-16 21:34:46 -070065import org.onosproject.netconf.NetconfController;
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -070066import org.onosproject.netconf.NetconfDevice;
andreaeb70a942015-10-16 21:34:46 -070067import org.onosproject.netconf.NetconfDeviceListener;
Andrea Campanella8b1cb672016-01-25 13:58:58 -080068import org.onosproject.netconf.NetconfException;
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -070069import org.onosproject.netconf.config.NetconfDeviceConfig;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -070070import org.osgi.service.component.ComponentContext;
Sanjay Se8dcfee2015-04-23 10:07:08 +053071import org.slf4j.Logger;
72
Andrea Campanella087ceb92015-12-07 09:58:34 -080073import java.io.IOException;
Andrea Campanella82e21892018-09-10 14:08:16 +020074import java.net.InetSocketAddress;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070075import java.net.Socket;
76import java.net.URI;
77import java.net.URISyntaxException;
78import java.util.Arrays;
Andrea Campanellac3627842017-04-04 18:06:54 +020079import java.util.Collection;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -070080import java.util.Dictionary;
mskala832d0472017-06-09 16:31:42 +020081import java.util.Map;
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -070082import java.util.Optional;
Andrea Campanella34cf65c2017-04-12 13:51:32 +020083import java.util.Set;
Andrea Campanella32813682017-10-23 15:29:24 +020084import java.util.concurrent.CompletableFuture;
mskala832d0472017-06-09 16:31:42 +020085import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella5c999e22016-03-01 15:12:53 -080086import java.util.concurrent.ExecutorService;
87import java.util.concurrent.Executors;
helenyrwufd296b62016-06-22 17:43:02 -070088import java.util.concurrent.ScheduledExecutorService;
89import java.util.concurrent.ScheduledFuture;
90import java.util.concurrent.TimeUnit;
mskala832d0472017-06-09 16:31:42 +020091import java.util.concurrent.atomic.AtomicInteger;
andreaeb70a942015-10-16 21:34:46 -070092
Yuta HIGUCHI1624df12016-07-21 16:54:33 -070093import static java.util.concurrent.Executors.newScheduledThreadPool;
Andrea Campanella5c999e22016-03-01 15:12:53 -080094import static org.onlab.util.Tools.groupedThreads;
andreaeb70a942015-10-16 21:34:46 -070095import static org.slf4j.LoggerFactory.getLogger;
96
Sanjay Se8dcfee2015-04-23 10:07:08 +053097/**
andreaeb70a942015-10-16 21:34:46 -070098 * Provider which uses an NETCONF controller to detect device.
Sanjay Se8dcfee2015-04-23 10:07:08 +053099 */
100@Component(immediate = true)
101public class NetconfDeviceProvider extends AbstractProvider
102 implements DeviceProvider {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700103
andreaeb70a942015-10-16 21:34:46 -0700104 private final Logger log = getLogger(getClass());
Sanjay Se8dcfee2015-04-23 10:07:08 +0530105
106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected DeviceProviderRegistry providerRegistry;
108
andreaeb70a942015-10-16 21:34:46 -0700109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella101417d2015-12-11 17:58:07 -0800110 protected NetconfController controller;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530111
112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -0700113 protected NetworkConfigRegistry cfgService;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530114
Thomas Vachuskad6811712015-04-29 21:37:04 -0700115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -0700116 protected CoreService coreService;
Thomas Vachuskad6811712015-04-29 21:37:04 -0700117
Aaron Kruglikov17b4c852016-01-15 16:37:04 -0800118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700119 protected DeviceService deviceService;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -0800120
121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700122 protected DeviceKeyAdminService deviceKeyAdminService;
123
124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
125 protected MastershipService mastershipService;
126
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
128 protected ComponentConfigService componentConfigService;
129
130
Michele Santuari576f09c2016-09-28 14:20:00 +0200131 protected static final String APP_NAME = "org.onosproject.netconf";
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200132 protected static final String SCHEME_NAME = "netconf";
Andrea Campanella101417d2015-12-11 17:58:07 -0800133 private static final String DEVICE_PROVIDER_PACKAGE = "org.onosproject.netconf.provider.device";
134 private static final String UNKNOWN = "unknown";
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700135 protected static final String ISNULL = "NetconfDeviceInfo is null";
136 private static final String IPADDRESS = "ipaddress";
137 private static final String NETCONF = "netconf";
138 private static final String PORT = "port";
helenyrwufd296b62016-06-22 17:43:02 -0700139 private static final int CORE_POOL_SIZE = 10;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700140
Thomas Vachuskadb29dcf2017-03-31 11:26:19 -0700141 private static final int DEFAULT_POLL_FREQUENCY_SECONDS = 30;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700142 @Property(name = "pollFrequency", intValue = DEFAULT_POLL_FREQUENCY_SECONDS,
143 label = "Configure poll frequency for port status and statistics; " +
Thomas Vachuskadb29dcf2017-03-31 11:26:19 -0700144 "default is 30 sec")
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700145 private int pollFrequency = DEFAULT_POLL_FREQUENCY_SECONDS;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530146
mskala832d0472017-06-09 16:31:42 +0200147 private static final int DEFAULT_MAX_RETRIES = 5;
148 @Property(name = "maxRetries", intValue = DEFAULT_MAX_RETRIES,
149 label = "Configure maximum allowed number of retries for obtaining list of ports; " +
150 "default is 5 times")
151 private int maxRetries = DEFAULT_MAX_RETRIES;
152
Michal Mach26a90fa2017-06-07 11:12:46 +0200153 protected ExecutorService executor =
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700154 Executors.newFixedThreadPool(5, groupedThreads("onos/netconfdeviceprovider",
155 "device-installer-%d", log));
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700156 protected ScheduledExecutorService connectionExecutor
157 = newScheduledThreadPool(CORE_POOL_SIZE,
158 groupedThreads("onos/netconfdeviceprovider",
159 "connection-executor-%d", log));
Andrea Campanella5c999e22016-03-01 15:12:53 -0800160
Michele Santuari576f09c2016-09-28 14:20:00 +0200161 protected DeviceProviderService providerService;
andreaeb70a942015-10-16 21:34:46 -0700162 private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700163 private InternalDeviceListener deviceListener = new InternalDeviceListener();
mskala832d0472017-06-09 16:31:42 +0200164 private final Map<DeviceId, AtomicInteger> retriedPortDiscoveryMap = new ConcurrentHashMap<>();
Michele Santuari576f09c2016-09-28 14:20:00 +0200165 protected ScheduledFuture<?> scheduledTask;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530166
Ray Milkey02710432018-02-13 17:08:28 -0800167 protected final ConfigFactory factory =
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700168 // TODO consider moving Config registration to NETCONF ctl bundle
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200169 new ConfigFactory<DeviceId, NetconfDeviceConfig>(
170 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700171 NetconfDeviceConfig.class, NetconfDeviceConfig.CONFIG_KEY) {
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200172 @Override
173 public NetconfDeviceConfig createConfig() {
174 return new NetconfDeviceConfig();
175 }
Ray Milkey02710432018-02-13 17:08:28 -0800176 };
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200177
Michele Santuari576f09c2016-09-28 14:20:00 +0200178 protected final NetworkConfigListener cfgListener = new InternalNetworkConfigListener();
andreaeb70a942015-10-16 21:34:46 -0700179 private ApplicationId appId;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700180 private boolean active;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530181
Sanjay Se8dcfee2015-04-23 10:07:08 +0530182
183 @Activate
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700184 public void activate(ComponentContext context) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700185 active = true;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700186 componentConfigService.registerProperties(getClass());
Sanjay Se8dcfee2015-04-23 10:07:08 +0530187 providerService = providerRegistry.register(this);
Andrea Campanella101417d2015-12-11 17:58:07 -0800188 appId = coreService.registerApplication(APP_NAME);
Ray Milkey02710432018-02-13 17:08:28 -0800189 cfgService.registerConfigFactory(factory);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700190 cfgService.addListener(cfgListener);
andreaeb70a942015-10-16 21:34:46 -0700191 controller.addDeviceListener(innerNodeListener);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700192 deviceService.addListener(deviceListener);
Andrea Campanella7d8449b2016-03-02 10:16:42 -0800193 executor.execute(NetconfDeviceProvider.this::connectDevices);
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700194 modified(context);
Thomas Vachuskad6811712015-04-29 21:37:04 -0700195 log.info("Started");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530196 }
197
andreaeb70a942015-10-16 21:34:46 -0700198
Sanjay Se8dcfee2015-04-23 10:07:08 +0530199 @Deactivate
andreaeb70a942015-10-16 21:34:46 -0700200 public void deactivate() {
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700201 componentConfigService.unregisterProperties(getClass(), false);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700202 deviceService.removeListener(deviceListener);
203 active = false;
204 controller.getNetconfDevices().forEach(id -> {
205 deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(id.toString()));
206 controller.disconnectDevice(id, true);
207 });
Andrea Campanella86294db2016-03-07 11:42:49 -0800208 controller.removeDeviceListener(innerNodeListener);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700209 deviceService.removeListener(deviceListener);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530210 providerRegistry.unregister(this);
211 providerService = null;
mskala832d0472017-06-09 16:31:42 +0200212 retriedPortDiscoveryMap.clear();
Ray Milkey02710432018-02-13 17:08:28 -0800213 cfgService.unregisterConfigFactory(factory);
helenyrwufd296b62016-06-22 17:43:02 -0700214 scheduledTask.cancel(true);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700215 executor.shutdown();
Sanjay Seb5eebb2015-04-24 15:44:50 +0530216 log.info("Stopped");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530217 }
218
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700219
220 @Modified
221 public void modified(ComponentContext context) {
222 if (context != null) {
223 Dictionary<?, ?> properties = context.getProperties();
224 pollFrequency = Tools.getIntegerProperty(properties, "pollFrequency",
225 DEFAULT_POLL_FREQUENCY_SECONDS);
226 log.info("Configured. Poll frequency is configured to {} seconds", pollFrequency);
mskala832d0472017-06-09 16:31:42 +0200227
228 maxRetries = Tools.getIntegerProperty(properties, "maxRetries",
229 DEFAULT_MAX_RETRIES);
230 log.info("Configured. Number of retries is configured to {} times", maxRetries);
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700231 }
232 if (scheduledTask != null) {
233 scheduledTask.cancel(false);
234 }
235 scheduledTask = schedulePolling();
236 }
237
andreaeb70a942015-10-16 21:34:46 -0700238 public NetconfDeviceProvider() {
Andrea Campanella101417d2015-12-11 17:58:07 -0800239 super(new ProviderId(SCHEME_NAME, DEVICE_PROVIDER_PACKAGE));
Sanjay Se8dcfee2015-04-23 10:07:08 +0530240 }
241
helenyrwufd296b62016-06-22 17:43:02 -0700242 // Checks connection to devices in the config file
243 // every DEFAULT_POLL_FREQUENCY_SECONDS seconds.
244 private ScheduledFuture schedulePolling() {
Yuta HIGUCHIc2b82e32017-03-10 14:33:41 -0800245 return connectionExecutor.scheduleAtFixedRate(exceptionSafe(this::checkAndUpdateDevices),
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700246 pollFrequency / 10,
247 pollFrequency, TimeUnit.SECONDS);
helenyrwufd296b62016-06-22 17:43:02 -0700248 }
249
Yuta HIGUCHIc2b82e32017-03-10 14:33:41 -0800250 private Runnable exceptionSafe(Runnable runnable) {
251 return new Runnable() {
252
253 @Override
254 public void run() {
255 try {
256 runnable.run();
257 } catch (Exception e) {
258 log.error("Unhandled Exception", e);
259 }
260 }
261 };
262 }
263
Sanjay Se8dcfee2015-04-23 10:07:08 +0530264 @Override
265 public void triggerProbe(DeviceId deviceId) {
andreaeb70a942015-10-16 21:34:46 -0700266 // TODO: This will be implemented later.
Yuta HIGUCHIe4cb8cf2017-05-01 22:18:28 -0700267 log.debug("Should be triggering probe on device {}", deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530268 }
269
270 @Override
271 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700272 if (active) {
273 switch (newRole) {
274 case MASTER:
275 initiateConnection(deviceId, newRole);
276 log.debug("Accepting mastership role change to {} for device {}", newRole, deviceId);
277 break;
278 case STANDBY:
279 controller.disconnectDevice(deviceId, false);
280 providerService.receivedRoleReply(deviceId, newRole, MastershipRole.STANDBY);
281 //else no-op
282 break;
283 case NONE:
284 controller.disconnectDevice(deviceId, false);
285 providerService.receivedRoleReply(deviceId, newRole, MastershipRole.NONE);
286 break;
287 default:
288 log.error("Unimplemented Mastership state : {}", newRole);
289
290 }
291 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530292 }
293
294 @Override
295 public boolean isReachable(DeviceId deviceId) {
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -0700296
297 boolean sessionExists =
298 Optional.ofNullable(controller.getDevicesMap().get(deviceId))
299 .map(NetconfDevice::isActive)
300 .orElse(false);
301 if (sessionExists) {
302 return true;
303 }
304
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700305 //FIXME this is a workaround util device state is shared
306 // between controller instances.
307 Device device = deviceService.getDevice(deviceId);
308 String ip;
309 int port;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700310 if (device != null) {
311 ip = device.annotations().value(IPADDRESS);
312 port = Integer.parseInt(device.annotations().value(PORT));
313 } else {
314 String[] info = deviceId.toString().split(":");
315 if (info.length == 3) {
316 ip = info[1];
317 port = Integer.parseInt(info[2]);
318 } else {
319 ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0])
320 && !el.equals(info[info.length - 1]))
321 .reduce((t, u) -> t + ":" + u)
322 .get();
323 log.debug("ip v6 {}", ip);
324 port = Integer.parseInt(info[info.length - 1]);
325 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530326 }
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -0700327 // FIXME just opening TCP session probably is not the appropriate
328 // method to test reachability.
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700329 //test connection to device opening a socket to it.
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -0700330 log.debug("Testing reachability for {}:{}", ip, port);
Andrea Campanella82e21892018-09-10 14:08:16 +0200331 Socket socket = new Socket();
332 try {
333 socket.connect(new InetSocketAddress(ip, port), 1000);
Yuta HIGUCHI0454d702017-03-17 10:08:38 -0700334 log.debug("rechability of {}, {}, {}", deviceId, socket.isConnected(), !socket.isClosed());
Andrea Campanella82e21892018-09-10 14:08:16 +0200335 boolean isConnected = socket.isConnected() && !socket.isClosed();
336 socket.close();
337 return isConnected;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700338 } catch (IOException e) {
339 log.info("Device {} is not reachable", deviceId);
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -0700340 log.debug(" error details", e);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700341 return false;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700342 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530343 }
344
Saurav Dasa2d37502016-03-25 17:50:40 -0700345 @Override
346 public void changePortState(DeviceId deviceId, PortNumber portNumber,
347 boolean enable) {
Andrea Campanella32813682017-10-23 15:29:24 +0200348 Device device = deviceService.getDevice(deviceId);
349 if (mastershipService.isLocalMaster(deviceId)) {
350 if (device.is(PortAdmin.class)) {
351 PortAdmin portAdmin =
352 device.as(PortAdmin.class);
353 CompletableFuture<Boolean> modified;
354 if (enable) {
355 modified = portAdmin.enable(portNumber);
356 } else {
357 modified = portAdmin.disable(portNumber);
358 }
359 modified.thenAccept(result -> {
360 if (result) {
361 Port port = deviceService.getPort(deviceId, portNumber);
362 //rebuilding port description with admin state changed.
363 providerService.portStatusChanged(deviceId,
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800364 DefaultPortDescription.builder()
365 .withPortNumber(portNumber)
366 .isEnabled(enable)
367 .isRemoved(false)
368 .type(port.type())
369 .portSpeed(port.portSpeed())
370 .annotations((SparseAnnotations) port.annotations())
371 .build());
Andrea Campanella32813682017-10-23 15:29:24 +0200372 } else {
373 log.warn("Your device {} port {} status can't be changed to {}",
374 deviceId, portNumber, enable);
375 }
376 });
377 } else {
378 log.warn("Device {} does not support Port Admin", deviceId);
379 }
380 } else {
381 log.debug("Not master but {}, not changing port state", mastershipService.getLocalRole(deviceId));
382 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700383 }
384
Thomas Vachuska5b38dc02018-05-10 15:24:40 -0700385 @Override
386 public void triggerDisconnect(DeviceId deviceId) {
387 log.debug("Forcing disconnect for device {}", deviceId);
388 controller.disconnectDevice(deviceId, true);
389 }
390
andreaeb70a942015-10-16 21:34:46 -0700391 private class InnerNetconfDeviceListener implements NetconfDeviceListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530392
Andrea Campanella101417d2015-12-11 17:58:07 -0800393
andreaeb70a942015-10-16 21:34:46 -0700394 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700395 public void deviceAdded(DeviceId deviceId) {
396 //no-op
397 log.debug("Netconf device {} added to Netconf subController", deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530398 }
399
400 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700401 public void deviceRemoved(DeviceId deviceId) {
402 Preconditions.checkNotNull(deviceId, ISNULL);
helenyrwufd296b62016-06-22 17:43:02 -0700403
404 if (deviceService.getDevice(deviceId) != null) {
405 providerService.deviceDisconnected(deviceId);
mskala832d0472017-06-09 16:31:42 +0200406 retriedPortDiscoveryMap.remove(deviceId);
helenyrwufd296b62016-06-22 17:43:02 -0700407 log.debug("Netconf device {} removed from Netconf subController", deviceId);
408 } else {
409 log.warn("Netconf device {} does not exist in the store, " +
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530410 "it may already have been removed", deviceId);
helenyrwufd296b62016-06-22 17:43:02 -0700411 }
andreaeb70a942015-10-16 21:34:46 -0700412 }
413 }
414
andreaeb70a942015-10-16 21:34:46 -0700415 private void connectDevices() {
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200416 Set<DeviceId> deviceSubjects =
417 cfgService.getSubjects(DeviceId.class, NetconfDeviceConfig.class);
418 deviceSubjects.forEach(deviceId -> {
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700419 connectDevice(cfgService.getConfig(deviceId, NetconfDeviceConfig.class));
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200420 });
helenyrwufd296b62016-06-22 17:43:02 -0700421 }
422
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700423
424 private void connectDevice(NetconfDeviceConfig config) {
425 if (config == null) {
426 return;
427 }
428 DeviceId deviceId = config.subject();
429 if (!deviceId.uri().getScheme().equals(SCHEME_NAME)) {
430 // not under my scheme, skipping
431 log.trace("{} not my scheme, skipping", deviceId);
432 return;
433 }
Andrea Campanella82e21892018-09-10 14:08:16 +0200434 if (!isReachable(deviceId)) {
435 log.warn("Can't connect to device {}", deviceId);
436 return;
437 }
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700438 DeviceDescription deviceDescription = createDeviceRepresentation(deviceId, config);
439 log.debug("Connecting NETCONF device {}, on {}:{} with username {}",
440 deviceId, config.ip(), config.port(), config.username());
441 storeDeviceKey(config.sshKey(), config.username(), config.password(), deviceId);
Andrea Campanella105736e2017-11-23 12:52:43 +0100442 retriedPortDiscoveryMap.put(deviceId, new AtomicInteger(0));
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700443 if (deviceService.getDevice(deviceId) == null) {
444 providerService.deviceConnected(deviceId, deviceDescription);
445 }
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700446 }
447
Andrea Campanella105736e2017-11-23 12:52:43 +0100448 private void checkAndUpdateDevice(DeviceId deviceId, DeviceDescription deviceDescription, boolean newlyConnected) {
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530449 Device device = deviceService.getDevice(deviceId);
450 if (device == null) {
Andrea Campanella105736e2017-11-23 12:52:43 +0100451 log.debug("Device {} has not been added to store, since it's not reachable", deviceId);
452 return;
453 }
454 boolean isReachable = isReachable(deviceId);
455 if (!isReachable && deviceService.isAvailable(deviceId)) {
456 providerService.deviceDisconnected(deviceId);
457 return;
Andrea Campanella82e21892018-09-10 14:08:16 +0200458 } else if (newlyConnected && mastershipService.isLocalMaster(deviceId)) {
Andrea Campanella105736e2017-11-23 12:52:43 +0100459 updateDeviceDescription(deviceId, deviceDescription, device);
460 }
461 if (isReachable && deviceService.isAvailable(deviceId) &&
462 mastershipService.isLocalMaster(deviceId)) {
463 //if ports are not discovered, retry the discovery
464 if (deviceService.getPorts(deviceId).isEmpty() &&
465 retriedPortDiscoveryMap.get(deviceId).getAndIncrement() < maxRetries) {
466 discoverPorts(deviceId);
467 }
468 updatePortStatistics(device);
469 }
470 }
471
472 private void updateDeviceDescription(DeviceId deviceId, DeviceDescription deviceDescription, Device device) {
473 if (device.is(DeviceDescriptionDiscovery.class)) {
474 if (mastershipService.isLocalMaster(deviceId)) {
475 DeviceDescriptionDiscovery deviceDescriptionDiscovery =
476 device.as(DeviceDescriptionDiscovery.class);
477 DeviceDescription updatedDeviceDescription =
478 deviceDescriptionDiscovery.discoverDeviceDetails();
479 if (updatedDeviceDescription != null &&
480 !descriptionEquals(device, updatedDeviceDescription)) {
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530481 providerService.deviceConnected(
482 deviceId, new DefaultDeviceDescription(
Andrea Campanella105736e2017-11-23 12:52:43 +0100483 updatedDeviceDescription, true,
484 updatedDeviceDescription.annotations()));
Andrea Campanella82e21892018-09-10 14:08:16 +0200485 } else if (updatedDeviceDescription == null && deviceDescription != null) {
Andrea Campanella105736e2017-11-23 12:52:43 +0100486 providerService.deviceConnected(
487 deviceId, new DefaultDeviceDescription(
488 deviceDescription, true,
489 deviceDescription.annotations()));
Andrea Campanella82e21892018-09-10 14:08:16 +0200490 } else {
491 providerService.deviceConnected(deviceId, new DefaultDeviceDescription(deviceId.uri(),
492 device.type(), device.manufacturer(), device.hwVersion(), device.swVersion(),
493 device.serialNumber(), device.chassisId(), true,
494 (SparseAnnotations) device.annotations()));
Konstantinos Kanonakis4d67dd82016-08-05 12:18:52 -0500495 }
helenyrwufd296b62016-06-22 17:43:02 -0700496 }
Andrea Campanella105736e2017-11-23 12:52:43 +0100497 } else {
498 log.warn("No DeviceDescriptionDiscovery behaviour for device {} " +
499 "using DefaultDeviceDescription", deviceId);
500 providerService.deviceConnected(
501 deviceId, new DefaultDeviceDescription(
502 deviceDescription, true, deviceDescription.annotations()));
helenyrwufd296b62016-06-22 17:43:02 -0700503 }
504 }
505
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530506 private void updatePortStatistics(Device device) {
507 if (device.is(PortStatisticsDiscovery.class)) {
508 PortStatisticsDiscovery d = device.as(PortStatisticsDiscovery.class);
Andrea Campanellac3627842017-04-04 18:06:54 +0200509 Collection<PortStatistics> portStatistics = d.discoverPortStatistics();
510 if (portStatistics != null) {
511 providerService.updatePortStatistics(device.id(),
512 portStatistics);
513 }
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530514 } else {
Yuta HIGUCHIe4cb8cf2017-05-01 22:18:28 -0700515 log.debug("No port statistics getter behaviour for device {}",
516 device.id());
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530517 }
518 }
519
Michele Santuari00cc1f72016-09-08 17:05:24 +0200520 private boolean descriptionEquals(Device device, DeviceDescription updatedDeviceDescription) {
Yuta HIGUCHIf381fc72017-01-03 10:39:36 -0800521 return Objects.equal(device.id().uri(), updatedDeviceDescription.deviceUri())
Michele Santuari00cc1f72016-09-08 17:05:24 +0200522 && Objects.equal(device.type(), updatedDeviceDescription.type())
523 && Objects.equal(device.manufacturer(), updatedDeviceDescription.manufacturer())
524 && Objects.equal(device.hwVersion(), updatedDeviceDescription.hwVersion())
525 && Objects.equal(device.swVersion(), updatedDeviceDescription.swVersion())
526 && Objects.equal(device.serialNumber(), updatedDeviceDescription.serialNumber())
527 && Objects.equal(device.chassisId(), updatedDeviceDescription.chassisId())
528 && Objects.equal(device.annotations(), updatedDeviceDescription.annotations());
529 }
530
helenyrwufd296b62016-06-22 17:43:02 -0700531 private void checkAndUpdateDevices() {
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200532 Set<DeviceId> deviceSubjects =
533 cfgService.getSubjects(DeviceId.class, NetconfDeviceConfig.class);
534 deviceSubjects.forEach(deviceId -> {
535 NetconfDeviceConfig config =
536 cfgService.getConfig(deviceId, NetconfDeviceConfig.class);
537 DeviceDescription deviceDescription = createDeviceRepresentation(deviceId, config);
538 storeDeviceKey(config.sshKey(), config.username(), config.password(), deviceId);
Andrea Campanella105736e2017-11-23 12:52:43 +0100539 checkAndUpdateDevice(deviceId, deviceDescription, false);
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200540 });
andreaeb70a942015-10-16 21:34:46 -0700541 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530542
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200543 private DeviceDescription createDeviceRepresentation(DeviceId deviceId, NetconfDeviceConfig config) {
544 Preconditions.checkNotNull(deviceId, ISNULL);
545 //Netconf configuration object
546 ChassisId cid = new ChassisId();
547 String ipAddress = config.ip().toString();
548 SparseAnnotations annotations = DefaultAnnotations.builder()
549 .set(IPADDRESS, ipAddress)
550 .set(PORT, String.valueOf(config.port()))
551 .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase())
Andrea Campanella82e21892018-09-10 14:08:16 +0200552 .set(AnnotationKeys.PROVIDER_MARK_ONLINE, String.valueOf(true))
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200553 .build();
554 return new DefaultDeviceDescription(
555 deviceId.uri(),
556 Device.Type.SWITCH,
557 UNKNOWN, UNKNOWN,
558 UNKNOWN, UNKNOWN,
559 cid, false,
560 annotations);
561 }
562
563 private void storeDeviceKey(String sshKey, String username, String password, DeviceId deviceId) {
564 if (sshKey.equals("")) {
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600565 deviceKeyAdminService.addKey(
566 DeviceKey.createDeviceKeyUsingUsernamePassword(
567 DeviceKeyId.deviceKeyId(deviceId.toString()),
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200568 null, username, password));
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600569 } else {
570 deviceKeyAdminService.addKey(
571 DeviceKey.createDeviceKeyUsingSshKey(
572 DeviceKeyId.deviceKeyId(deviceId.toString()),
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200573 null, username, password,
574 sshKey));
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600575 }
576 }
577
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700578 private void initiateConnection(DeviceId deviceId, MastershipRole newRole) {
579 try {
580 if (isReachable(deviceId)) {
Andrea Campanella82e21892018-09-10 14:08:16 +0200581 NetconfDevice device = controller.connectDevice(deviceId);
582 if (device != null) {
583 providerService.receivedRoleReply(deviceId, newRole, MastershipRole.MASTER);
584 try {
585 checkAndUpdateDevice(deviceId, null, true);
586 } catch (Exception e) {
587 log.error("Unhandled exception checking {}", deviceId, e);
588 }
589 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700590 }
591 } catch (Exception e) {
592 if (deviceService.getDevice(deviceId) != null) {
593 providerService.deviceDisconnected(deviceId);
594 }
595 deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(deviceId.toString()));
Ray Milkey986a47a2018-01-25 11:38:51 -0800596 throw new IllegalStateException(new NetconfException(
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200597 "Can't connect to NETCONF device " + deviceId, e));
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700598
599 }
600 }
601
602 private void discoverPorts(DeviceId deviceId) {
603 Device device = deviceService.getDevice(deviceId);
Andrea Campanella6c71a052016-04-22 11:56:31 -0700604 //TODO remove when PortDiscovery is removed from master
Ray Milkey640fedd2018-02-08 09:02:26 -0800605 if (device.is(DeviceDescriptionDiscovery.class)) {
Andrea Campanella6c71a052016-04-22 11:56:31 -0700606 DeviceDescriptionDiscovery deviceDescriptionDiscovery =
607 device.as(DeviceDescriptionDiscovery.class);
608 providerService.updatePorts(deviceId,
609 deviceDescriptionDiscovery.discoverPortDetails());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700610 } else {
611 log.warn("No portGetter behaviour for device {}", deviceId);
612 }
Gaurav Agrawaldab4d772017-03-29 15:15:13 +0530613
614 // Port statistics discovery
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530615 updatePortStatistics(device);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700616 }
617
618 /**
619 * Return the DeviceId about the device containing the URI.
620 *
Andrea Campanella6c71a052016-04-22 11:56:31 -0700621 * @param ip IP address
Ray Milkeyd4334db2016-04-05 17:39:44 -0700622 * @param port port number
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700623 * @return DeviceId
624 */
625 public DeviceId getDeviceId(String ip, int port) {
626 try {
627 return DeviceId.deviceId(new URI(NETCONF, ip + ":" + port, null));
628 } catch (URISyntaxException e) {
629 throw new IllegalArgumentException("Unable to build deviceID for device "
630 + ip + ":" + port, e);
631 }
632 }
633
634 /**
635 * Listener for configuration events.
636 */
andreaeb70a942015-10-16 21:34:46 -0700637 private class InternalNetworkConfigListener implements NetworkConfigListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530638
andreaeb70a942015-10-16 21:34:46 -0700639
640 @Override
641 public void event(NetworkConfigEvent event) {
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200642 if (event.configClass().equals(NetconfDeviceConfig.class)) {
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700643 executor.execute(() -> connectDevice((NetconfDeviceConfig) event.config().get()));
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200644 } else {
645 log.warn("Injecting device via this Json is deprecated, " +
646 "please put configuration under devices/ as shown in the wiki");
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200647 }
648
Sanjay Se8dcfee2015-04-23 10:07:08 +0530649 }
650
andreaeb70a942015-10-16 21:34:46 -0700651 @Override
652 public boolean isRelevant(NetworkConfigEvent event) {
Ray Milkey02710432018-02-13 17:08:28 -0800653 return (event.configClass().equals(NetconfDeviceConfig.class)) &&
andreaeb70a942015-10-16 21:34:46 -0700654 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
655 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530656 }
657 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700658
659 /**
660 * Listener for core device events.
661 */
662 private class InternalDeviceListener implements DeviceListener {
663 @Override
664 public void event(DeviceEvent event) {
Andrea Campanella82e21892018-09-10 14:08:16 +0200665 if (deviceService.isAvailable(event.subject().id())) {
666 executor.execute(() -> discoverPorts(event.subject().id()));
667 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700668 }
669
670 @Override
671 public boolean isRelevant(DeviceEvent event) {
Andrea Campanella82e21892018-09-10 14:08:16 +0200672 if (event.type() != DeviceEvent.Type.DEVICE_ADDED &&
673 event.type() != DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED) {
Thomas Vachuska5b38dc02018-05-10 15:24:40 -0700674 return false;
675 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700676 if (mastershipService.getMasterFor(event.subject().id()) == null) {
677 return true;
678 }
Andrea Campanella968f93f2017-06-08 11:09:28 +0200679 return (SCHEME_NAME.equalsIgnoreCase(event.subject().annotations().value(AnnotationKeys.PROTOCOL)) ||
680 (SCHEME_NAME.equalsIgnoreCase(event.subject().id().uri().getScheme()))) &&
Michele Santuari576f09c2016-09-28 14:20:00 +0200681 mastershipService.isLocalMaster(event.subject().id());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700682 }
683 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530684}