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