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