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