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