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