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