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