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