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