Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 16 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 17 | package org.onosproject.provider.netconf.device.impl; |
| 18 | |
Michele Santuari | 00cc1f7 | 2016-09-08 17:05:24 +0200 | [diff] [blame] | 19 | import com.google.common.base.Objects; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 20 | import com.google.common.base.Preconditions; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Modified; |
| 25 | import org.apache.felix.scr.annotations.Property; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.onlab.packet.ChassisId; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 29 | import org.onlab.util.Tools; |
| 30 | import org.onosproject.cfg.ComponentConfigService; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 33 | import org.onosproject.mastership.MastershipService; |
Marc De Leenheer | b0d131c | 2016-03-01 20:34:59 -0800 | [diff] [blame] | 34 | import org.onosproject.net.AnnotationKeys; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 35 | import org.onosproject.net.DefaultAnnotations; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 36 | import org.onosproject.net.Device; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.MastershipRole; |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 39 | import org.onosproject.net.Port; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 40 | import org.onosproject.net.PortNumber; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 41 | import org.onosproject.net.SparseAnnotations; |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 42 | import org.onosproject.net.behaviour.PortAdmin; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 43 | import org.onosproject.net.config.ConfigFactory; |
| 44 | import org.onosproject.net.config.NetworkConfigEvent; |
| 45 | import org.onosproject.net.config.NetworkConfigListener; |
| 46 | import org.onosproject.net.config.NetworkConfigRegistry; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 47 | import org.onosproject.net.config.basics.SubjectFactories; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 48 | import org.onosproject.net.device.DefaultDeviceDescription; |
Andrea Campanella | 3281368 | 2017-10-23 15:29:24 +0200 | [diff] [blame] | 49 | import org.onosproject.net.device.DefaultPortDescription; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 50 | import org.onosproject.net.device.DeviceDescription; |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 51 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 52 | import org.onosproject.net.device.DeviceEvent; |
| 53 | import org.onosproject.net.device.DeviceListener; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 54 | import org.onosproject.net.device.DeviceProvider; |
| 55 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 56 | import org.onosproject.net.device.DeviceProviderService; |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 57 | import org.onosproject.net.device.DeviceService; |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 58 | import org.onosproject.net.device.PortStatistics; |
Gaurav Agrawal | dab4d77 | 2017-03-29 15:15:13 +0530 | [diff] [blame] | 59 | import org.onosproject.net.device.PortStatisticsDiscovery; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 60 | import org.onosproject.net.key.DeviceKey; |
| 61 | import org.onosproject.net.key.DeviceKeyAdminService; |
| 62 | import org.onosproject.net.key.DeviceKeyId; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 63 | import org.onosproject.net.provider.AbstractProvider; |
| 64 | import org.onosproject.net.provider.ProviderId; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 65 | import org.onosproject.netconf.NetconfController; |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 66 | import org.onosproject.netconf.NetconfDevice; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 67 | import org.onosproject.netconf.NetconfDeviceListener; |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 68 | import org.onosproject.netconf.NetconfException; |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 69 | import org.onosproject.netconf.config.NetconfDeviceConfig; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 70 | import org.osgi.service.component.ComponentContext; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 71 | import org.slf4j.Logger; |
| 72 | |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 73 | import java.io.IOException; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 74 | import java.net.Socket; |
| 75 | import java.net.URI; |
| 76 | import java.net.URISyntaxException; |
| 77 | import java.util.Arrays; |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 78 | import java.util.Collection; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 79 | import java.util.Dictionary; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 80 | import java.util.Map; |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 81 | import java.util.Optional; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 82 | import java.util.Set; |
Andrea Campanella | 3281368 | 2017-10-23 15:29:24 +0200 | [diff] [blame] | 83 | import java.util.concurrent.CompletableFuture; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 84 | import java.util.concurrent.ConcurrentHashMap; |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 85 | import java.util.concurrent.ExecutorService; |
| 86 | import java.util.concurrent.Executors; |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 87 | import java.util.concurrent.ScheduledExecutorService; |
| 88 | import java.util.concurrent.ScheduledFuture; |
| 89 | import java.util.concurrent.TimeUnit; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 90 | import java.util.concurrent.atomic.AtomicInteger; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 91 | |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 92 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 93 | import static org.onlab.util.Tools.groupedThreads; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 94 | import static org.slf4j.LoggerFactory.getLogger; |
| 95 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 96 | /** |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 97 | * Provider which uses an NETCONF controller to detect device. |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 98 | */ |
| 99 | @Component(immediate = true) |
| 100 | public class NetconfDeviceProvider extends AbstractProvider |
| 101 | implements DeviceProvider { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 102 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 103 | private final Logger log = getLogger(getClass()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 104 | |
| 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | protected DeviceProviderRegistry providerRegistry; |
| 107 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 109 | protected NetconfController controller; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 110 | |
| 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 112 | protected NetworkConfigRegistry cfgService; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 113 | |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 115 | protected CoreService coreService; |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 116 | |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 118 | protected DeviceService deviceService; |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 121 | protected DeviceKeyAdminService deviceKeyAdminService; |
| 122 | |
| 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | protected MastershipService mastershipService; |
| 125 | |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 127 | protected ComponentConfigService componentConfigService; |
| 128 | |
| 129 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 130 | protected static final String APP_NAME = "org.onosproject.netconf"; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 131 | protected static final String SCHEME_NAME = "netconf"; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 132 | private static final String DEVICE_PROVIDER_PACKAGE = "org.onosproject.netconf.provider.device"; |
| 133 | private static final String UNKNOWN = "unknown"; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 134 | 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"; |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 138 | private static final int CORE_POOL_SIZE = 10; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 139 | |
Thomas Vachuska | db29dcf | 2017-03-31 11:26:19 -0700 | [diff] [blame] | 140 | private static final int DEFAULT_POLL_FREQUENCY_SECONDS = 30; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 141 | @Property(name = "pollFrequency", intValue = DEFAULT_POLL_FREQUENCY_SECONDS, |
| 142 | label = "Configure poll frequency for port status and statistics; " + |
Thomas Vachuska | db29dcf | 2017-03-31 11:26:19 -0700 | [diff] [blame] | 143 | "default is 30 sec") |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 144 | private int pollFrequency = DEFAULT_POLL_FREQUENCY_SECONDS; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 145 | |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 146 | 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 Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 152 | protected ExecutorService executor = |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 153 | Executors.newFixedThreadPool(5, groupedThreads("onos/netconfdeviceprovider", |
| 154 | "device-installer-%d", log)); |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 155 | protected ScheduledExecutorService connectionExecutor |
| 156 | = newScheduledThreadPool(CORE_POOL_SIZE, |
| 157 | groupedThreads("onos/netconfdeviceprovider", |
| 158 | "connection-executor-%d", log)); |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 159 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 160 | protected DeviceProviderService providerService; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 161 | private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener(); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 162 | private InternalDeviceListener deviceListener = new InternalDeviceListener(); |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 163 | private final Map<DeviceId, AtomicInteger> retriedPortDiscoveryMap = new ConcurrentHashMap<>(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 164 | protected ScheduledFuture<?> scheduledTask; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 165 | |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 166 | protected final ConfigFactory factory = |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 167 | // TODO consider moving Config registration to NETCONF ctl bundle |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 168 | new ConfigFactory<DeviceId, NetconfDeviceConfig>( |
| 169 | SubjectFactories.DEVICE_SUBJECT_FACTORY, |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 170 | NetconfDeviceConfig.class, NetconfDeviceConfig.CONFIG_KEY) { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 171 | @Override |
| 172 | public NetconfDeviceConfig createConfig() { |
| 173 | return new NetconfDeviceConfig(); |
| 174 | } |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 175 | }; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 176 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 177 | protected final NetworkConfigListener cfgListener = new InternalNetworkConfigListener(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 178 | private ApplicationId appId; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 179 | private boolean active; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 180 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 181 | |
| 182 | @Activate |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 183 | public void activate(ComponentContext context) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 184 | active = true; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 185 | componentConfigService.registerProperties(getClass()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 186 | providerService = providerRegistry.register(this); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 187 | appId = coreService.registerApplication(APP_NAME); |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 188 | cfgService.registerConfigFactory(factory); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 189 | cfgService.addListener(cfgListener); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 190 | controller.addDeviceListener(innerNodeListener); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 191 | deviceService.addListener(deviceListener); |
Andrea Campanella | 7d8449b | 2016-03-02 10:16:42 -0800 | [diff] [blame] | 192 | executor.execute(NetconfDeviceProvider.this::connectDevices); |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 193 | modified(context); |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 194 | log.info("Started"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 195 | } |
| 196 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 197 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 198 | @Deactivate |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 199 | public void deactivate() { |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 200 | componentConfigService.unregisterProperties(getClass(), false); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 201 | 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 Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 207 | controller.removeDeviceListener(innerNodeListener); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 208 | deviceService.removeListener(deviceListener); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 209 | providerRegistry.unregister(this); |
| 210 | providerService = null; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 211 | retriedPortDiscoveryMap.clear(); |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 212 | cfgService.unregisterConfigFactory(factory); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 213 | scheduledTask.cancel(true); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 214 | executor.shutdown(); |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 215 | log.info("Stopped"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 216 | } |
| 217 | |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 218 | |
| 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); |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 226 | |
| 227 | maxRetries = Tools.getIntegerProperty(properties, "maxRetries", |
| 228 | DEFAULT_MAX_RETRIES); |
| 229 | log.info("Configured. Number of retries is configured to {} times", maxRetries); |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 230 | } |
| 231 | if (scheduledTask != null) { |
| 232 | scheduledTask.cancel(false); |
| 233 | } |
| 234 | scheduledTask = schedulePolling(); |
| 235 | } |
| 236 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 237 | public NetconfDeviceProvider() { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 238 | super(new ProviderId(SCHEME_NAME, DEVICE_PROVIDER_PACKAGE)); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 239 | } |
| 240 | |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 241 | // Checks connection to devices in the config file |
| 242 | // every DEFAULT_POLL_FREQUENCY_SECONDS seconds. |
| 243 | private ScheduledFuture schedulePolling() { |
Yuta HIGUCHI | c2b82e3 | 2017-03-10 14:33:41 -0800 | [diff] [blame] | 244 | return connectionExecutor.scheduleAtFixedRate(exceptionSafe(this::checkAndUpdateDevices), |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 245 | pollFrequency / 10, |
| 246 | pollFrequency, TimeUnit.SECONDS); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Yuta HIGUCHI | c2b82e3 | 2017-03-10 14:33:41 -0800 | [diff] [blame] | 249 | 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 S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 263 | @Override |
| 264 | public void triggerProbe(DeviceId deviceId) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 265 | // TODO: This will be implemented later. |
Yuta HIGUCHI | e4cb8cf | 2017-05-01 22:18:28 -0700 | [diff] [blame] | 266 | log.debug("Should be triggering probe on device {}", deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 271 | 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 S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public boolean isReachable(DeviceId deviceId) { |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 295 | |
| 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 304 | //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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 309 | 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 S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 325 | } |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 326 | // FIXME just opening TCP session probably is not the appropriate |
| 327 | // method to test reachability. |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 328 | //test connection to device opening a socket to it. |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 329 | log.debug("Testing reachability for {}:{}", ip, port); |
Ray Milkey | 33306ba | 2018-09-20 13:27:25 -0700 | [diff] [blame] | 330 | try (Socket socket = new Socket(ip, port)) { |
Yuta HIGUCHI | 0454d70 | 2017-03-17 10:08:38 -0700 | [diff] [blame] | 331 | log.debug("rechability of {}, {}, {}", deviceId, socket.isConnected(), !socket.isClosed()); |
Ray Milkey | 33306ba | 2018-09-20 13:27:25 -0700 | [diff] [blame] | 332 | return socket.isConnected() && !socket.isClosed(); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 333 | } catch (IOException e) { |
| 334 | log.info("Device {} is not reachable", deviceId); |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 335 | log.debug(" error details", e); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 336 | return false; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 337 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 338 | } |
| 339 | |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 340 | @Override |
| 341 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 342 | boolean enable) { |
Andrea Campanella | 3281368 | 2017-10-23 15:29:24 +0200 | [diff] [blame] | 343 | 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 HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 359 | 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 Campanella | 3281368 | 2017-10-23 15:29:24 +0200 | [diff] [blame] | 367 | } 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 Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 380 | @Override |
| 381 | public void triggerDisconnect(DeviceId deviceId) { |
| 382 | log.debug("Forcing disconnect for device {}", deviceId); |
| 383 | controller.disconnectDevice(deviceId, true); |
| 384 | } |
| 385 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 386 | private class InnerNetconfDeviceListener implements NetconfDeviceListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 387 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 388 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 389 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 390 | public void deviceAdded(DeviceId deviceId) { |
| 391 | //no-op |
| 392 | log.debug("Netconf device {} added to Netconf subController", deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 396 | public void deviceRemoved(DeviceId deviceId) { |
| 397 | Preconditions.checkNotNull(deviceId, ISNULL); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 398 | |
| 399 | if (deviceService.getDevice(deviceId) != null) { |
| 400 | providerService.deviceDisconnected(deviceId); |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 401 | retriedPortDiscoveryMap.remove(deviceId); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 402 | log.debug("Netconf device {} removed from Netconf subController", deviceId); |
| 403 | } else { |
| 404 | log.warn("Netconf device {} does not exist in the store, " + |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 405 | "it may already have been removed", deviceId); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 406 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 410 | private void connectDevices() { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 411 | Set<DeviceId> deviceSubjects = |
| 412 | cfgService.getSubjects(DeviceId.class, NetconfDeviceConfig.class); |
| 413 | deviceSubjects.forEach(deviceId -> { |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 414 | connectDevice(cfgService.getConfig(deviceId, NetconfDeviceConfig.class)); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 415 | }); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 418 | |
| 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 Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 433 | retriedPortDiscoveryMap.put(deviceId, new AtomicInteger(0)); |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 434 | if (deviceService.getDevice(deviceId) == null) { |
| 435 | providerService.deviceConnected(deviceId, deviceDescription); |
| 436 | } |
Ray Milkey | 33306ba | 2018-09-20 13:27:25 -0700 | [diff] [blame] | 437 | try { |
| 438 | checkAndUpdateDevice(deviceId, deviceDescription, true); |
| 439 | } catch (Exception e) { |
| 440 | log.error("Unhandled exception checking {}", deviceId, e); |
| 441 | } |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 444 | private void checkAndUpdateDevice(DeviceId deviceId, DeviceDescription deviceDescription, boolean newlyConnected) { |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 445 | Device device = deviceService.getDevice(deviceId); |
| 446 | if (device == null) { |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 447 | 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; |
Ray Milkey | 33306ba | 2018-09-20 13:27:25 -0700 | [diff] [blame] | 454 | } else if (newlyConnected) { |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 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 Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 477 | providerService.deviceConnected( |
| 478 | deviceId, new DefaultDeviceDescription( |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 479 | updatedDeviceDescription, true, |
| 480 | updatedDeviceDescription.annotations())); |
Ray Milkey | 33306ba | 2018-09-20 13:27:25 -0700 | [diff] [blame] | 481 | } else if (updatedDeviceDescription == null) { |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 482 | providerService.deviceConnected( |
| 483 | deviceId, new DefaultDeviceDescription( |
| 484 | deviceDescription, true, |
| 485 | deviceDescription.annotations())); |
Konstantinos Kanonakis | 4d67dd8 | 2016-08-05 12:18:52 -0500 | [diff] [blame] | 486 | } |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 487 | } |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 488 | } 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())); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 497 | private void updatePortStatistics(Device device) { |
| 498 | if (device.is(PortStatisticsDiscovery.class)) { |
| 499 | PortStatisticsDiscovery d = device.as(PortStatisticsDiscovery.class); |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 500 | Collection<PortStatistics> portStatistics = d.discoverPortStatistics(); |
| 501 | if (portStatistics != null) { |
| 502 | providerService.updatePortStatistics(device.id(), |
| 503 | portStatistics); |
| 504 | } |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 505 | } else { |
Yuta HIGUCHI | e4cb8cf | 2017-05-01 22:18:28 -0700 | [diff] [blame] | 506 | log.debug("No port statistics getter behaviour for device {}", |
| 507 | device.id()); |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
Michele Santuari | 00cc1f7 | 2016-09-08 17:05:24 +0200 | [diff] [blame] | 511 | private boolean descriptionEquals(Device device, DeviceDescription updatedDeviceDescription) { |
Yuta HIGUCHI | f381fc7 | 2017-01-03 10:39:36 -0800 | [diff] [blame] | 512 | return Objects.equal(device.id().uri(), updatedDeviceDescription.deviceUri()) |
Michele Santuari | 00cc1f7 | 2016-09-08 17:05:24 +0200 | [diff] [blame] | 513 | && 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 | |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 522 | private void checkAndUpdateDevices() { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 523 | 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 Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 530 | checkAndUpdateDevice(deviceId, deviceDescription, false); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 531 | }); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 532 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 533 | |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 534 | 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 Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 555 | deviceKeyAdminService.addKey( |
| 556 | DeviceKey.createDeviceKeyUsingUsernamePassword( |
| 557 | DeviceKeyId.deviceKeyId(deviceId.toString()), |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 558 | null, username, password)); |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 559 | } else { |
| 560 | deviceKeyAdminService.addKey( |
| 561 | DeviceKey.createDeviceKeyUsingSshKey( |
| 562 | DeviceKeyId.deviceKeyId(deviceId.toString()), |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 563 | null, username, password, |
| 564 | sshKey)); |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 568 | private void initiateConnection(DeviceId deviceId, MastershipRole newRole) { |
| 569 | try { |
| 570 | if (isReachable(deviceId)) { |
Ray Milkey | 33306ba | 2018-09-20 13:27:25 -0700 | [diff] [blame] | 571 | controller.connectDevice(deviceId); |
| 572 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.MASTER); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 573 | } |
| 574 | } catch (Exception e) { |
| 575 | if (deviceService.getDevice(deviceId) != null) { |
| 576 | providerService.deviceDisconnected(deviceId); |
| 577 | } |
| 578 | deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(deviceId.toString())); |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 579 | throw new IllegalStateException(new NetconfException( |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 580 | "Can't connect to NETCONF device " + deviceId, e)); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 581 | |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | private void discoverPorts(DeviceId deviceId) { |
| 586 | Device device = deviceService.getDevice(deviceId); |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 587 | //TODO remove when PortDiscovery is removed from master |
Ray Milkey | 640fedd | 2018-02-08 09:02:26 -0800 | [diff] [blame] | 588 | if (device.is(DeviceDescriptionDiscovery.class)) { |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 589 | DeviceDescriptionDiscovery deviceDescriptionDiscovery = |
| 590 | device.as(DeviceDescriptionDiscovery.class); |
| 591 | providerService.updatePorts(deviceId, |
| 592 | deviceDescriptionDiscovery.discoverPortDetails()); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 593 | } else { |
| 594 | log.warn("No portGetter behaviour for device {}", deviceId); |
| 595 | } |
Gaurav Agrawal | dab4d77 | 2017-03-29 15:15:13 +0530 | [diff] [blame] | 596 | |
| 597 | // Port statistics discovery |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 598 | updatePortStatistics(device); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /** |
| 602 | * Return the DeviceId about the device containing the URI. |
| 603 | * |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 604 | * @param ip IP address |
Ray Milkey | d4334db | 2016-04-05 17:39:44 -0700 | [diff] [blame] | 605 | * @param port port number |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 606 | * @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 | */ |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 620 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 621 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 622 | |
| 623 | @Override |
| 624 | public void event(NetworkConfigEvent event) { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 625 | if (event.configClass().equals(NetconfDeviceConfig.class)) { |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 626 | executor.execute(() -> connectDevice((NetconfDeviceConfig) event.config().get())); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 627 | } else { |
| 628 | log.warn("Injecting device via this Json is deprecated, " + |
| 629 | "please put configuration under devices/ as shown in the wiki"); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 632 | } |
| 633 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 634 | @Override |
| 635 | public boolean isRelevant(NetworkConfigEvent event) { |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 636 | return (event.configClass().equals(NetconfDeviceConfig.class)) && |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 637 | (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 638 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 639 | } |
| 640 | } |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 641 | |
| 642 | /** |
| 643 | * Listener for core device events. |
| 644 | */ |
| 645 | private class InternalDeviceListener implements DeviceListener { |
| 646 | @Override |
| 647 | public void event(DeviceEvent event) { |
Ray Milkey | 33306ba | 2018-09-20 13:27:25 -0700 | [diff] [blame] | 648 | executor.execute(() -> discoverPorts(event.subject().id())); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | @Override |
| 652 | public boolean isRelevant(DeviceEvent event) { |
Ray Milkey | 33306ba | 2018-09-20 13:27:25 -0700 | [diff] [blame] | 653 | if (event.type() != DeviceEvent.Type.DEVICE_ADDED) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 654 | return false; |
| 655 | } |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 656 | if (mastershipService.getMasterFor(event.subject().id()) == null) { |
| 657 | return true; |
| 658 | } |
Andrea Campanella | 968f93f | 2017-06-08 11:09:28 +0200 | [diff] [blame] | 659 | return (SCHEME_NAME.equalsIgnoreCase(event.subject().annotations().value(AnnotationKeys.PROTOCOL)) || |
| 660 | (SCHEME_NAME.equalsIgnoreCase(event.subject().id().uri().getScheme()))) && |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 661 | mastershipService.isLocalMaster(event.subject().id()); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 662 | } |
| 663 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 664 | } |