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