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; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 70 | |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 71 | import static org.onlab.util.Tools.groupedThreads; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 72 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 73 | import static org.slf4j.LoggerFactory.getLogger; |
| 74 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 75 | /** |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 76 | * Provider which uses an NETCONF controller to detect device. |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 77 | */ |
| 78 | @Component(immediate = true) |
| 79 | public class NetconfDeviceProvider extends AbstractProvider |
| 80 | implements DeviceProvider { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 81 | |
| 82 | public static final String ACTIVE = "active"; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 83 | private final Logger log = getLogger(getClass()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected DeviceProviderRegistry providerRegistry; |
| 87 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 89 | protected NetconfController controller; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 92 | protected NetworkConfigRegistry cfgService; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 93 | |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 95 | protected CoreService coreService; |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 96 | |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 98 | protected DeviceService deviceService; |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 101 | protected DeviceKeyAdminService deviceKeyAdminService; |
| 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected MastershipService mastershipService; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected ClusterService clusterService; |
Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame] | 108 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 109 | private static final String APP_NAME = "org.onosproject.netconf"; |
| 110 | private static final String SCHEME_NAME = "netconf"; |
| 111 | private static final String DEVICE_PROVIDER_PACKAGE = "org.onosproject.netconf.provider.device"; |
| 112 | private static final String UNKNOWN = "unknown"; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 113 | protected static final String ISNULL = "NetconfDeviceInfo is null"; |
| 114 | private static final String IPADDRESS = "ipaddress"; |
| 115 | private static final String NETCONF = "netconf"; |
| 116 | private static final String PORT = "port"; |
| 117 | //FIXME eventually a property |
| 118 | private static final int ISREACHABLE_TIMEOUT = 2000; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 119 | |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 120 | private final ExecutorService executor = |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 121 | Executors.newFixedThreadPool(5, groupedThreads("onos/netconfdeviceprovider", |
| 122 | "device-installer-%d", log)); |
Andrea Campanella | 5c999e2 | 2016-03-01 15:12:53 -0800 | [diff] [blame] | 123 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 124 | private DeviceProviderService providerService; |
| 125 | private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener(); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 126 | private InternalDeviceListener deviceListener = new InternalDeviceListener(); |
| 127 | private NodeId localNodeId; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 128 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 129 | private final ConfigFactory factory = |
| 130 | new ConfigFactory<ApplicationId, NetconfProviderConfig>(APP_SUBJECT_FACTORY, |
| 131 | NetconfProviderConfig.class, |
| 132 | "devices", |
| 133 | true) { |
| 134 | @Override |
| 135 | public NetconfProviderConfig createConfig() { |
| 136 | return new NetconfProviderConfig(); |
| 137 | } |
| 138 | }; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 139 | private final NetworkConfigListener cfgListener = new InternalNetworkConfigListener(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 140 | private ApplicationId appId; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 141 | private boolean active; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 142 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 143 | |
| 144 | @Activate |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 145 | public void activate() { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 146 | active = true; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 147 | providerService = providerRegistry.register(this); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 148 | appId = coreService.registerApplication(APP_NAME); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 149 | cfgService.registerConfigFactory(factory); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 150 | cfgService.addListener(cfgListener); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 151 | controller.addDeviceListener(innerNodeListener); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 152 | deviceService.addListener(deviceListener); |
Andrea Campanella | 7d8449b | 2016-03-02 10:16:42 -0800 | [diff] [blame] | 153 | executor.execute(NetconfDeviceProvider.this::connectDevices); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 154 | localNodeId = clusterService.getLocalNode().id(); |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 155 | log.info("Started"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 156 | } |
| 157 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 158 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 159 | @Deactivate |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 160 | public void deactivate() { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 161 | deviceService.removeListener(deviceListener); |
| 162 | active = false; |
| 163 | controller.getNetconfDevices().forEach(id -> { |
| 164 | deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(id.toString())); |
| 165 | controller.disconnectDevice(id, true); |
| 166 | }); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 167 | controller.removeDeviceListener(innerNodeListener); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 168 | deviceService.removeListener(deviceListener); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 169 | providerRegistry.unregister(this); |
| 170 | providerService = null; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 171 | cfgService.unregisterConfigFactory(factory); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 172 | executor.shutdown(); |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 173 | log.info("Stopped"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 174 | } |
| 175 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 176 | public NetconfDeviceProvider() { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 177 | super(new ProviderId(SCHEME_NAME, DEVICE_PROVIDER_PACKAGE)); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void triggerProbe(DeviceId deviceId) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 182 | // TODO: This will be implemented later. |
| 183 | log.info("Triggering probe on device {}", deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 188 | if (active) { |
| 189 | switch (newRole) { |
| 190 | case MASTER: |
| 191 | initiateConnection(deviceId, newRole); |
| 192 | log.debug("Accepting mastership role change to {} for device {}", newRole, deviceId); |
| 193 | break; |
| 194 | case STANDBY: |
| 195 | controller.disconnectDevice(deviceId, false); |
| 196 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.STANDBY); |
| 197 | //else no-op |
| 198 | break; |
| 199 | case NONE: |
| 200 | controller.disconnectDevice(deviceId, false); |
| 201 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.NONE); |
| 202 | break; |
| 203 | default: |
| 204 | log.error("Unimplemented Mastership state : {}", newRole); |
| 205 | |
| 206 | } |
| 207 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public boolean isReachable(DeviceId deviceId) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 212 | //FIXME this is a workaround util device state is shared |
| 213 | // between controller instances. |
| 214 | Device device = deviceService.getDevice(deviceId); |
| 215 | String ip; |
| 216 | int port; |
| 217 | Socket socket = null; |
| 218 | if (device != null) { |
| 219 | ip = device.annotations().value(IPADDRESS); |
| 220 | port = Integer.parseInt(device.annotations().value(PORT)); |
| 221 | } else { |
| 222 | String[] info = deviceId.toString().split(":"); |
| 223 | if (info.length == 3) { |
| 224 | ip = info[1]; |
| 225 | port = Integer.parseInt(info[2]); |
| 226 | } else { |
| 227 | ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0]) |
| 228 | && !el.equals(info[info.length - 1])) |
| 229 | .reduce((t, u) -> t + ":" + u) |
| 230 | .get(); |
| 231 | log.debug("ip v6 {}", ip); |
| 232 | port = Integer.parseInt(info[info.length - 1]); |
| 233 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 234 | } |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 235 | //test connection to device opening a socket to it. |
| 236 | try { |
| 237 | socket = new Socket(ip, port); |
| 238 | log.debug("rechability of {}, {}, {}", deviceId, socket.isConnected() && !socket.isClosed()); |
| 239 | return socket.isConnected() && !socket.isClosed(); |
| 240 | } catch (IOException e) { |
| 241 | log.info("Device {} is not reachable", deviceId); |
| 242 | return false; |
| 243 | } finally { |
| 244 | if (socket != null) { |
| 245 | try { |
| 246 | socket.close(); |
| 247 | } catch (IOException e) { |
| 248 | log.debug("Test Socket failed {} ", deviceId); |
| 249 | return false; |
| 250 | } |
| 251 | } |
| 252 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 253 | } |
| 254 | |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 255 | @Override |
| 256 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 257 | boolean enable) { |
| 258 | // TODO if required |
| 259 | } |
| 260 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 261 | private class InnerNetconfDeviceListener implements NetconfDeviceListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 262 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 263 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 264 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 265 | public void deviceAdded(DeviceId deviceId) { |
| 266 | //no-op |
| 267 | log.debug("Netconf device {} added to Netconf subController", deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 271 | public void deviceRemoved(DeviceId deviceId) { |
| 272 | Preconditions.checkNotNull(deviceId, ISNULL); |
| 273 | log.debug("Netconf device {} removed from Netconf subController", deviceId); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 274 | providerService.deviceDisconnected(deviceId); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 278 | private void connectDevices() { |
| 279 | NetconfProviderConfig cfg = cfgService.getConfig(appId, NetconfProviderConfig.class); |
| 280 | if (cfg != null) { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 281 | try { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 282 | cfg.getDevicesAddresses().stream().forEach(addr -> { |
| 283 | DeviceId deviceId = getDeviceId(addr.ip().toString(), addr.port()); |
| 284 | Preconditions.checkNotNull(deviceId, ISNULL); |
| 285 | //Netconf configuration object |
| 286 | ChassisId cid = new ChassisId(); |
| 287 | String ipAddress = addr.ip().toString(); |
| 288 | SparseAnnotations annotations = DefaultAnnotations.builder() |
| 289 | .set(IPADDRESS, ipAddress) |
| 290 | .set(PORT, String.valueOf(addr.port())) |
| 291 | .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()) |
| 292 | .build(); |
| 293 | DeviceDescription deviceDescription = new DefaultDeviceDescription( |
| 294 | deviceId.uri(), |
| 295 | Device.Type.SWITCH, |
| 296 | UNKNOWN, UNKNOWN, |
| 297 | UNKNOWN, UNKNOWN, |
| 298 | cid, |
| 299 | annotations); |
| 300 | deviceKeyAdminService.addKey( |
| 301 | DeviceKey.createDeviceKeyUsingUsernamePassword( |
| 302 | DeviceKeyId.deviceKeyId(deviceId.toString()), |
| 303 | null, addr.name(), addr.password())); |
| 304 | providerService.deviceConnected(deviceId, deviceDescription); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 305 | }); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 306 | } catch (ConfigException e) { |
| 307 | log.error("Cannot read config error " + e); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 308 | } |
| 309 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 310 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 311 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 312 | private void initiateConnection(DeviceId deviceId, MastershipRole newRole) { |
| 313 | try { |
| 314 | if (isReachable(deviceId)) { |
| 315 | controller.connectDevice(deviceId); |
| 316 | providerService.receivedRoleReply(deviceId, newRole, MastershipRole.MASTER); |
| 317 | } else { |
| 318 | return; |
| 319 | } |
| 320 | } catch (Exception e) { |
| 321 | if (deviceService.getDevice(deviceId) != null) { |
| 322 | providerService.deviceDisconnected(deviceId); |
| 323 | } |
| 324 | deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(deviceId.toString())); |
| 325 | throw new RuntimeException(new NetconfException( |
| 326 | "Can't connect to NETCONF " + "device on " + deviceId + ":" + deviceId, e)); |
| 327 | |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | private void discoverPorts(DeviceId deviceId) { |
| 332 | Device device = deviceService.getDevice(deviceId); |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 333 | //TODO remove when PortDiscovery is removed from master |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 334 | if (device.is(PortDiscovery.class)) { |
| 335 | PortDiscovery portConfig = device.as(PortDiscovery.class); |
| 336 | providerService.updatePorts(deviceId, |
| 337 | portConfig.getPorts()); |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 338 | } else if (device.is(DeviceDescriptionDiscovery.class)) { |
| 339 | DeviceDescriptionDiscovery deviceDescriptionDiscovery = |
| 340 | device.as(DeviceDescriptionDiscovery.class); |
| 341 | providerService.updatePorts(deviceId, |
| 342 | deviceDescriptionDiscovery.discoverPortDetails()); |
| 343 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 344 | } else { |
| 345 | log.warn("No portGetter behaviour for device {}", deviceId); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Return the DeviceId about the device containing the URI. |
| 351 | * |
Andrea Campanella | 6c71a05 | 2016-04-22 11:56:31 -0700 | [diff] [blame] | 352 | * @param ip IP address |
Ray Milkey | d4334db | 2016-04-05 17:39:44 -0700 | [diff] [blame] | 353 | * @param port port number |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 354 | * @return DeviceId |
| 355 | */ |
| 356 | public DeviceId getDeviceId(String ip, int port) { |
| 357 | try { |
| 358 | return DeviceId.deviceId(new URI(NETCONF, ip + ":" + port, null)); |
| 359 | } catch (URISyntaxException e) { |
| 360 | throw new IllegalArgumentException("Unable to build deviceID for device " |
| 361 | + ip + ":" + port, e); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Listener for configuration events. |
| 367 | */ |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 368 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 369 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 370 | |
| 371 | @Override |
| 372 | public void event(NetworkConfigEvent event) { |
Andrea Campanella | 90f044f | 2016-03-02 09:14:57 -0800 | [diff] [blame] | 373 | executor.execute(NetconfDeviceProvider.this::connectDevices); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 374 | } |
| 375 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 376 | @Override |
| 377 | public boolean isRelevant(NetworkConfigEvent event) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 378 | return event.configClass().equals(NetconfProviderConfig.class) && |
| 379 | (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 380 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 381 | } |
| 382 | } |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 383 | |
| 384 | /** |
| 385 | * Listener for core device events. |
| 386 | */ |
| 387 | private class InternalDeviceListener implements DeviceListener { |
| 388 | @Override |
| 389 | public void event(DeviceEvent event) { |
| 390 | if ((event.type() == DeviceEvent.Type.DEVICE_ADDED)) { |
| 391 | executor.execute(() -> discoverPorts(event.subject().id())); |
| 392 | } else if ((event.type() == DeviceEvent.Type.DEVICE_REMOVED)) { |
| 393 | log.debug("removing device {}", event.subject().id()); |
| 394 | deviceService.getDevice(event.subject().id()).annotations().keys(); |
| 395 | controller.disconnectDevice(event.subject().id(), true); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public boolean isRelevant(DeviceEvent event) { |
| 401 | if (mastershipService.getMasterFor(event.subject().id()) == null) { |
| 402 | return true; |
| 403 | } |
| 404 | return event.subject().annotations().value(AnnotationKeys.PROTOCOL) |
| 405 | .equals(SCHEME_NAME.toUpperCase()) && |
| 406 | mastershipService.getMasterFor(event.subject().id()).equals(localNodeId); |
| 407 | } |
| 408 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 409 | } |