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