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