Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 16 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 17 | package org.onosproject.provider.netconf.device.impl; |
| 18 | |
Michele Santuari | 00cc1f7 | 2016-09-08 17:05:24 +0200 | [diff] [blame] | 19 | import com.google.common.base.Objects; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 20 | import com.google.common.base.Preconditions; |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 21 | import com.google.common.util.concurrent.Striped; |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 22 | import org.apache.commons.lang3.tuple.Triple; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 23 | import org.onlab.packet.ChassisId; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 24 | import org.onlab.util.Tools; |
| 25 | import org.onosproject.cfg.ComponentConfigService; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 28 | import org.onosproject.mastership.MastershipService; |
Marc De Leenheer | b0d131c | 2016-03-01 20:34:59 -0800 | [diff] [blame] | 29 | import org.onosproject.net.AnnotationKeys; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 30 | import org.onosproject.net.DefaultAnnotations; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 31 | import org.onosproject.net.Device; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.MastershipRole; |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 34 | import org.onosproject.net.Port; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 35 | import org.onosproject.net.PortNumber; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 36 | import org.onosproject.net.SparseAnnotations; |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 37 | import org.onosproject.net.behaviour.PortAdmin; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 38 | import org.onosproject.net.config.ConfigFactory; |
| 39 | import org.onosproject.net.config.NetworkConfigEvent; |
| 40 | import org.onosproject.net.config.NetworkConfigListener; |
| 41 | import org.onosproject.net.config.NetworkConfigRegistry; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 42 | import org.onosproject.net.config.basics.SubjectFactories; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 43 | import org.onosproject.net.device.DefaultDeviceDescription; |
Andrea Campanella | 3281368 | 2017-10-23 15:29:24 +0200 | [diff] [blame] | 44 | import org.onosproject.net.device.DefaultPortDescription; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 45 | import org.onosproject.net.device.DeviceDescription; |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 46 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 47 | import org.onosproject.net.device.DeviceEvent; |
| 48 | import org.onosproject.net.device.DeviceListener; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 49 | import org.onosproject.net.device.DeviceProvider; |
| 50 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 51 | import org.onosproject.net.device.DeviceProviderService; |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 52 | import org.onosproject.net.device.DeviceService; |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 53 | import org.onosproject.net.device.PortStatistics; |
Gaurav Agrawal | dab4d77 | 2017-03-29 15:15:13 +0530 | [diff] [blame] | 54 | import org.onosproject.net.device.PortStatisticsDiscovery; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 55 | import org.onosproject.net.key.DeviceKey; |
| 56 | import org.onosproject.net.key.DeviceKeyAdminService; |
| 57 | import org.onosproject.net.key.DeviceKeyId; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 58 | import org.onosproject.net.provider.AbstractProvider; |
| 59 | import org.onosproject.net.provider.ProviderId; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 60 | import org.onosproject.netconf.NetconfController; |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 61 | import org.onosproject.netconf.NetconfDevice; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 62 | import org.onosproject.netconf.NetconfDeviceListener; |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 63 | import org.onosproject.netconf.NetconfException; |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 64 | import org.onosproject.netconf.config.NetconfDeviceConfig; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 65 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 66 | import org.osgi.service.component.annotations.Activate; |
| 67 | import org.osgi.service.component.annotations.Component; |
| 68 | import org.osgi.service.component.annotations.Deactivate; |
| 69 | import org.osgi.service.component.annotations.Modified; |
| 70 | import org.osgi.service.component.annotations.Reference; |
| 71 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 72 | import org.slf4j.Logger; |
| 73 | |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 74 | import java.io.IOException; |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 75 | import java.net.InetSocketAddress; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 76 | import java.net.Socket; |
| 77 | import java.net.URI; |
| 78 | import java.net.URISyntaxException; |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 79 | import java.util.Collection; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 80 | import java.util.Dictionary; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 81 | import java.util.Map; |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 82 | import java.util.Optional; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 83 | import java.util.Set; |
Andrea Campanella | 3281368 | 2017-10-23 15:29:24 +0200 | [diff] [blame] | 84 | import java.util.concurrent.CompletableFuture; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 85 | import java.util.concurrent.ConcurrentHashMap; |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 86 | import java.util.concurrent.ExecutorService; |
| 87 | import java.util.concurrent.Executors; |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 88 | import java.util.concurrent.ScheduledExecutorService; |
| 89 | import java.util.concurrent.ScheduledFuture; |
| 90 | import java.util.concurrent.TimeUnit; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 91 | import java.util.concurrent.atomic.AtomicInteger; |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 92 | import java.util.concurrent.locks.Lock; |
| 93 | import java.util.function.Supplier; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 94 | |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 95 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 96 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 97 | import static org.onosproject.provider.netconf.device.impl.OsgiPropertyConstants.*; |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 98 | import static org.onosproject.netconf.NetconfDeviceInfo.extractIpPortPath; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 99 | import static org.slf4j.LoggerFactory.getLogger; |
| 100 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 101 | /** |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 102 | * Provider which uses an NETCONF controller to detect device. |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 103 | */ |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 104 | @Component(immediate = true, |
| 105 | property = { |
| 106 | POLL_FREQUENCY_SECONDS_DEFAULT + ":Integer=" + POLL_FREQUENCY_SECONDS_DEFAULT, |
| 107 | MAX_RETRIES + ":Integer=" + MAX_RETRIES_DEFAULT, |
| 108 | }) |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 109 | public class NetconfDeviceProvider extends AbstractProvider |
| 110 | implements DeviceProvider { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 111 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 112 | private final Logger log = getLogger(getClass()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 113 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 115 | protected DeviceProviderRegistry providerRegistry; |
| 116 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 118 | protected NetconfController controller; |
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) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 121 | protected NetworkConfigRegistry cfgService; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 122 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 124 | protected CoreService coreService; |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 125 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 127 | protected DeviceService deviceService; |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 128 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 130 | protected DeviceKeyAdminService deviceKeyAdminService; |
| 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 MastershipService mastershipService; |
| 134 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 136 | protected ComponentConfigService componentConfigService; |
| 137 | |
| 138 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 139 | protected static final String APP_NAME = "org.onosproject.netconf"; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 140 | protected static final String SCHEME_NAME = "netconf"; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 141 | private static final String DEVICE_PROVIDER_PACKAGE = "org.onosproject.netconf.provider.device"; |
| 142 | private static final String UNKNOWN = "unknown"; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 143 | protected static final String ISNULL = "NetconfDeviceInfo is null"; |
| 144 | private static final String IPADDRESS = "ipaddress"; |
| 145 | private static final String NETCONF = "netconf"; |
| 146 | private static final String PORT = "port"; |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 147 | private static final String PATH = "path"; |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 148 | private static final int CORE_POOL_SIZE = 10; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 149 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 150 | /** Configure poll frequency for port status and statistics; default is 30 sec. */ |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 151 | private int pollFrequency = POLL_FREQUENCY_SECONDS_DEFAULT; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 152 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 153 | /** Configure maximum allowed number of retries for obtaining list of ports; default is 5 times. */ |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 154 | private int maxRetries = MAX_RETRIES_DEFAULT; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 155 | |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 156 | protected ExecutorService executor = |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 157 | Executors.newFixedThreadPool(5, groupedThreads("onos/netconfdeviceprovider", |
| 158 | "device-installer-%d", log)); |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 159 | protected ScheduledExecutorService connectionExecutor |
| 160 | = newScheduledThreadPool(CORE_POOL_SIZE, |
| 161 | groupedThreads("onos/netconfdeviceprovider", |
| 162 | "connection-executor-%d", log)); |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 163 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 164 | protected DeviceProviderService providerService; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 165 | private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener(); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 166 | private InternalDeviceListener deviceListener = new InternalDeviceListener(); |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 167 | private final Map<DeviceId, AtomicInteger> retriedPortDiscoveryMap = new ConcurrentHashMap<>(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 168 | protected ScheduledFuture<?> scheduledTask; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 169 | |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 170 | protected final ConfigFactory factory = |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 171 | // TODO consider moving Config registration to NETCONF ctl bundle |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 172 | new ConfigFactory<DeviceId, NetconfDeviceConfig>( |
| 173 | SubjectFactories.DEVICE_SUBJECT_FACTORY, |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 174 | NetconfDeviceConfig.class, NetconfDeviceConfig.CONFIG_KEY) { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 175 | @Override |
| 176 | public NetconfDeviceConfig createConfig() { |
| 177 | return new NetconfDeviceConfig(); |
| 178 | } |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 179 | }; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 180 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 181 | protected final NetworkConfigListener cfgListener = new InternalNetworkConfigListener(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 182 | private ApplicationId appId; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 183 | private boolean active; |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 184 | private final Striped<Lock> deviceLocks = Striped.lock(30); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 185 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 186 | |
| 187 | @Activate |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 188 | public void activate(ComponentContext context) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 189 | active = true; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 190 | componentConfigService.registerProperties(getClass()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 191 | providerService = providerRegistry.register(this); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 192 | appId = coreService.registerApplication(APP_NAME); |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 193 | cfgService.registerConfigFactory(factory); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 194 | cfgService.addListener(cfgListener); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 195 | controller.addDeviceListener(innerNodeListener); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 196 | deviceService.addListener(deviceListener); |
Andrea Campanella | 7d8449b | 2016-03-02 10:16:42 -0800 | [diff] [blame] | 197 | executor.execute(NetconfDeviceProvider.this::connectDevices); |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 198 | modified(context); |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 199 | log.info("Started"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 200 | } |
| 201 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 202 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 203 | @Deactivate |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 204 | public void deactivate() { |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 205 | componentConfigService.unregisterProperties(getClass(), false); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 206 | deviceService.removeListener(deviceListener); |
| 207 | active = false; |
| 208 | controller.getNetconfDevices().forEach(id -> { |
| 209 | deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(id.toString())); |
| 210 | controller.disconnectDevice(id, true); |
| 211 | }); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 212 | controller.removeDeviceListener(innerNodeListener); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 213 | deviceService.removeListener(deviceListener); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 214 | providerRegistry.unregister(this); |
| 215 | providerService = null; |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 216 | retriedPortDiscoveryMap.clear(); |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 217 | cfgService.unregisterConfigFactory(factory); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 218 | scheduledTask.cancel(true); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 219 | executor.shutdown(); |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 220 | log.info("Stopped"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 221 | } |
| 222 | |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 223 | |
| 224 | @Modified |
| 225 | public void modified(ComponentContext context) { |
| 226 | if (context != null) { |
| 227 | Dictionary<?, ?> properties = context.getProperties(); |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 228 | pollFrequency = Tools.getIntegerProperty(properties, POLL_FREQUENCY_SECONDS, |
| 229 | POLL_FREQUENCY_SECONDS_DEFAULT); |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 230 | log.info("Configured. Poll frequency is configured to {} seconds", pollFrequency); |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 231 | |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 232 | maxRetries = Tools.getIntegerProperty(properties, MAX_RETRIES, MAX_RETRIES_DEFAULT); |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 233 | log.info("Configured. Number of retries is configured to {} times", maxRetries); |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 234 | } |
| 235 | if (scheduledTask != null) { |
| 236 | scheduledTask.cancel(false); |
| 237 | } |
| 238 | scheduledTask = schedulePolling(); |
| 239 | } |
| 240 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 241 | public NetconfDeviceProvider() { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 242 | super(new ProviderId(SCHEME_NAME, DEVICE_PROVIDER_PACKAGE)); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 243 | } |
| 244 | |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 245 | // Checks connection to devices in the config file |
| 246 | // every DEFAULT_POLL_FREQUENCY_SECONDS seconds. |
| 247 | private ScheduledFuture schedulePolling() { |
Yuta HIGUCHI | c2b82e3 | 2017-03-10 14:33:41 -0800 | [diff] [blame] | 248 | return connectionExecutor.scheduleAtFixedRate(exceptionSafe(this::checkAndUpdateDevices), |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 249 | pollFrequency / 10, |
| 250 | pollFrequency, TimeUnit.SECONDS); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Yuta HIGUCHI | c2b82e3 | 2017-03-10 14:33:41 -0800 | [diff] [blame] | 253 | private Runnable exceptionSafe(Runnable runnable) { |
| 254 | return new Runnable() { |
| 255 | |
| 256 | @Override |
| 257 | public void run() { |
| 258 | try { |
| 259 | runnable.run(); |
| 260 | } catch (Exception e) { |
| 261 | log.error("Unhandled Exception", e); |
| 262 | } |
| 263 | } |
| 264 | }; |
| 265 | } |
| 266 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 267 | @Override |
| 268 | public void triggerProbe(DeviceId deviceId) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 269 | // TODO: This will be implemented later. |
Yuta HIGUCHI | e4cb8cf | 2017-05-01 22:18:28 -0700 | [diff] [blame] | 270 | log.debug("Should be triggering probe on device {}", deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 275 | if (active) { |
| 276 | switch (newRole) { |
| 277 | case MASTER: |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 278 | withDeviceLock( |
| 279 | () -> initiateConnection(deviceId, newRole), deviceId).run(); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 280 | log.debug("Accepting mastership role change to {} for device {}", newRole, deviceId); |
| 281 | break; |
| 282 | case STANDBY: |
| 283 | controller.disconnectDevice(deviceId, false); |
| 284 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.STANDBY); |
| 285 | //else no-op |
| 286 | break; |
| 287 | case NONE: |
| 288 | controller.disconnectDevice(deviceId, false); |
| 289 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.NONE); |
| 290 | break; |
| 291 | default: |
| 292 | log.error("Unimplemented Mastership state : {}", newRole); |
| 293 | |
| 294 | } |
| 295 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public boolean isReachable(DeviceId deviceId) { |
Yuta HIGUCHI | 3a50b0d | 2017-07-10 11:11:54 -0700 | [diff] [blame] | 300 | |
| 301 | boolean sessionExists = |
| 302 | Optional.ofNullable(controller.getDevicesMap().get(deviceId)) |
| 303 | .map(NetconfDevice::isActive) |
| 304 | .orElse(false); |
| 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 |
| 341 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 342 | boolean enable) { |
Andrea Campanella | 3281368 | 2017-10-23 15:29:24 +0200 | [diff] [blame] | 343 | Device device = deviceService.getDevice(deviceId); |
| 344 | if (mastershipService.isLocalMaster(deviceId)) { |
| 345 | if (device.is(PortAdmin.class)) { |
| 346 | PortAdmin portAdmin = |
| 347 | device.as(PortAdmin.class); |
| 348 | CompletableFuture<Boolean> modified; |
| 349 | if (enable) { |
| 350 | modified = portAdmin.enable(portNumber); |
| 351 | } else { |
| 352 | modified = portAdmin.disable(portNumber); |
| 353 | } |
| 354 | modified.thenAccept(result -> { |
| 355 | if (result) { |
| 356 | Port port = deviceService.getPort(deviceId, portNumber); |
| 357 | //rebuilding port description with admin state changed. |
| 358 | providerService.portStatusChanged(deviceId, |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 359 | DefaultPortDescription.builder() |
| 360 | .withPortNumber(portNumber) |
| 361 | .isEnabled(enable) |
| 362 | .isRemoved(false) |
| 363 | .type(port.type()) |
| 364 | .portSpeed(port.portSpeed()) |
| 365 | .annotations((SparseAnnotations) port.annotations()) |
| 366 | .build()); |
Andrea Campanella | 3281368 | 2017-10-23 15:29:24 +0200 | [diff] [blame] | 367 | } else { |
| 368 | log.warn("Your device {} port {} status can't be changed to {}", |
| 369 | deviceId, portNumber, enable); |
| 370 | } |
| 371 | }); |
| 372 | } else { |
| 373 | log.warn("Device {} does not support Port Admin", deviceId); |
| 374 | } |
| 375 | } else { |
| 376 | log.debug("Not master but {}, not changing port state", mastershipService.getLocalRole(deviceId)); |
| 377 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 380 | @Override |
| 381 | public void triggerDisconnect(DeviceId deviceId) { |
| 382 | log.debug("Forcing disconnect for device {}", deviceId); |
| 383 | controller.disconnectDevice(deviceId, true); |
| 384 | } |
| 385 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 386 | private class InnerNetconfDeviceListener implements NetconfDeviceListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 387 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 388 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 389 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 390 | public void deviceAdded(DeviceId deviceId) { |
| 391 | //no-op |
| 392 | log.debug("Netconf device {} added to Netconf subController", deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 396 | public void deviceRemoved(DeviceId deviceId) { |
| 397 | Preconditions.checkNotNull(deviceId, ISNULL); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 398 | |
| 399 | if (deviceService.getDevice(deviceId) != null) { |
| 400 | providerService.deviceDisconnected(deviceId); |
mskala | 832d047 | 2017-06-09 16:31:42 +0200 | [diff] [blame] | 401 | retriedPortDiscoveryMap.remove(deviceId); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 402 | log.debug("Netconf device {} removed from Netconf subController", deviceId); |
| 403 | } else { |
| 404 | log.warn("Netconf device {} does not exist in the store, " + |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 405 | "it may already have been removed", deviceId); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 406 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 410 | private void connectDevices() { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 411 | Set<DeviceId> deviceSubjects = |
| 412 | cfgService.getSubjects(DeviceId.class, NetconfDeviceConfig.class); |
| 413 | deviceSubjects.forEach(deviceId -> { |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 414 | connectDevice(cfgService.getConfig(deviceId, NetconfDeviceConfig.class)); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 415 | }); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 418 | |
| 419 | private void connectDevice(NetconfDeviceConfig config) { |
| 420 | if (config == null) { |
| 421 | return; |
| 422 | } |
| 423 | DeviceId deviceId = config.subject(); |
| 424 | if (!deviceId.uri().getScheme().equals(SCHEME_NAME)) { |
| 425 | // not under my scheme, skipping |
| 426 | log.trace("{} not my scheme, skipping", deviceId); |
| 427 | return; |
| 428 | } |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 429 | if (!isReachable(deviceId)) { |
| 430 | log.warn("Can't connect to device {}", deviceId); |
| 431 | return; |
| 432 | } |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 433 | DeviceDescription deviceDescription = createDeviceRepresentation(deviceId, config); |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 434 | log.debug("Connecting NETCONF device {}, on {}:{}{} with username {}", |
| 435 | deviceId, config.ip(), config.port(), |
| 436 | (config.path().isPresent() ? "/" + config.path().get() : ""), |
| 437 | config.username()); |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 438 | storeDeviceKey(config.sshKey(), config.username(), config.password(), deviceId); |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 439 | retriedPortDiscoveryMap.put(deviceId, new AtomicInteger(0)); |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 440 | if (deviceService.getDevice(deviceId) == null) { |
| 441 | providerService.deviceConnected(deviceId, deviceDescription); |
| 442 | } |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 445 | private void checkAndUpdateDevice(DeviceId deviceId, DeviceDescription deviceDescription, boolean newlyConnected) { |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 446 | Device device = deviceService.getDevice(deviceId); |
| 447 | if (device == null) { |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 448 | log.debug("Device {} has not been added to store, since it's not reachable", deviceId); |
| 449 | return; |
| 450 | } |
| 451 | boolean isReachable = isReachable(deviceId); |
| 452 | if (!isReachable && deviceService.isAvailable(deviceId)) { |
| 453 | providerService.deviceDisconnected(deviceId); |
| 454 | return; |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 455 | } else if (newlyConnected && mastershipService.isLocalMaster(deviceId)) { |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 456 | updateDeviceDescription(deviceId, deviceDescription, device); |
| 457 | } |
| 458 | if (isReachable && deviceService.isAvailable(deviceId) && |
| 459 | mastershipService.isLocalMaster(deviceId)) { |
| 460 | //if ports are not discovered, retry the discovery |
David K. Bainbridge | e6a00f0 | 2018-11-07 11:43:43 -0800 | [diff] [blame] | 461 | AtomicInteger count = retriedPortDiscoveryMap.get(deviceId); |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 462 | if (deviceService.getPorts(deviceId).isEmpty() && |
David K. Bainbridge | e6a00f0 | 2018-11-07 11:43:43 -0800 | [diff] [blame] | 463 | count != null && count.getAndIncrement() < maxRetries) { |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 464 | discoverPorts(deviceId); |
| 465 | } |
| 466 | updatePortStatistics(device); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | private void updateDeviceDescription(DeviceId deviceId, DeviceDescription deviceDescription, Device device) { |
| 471 | if (device.is(DeviceDescriptionDiscovery.class)) { |
| 472 | if (mastershipService.isLocalMaster(deviceId)) { |
| 473 | DeviceDescriptionDiscovery deviceDescriptionDiscovery = |
| 474 | device.as(DeviceDescriptionDiscovery.class); |
| 475 | DeviceDescription updatedDeviceDescription = |
| 476 | deviceDescriptionDiscovery.discoverDeviceDetails(); |
| 477 | if (updatedDeviceDescription != null && |
| 478 | !descriptionEquals(device, updatedDeviceDescription)) { |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 479 | providerService.deviceConnected( |
| 480 | deviceId, new DefaultDeviceDescription( |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 481 | updatedDeviceDescription, true, |
| 482 | updatedDeviceDescription.annotations())); |
Ray Milkey | 33306ba | 2018-09-20 13:27:25 -0700 | [diff] [blame] | 483 | } else if (updatedDeviceDescription == null) { |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 484 | providerService.deviceConnected( |
| 485 | deviceId, new DefaultDeviceDescription( |
| 486 | deviceDescription, true, |
| 487 | deviceDescription.annotations())); |
Konstantinos Kanonakis | 4d67dd8 | 2016-08-05 12:18:52 -0500 | [diff] [blame] | 488 | } |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 489 | } |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 490 | } else { |
| 491 | log.warn("No DeviceDescriptionDiscovery behaviour for device {} " + |
| 492 | "using DefaultDeviceDescription", deviceId); |
| 493 | providerService.deviceConnected( |
| 494 | deviceId, new DefaultDeviceDescription( |
| 495 | deviceDescription, true, deviceDescription.annotations())); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 499 | private void updatePortStatistics(Device device) { |
| 500 | if (device.is(PortStatisticsDiscovery.class)) { |
| 501 | PortStatisticsDiscovery d = device.as(PortStatisticsDiscovery.class); |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 502 | Collection<PortStatistics> portStatistics = d.discoverPortStatistics(); |
| 503 | if (portStatistics != null) { |
| 504 | providerService.updatePortStatistics(device.id(), |
| 505 | portStatistics); |
| 506 | } |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 507 | } else { |
Yuta HIGUCHI | e4cb8cf | 2017-05-01 22:18:28 -0700 | [diff] [blame] | 508 | log.debug("No port statistics getter behaviour for device {}", |
| 509 | device.id()); |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Michele Santuari | 00cc1f7 | 2016-09-08 17:05:24 +0200 | [diff] [blame] | 513 | private boolean descriptionEquals(Device device, DeviceDescription updatedDeviceDescription) { |
Yuta HIGUCHI | f381fc7 | 2017-01-03 10:39:36 -0800 | [diff] [blame] | 514 | return Objects.equal(device.id().uri(), updatedDeviceDescription.deviceUri()) |
Michele Santuari | 00cc1f7 | 2016-09-08 17:05:24 +0200 | [diff] [blame] | 515 | && Objects.equal(device.type(), updatedDeviceDescription.type()) |
| 516 | && Objects.equal(device.manufacturer(), updatedDeviceDescription.manufacturer()) |
| 517 | && Objects.equal(device.hwVersion(), updatedDeviceDescription.hwVersion()) |
| 518 | && Objects.equal(device.swVersion(), updatedDeviceDescription.swVersion()) |
| 519 | && Objects.equal(device.serialNumber(), updatedDeviceDescription.serialNumber()) |
| 520 | && Objects.equal(device.chassisId(), updatedDeviceDescription.chassisId()) |
| 521 | && Objects.equal(device.annotations(), updatedDeviceDescription.annotations()); |
| 522 | } |
| 523 | |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 524 | private void checkAndUpdateDevices() { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 525 | Set<DeviceId> deviceSubjects = |
| 526 | cfgService.getSubjects(DeviceId.class, NetconfDeviceConfig.class); |
| 527 | deviceSubjects.forEach(deviceId -> { |
| 528 | NetconfDeviceConfig config = |
| 529 | cfgService.getConfig(deviceId, NetconfDeviceConfig.class); |
| 530 | DeviceDescription deviceDescription = createDeviceRepresentation(deviceId, config); |
| 531 | storeDeviceKey(config.sshKey(), config.username(), config.password(), deviceId); |
Andrea Campanella | 105736e | 2017-11-23 12:52:43 +0100 | [diff] [blame] | 532 | checkAndUpdateDevice(deviceId, deviceDescription, false); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 533 | }); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 534 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 535 | |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 536 | private DeviceDescription createDeviceRepresentation(DeviceId deviceId, NetconfDeviceConfig config) { |
| 537 | Preconditions.checkNotNull(deviceId, ISNULL); |
| 538 | //Netconf configuration object |
| 539 | ChassisId cid = new ChassisId(); |
| 540 | String ipAddress = config.ip().toString(); |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 541 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder() |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 542 | .set(IPADDRESS, ipAddress) |
| 543 | .set(PORT, String.valueOf(config.port())) |
| 544 | .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()) |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 545 | .set(AnnotationKeys.PROVIDER_MARK_ONLINE, String.valueOf(true)); |
| 546 | if (config.path().isPresent()) { |
| 547 | annotations.set(PATH, config.path().get()); |
| 548 | } |
| 549 | |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 550 | return new DefaultDeviceDescription( |
| 551 | deviceId.uri(), |
| 552 | Device.Type.SWITCH, |
| 553 | UNKNOWN, UNKNOWN, |
| 554 | UNKNOWN, UNKNOWN, |
| 555 | cid, false, |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 556 | annotations.build()); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | private void storeDeviceKey(String sshKey, String username, String password, DeviceId deviceId) { |
| 560 | if (sshKey.equals("")) { |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 561 | deviceKeyAdminService.addKey( |
| 562 | DeviceKey.createDeviceKeyUsingUsernamePassword( |
| 563 | DeviceKeyId.deviceKeyId(deviceId.toString()), |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 564 | null, username, password)); |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 565 | } else { |
| 566 | deviceKeyAdminService.addKey( |
| 567 | DeviceKey.createDeviceKeyUsingSshKey( |
| 568 | DeviceKeyId.deviceKeyId(deviceId.toString()), |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 569 | null, username, password, |
| 570 | sshKey)); |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 574 | private void initiateConnection(DeviceId deviceId, MastershipRole newRole) { |
| 575 | try { |
| 576 | if (isReachable(deviceId)) { |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 577 | NetconfDevice device = controller.connectDevice(deviceId); |
| 578 | if (device != null) { |
| 579 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.MASTER); |
| 580 | } else { |
| 581 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.NONE); |
| 582 | } |
| 583 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 584 | } |
| 585 | } catch (Exception e) { |
| 586 | if (deviceService.getDevice(deviceId) != null) { |
| 587 | providerService.deviceDisconnected(deviceId); |
| 588 | } |
| 589 | deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(deviceId.toString())); |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 590 | throw new IllegalStateException(new NetconfException( |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 591 | "Can't connect to NETCONF device " + deviceId, e)); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 592 | |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | private void discoverPorts(DeviceId deviceId) { |
| 597 | Device device = deviceService.getDevice(deviceId); |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 598 | //TODO remove when PortDiscovery is removed from master |
Ray Milkey | 640fedd | 2018-02-08 09:02:26 -0800 | [diff] [blame] | 599 | if (device.is(DeviceDescriptionDiscovery.class)) { |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 600 | DeviceDescriptionDiscovery deviceDescriptionDiscovery = |
| 601 | device.as(DeviceDescriptionDiscovery.class); |
| 602 | providerService.updatePorts(deviceId, |
| 603 | deviceDescriptionDiscovery.discoverPortDetails()); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 604 | } else { |
| 605 | log.warn("No portGetter behaviour for device {}", deviceId); |
| 606 | } |
Gaurav Agrawal | dab4d77 | 2017-03-29 15:15:13 +0530 | [diff] [blame] | 607 | |
| 608 | // Port statistics discovery |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 609 | updatePortStatistics(device); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /** |
| 613 | * Return the DeviceId about the device containing the URI. |
| 614 | * |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 615 | * @param ip IP address |
Ray Milkey | d4334db | 2016-04-05 17:39:44 -0700 | [diff] [blame] | 616 | * @param port port number |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 617 | * @param path path aspect |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 618 | * @return DeviceId |
| 619 | */ |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 620 | public DeviceId getDeviceId(String ip, int port, Optional<String> path) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 621 | try { |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 622 | return DeviceId.deviceId(new URI(NETCONF, ip + ":" + port + |
| 623 | (path.isPresent() ? "/" + path : ""), null)); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 624 | } catch (URISyntaxException e) { |
| 625 | throw new IllegalArgumentException("Unable to build deviceID for device " |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 626 | + ip + ":" + port + (path.isPresent() ? "/" + path : ""), e); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 630 | private <U> U withDeviceLock(Supplier<U> task, DeviceId deviceId) { |
| 631 | final Lock lock = deviceLocks.get(deviceId); |
| 632 | lock.lock(); |
| 633 | try { |
| 634 | return task.get(); |
| 635 | } finally { |
| 636 | lock.unlock(); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | private Runnable withDeviceLock(Runnable task, DeviceId deviceId) { |
| 641 | // Wrapper of withDeviceLock(Supplier, ...) for void tasks. |
| 642 | return () -> withDeviceLock(() -> { |
| 643 | task.run(); |
| 644 | return null; |
| 645 | }, deviceId); |
| 646 | } |
| 647 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 648 | /** |
| 649 | * Listener for configuration events. |
| 650 | */ |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 651 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 652 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 653 | @Override |
| 654 | public void event(NetworkConfigEvent event) { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 655 | if (event.configClass().equals(NetconfDeviceConfig.class)) { |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 656 | executor.execute(() -> connectDevice((NetconfDeviceConfig) event.config().get())); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 657 | } else { |
| 658 | log.warn("Injecting device via this Json is deprecated, " + |
| 659 | "please put configuration under devices/ as shown in the wiki"); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 660 | } |
| 661 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 662 | } |
| 663 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 664 | @Override |
| 665 | public boolean isRelevant(NetworkConfigEvent event) { |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 666 | return (event.configClass().equals(NetconfDeviceConfig.class)) && |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 667 | (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 668 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 669 | } |
| 670 | } |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 671 | |
| 672 | /** |
| 673 | * Listener for core device events. |
| 674 | */ |
| 675 | private class InternalDeviceListener implements DeviceListener { |
| 676 | @Override |
| 677 | public void event(DeviceEvent event) { |
Andrea Campanella | 36f1df9 | 2018-10-01 14:54:01 +0200 | [diff] [blame] | 678 | Device device = event.subject(); |
| 679 | DeviceId deviceId = device.id(); |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 680 | if (event.type() == DeviceEvent.Type.DEVICE_ADDED && !deviceService.isAvailable(event.subject().id())) { |
| 681 | try { |
Andrea Campanella | 36f1df9 | 2018-10-01 14:54:01 +0200 | [diff] [blame] | 682 | DeviceDescription description = new DefaultDeviceDescription(deviceId.uri(), device.type(), |
| 683 | device.manufacturer(), device.hwVersion(), device.swVersion(), |
| 684 | device.serialNumber(), device.chassisId(), |
| 685 | (SparseAnnotations) device.annotations()); |
| 686 | checkAndUpdateDevice(deviceId, description, true); |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 687 | } catch (Exception e) { |
| 688 | log.error("Unhandled exception checking {}", deviceId, e); |
| 689 | } |
| 690 | } |
| 691 | if (deviceService.isAvailable(event.subject().id())) { |
| 692 | executor.execute(() -> discoverPorts(event.subject().id())); |
| 693 | } |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | @Override |
| 697 | public boolean isRelevant(DeviceEvent event) { |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 698 | if (event.type() != DeviceEvent.Type.DEVICE_ADDED && |
| 699 | event.type() != DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 700 | return false; |
| 701 | } |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 702 | if (mastershipService.getMasterFor(event.subject().id()) == null) { |
| 703 | return true; |
| 704 | } |
Andrea Campanella | 968f93f | 2017-06-08 11:09:28 +0200 | [diff] [blame] | 705 | return (SCHEME_NAME.equalsIgnoreCase(event.subject().annotations().value(AnnotationKeys.PROTOCOL)) || |
| 706 | (SCHEME_NAME.equalsIgnoreCase(event.subject().id().uri().getScheme()))) && |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 707 | mastershipService.isLocalMaster(event.subject().id()); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 708 | } |
| 709 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 710 | } |