blob: 8281f4e5546098bd6950d0ef8ecbb066442ef9b7 [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 Campanella7e6200a2016-03-21 09:48:40 -070074import java.net.Socket;
75import java.net.URI;
76import java.net.URISyntaxException;
77import java.util.Arrays;
Andrea Campanellac3627842017-04-04 18:06:54 +020078import java.util.Collection;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -070079import java.util.Dictionary;
mskala832d0472017-06-09 16:31:42 +020080import java.util.Map;
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -070081import java.util.Optional;
Andrea Campanella34cf65c2017-04-12 13:51:32 +020082import java.util.Set;
Andrea Campanella32813682017-10-23 15:29:24 +020083import java.util.concurrent.CompletableFuture;
mskala832d0472017-06-09 16:31:42 +020084import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella5c999e22016-03-01 15:12:53 -080085import java.util.concurrent.ExecutorService;
86import java.util.concurrent.Executors;
helenyrwufd296b62016-06-22 17:43:02 -070087import java.util.concurrent.ScheduledExecutorService;
88import java.util.concurrent.ScheduledFuture;
89import java.util.concurrent.TimeUnit;
mskala832d0472017-06-09 16:31:42 +020090import java.util.concurrent.atomic.AtomicInteger;
andreaeb70a942015-10-16 21:34:46 -070091
Yuta HIGUCHI1624df12016-07-21 16:54:33 -070092import static java.util.concurrent.Executors.newScheduledThreadPool;
Andrea Campanella5c999e22016-03-01 15:12:53 -080093import static org.onlab.util.Tools.groupedThreads;
andreaeb70a942015-10-16 21:34:46 -070094import static org.slf4j.LoggerFactory.getLogger;
95
Sanjay Se8dcfee2015-04-23 10:07:08 +053096/**
andreaeb70a942015-10-16 21:34:46 -070097 * Provider which uses an NETCONF controller to detect device.
Sanjay Se8dcfee2015-04-23 10:07:08 +053098 */
99@Component(immediate = true)
100public class NetconfDeviceProvider extends AbstractProvider
101 implements DeviceProvider {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700102
andreaeb70a942015-10-16 21:34:46 -0700103 private final Logger log = getLogger(getClass());
Sanjay Se8dcfee2015-04-23 10:07:08 +0530104
105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected DeviceProviderRegistry providerRegistry;
107
andreaeb70a942015-10-16 21:34:46 -0700108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella101417d2015-12-11 17:58:07 -0800109 protected NetconfController controller;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530110
111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -0700112 protected NetworkConfigRegistry cfgService;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530113
Thomas Vachuskad6811712015-04-29 21:37:04 -0700114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -0700115 protected CoreService coreService;
Thomas Vachuskad6811712015-04-29 21:37:04 -0700116
Aaron Kruglikov17b4c852016-01-15 16:37:04 -0800117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700118 protected DeviceService deviceService;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -0800119
120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700121 protected DeviceKeyAdminService deviceKeyAdminService;
122
123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
124 protected MastershipService mastershipService;
125
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
127 protected ComponentConfigService componentConfigService;
128
129
Michele Santuari576f09c2016-09-28 14:20:00 +0200130 protected static final String APP_NAME = "org.onosproject.netconf";
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200131 protected static final String SCHEME_NAME = "netconf";
Andrea Campanella101417d2015-12-11 17:58:07 -0800132 private static final String DEVICE_PROVIDER_PACKAGE = "org.onosproject.netconf.provider.device";
133 private static final String UNKNOWN = "unknown";
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700134 protected static final String ISNULL = "NetconfDeviceInfo is null";
135 private static final String IPADDRESS = "ipaddress";
136 private static final String NETCONF = "netconf";
137 private static final String PORT = "port";
helenyrwufd296b62016-06-22 17:43:02 -0700138 private static final int CORE_POOL_SIZE = 10;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700139
Thomas Vachuskadb29dcf2017-03-31 11:26:19 -0700140 private static final int DEFAULT_POLL_FREQUENCY_SECONDS = 30;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700141 @Property(name = "pollFrequency", intValue = DEFAULT_POLL_FREQUENCY_SECONDS,
142 label = "Configure poll frequency for port status and statistics; " +
Thomas Vachuskadb29dcf2017-03-31 11:26:19 -0700143 "default is 30 sec")
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700144 private int pollFrequency = DEFAULT_POLL_FREQUENCY_SECONDS;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530145
mskala832d0472017-06-09 16:31:42 +0200146 private static final int DEFAULT_MAX_RETRIES = 5;
147 @Property(name = "maxRetries", intValue = DEFAULT_MAX_RETRIES,
148 label = "Configure maximum allowed number of retries for obtaining list of ports; " +
149 "default is 5 times")
150 private int maxRetries = DEFAULT_MAX_RETRIES;
151
Michal Mach26a90fa2017-06-07 11:12:46 +0200152 protected ExecutorService executor =
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700153 Executors.newFixedThreadPool(5, groupedThreads("onos/netconfdeviceprovider",
154 "device-installer-%d", log));
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700155 protected ScheduledExecutorService connectionExecutor
156 = newScheduledThreadPool(CORE_POOL_SIZE,
157 groupedThreads("onos/netconfdeviceprovider",
158 "connection-executor-%d", log));
Andrea Campanella5c999e22016-03-01 15:12:53 -0800159
Michele Santuari576f09c2016-09-28 14:20:00 +0200160 protected DeviceProviderService providerService;
andreaeb70a942015-10-16 21:34:46 -0700161 private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700162 private InternalDeviceListener deviceListener = new InternalDeviceListener();
mskala832d0472017-06-09 16:31:42 +0200163 private final Map<DeviceId, AtomicInteger> retriedPortDiscoveryMap = new ConcurrentHashMap<>();
Michele Santuari576f09c2016-09-28 14:20:00 +0200164 protected ScheduledFuture<?> scheduledTask;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530165
Ray Milkey02710432018-02-13 17:08:28 -0800166 protected final ConfigFactory factory =
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700167 // TODO consider moving Config registration to NETCONF ctl bundle
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200168 new ConfigFactory<DeviceId, NetconfDeviceConfig>(
169 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700170 NetconfDeviceConfig.class, NetconfDeviceConfig.CONFIG_KEY) {
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200171 @Override
172 public NetconfDeviceConfig createConfig() {
173 return new NetconfDeviceConfig();
174 }
Ray Milkey02710432018-02-13 17:08:28 -0800175 };
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200176
Michele Santuari576f09c2016-09-28 14:20:00 +0200177 protected final NetworkConfigListener cfgListener = new InternalNetworkConfigListener();
andreaeb70a942015-10-16 21:34:46 -0700178 private ApplicationId appId;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700179 private boolean active;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530180
Sanjay Se8dcfee2015-04-23 10:07:08 +0530181
182 @Activate
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700183 public void activate(ComponentContext context) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700184 active = true;
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700185 componentConfigService.registerProperties(getClass());
Sanjay Se8dcfee2015-04-23 10:07:08 +0530186 providerService = providerRegistry.register(this);
Andrea Campanella101417d2015-12-11 17:58:07 -0800187 appId = coreService.registerApplication(APP_NAME);
Ray Milkey02710432018-02-13 17:08:28 -0800188 cfgService.registerConfigFactory(factory);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700189 cfgService.addListener(cfgListener);
andreaeb70a942015-10-16 21:34:46 -0700190 controller.addDeviceListener(innerNodeListener);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700191 deviceService.addListener(deviceListener);
Andrea Campanella7d8449b2016-03-02 10:16:42 -0800192 executor.execute(NetconfDeviceProvider.this::connectDevices);
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700193 modified(context);
Thomas Vachuskad6811712015-04-29 21:37:04 -0700194 log.info("Started");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530195 }
196
andreaeb70a942015-10-16 21:34:46 -0700197
Sanjay Se8dcfee2015-04-23 10:07:08 +0530198 @Deactivate
andreaeb70a942015-10-16 21:34:46 -0700199 public void deactivate() {
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700200 componentConfigService.unregisterProperties(getClass(), false);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700201 deviceService.removeListener(deviceListener);
202 active = false;
203 controller.getNetconfDevices().forEach(id -> {
204 deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(id.toString()));
205 controller.disconnectDevice(id, true);
206 });
Andrea Campanella86294db2016-03-07 11:42:49 -0800207 controller.removeDeviceListener(innerNodeListener);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700208 deviceService.removeListener(deviceListener);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530209 providerRegistry.unregister(this);
210 providerService = null;
mskala832d0472017-06-09 16:31:42 +0200211 retriedPortDiscoveryMap.clear();
Ray Milkey02710432018-02-13 17:08:28 -0800212 cfgService.unregisterConfigFactory(factory);
helenyrwufd296b62016-06-22 17:43:02 -0700213 scheduledTask.cancel(true);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700214 executor.shutdown();
Sanjay Seb5eebb2015-04-24 15:44:50 +0530215 log.info("Stopped");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530216 }
217
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700218
219 @Modified
220 public void modified(ComponentContext context) {
221 if (context != null) {
222 Dictionary<?, ?> properties = context.getProperties();
223 pollFrequency = Tools.getIntegerProperty(properties, "pollFrequency",
224 DEFAULT_POLL_FREQUENCY_SECONDS);
225 log.info("Configured. Poll frequency is configured to {} seconds", pollFrequency);
mskala832d0472017-06-09 16:31:42 +0200226
227 maxRetries = Tools.getIntegerProperty(properties, "maxRetries",
228 DEFAULT_MAX_RETRIES);
229 log.info("Configured. Number of retries is configured to {} times", maxRetries);
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700230 }
231 if (scheduledTask != null) {
232 scheduledTask.cancel(false);
233 }
234 scheduledTask = schedulePolling();
235 }
236
andreaeb70a942015-10-16 21:34:46 -0700237 public NetconfDeviceProvider() {
Andrea Campanella101417d2015-12-11 17:58:07 -0800238 super(new ProviderId(SCHEME_NAME, DEVICE_PROVIDER_PACKAGE));
Sanjay Se8dcfee2015-04-23 10:07:08 +0530239 }
240
helenyrwufd296b62016-06-22 17:43:02 -0700241 // Checks connection to devices in the config file
242 // every DEFAULT_POLL_FREQUENCY_SECONDS seconds.
243 private ScheduledFuture schedulePolling() {
Yuta HIGUCHIc2b82e32017-03-10 14:33:41 -0800244 return connectionExecutor.scheduleAtFixedRate(exceptionSafe(this::checkAndUpdateDevices),
Thomas Vachuskaf3aaa8d2017-03-31 10:43:58 -0700245 pollFrequency / 10,
246 pollFrequency, TimeUnit.SECONDS);
helenyrwufd296b62016-06-22 17:43:02 -0700247 }
248
Yuta HIGUCHIc2b82e32017-03-10 14:33:41 -0800249 private Runnable exceptionSafe(Runnable runnable) {
250 return new Runnable() {
251
252 @Override
253 public void run() {
254 try {
255 runnable.run();
256 } catch (Exception e) {
257 log.error("Unhandled Exception", e);
258 }
259 }
260 };
261 }
262
Sanjay Se8dcfee2015-04-23 10:07:08 +0530263 @Override
264 public void triggerProbe(DeviceId deviceId) {
andreaeb70a942015-10-16 21:34:46 -0700265 // TODO: This will be implemented later.
Yuta HIGUCHIe4cb8cf2017-05-01 22:18:28 -0700266 log.debug("Should be triggering probe on device {}", deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530267 }
268
269 @Override
270 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700271 if (active) {
272 switch (newRole) {
273 case MASTER:
274 initiateConnection(deviceId, newRole);
275 log.debug("Accepting mastership role change to {} for device {}", newRole, deviceId);
276 break;
277 case STANDBY:
278 controller.disconnectDevice(deviceId, false);
279 providerService.receivedRoleReply(deviceId, newRole, MastershipRole.STANDBY);
280 //else no-op
281 break;
282 case NONE:
283 controller.disconnectDevice(deviceId, false);
284 providerService.receivedRoleReply(deviceId, newRole, MastershipRole.NONE);
285 break;
286 default:
287 log.error("Unimplemented Mastership state : {}", newRole);
288
289 }
290 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530291 }
292
293 @Override
294 public boolean isReachable(DeviceId deviceId) {
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -0700295
296 boolean sessionExists =
297 Optional.ofNullable(controller.getDevicesMap().get(deviceId))
298 .map(NetconfDevice::isActive)
299 .orElse(false);
300 if (sessionExists) {
301 return true;
302 }
303
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700304 //FIXME this is a workaround util device state is shared
305 // between controller instances.
306 Device device = deviceService.getDevice(deviceId);
307 String ip;
308 int port;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700309 if (device != null) {
310 ip = device.annotations().value(IPADDRESS);
311 port = Integer.parseInt(device.annotations().value(PORT));
312 } else {
313 String[] info = deviceId.toString().split(":");
314 if (info.length == 3) {
315 ip = info[1];
316 port = Integer.parseInt(info[2]);
317 } else {
318 ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0])
319 && !el.equals(info[info.length - 1]))
320 .reduce((t, u) -> t + ":" + u)
321 .get();
322 log.debug("ip v6 {}", ip);
323 port = Integer.parseInt(info[info.length - 1]);
324 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530325 }
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -0700326 // FIXME just opening TCP session probably is not the appropriate
327 // method to test reachability.
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700328 //test connection to device opening a socket to it.
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -0700329 log.debug("Testing reachability for {}:{}", ip, port);
Yuta HIGUCHI70c21472017-04-20 20:40:46 -0700330 try (Socket socket = new Socket(ip, port)) {
Yuta HIGUCHI0454d702017-03-17 10:08:38 -0700331 log.debug("rechability of {}, {}, {}", deviceId, socket.isConnected(), !socket.isClosed());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700332 return socket.isConnected() && !socket.isClosed();
333 } catch (IOException e) {
334 log.info("Device {} is not reachable", deviceId);
Yuta HIGUCHI3a50b0d2017-07-10 11:11:54 -0700335 log.debug(" error details", e);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700336 return false;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700337 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530338 }
339
Saurav Dasa2d37502016-03-25 17:50:40 -0700340 @Override
341 public void changePortState(DeviceId deviceId, PortNumber portNumber,
342 boolean enable) {
Andrea Campanella32813682017-10-23 15:29:24 +0200343 Device device = deviceService.getDevice(deviceId);
344 if (mastershipService.isLocalMaster(deviceId)) {
345 if (device.is(PortAdmin.class)) {
346 PortAdmin portAdmin =
347 device.as(PortAdmin.class);
348 CompletableFuture<Boolean> modified;
349 if (enable) {
350 modified = portAdmin.enable(portNumber);
351 } else {
352 modified = portAdmin.disable(portNumber);
353 }
354 modified.thenAccept(result -> {
355 if (result) {
356 Port port = deviceService.getPort(deviceId, portNumber);
357 //rebuilding port description with admin state changed.
358 providerService.portStatusChanged(deviceId,
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800359 DefaultPortDescription.builder()
360 .withPortNumber(portNumber)
361 .isEnabled(enable)
362 .isRemoved(false)
363 .type(port.type())
364 .portSpeed(port.portSpeed())
365 .annotations((SparseAnnotations) port.annotations())
366 .build());
Andrea Campanella32813682017-10-23 15:29:24 +0200367 } else {
368 log.warn("Your device {} port {} status can't be changed to {}",
369 deviceId, portNumber, enable);
370 }
371 });
372 } else {
373 log.warn("Device {} does not support Port Admin", deviceId);
374 }
375 } else {
376 log.debug("Not master but {}, not changing port state", mastershipService.getLocalRole(deviceId));
377 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700378 }
379
Thomas Vachuska5b38dc02018-05-10 15:24:40 -0700380 @Override
381 public void triggerDisconnect(DeviceId deviceId) {
382 log.debug("Forcing disconnect for device {}", deviceId);
383 controller.disconnectDevice(deviceId, true);
384 }
385
andreaeb70a942015-10-16 21:34:46 -0700386 private class InnerNetconfDeviceListener implements NetconfDeviceListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530387
Andrea Campanella101417d2015-12-11 17:58:07 -0800388
andreaeb70a942015-10-16 21:34:46 -0700389 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700390 public void deviceAdded(DeviceId deviceId) {
391 //no-op
392 log.debug("Netconf device {} added to Netconf subController", deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530393 }
394
395 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700396 public void deviceRemoved(DeviceId deviceId) {
397 Preconditions.checkNotNull(deviceId, ISNULL);
helenyrwufd296b62016-06-22 17:43:02 -0700398
399 if (deviceService.getDevice(deviceId) != null) {
400 providerService.deviceDisconnected(deviceId);
mskala832d0472017-06-09 16:31:42 +0200401 retriedPortDiscoveryMap.remove(deviceId);
helenyrwufd296b62016-06-22 17:43:02 -0700402 log.debug("Netconf device {} removed from Netconf subController", deviceId);
403 } else {
404 log.warn("Netconf device {} does not exist in the store, " +
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530405 "it may already have been removed", deviceId);
helenyrwufd296b62016-06-22 17:43:02 -0700406 }
andreaeb70a942015-10-16 21:34:46 -0700407 }
408 }
409
andreaeb70a942015-10-16 21:34:46 -0700410 private void connectDevices() {
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200411 Set<DeviceId> deviceSubjects =
412 cfgService.getSubjects(DeviceId.class, NetconfDeviceConfig.class);
413 deviceSubjects.forEach(deviceId -> {
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700414 connectDevice(cfgService.getConfig(deviceId, NetconfDeviceConfig.class));
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200415 });
helenyrwufd296b62016-06-22 17:43:02 -0700416 }
417
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700418
419 private void connectDevice(NetconfDeviceConfig config) {
420 if (config == null) {
421 return;
422 }
423 DeviceId deviceId = config.subject();
424 if (!deviceId.uri().getScheme().equals(SCHEME_NAME)) {
425 // not under my scheme, skipping
426 log.trace("{} not my scheme, skipping", deviceId);
427 return;
428 }
429 DeviceDescription deviceDescription = createDeviceRepresentation(deviceId, config);
430 log.debug("Connecting NETCONF device {}, on {}:{} with username {}",
431 deviceId, config.ip(), config.port(), config.username());
432 storeDeviceKey(config.sshKey(), config.username(), config.password(), deviceId);
Andrea Campanella105736e2017-11-23 12:52:43 +0100433 retriedPortDiscoveryMap.put(deviceId, new AtomicInteger(0));
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700434 if (deviceService.getDevice(deviceId) == null) {
435 providerService.deviceConnected(deviceId, deviceDescription);
436 }
437 try {
Andrea Campanella105736e2017-11-23 12:52:43 +0100438 checkAndUpdateDevice(deviceId, deviceDescription, true);
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700439 } catch (Exception e) {
440 log.error("Unhandled exception checking {}", deviceId, e);
441 }
442 }
443
Andrea Campanella105736e2017-11-23 12:52:43 +0100444 private void checkAndUpdateDevice(DeviceId deviceId, DeviceDescription deviceDescription, boolean newlyConnected) {
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530445 Device device = deviceService.getDevice(deviceId);
446 if (device == null) {
Andrea Campanella105736e2017-11-23 12:52:43 +0100447 log.debug("Device {} has not been added to store, since it's not reachable", deviceId);
448 return;
449 }
450 boolean isReachable = isReachable(deviceId);
451 if (!isReachable && deviceService.isAvailable(deviceId)) {
452 providerService.deviceDisconnected(deviceId);
453 return;
454 } else if (newlyConnected) {
455 updateDeviceDescription(deviceId, deviceDescription, device);
456 }
457 if (isReachable && deviceService.isAvailable(deviceId) &&
458 mastershipService.isLocalMaster(deviceId)) {
459 //if ports are not discovered, retry the discovery
460 if (deviceService.getPorts(deviceId).isEmpty() &&
461 retriedPortDiscoveryMap.get(deviceId).getAndIncrement() < maxRetries) {
462 discoverPorts(deviceId);
463 }
464 updatePortStatistics(device);
465 }
466 }
467
468 private void updateDeviceDescription(DeviceId deviceId, DeviceDescription deviceDescription, Device device) {
469 if (device.is(DeviceDescriptionDiscovery.class)) {
470 if (mastershipService.isLocalMaster(deviceId)) {
471 DeviceDescriptionDiscovery deviceDescriptionDiscovery =
472 device.as(DeviceDescriptionDiscovery.class);
473 DeviceDescription updatedDeviceDescription =
474 deviceDescriptionDiscovery.discoverDeviceDetails();
475 if (updatedDeviceDescription != null &&
476 !descriptionEquals(device, updatedDeviceDescription)) {
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530477 providerService.deviceConnected(
478 deviceId, new DefaultDeviceDescription(
Andrea Campanella105736e2017-11-23 12:52:43 +0100479 updatedDeviceDescription, true,
480 updatedDeviceDescription.annotations()));
481 } else if (updatedDeviceDescription == null) {
482 providerService.deviceConnected(
483 deviceId, new DefaultDeviceDescription(
484 deviceDescription, true,
485 deviceDescription.annotations()));
Konstantinos Kanonakis4d67dd82016-08-05 12:18:52 -0500486 }
helenyrwufd296b62016-06-22 17:43:02 -0700487 }
Andrea Campanella105736e2017-11-23 12:52:43 +0100488 } else {
489 log.warn("No DeviceDescriptionDiscovery behaviour for device {} " +
490 "using DefaultDeviceDescription", deviceId);
491 providerService.deviceConnected(
492 deviceId, new DefaultDeviceDescription(
493 deviceDescription, true, deviceDescription.annotations()));
helenyrwufd296b62016-06-22 17:43:02 -0700494 }
495 }
496
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530497 private void updatePortStatistics(Device device) {
498 if (device.is(PortStatisticsDiscovery.class)) {
499 PortStatisticsDiscovery d = device.as(PortStatisticsDiscovery.class);
Andrea Campanellac3627842017-04-04 18:06:54 +0200500 Collection<PortStatistics> portStatistics = d.discoverPortStatistics();
501 if (portStatistics != null) {
502 providerService.updatePortStatistics(device.id(),
503 portStatistics);
504 }
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530505 } else {
Yuta HIGUCHIe4cb8cf2017-05-01 22:18:28 -0700506 log.debug("No port statistics getter behaviour for device {}",
507 device.id());
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530508 }
509 }
510
Michele Santuari00cc1f72016-09-08 17:05:24 +0200511 private boolean descriptionEquals(Device device, DeviceDescription updatedDeviceDescription) {
Yuta HIGUCHIf381fc72017-01-03 10:39:36 -0800512 return Objects.equal(device.id().uri(), updatedDeviceDescription.deviceUri())
Michele Santuari00cc1f72016-09-08 17:05:24 +0200513 && Objects.equal(device.type(), updatedDeviceDescription.type())
514 && Objects.equal(device.manufacturer(), updatedDeviceDescription.manufacturer())
515 && Objects.equal(device.hwVersion(), updatedDeviceDescription.hwVersion())
516 && Objects.equal(device.swVersion(), updatedDeviceDescription.swVersion())
517 && Objects.equal(device.serialNumber(), updatedDeviceDescription.serialNumber())
518 && Objects.equal(device.chassisId(), updatedDeviceDescription.chassisId())
519 && Objects.equal(device.annotations(), updatedDeviceDescription.annotations());
520 }
521
helenyrwufd296b62016-06-22 17:43:02 -0700522 private void checkAndUpdateDevices() {
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200523 Set<DeviceId> deviceSubjects =
524 cfgService.getSubjects(DeviceId.class, NetconfDeviceConfig.class);
525 deviceSubjects.forEach(deviceId -> {
526 NetconfDeviceConfig config =
527 cfgService.getConfig(deviceId, NetconfDeviceConfig.class);
528 DeviceDescription deviceDescription = createDeviceRepresentation(deviceId, config);
529 storeDeviceKey(config.sshKey(), config.username(), config.password(), deviceId);
Andrea Campanella105736e2017-11-23 12:52:43 +0100530 checkAndUpdateDevice(deviceId, deviceDescription, false);
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200531 });
andreaeb70a942015-10-16 21:34:46 -0700532 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530533
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200534 private DeviceDescription createDeviceRepresentation(DeviceId deviceId, NetconfDeviceConfig config) {
535 Preconditions.checkNotNull(deviceId, ISNULL);
536 //Netconf configuration object
537 ChassisId cid = new ChassisId();
538 String ipAddress = config.ip().toString();
539 SparseAnnotations annotations = DefaultAnnotations.builder()
540 .set(IPADDRESS, ipAddress)
541 .set(PORT, String.valueOf(config.port()))
542 .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase())
543 .build();
544 return new DefaultDeviceDescription(
545 deviceId.uri(),
546 Device.Type.SWITCH,
547 UNKNOWN, UNKNOWN,
548 UNKNOWN, UNKNOWN,
549 cid, false,
550 annotations);
551 }
552
553 private void storeDeviceKey(String sshKey, String username, String password, DeviceId deviceId) {
554 if (sshKey.equals("")) {
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600555 deviceKeyAdminService.addKey(
556 DeviceKey.createDeviceKeyUsingUsernamePassword(
557 DeviceKeyId.deviceKeyId(deviceId.toString()),
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200558 null, username, password));
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600559 } else {
560 deviceKeyAdminService.addKey(
561 DeviceKey.createDeviceKeyUsingSshKey(
562 DeviceKeyId.deviceKeyId(deviceId.toString()),
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200563 null, username, password,
564 sshKey));
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600565 }
566 }
567
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700568 private void initiateConnection(DeviceId deviceId, MastershipRole newRole) {
569 try {
570 if (isReachable(deviceId)) {
571 controller.connectDevice(deviceId);
572 providerService.receivedRoleReply(deviceId, newRole, MastershipRole.MASTER);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700573 }
574 } catch (Exception e) {
575 if (deviceService.getDevice(deviceId) != null) {
576 providerService.deviceDisconnected(deviceId);
577 }
578 deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(deviceId.toString()));
Ray Milkey986a47a2018-01-25 11:38:51 -0800579 throw new IllegalStateException(new NetconfException(
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200580 "Can't connect to NETCONF device " + deviceId, e));
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700581
582 }
583 }
584
585 private void discoverPorts(DeviceId deviceId) {
586 Device device = deviceService.getDevice(deviceId);
Andrea Campanella6c71a052016-04-22 11:56:31 -0700587 //TODO remove when PortDiscovery is removed from master
Ray Milkey640fedd2018-02-08 09:02:26 -0800588 if (device.is(DeviceDescriptionDiscovery.class)) {
Andrea Campanella6c71a052016-04-22 11:56:31 -0700589 DeviceDescriptionDiscovery deviceDescriptionDiscovery =
590 device.as(DeviceDescriptionDiscovery.class);
591 providerService.updatePorts(deviceId,
592 deviceDescriptionDiscovery.discoverPortDetails());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700593 } else {
594 log.warn("No portGetter behaviour for device {}", deviceId);
595 }
Gaurav Agrawaldab4d772017-03-29 15:15:13 +0530596
597 // Port statistics discovery
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530598 updatePortStatistics(device);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700599 }
600
601 /**
602 * Return the DeviceId about the device containing the URI.
603 *
Andrea Campanella6c71a052016-04-22 11:56:31 -0700604 * @param ip IP address
Ray Milkeyd4334db2016-04-05 17:39:44 -0700605 * @param port port number
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700606 * @return DeviceId
607 */
608 public DeviceId getDeviceId(String ip, int port) {
609 try {
610 return DeviceId.deviceId(new URI(NETCONF, ip + ":" + port, null));
611 } catch (URISyntaxException e) {
612 throw new IllegalArgumentException("Unable to build deviceID for device "
613 + ip + ":" + port, e);
614 }
615 }
616
617 /**
618 * Listener for configuration events.
619 */
andreaeb70a942015-10-16 21:34:46 -0700620 private class InternalNetworkConfigListener implements NetworkConfigListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530621
andreaeb70a942015-10-16 21:34:46 -0700622
623 @Override
624 public void event(NetworkConfigEvent event) {
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200625 if (event.configClass().equals(NetconfDeviceConfig.class)) {
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700626 executor.execute(() -> connectDevice((NetconfDeviceConfig) event.config().get()));
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200627 } else {
628 log.warn("Injecting device via this Json is deprecated, " +
629 "please put configuration under devices/ as shown in the wiki");
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200630 }
631
Sanjay Se8dcfee2015-04-23 10:07:08 +0530632 }
633
andreaeb70a942015-10-16 21:34:46 -0700634 @Override
635 public boolean isRelevant(NetworkConfigEvent event) {
Ray Milkey02710432018-02-13 17:08:28 -0800636 return (event.configClass().equals(NetconfDeviceConfig.class)) &&
andreaeb70a942015-10-16 21:34:46 -0700637 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
638 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530639 }
640 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700641
642 /**
643 * Listener for core device events.
644 */
645 private class InternalDeviceListener implements DeviceListener {
646 @Override
647 public void event(DeviceEvent event) {
Thomas Vachuska5b38dc02018-05-10 15:24:40 -0700648 executor.execute(() -> discoverPorts(event.subject().id()));
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700649 }
650
651 @Override
652 public boolean isRelevant(DeviceEvent event) {
Thomas Vachuska5b38dc02018-05-10 15:24:40 -0700653 if (event.type() != DeviceEvent.Type.DEVICE_ADDED) {
654 return false;
655 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700656 if (mastershipService.getMasterFor(event.subject().id()) == null) {
657 return true;
658 }
Andrea Campanella968f93f2017-06-08 11:09:28 +0200659 return (SCHEME_NAME.equalsIgnoreCase(event.subject().annotations().value(AnnotationKeys.PROTOCOL)) ||
660 (SCHEME_NAME.equalsIgnoreCase(event.subject().id().uri().getScheme()))) &&
Michele Santuari576f09c2016-09-28 14:20:00 +0200661 mastershipService.isLocalMaster(event.subject().id());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700662 }
663 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530664}