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