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