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 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 20 | import org.apache.commons.lang3.tuple.Triple; |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 21 | import com.google.common.util.concurrent.Striped; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 22 | import org.onlab.packet.ChassisId; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 23 | import org.onlab.util.Tools; |
| 24 | import org.onosproject.cfg.ComponentConfigService; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 25 | import org.onosproject.core.CoreService; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 26 | import org.onosproject.mastership.MastershipService; |
Marc De Leenheer | b0d131c | 2016-03-01 20:34:59 -0800 | [diff] [blame] | 27 | import org.onosproject.net.AnnotationKeys; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 28 | import org.onosproject.net.DefaultAnnotations; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 29 | import org.onosproject.net.Device; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.MastershipRole; |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 32 | import org.onosproject.net.Port; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 33 | import org.onosproject.net.PortNumber; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 34 | import org.onosproject.net.SparseAnnotations; |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 35 | import org.onosproject.net.behaviour.PortAdmin; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 36 | import org.onosproject.net.config.ConfigFactory; |
| 37 | import org.onosproject.net.config.NetworkConfigEvent; |
| 38 | import org.onosproject.net.config.NetworkConfigListener; |
| 39 | import org.onosproject.net.config.NetworkConfigRegistry; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 40 | import org.onosproject.net.config.basics.SubjectFactories; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 41 | import org.onosproject.net.device.DefaultDeviceDescription; |
Andrea Campanella | 3281368 | 2017-10-23 15:29:24 +0200 | [diff] [blame] | 42 | import org.onosproject.net.device.DefaultPortDescription; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 43 | import org.onosproject.net.device.DeviceDescription; |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 44 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 45 | import org.onosproject.net.device.DeviceEvent; |
| 46 | import org.onosproject.net.device.DeviceListener; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 47 | import org.onosproject.net.device.DeviceProvider; |
| 48 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 49 | import org.onosproject.net.device.DeviceProviderService; |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 50 | import org.onosproject.net.device.DeviceService; |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 51 | import org.onosproject.net.device.PortStatistics; |
Gaurav Agrawal | dab4d77 | 2017-03-29 15:15:13 +0530 | [diff] [blame] | 52 | import org.onosproject.net.device.PortStatisticsDiscovery; |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 53 | import org.onosproject.net.driver.DefaultDriverData; |
| 54 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 55 | import org.onosproject.net.driver.Driver; |
| 56 | import org.onosproject.net.driver.DriverData; |
| 57 | import org.onosproject.net.driver.DriverHandler; |
| 58 | import org.onosproject.net.driver.DriverService; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 59 | import org.onosproject.net.key.DeviceKey; |
| 60 | import org.onosproject.net.key.DeviceKeyAdminService; |
| 61 | import org.onosproject.net.key.DeviceKeyId; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 62 | import org.onosproject.net.provider.AbstractProvider; |
| 63 | import org.onosproject.net.provider.ProviderId; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 64 | import org.onosproject.netconf.NetconfController; |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 65 | import org.onosproject.netconf.NetconfDevice; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 66 | import org.onosproject.netconf.NetconfDeviceListener; |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 67 | import org.onosproject.netconf.NetconfException; |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 68 | import org.onosproject.netconf.config.NetconfDeviceConfig; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 69 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 70 | import org.osgi.service.component.annotations.Activate; |
| 71 | import org.osgi.service.component.annotations.Component; |
| 72 | import org.osgi.service.component.annotations.Deactivate; |
| 73 | import org.osgi.service.component.annotations.Modified; |
| 74 | import org.osgi.service.component.annotations.Reference; |
| 75 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 76 | import org.slf4j.Logger; |
| 77 | |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 78 | import java.io.IOException; |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 79 | import java.net.InetSocketAddress; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 80 | import java.net.Socket; |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 81 | import java.util.Collection; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 82 | import java.util.Dictionary; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 83 | import java.util.Map; |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 84 | import java.util.Optional; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 85 | import java.util.Set; |
Andrea Campanella | 3281368 | 2017-10-23 15:29:24 +0200 | [diff] [blame] | 86 | import java.util.concurrent.CompletableFuture; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 87 | import java.util.concurrent.ConcurrentHashMap; |
DongRyeol Cha | 527e7b2 | 2019-04-19 13:24:53 +0900 | [diff] [blame^] | 88 | import java.util.concurrent.CancellationException; |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 89 | import java.util.concurrent.ExecutionException; |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 90 | import java.util.concurrent.ExecutorService; |
| 91 | import java.util.concurrent.Executors; |
DongRyeol Cha | 527e7b2 | 2019-04-19 13:24:53 +0900 | [diff] [blame^] | 92 | import java.util.concurrent.ForkJoinPool; |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 93 | import java.util.concurrent.ScheduledExecutorService; |
| 94 | import java.util.concurrent.ScheduledFuture; |
| 95 | import java.util.concurrent.TimeUnit; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 96 | import java.util.concurrent.atomic.AtomicInteger; |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 97 | import java.util.concurrent.locks.Lock; |
| 98 | import java.util.function.Supplier; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 99 | |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 100 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 101 | import static org.onlab.util.Tools.groupedThreads; |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 102 | import static org.onosproject.netconf.NetconfDeviceInfo.extractIpPortPath; |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 103 | import static org.onosproject.provider.netconf.device.impl.OsgiPropertyConstants.MAX_RETRIES; |
| 104 | import static org.onosproject.provider.netconf.device.impl.OsgiPropertyConstants.MAX_RETRIES_DEFAULT; |
| 105 | import static org.onosproject.provider.netconf.device.impl.OsgiPropertyConstants.POLL_FREQUENCY_SECONDS; |
| 106 | import static org.onosproject.provider.netconf.device.impl.OsgiPropertyConstants.POLL_FREQUENCY_SECONDS_DEFAULT; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 107 | import static org.slf4j.LoggerFactory.getLogger; |
| 108 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 109 | /** |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 110 | * Provider which uses an NETCONF controller to detect device. |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 111 | */ |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 112 | @Component(immediate = true, |
| 113 | property = { |
Andrea Campanella | 02f86c8 | 2019-03-18 16:57:27 -0700 | [diff] [blame] | 114 | POLL_FREQUENCY_SECONDS + ":Integer=" + POLL_FREQUENCY_SECONDS_DEFAULT, |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 115 | MAX_RETRIES + ":Integer=" + MAX_RETRIES_DEFAULT, |
| 116 | }) |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 117 | public class NetconfDeviceProvider extends AbstractProvider |
| 118 | implements DeviceProvider { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 119 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 120 | private final Logger log = getLogger(getClass()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 121 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 123 | protected DeviceProviderRegistry providerRegistry; |
| 124 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 126 | protected NetconfController controller; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 127 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 129 | protected NetworkConfigRegistry cfgService; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 130 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 132 | protected CoreService coreService; |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 133 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 135 | protected DeviceService deviceService; |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 136 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 138 | protected DriverService driverService; |
| 139 | |
| 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 141 | protected DeviceKeyAdminService deviceKeyAdminService; |
| 142 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 143 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 144 | protected MastershipService mastershipService; |
| 145 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 146 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 147 | protected ComponentConfigService componentConfigService; |
| 148 | |
| 149 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 150 | protected static final String APP_NAME = "org.onosproject.netconf"; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 151 | protected static final String SCHEME_NAME = "netconf"; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 152 | private static final String DEVICE_PROVIDER_PACKAGE = "org.onosproject.netconf.provider.device"; |
| 153 | private static final String UNKNOWN = "unknown"; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 154 | protected static final String ISNULL = "NetconfDeviceInfo is null"; |
| 155 | private static final String IPADDRESS = "ipaddress"; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 156 | private static final String PORT = "port"; |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 157 | private static final String PATH = "path"; |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 158 | private static final int CORE_POOL_SIZE = 10; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 159 | |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 160 | /** |
| 161 | * Configure poll frequency for port status and statistics; default is 30 sec. |
| 162 | */ |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 163 | private int pollFrequency = POLL_FREQUENCY_SECONDS_DEFAULT; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 164 | |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 165 | /** |
| 166 | * Configure maximum allowed number of retries for obtaining list of ports; default is 5 times. |
| 167 | */ |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 168 | private int maxRetries = MAX_RETRIES_DEFAULT; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 169 | |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 170 | protected ExecutorService connectionExecutor = Executors.newFixedThreadPool(CORE_POOL_SIZE, |
| 171 | groupedThreads("onos/netconfDeviceProviderConnection", |
| 172 | "connection-executor-%d", log)); |
| 173 | protected ScheduledExecutorService pollingExecutor = newScheduledThreadPool(CORE_POOL_SIZE, |
| 174 | groupedThreads("onos/netconfDeviceProviderPoll", |
| 175 | "polling-executor-%d", log)); |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 176 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 177 | protected DeviceProviderService providerService; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 178 | private final Map<DeviceId, AtomicInteger> retriedPortDiscoveryMap = new ConcurrentHashMap<>(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 179 | protected ScheduledFuture<?> scheduledTask; |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 180 | private final Striped<Lock> deviceLocks = Striped.lock(30); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 181 | |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 182 | protected final ConfigFactory factory = |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 183 | // TODO consider moving Config registration to NETCONF ctl bundle |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 184 | new ConfigFactory<DeviceId, NetconfDeviceConfig>( |
| 185 | SubjectFactories.DEVICE_SUBJECT_FACTORY, |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 186 | NetconfDeviceConfig.class, NetconfDeviceConfig.CONFIG_KEY) { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 187 | @Override |
| 188 | public NetconfDeviceConfig createConfig() { |
| 189 | return new NetconfDeviceConfig(); |
| 190 | } |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 191 | }; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 192 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 193 | protected final NetworkConfigListener cfgListener = new InternalNetworkConfigListener(); |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 194 | private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener(); |
| 195 | private InternalDeviceListener deviceListener = new InternalDeviceListener(); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 196 | private boolean active; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 197 | |
DongRyeol Cha | 527e7b2 | 2019-04-19 13:24:53 +0900 | [diff] [blame^] | 198 | private ForkJoinPool scheduledTaskPool = new ForkJoinPool(CORE_POOL_SIZE); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 199 | |
| 200 | @Activate |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 201 | public void activate(ComponentContext context) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 202 | active = true; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 203 | componentConfigService.registerProperties(getClass()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 204 | providerService = providerRegistry.register(this); |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 205 | coreService.registerApplication(APP_NAME); |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 206 | cfgService.registerConfigFactory(factory); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 207 | cfgService.addListener(cfgListener); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 208 | controller.addDeviceListener(innerNodeListener); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 209 | deviceService.addListener(deviceListener); |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 210 | pollingExecutor.execute(NetconfDeviceProvider.this::connectDevices); |
DongRyeol Cha | 4025900 | 2019-03-15 18:26:11 +0900 | [diff] [blame] | 211 | scheduledTask = schedulePolling(); |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 212 | modified(context); |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 213 | log.info("Started"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 214 | } |
| 215 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 216 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 217 | @Deactivate |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 218 | public void deactivate() { |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 219 | cfgService.removeListener(cfgListener); |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 220 | componentConfigService.unregisterProperties(getClass(), false); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 221 | deviceService.removeListener(deviceListener); |
| 222 | active = false; |
| 223 | controller.getNetconfDevices().forEach(id -> { |
| 224 | deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(id.toString())); |
| 225 | controller.disconnectDevice(id, true); |
| 226 | }); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 227 | controller.removeDeviceListener(innerNodeListener); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 228 | providerRegistry.unregister(this); |
| 229 | providerService = null; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 230 | retriedPortDiscoveryMap.clear(); |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 231 | cfgService.unregisterConfigFactory(factory); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 232 | scheduledTask.cancel(true); |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 233 | connectionExecutor.shutdown(); |
| 234 | pollingExecutor.shutdown(); |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 235 | log.info("Stopped"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 236 | } |
| 237 | |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 238 | |
| 239 | @Modified |
| 240 | public void modified(ComponentContext context) { |
| 241 | if (context != null) { |
| 242 | Dictionary<?, ?> properties = context.getProperties(); |
DongRyeol Cha | 4025900 | 2019-03-15 18:26:11 +0900 | [diff] [blame] | 243 | int newPollFrequency = Tools.getIntegerProperty(properties, POLL_FREQUENCY_SECONDS, |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 244 | POLL_FREQUENCY_SECONDS_DEFAULT); |
DongRyeol Cha | 4025900 | 2019-03-15 18:26:11 +0900 | [diff] [blame] | 245 | |
| 246 | if (newPollFrequency != pollFrequency) { |
| 247 | pollFrequency = newPollFrequency; |
| 248 | |
| 249 | if (scheduledTask != null) { |
| 250 | scheduledTask.cancel(false); |
| 251 | } |
| 252 | scheduledTask = schedulePolling(); |
| 253 | log.info("Configured. Poll frequency is configured to {} seconds", pollFrequency); |
| 254 | } |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 255 | |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 256 | maxRetries = Tools.getIntegerProperty(properties, MAX_RETRIES, MAX_RETRIES_DEFAULT); |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 257 | log.info("Configured. Number of retries is configured to {} times", maxRetries); |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 258 | } |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 259 | } |
| 260 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 261 | public NetconfDeviceProvider() { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 262 | super(new ProviderId(SCHEME_NAME, DEVICE_PROVIDER_PACKAGE)); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void triggerProbe(DeviceId deviceId) { |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 267 | //Not implemented, unused in netconf cases. |
| 268 | log.debug("Probing {} not implemented, not useful for NETCONF", deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 273 | log.debug("Request role change {}, {}", deviceId, newRole); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 274 | if (active) { |
| 275 | switch (newRole) { |
| 276 | case MASTER: |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 277 | if (controller.getNetconfDevice(deviceId) == null) { |
| 278 | connectionExecutor.execute(exceptionSafe(() -> withDeviceLock( |
| 279 | () -> initiateConnection(deviceId), deviceId).run())); |
| 280 | log.debug("Accepting mastership role change to {} for device {}", newRole, deviceId); |
| 281 | } |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 282 | break; |
| 283 | case STANDBY: |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 284 | //TODO this issue a warning on the first election/connection |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 285 | controller.disconnectDevice(deviceId, false); |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 286 | withDeviceLock( |
| 287 | () -> initiateConnection(deviceId, newRole), deviceId).run(); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 288 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.STANDBY); |
| 289 | //else no-op |
| 290 | break; |
| 291 | case NONE: |
| 292 | controller.disconnectDevice(deviceId, false); |
| 293 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.NONE); |
| 294 | break; |
| 295 | default: |
| 296 | log.error("Unimplemented Mastership state : {}", newRole); |
| 297 | |
| 298 | } |
| 299 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public boolean isReachable(DeviceId deviceId) { |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 304 | boolean sessionExists = |
| 305 | Optional.ofNullable(controller.getDevicesMap().get(deviceId)) |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 306 | .map(NetconfDevice::isActive) |
| 307 | .orElse(false); |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 308 | if (sessionExists) { |
| 309 | return true; |
| 310 | } |
| 311 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 312 | //FIXME this is a workaround util device state is shared |
| 313 | // between controller instances. |
| 314 | Device device = deviceService.getDevice(deviceId); |
| 315 | String ip; |
| 316 | int port; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 317 | if (device != null) { |
| 318 | ip = device.annotations().value(IPADDRESS); |
| 319 | port = Integer.parseInt(device.annotations().value(PORT)); |
| 320 | } else { |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 321 | Triple<String, Integer, Optional<String>> info = extractIpPortPath(deviceId); |
| 322 | ip = info.getLeft(); |
| 323 | port = info.getMiddle(); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 324 | } |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 325 | // FIXME just opening TCP session probably is not the appropriate |
| 326 | // method to test reachability. |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 327 | //test connection to device opening a socket to it. |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 328 | log.debug("Testing reachability for {}:{}", ip, port); |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 329 | Socket socket = new Socket(); |
| 330 | try { |
| 331 | socket.connect(new InetSocketAddress(ip, port), 1000); |
Yuta HIGUCHI | 0454d70 | 2017-03-17 10:08:38 -0700 | [diff] [blame] | 332 | log.debug("rechability of {}, {}, {}", deviceId, socket.isConnected(), !socket.isClosed()); |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 333 | boolean isConnected = socket.isConnected() && !socket.isClosed(); |
| 334 | socket.close(); |
| 335 | return isConnected; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 336 | } catch (IOException e) { |
| 337 | log.info("Device {} is not reachable", deviceId); |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 338 | log.debug(" error details", e); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 339 | return false; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 340 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 341 | } |
| 342 | |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 343 | @Override |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 344 | public void changePortState(DeviceId deviceId, PortNumber portNumber, boolean enable) { |
Andrea Campanella | 3281368 | 2017-10-23 15:29:24 +0200 | [diff] [blame] | 345 | Device device = deviceService.getDevice(deviceId); |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 346 | if (device == null) { |
| 347 | log.error("Device {} is not present in the store", deviceId); |
| 348 | return; |
Andrea Campanella | 3281368 | 2017-10-23 15:29:24 +0200 | [diff] [blame] | 349 | } |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 350 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 351 | log.info("Not master but {}, not changing port state", mastershipService.getLocalRole(deviceId)); |
| 352 | return; |
| 353 | } |
| 354 | if (!device.is(PortAdmin.class)) { |
| 355 | log.warn("Device {} does not support Port Admin", deviceId); |
| 356 | return; |
| 357 | } |
| 358 | PortAdmin portAdmin = device.as(PortAdmin.class); |
| 359 | CompletableFuture<Boolean> modified; |
| 360 | if (enable) { |
| 361 | modified = portAdmin.enable(portNumber); |
| 362 | } else { |
| 363 | modified = portAdmin.disable(portNumber); |
| 364 | } |
| 365 | modified.thenAccept(result -> { |
| 366 | if (result) { |
| 367 | Port port = deviceService.getPort(deviceId, portNumber); |
| 368 | //rebuilding port description with admin state changed. |
| 369 | providerService.portStatusChanged(deviceId, |
| 370 | DefaultPortDescription.builder() |
| 371 | .withPortNumber(portNumber) |
| 372 | .isEnabled(enable) |
| 373 | .isRemoved(false) |
| 374 | .type(port.type()) |
| 375 | .portSpeed(port.portSpeed()) |
| 376 | .annotations((SparseAnnotations) port.annotations()) |
| 377 | .build()); |
| 378 | } else { |
| 379 | log.warn("Your device {} port {} status can't be changed to {}", |
| 380 | deviceId, portNumber, enable); |
| 381 | } |
| 382 | }); |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 385 | @Override |
| 386 | public void triggerDisconnect(DeviceId deviceId) { |
| 387 | log.debug("Forcing disconnect for device {}", deviceId); |
| 388 | controller.disconnectDevice(deviceId, true); |
| 389 | } |
| 390 | |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 391 | private ScheduledFuture schedulePolling() { |
| 392 | return pollingExecutor.scheduleAtFixedRate(exceptionSafe(this::checkAndUpdateDevices), |
| 393 | pollFrequency / 10, |
| 394 | pollFrequency, TimeUnit.SECONDS); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 397 | private Runnable exceptionSafe(Runnable runnable) { |
| 398 | return () -> { |
| 399 | try { |
| 400 | runnable.run(); |
| 401 | } catch (Exception e) { |
| 402 | log.error("Unhandled Exception", e); |
| 403 | } |
| 404 | }; |
| 405 | } |
| 406 | |
| 407 | //Connecting devices with initial config |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 408 | private void connectDevices() { |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 409 | Set<DeviceId> deviceSubjects = cfgService.getSubjects(DeviceId.class, NetconfDeviceConfig.class); |
DongRyeol Cha | 07f0d37 | 2019-04-03 12:29:00 +0900 | [diff] [blame] | 410 | deviceSubjects.parallelStream().forEach(deviceId -> { |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 411 | connectionExecutor.execute(exceptionSafe(() -> runElectionFor(deviceId))); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 412 | }); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 415 | //updating keys and device info |
| 416 | private void checkAndUpdateDevices() { |
| 417 | Set<DeviceId> deviceSubjects = cfgService.getSubjects(DeviceId.class, NetconfDeviceConfig.class); |
DongRyeol Cha | 527e7b2 | 2019-04-19 13:24:53 +0900 | [diff] [blame^] | 418 | try { |
| 419 | scheduledTaskPool.submit(() -> { |
| 420 | deviceSubjects.parallelStream().forEach(deviceId -> { |
| 421 | log.debug("check and update {}", deviceId); |
| 422 | NetconfDeviceConfig config = cfgService.getConfig(deviceId, NetconfDeviceConfig.class); |
| 423 | storeDeviceKey(config.sshKey(), config.username(), config.password(), deviceId); |
| 424 | discoverOrUpdatePorts(deviceId); |
| 425 | }); |
| 426 | }).get(); |
| 427 | } catch (ExecutionException e) { |
| 428 | log.error("Can't update the devices due to {}", e.getMessage()); |
| 429 | } catch (InterruptedException | CancellationException e) { |
| 430 | log.info("Update device is cancelled due to {}", e.getMessage()); |
| 431 | } |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 432 | } |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 433 | |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 434 | //Saving device keys in the store |
| 435 | private void storeDeviceKey(String sshKey, String username, String password, DeviceId deviceId) { |
| 436 | if (sshKey.equals("")) { |
| 437 | deviceKeyAdminService.addKey( |
| 438 | DeviceKey.createDeviceKeyUsingUsernamePassword( |
| 439 | DeviceKeyId.deviceKeyId(deviceId.toString()), null, username, password)); |
| 440 | } else { |
| 441 | deviceKeyAdminService.addKey( |
| 442 | DeviceKey.createDeviceKeyUsingSshKey( |
| 443 | DeviceKeyId.deviceKeyId(deviceId.toString()), null, username, password, sshKey)); |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 444 | } |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | //running an election and applying the role to a given device |
| 448 | private void runElectionFor(DeviceId deviceId) { |
| 449 | //Triggering an election for the deviceId thus only master will connect |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 450 | if (!deviceId.uri().getScheme().equals(SCHEME_NAME)) { |
| 451 | // not under my scheme, skipping |
David K. Bainbridge | 1070de9 | 2019-01-29 22:23:11 -0800 | [diff] [blame] | 452 | log.debug("{} not of schema {}, skipping", deviceId, SCHEME_NAME); |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 453 | return; |
| 454 | } |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 455 | connectionExecutor.submit(exceptionSafe(() -> { |
| 456 | CompletableFuture<MastershipRole> role = mastershipService.requestRoleFor(deviceId); |
| 457 | try { |
| 458 | roleChanged(deviceId, role.get()); |
| 459 | } catch (InterruptedException | ExecutionException e) { |
| 460 | log.error("Can't get role for {} ", deviceId, e); |
| 461 | } |
| 462 | })); |
| 463 | } |
| 464 | |
| 465 | //initiating the SSh connection the a given device. |
| 466 | private void initiateConnection(DeviceId deviceId) { |
| 467 | |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 468 | if (!isReachable(deviceId)) { |
| 469 | log.warn("Can't connect to device {}", deviceId); |
| 470 | return; |
| 471 | } |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 472 | |
| 473 | try { |
| 474 | NetconfDevice deviceNetconf = controller.connectDevice(deviceId); |
| 475 | if (deviceNetconf != null) { |
| 476 | //storeDeviceKey(config.sshKey(), config.username(), config.password(), deviceId); |
| 477 | NetconfDeviceConfig config = cfgService.getConfig(deviceId, NetconfDeviceConfig.class); |
| 478 | //getting the device description |
| 479 | DeviceDescription deviceDescription = getDeviceDescription(deviceId, config); |
| 480 | //connecting device to ONOS |
| 481 | log.debug("Connected NETCONF device {}, on {}:{} with username {}", |
| 482 | deviceId, config.ip(), config.port(), |
| 483 | (config.path().isPresent() ? "/" + config.path().get() : ""), |
| 484 | config.username()); |
| 485 | providerService.deviceConnected(deviceId, deviceDescription); |
| 486 | } else { |
| 487 | mastershipService.relinquishMastership(deviceId); |
| 488 | deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(deviceId.toString())); |
| 489 | log.error("Can't connect to NETCONF device {}", deviceId); |
| 490 | } |
| 491 | } catch (Exception e) { |
| 492 | mastershipService.relinquishMastership(deviceId); |
| 493 | deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(deviceId.toString())); |
| 494 | throw new IllegalStateException(new NetconfException( |
| 495 | "Can't connect to NETCONF device " + deviceId, e)); |
| 496 | |
| 497 | } |
| 498 | |
| 499 | } |
| 500 | |
| 501 | private DeviceDescription getDeviceDescription(DeviceId deviceId, NetconfDeviceConfig config) { |
| 502 | Driver driver = driverService.getDriver(deviceId); |
| 503 | if (driver.hasBehaviour(DeviceDescriptionDiscovery.class)) { |
| 504 | final DriverData data = new DefaultDriverData(driver, deviceId); |
| 505 | final DriverHandler handler = new DefaultDriverHandler(data); |
| 506 | //creating the behaviour because the core has yet no notion of device. |
| 507 | DeviceDescriptionDiscovery deviceDescriptionDiscovery = |
| 508 | driver.createBehaviour(handler, DeviceDescriptionDiscovery.class); |
| 509 | return getDeviceRepresentation(deviceId, config, deviceDescriptionDiscovery); |
| 510 | } else { |
Andrea Campanella | 7eb0171 | 2019-02-06 10:02:15 +0100 | [diff] [blame] | 511 | return existingOrEmptyDescription(deviceId, config); |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 512 | } |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Andrea Campanella | 7eb0171 | 2019-02-06 10:02:15 +0100 | [diff] [blame] | 515 | private DeviceDescription getDeviceRepresentation(DeviceId deviceId, NetconfDeviceConfig config, |
| 516 | DeviceDescriptionDiscovery deviceDescriptionDiscovery) { |
| 517 | |
| 518 | DeviceDescription existingOrEmptyDescription = existingOrEmptyDescription(deviceId, config); |
| 519 | DeviceDescription newDescription = deviceDescriptionDiscovery.discoverDeviceDetails(); |
| 520 | if (newDescription == null) { |
| 521 | return existingOrEmptyDescription; |
| 522 | } |
| 523 | //merging and returning |
| 524 | return new DefaultDeviceDescription(newDescription, true, |
| 525 | DefaultAnnotations.merge((DefaultAnnotations) newDescription.annotations(), |
| 526 | existingOrEmptyDescription.annotations())); |
| 527 | } |
| 528 | |
| 529 | private DeviceDescription existingOrEmptyDescription(DeviceId deviceId, NetconfDeviceConfig config) { |
| 530 | Device device = deviceService.getDevice(deviceId); |
| 531 | |
| 532 | if (deviceService.getDevice(deviceId) != null) { |
| 533 | //getting the previous description |
| 534 | return new DefaultDeviceDescription(device.id().uri(), device.type(), |
| 535 | device.manufacturer(), device.hwVersion(), |
| 536 | device.swVersion(), device.serialNumber(), |
| 537 | device.chassisId(), (SparseAnnotations) device.annotations()); |
| 538 | } |
| 539 | |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 540 | ChassisId cid = new ChassisId(); |
| 541 | String ipAddress = config.ip().toString(); |
| 542 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder() |
| 543 | .set(IPADDRESS, ipAddress) |
| 544 | .set(PORT, String.valueOf(config.port())) |
| 545 | .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()); |
| 546 | if (config.path().isPresent()) { |
| 547 | annotations.set(PATH, config.path().get()); |
| 548 | } |
| 549 | return new DefaultDeviceDescription(deviceId.uri(), Device.Type.SWITCH, |
| 550 | UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, cid, true, annotations.build()); |
| 551 | } |
| 552 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 553 | /** |
| 554 | * Will appropriately create connections with the device. |
| 555 | * For Master role: will create secure transport and proxy sessions. |
| 556 | * For Standby role: will create only proxy session and disconnect secure transport session. |
| 557 | * For none role: will disconnect all sessions. |
| 558 | * |
| 559 | * @param deviceId device id |
| 560 | * @param newRole new role |
| 561 | */ |
| 562 | private void initiateConnection(DeviceId deviceId, MastershipRole newRole) { |
| 563 | try { |
| 564 | if (isReachable(deviceId)) { |
| 565 | NetconfDevice device = null; |
| 566 | if (newRole.equals(MastershipRole.MASTER)) { |
| 567 | device = controller.connectDevice(deviceId, true); |
| 568 | } else if (newRole.equals(MastershipRole.STANDBY)) { |
| 569 | device = controller.connectDevice(deviceId, false); |
| 570 | } |
| 571 | |
| 572 | if (device != null) { |
| 573 | providerService.receivedRoleReply(deviceId, newRole, newRole); |
| 574 | } else { |
| 575 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.NONE); |
| 576 | } |
| 577 | |
| 578 | } |
| 579 | } catch (Exception e) { |
| 580 | if (deviceService.getDevice(deviceId) != null) { |
| 581 | providerService.deviceDisconnected(deviceId); |
| 582 | } |
| 583 | deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(deviceId.toString())); |
| 584 | throw new IllegalStateException(new NetconfException( |
| 585 | "Can't connect to NETCONF device " + deviceId, e)); |
| 586 | } |
| 587 | } |
| 588 | |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 589 | |
| 590 | private void discoverOrUpdatePorts(DeviceId deviceId) { |
| 591 | retriedPortDiscoveryMap.put(deviceId, new AtomicInteger(0)); |
| 592 | AtomicInteger count = retriedPortDiscoveryMap.get(deviceId); |
| 593 | //TODO this does not enable port discovery if port changes. |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 594 | Device device = deviceService.getDevice(deviceId); |
| 595 | if (device == null) { |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 596 | log.debug("Cant' reach device {}, not updating ports", deviceId); |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 597 | return; |
| 598 | } |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 599 | if (deviceService.getPorts(deviceId).isEmpty() |
| 600 | && count != null && count.getAndIncrement() < maxRetries) { |
| 601 | if (device.is(DeviceDescriptionDiscovery.class)) { |
| 602 | providerService.updatePorts(deviceId, |
| 603 | device.as(DeviceDescriptionDiscovery.class).discoverPortDetails()); |
| 604 | } else { |
| 605 | log.warn("No DeviceDescirption behaviour for device {}", deviceId); |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 606 | } |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 607 | |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 608 | } |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 609 | updatePortStatistics(device); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 612 | private void updatePortStatistics(Device device) { |
| 613 | if (device.is(PortStatisticsDiscovery.class)) { |
| 614 | PortStatisticsDiscovery d = device.as(PortStatisticsDiscovery.class); |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 615 | Collection<PortStatistics> portStatistics = d.discoverPortStatistics(); |
| 616 | if (portStatistics != null) { |
| 617 | providerService.updatePortStatistics(device.id(), |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 618 | portStatistics); |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 619 | } |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 620 | } else { |
Yuta HIGUCHI | e4cb8cf | 2017-05-01 22:18:28 -0700 | [diff] [blame] | 621 | log.debug("No port statistics getter behaviour for device {}", |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 622 | device.id()); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 626 | private <U> U withDeviceLock(Supplier<U> task, DeviceId deviceId) { |
| 627 | final Lock lock = deviceLocks.get(deviceId); |
| 628 | lock.lock(); |
| 629 | try { |
| 630 | return task.get(); |
| 631 | } finally { |
| 632 | lock.unlock(); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | private Runnable withDeviceLock(Runnable task, DeviceId deviceId) { |
| 637 | // Wrapper of withDeviceLock(Supplier, ...) for void tasks. |
| 638 | return () -> withDeviceLock(() -> { |
| 639 | task.run(); |
| 640 | return null; |
| 641 | }, deviceId); |
| 642 | } |
| 643 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 644 | /** |
| 645 | * Listener for configuration events. |
| 646 | */ |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 647 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 648 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 649 | @Override |
| 650 | public void event(NetworkConfigEvent event) { |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 651 | if (event.configClass().equals(NetconfDeviceConfig.class) && event.config().isPresent()) { |
| 652 | connectionExecutor.execute(exceptionSafe(() -> |
| 653 | runElectionFor((DeviceId) event.config().get().subject()))); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 654 | } else { |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 655 | log.warn("Incorrect or absent Class for Netconf Configuration"); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 656 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 657 | } |
| 658 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 659 | @Override |
| 660 | public boolean isRelevant(NetworkConfigEvent event) { |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 661 | return (event.configClass().equals(NetconfDeviceConfig.class)) && |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 662 | (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 663 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 664 | } |
| 665 | } |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 666 | |
| 667 | /** |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 668 | * Listener for Netconf Controller Events. |
| 669 | */ |
| 670 | private class InnerNetconfDeviceListener implements NetconfDeviceListener { |
| 671 | |
| 672 | @Override |
| 673 | public void deviceAdded(DeviceId deviceId) { |
| 674 | //no-op |
| 675 | log.debug("Netconf device {} added to Netconf controller", deviceId); |
| 676 | } |
| 677 | |
| 678 | @Override |
| 679 | public void deviceRemoved(DeviceId deviceId) { |
| 680 | Preconditions.checkNotNull(deviceId, ISNULL); |
| 681 | |
| 682 | if (deviceService.getDevice(deviceId) != null) { |
| 683 | providerService.deviceDisconnected(deviceId); |
| 684 | retriedPortDiscoveryMap.remove(deviceId); |
| 685 | log.debug("Netconf device {} removed from Netconf controller", deviceId); |
| 686 | } else { |
| 687 | log.warn("Netconf device {} does not exist in the store, " + |
| 688 | "it may already have been removed", deviceId); |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /** |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 694 | * Listener for core device events. |
| 695 | */ |
| 696 | private class InternalDeviceListener implements DeviceListener { |
| 697 | @Override |
| 698 | public void event(DeviceEvent event) { |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 699 | connectionExecutor.submit(exceptionSafe(() -> discoverOrUpdatePorts(event.subject().id()))); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | @Override |
| 703 | public boolean isRelevant(DeviceEvent event) { |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 704 | if (event.type() != DeviceEvent.Type.DEVICE_ADDED && |
| 705 | event.type() != DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 706 | return false; |
| 707 | } |
Andrea Campanella | 968f93f | 2017-06-08 11:09:28 +0200 | [diff] [blame] | 708 | return (SCHEME_NAME.equalsIgnoreCase(event.subject().annotations().value(AnnotationKeys.PROTOCOL)) || |
| 709 | (SCHEME_NAME.equalsIgnoreCase(event.subject().id().uri().getScheme()))) && |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 710 | mastershipService.isLocalMaster(event.subject().id()); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 711 | } |
| 712 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 713 | } |