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