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