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