andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [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 | */ |
| 16 | |
| 17 | package org.onosproject.netconf.ctl; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Modified; |
| 23 | import org.apache.felix.scr.annotations.Property; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.packet.IpAddress; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 28 | import org.onosproject.cfg.ComponentConfigService; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 29 | import org.onosproject.net.Device; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 31 | import org.onosproject.net.device.DeviceService; |
| 32 | import org.onosproject.net.key.DeviceKeyId; |
| 33 | import org.onosproject.net.key.DeviceKeyService; |
| 34 | import org.onosproject.net.key.UsernamePassword; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 35 | import org.onosproject.netconf.NetconfController; |
| 36 | import org.onosproject.netconf.NetconfDevice; |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 37 | import org.onosproject.netconf.NetconfDeviceFactory; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 38 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 39 | import org.onosproject.netconf.NetconfDeviceListener; |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 40 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 41 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 42 | import org.onosproject.netconf.NetconfException; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 43 | import org.osgi.service.component.ComponentContext; |
| 44 | import org.slf4j.Logger; |
| 45 | import org.slf4j.LoggerFactory; |
| 46 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 47 | import java.util.Arrays; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 48 | import java.util.Dictionary; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 49 | import java.util.Map; |
| 50 | import java.util.Set; |
| 51 | import java.util.concurrent.ConcurrentHashMap; |
| 52 | import java.util.concurrent.CopyOnWriteArraySet; |
| 53 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 54 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 55 | import static org.onlab.util.Tools.get; |
| 56 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 57 | /** |
| 58 | * The implementation of NetconfController. |
| 59 | */ |
| 60 | @Component(immediate = true) |
| 61 | @Service |
| 62 | public class NetconfControllerImpl implements NetconfController { |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame^] | 63 | private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 5; |
| 64 | private static final String PROP_NETCONF_CONNECT_TIMEOUT = "netconfConnectTimeout"; |
| 65 | @Property(name = PROP_NETCONF_CONNECT_TIMEOUT, intValue = DEFAULT_CONNECT_TIMEOUT_SECONDS, |
| 66 | label = "Time (in seconds) to wait for a NETCONF connect.") |
| 67 | protected static int netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS; |
| 68 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 69 | private static final String PROP_NETCONF_REPLY_TIMEOUT = "netconfReplyTimeout"; |
| 70 | private static final int DEFAULT_REPLY_TIMEOUT_SECONDS = 5; |
| 71 | @Property(name = PROP_NETCONF_REPLY_TIMEOUT, intValue = DEFAULT_REPLY_TIMEOUT_SECONDS, |
| 72 | label = "Time (in seconds) waiting for a NetConf reply") |
| 73 | protected static int netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS; |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected ComponentConfigService cfgService; |
| 77 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected DeviceService deviceService; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected DeviceKeyService deviceKeyService; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 83 | |
| 84 | public static final Logger log = LoggerFactory |
| 85 | .getLogger(NetconfControllerImpl.class); |
| 86 | |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 87 | private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 88 | |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 89 | private final NetconfDeviceOutputEventListener downListener = new DeviceDownEventListener(); |
| 90 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 91 | protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>(); |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 92 | protected NetconfDeviceFactory deviceFactory = new DefaultNetconfDeviceFactory(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 93 | |
| 94 | @Activate |
| 95 | public void activate(ComponentContext context) { |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 96 | cfgService.registerProperties(getClass()); |
| 97 | modified(context); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 98 | log.info("Started"); |
| 99 | } |
| 100 | |
| 101 | @Deactivate |
| 102 | public void deactivate() { |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 103 | cfgService.unregisterProperties(getClass(), false); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 104 | netconfDeviceMap.clear(); |
| 105 | log.info("Stopped"); |
| 106 | } |
| 107 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 108 | @Modified |
| 109 | public void modified(ComponentContext context) { |
| 110 | if (context == null) { |
| 111 | netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS; |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame^] | 112 | netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 113 | log.info("No component configuration"); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | Dictionary<?, ?> properties = context.getProperties(); |
| 118 | |
| 119 | int newNetconfReplyTimeout; |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame^] | 120 | int newNetconfConnectTimeout; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 121 | try { |
| 122 | String s = get(properties, PROP_NETCONF_REPLY_TIMEOUT); |
| 123 | newNetconfReplyTimeout = isNullOrEmpty(s) ? |
| 124 | netconfReplyTimeout : Integer.parseInt(s.trim()); |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame^] | 125 | |
| 126 | s = get(properties, PROP_NETCONF_CONNECT_TIMEOUT); |
| 127 | newNetconfConnectTimeout = isNullOrEmpty(s) ? |
| 128 | netconfConnectTimeout : Integer.parseInt(s.trim()); |
| 129 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 130 | } catch (NumberFormatException e) { |
| 131 | log.warn("Component configuration had invalid value", e); |
| 132 | return; |
| 133 | } |
| 134 | |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame^] | 135 | if (newNetconfConnectTimeout < 0) { |
| 136 | log.warn("netconfConnectTimeout is invalid - less than 0"); |
| 137 | return; |
| 138 | } else if (newNetconfReplyTimeout <= 0) { |
| 139 | log.warn("netconfReplyTimeout is invalid - 0 or less."); |
| 140 | return; |
| 141 | } |
| 142 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 143 | netconfReplyTimeout = newNetconfReplyTimeout; |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame^] | 144 | netconfConnectTimeout = newNetconfConnectTimeout; |
| 145 | log.info("Settings: {} = {}, {} = {}", |
| 146 | PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout, PROP_NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 147 | } |
| 148 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 149 | @Override |
| 150 | public void addDeviceListener(NetconfDeviceListener listener) { |
| 151 | if (!netconfDeviceListeners.contains(listener)) { |
| 152 | netconfDeviceListeners.add(listener); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void removeDeviceListener(NetconfDeviceListener listener) { |
| 158 | netconfDeviceListeners.remove(listener); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public NetconfDevice getNetconfDevice(DeviceId deviceInfo) { |
| 163 | return netconfDeviceMap.get(deviceInfo); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public NetconfDevice getNetconfDevice(IpAddress ip, int port) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 168 | for (DeviceId info : netconfDeviceMap.keySet()) { |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 169 | if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 170 | return netconfDeviceMap.get(info); |
| 171 | } |
| 172 | } |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 173 | return null; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 177 | public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException { |
| 178 | if (netconfDeviceMap.containsKey(deviceId)) { |
| 179 | log.debug("Device {} is already present", deviceId); |
| 180 | return netconfDeviceMap.get(deviceId); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 181 | } else { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 182 | log.debug("Creating NETCONF device {}", deviceId); |
| 183 | Device device = deviceService.getDevice(deviceId); |
| 184 | String ip; |
| 185 | int port; |
| 186 | if (device != null) { |
| 187 | ip = device.annotations().value("ipaddress"); |
| 188 | port = Integer.parseInt(device.annotations().value("port")); |
| 189 | } else { |
| 190 | String[] info = deviceId.toString().split(":"); |
| 191 | if (info.length == 3) { |
| 192 | ip = info[1]; |
| 193 | port = Integer.parseInt(info[2]); |
| 194 | } else { |
| 195 | ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0]) |
| 196 | && !el.equals(info[info.length - 1])) |
| 197 | .reduce((t, u) -> t + ":" + u) |
| 198 | .get(); |
| 199 | log.debug("ip v6 {}", ip); |
| 200 | port = Integer.parseInt(info[info.length - 1]); |
| 201 | } |
| 202 | } |
| 203 | try { |
| 204 | UsernamePassword deviceKey = deviceKeyService.getDeviceKey( |
| 205 | DeviceKeyId.deviceKeyId(deviceId.toString())).asUsernamePassword(); |
| 206 | |
| 207 | NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(deviceKey.username(), |
| 208 | deviceKey.password(), |
| 209 | IpAddress.valueOf(ip), |
| 210 | port); |
| 211 | NetconfDevice netconfDevicedevice = createDevice(deviceInfo); |
| 212 | |
| 213 | netconfDevicedevice.getSession().addDeviceOutputListener(downListener); |
| 214 | return netconfDevicedevice; |
| 215 | } catch (NullPointerException e) { |
| 216 | throw new NetconfException("No Device Key for device " + deviceId, e); |
| 217 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 222 | public void disconnectDevice(DeviceId deviceId, boolean remove) { |
| 223 | if (!netconfDeviceMap.containsKey(deviceId)) { |
| 224 | log.warn("Device {} is not present", deviceId); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 225 | } else { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 226 | stopDevice(deviceId, remove); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 230 | private void stopDevice(DeviceId deviceId, boolean remove) { |
| 231 | netconfDeviceMap.get(deviceId).disconnect(); |
| 232 | netconfDeviceMap.remove(deviceId); |
| 233 | if (remove) { |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 234 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 235 | l.deviceRemoved(deviceId); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public void removeDevice(DeviceId deviceId) { |
| 242 | if (!netconfDeviceMap.containsKey(deviceId)) { |
| 243 | log.warn("Device {} is not present", deviceId); |
| 244 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 245 | l.deviceRemoved(deviceId); |
| 246 | } |
| 247 | } else { |
| 248 | netconfDeviceMap.remove(deviceId); |
| 249 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 250 | l.deviceRemoved(deviceId); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 255 | private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException { |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 256 | NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo); |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 257 | netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 258 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 259 | l.deviceAdded(deviceInfo.getDeviceId()); |
| 260 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 261 | return netconfDevice; |
| 262 | } |
| 263 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 264 | |
| 265 | @Override |
| 266 | public Map<DeviceId, NetconfDevice> getDevicesMap() { |
| 267 | return netconfDeviceMap; |
| 268 | } |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 269 | |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 270 | @Override |
| 271 | public Set<DeviceId> getNetconfDevices() { |
| 272 | return netconfDeviceMap.keySet(); |
| 273 | } |
| 274 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 275 | |
| 276 | |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 277 | //Device factory for the specific NetconfDeviceImpl |
| 278 | private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory { |
| 279 | |
| 280 | @Override |
| 281 | public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException { |
| 282 | return new DefaultNetconfDevice(netconfDeviceInfo); |
| 283 | } |
| 284 | } |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 285 | |
| 286 | //Listener for closed session with devices, gets triggered whe devices goes down |
| 287 | // or sends the endpattern ]]>]]> |
| 288 | private class DeviceDownEventListener implements NetconfDeviceOutputEventListener { |
| 289 | |
| 290 | @Override |
| 291 | public void event(NetconfDeviceOutputEvent event) { |
| 292 | if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 293 | removeDevice(event.getDeviceInfo().getDeviceId()); |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public boolean isRelevant(NetconfDeviceOutputEvent event) { |
| 299 | return getDevicesMap().containsKey(event.getDeviceInfo().getDeviceId()); |
| 300 | } |
| 301 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 302 | } |