andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [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 | */ |
| 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; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onlab.packet.IpAddress; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 26 | import org.onosproject.net.Device; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 28 | import org.onosproject.net.device.DeviceService; |
| 29 | import org.onosproject.net.key.DeviceKeyId; |
| 30 | import org.onosproject.net.key.DeviceKeyService; |
| 31 | import org.onosproject.net.key.UsernamePassword; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 32 | import org.onosproject.netconf.NetconfController; |
| 33 | import org.onosproject.netconf.NetconfDevice; |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 34 | import org.onosproject.netconf.NetconfDeviceFactory; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 35 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 36 | import org.onosproject.netconf.NetconfDeviceListener; |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 37 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 38 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 39 | import org.onosproject.netconf.NetconfException; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 40 | import org.osgi.service.component.ComponentContext; |
| 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
| 43 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 44 | import java.util.Arrays; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 45 | import java.util.Map; |
| 46 | import java.util.Set; |
| 47 | import java.util.concurrent.ConcurrentHashMap; |
| 48 | import java.util.concurrent.CopyOnWriteArraySet; |
| 49 | |
| 50 | /** |
| 51 | * The implementation of NetconfController. |
| 52 | */ |
| 53 | @Component(immediate = true) |
| 54 | @Service |
| 55 | public class NetconfControllerImpl implements NetconfController { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected DeviceService deviceService; |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected DeviceKeyService deviceKeyService; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 61 | |
| 62 | public static final Logger log = LoggerFactory |
| 63 | .getLogger(NetconfControllerImpl.class); |
| 64 | |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 65 | private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 66 | |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 67 | private final NetconfDeviceOutputEventListener downListener = new DeviceDownEventListener(); |
| 68 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 69 | protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>(); |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 70 | protected NetconfDeviceFactory deviceFactory = new DefaultNetconfDeviceFactory(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 71 | |
| 72 | @Activate |
| 73 | public void activate(ComponentContext context) { |
| 74 | log.info("Started"); |
| 75 | } |
| 76 | |
| 77 | @Deactivate |
| 78 | public void deactivate() { |
| 79 | netconfDeviceMap.clear(); |
| 80 | log.info("Stopped"); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void addDeviceListener(NetconfDeviceListener listener) { |
| 85 | if (!netconfDeviceListeners.contains(listener)) { |
| 86 | netconfDeviceListeners.add(listener); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void removeDeviceListener(NetconfDeviceListener listener) { |
| 92 | netconfDeviceListeners.remove(listener); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public NetconfDevice getNetconfDevice(DeviceId deviceInfo) { |
| 97 | return netconfDeviceMap.get(deviceInfo); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public NetconfDevice getNetconfDevice(IpAddress ip, int port) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 102 | for (DeviceId info : netconfDeviceMap.keySet()) { |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 103 | if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 104 | return netconfDeviceMap.get(info); |
| 105 | } |
| 106 | } |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 107 | return null; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 111 | public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException { |
| 112 | if (netconfDeviceMap.containsKey(deviceId)) { |
| 113 | log.debug("Device {} is already present", deviceId); |
| 114 | return netconfDeviceMap.get(deviceId); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 115 | } else { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 116 | log.debug("Creating NETCONF device {}", deviceId); |
| 117 | Device device = deviceService.getDevice(deviceId); |
| 118 | String ip; |
| 119 | int port; |
| 120 | if (device != null) { |
| 121 | ip = device.annotations().value("ipaddress"); |
| 122 | port = Integer.parseInt(device.annotations().value("port")); |
| 123 | } else { |
| 124 | String[] info = deviceId.toString().split(":"); |
| 125 | if (info.length == 3) { |
| 126 | ip = info[1]; |
| 127 | port = Integer.parseInt(info[2]); |
| 128 | } else { |
| 129 | ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0]) |
| 130 | && !el.equals(info[info.length - 1])) |
| 131 | .reduce((t, u) -> t + ":" + u) |
| 132 | .get(); |
| 133 | log.debug("ip v6 {}", ip); |
| 134 | port = Integer.parseInt(info[info.length - 1]); |
| 135 | } |
| 136 | } |
| 137 | try { |
| 138 | UsernamePassword deviceKey = deviceKeyService.getDeviceKey( |
| 139 | DeviceKeyId.deviceKeyId(deviceId.toString())).asUsernamePassword(); |
| 140 | |
| 141 | NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(deviceKey.username(), |
| 142 | deviceKey.password(), |
| 143 | IpAddress.valueOf(ip), |
| 144 | port); |
| 145 | NetconfDevice netconfDevicedevice = createDevice(deviceInfo); |
| 146 | |
| 147 | netconfDevicedevice.getSession().addDeviceOutputListener(downListener); |
| 148 | return netconfDevicedevice; |
| 149 | } catch (NullPointerException e) { |
| 150 | throw new NetconfException("No Device Key for device " + deviceId, e); |
| 151 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 156 | public void disconnectDevice(DeviceId deviceId, boolean remove) { |
| 157 | if (!netconfDeviceMap.containsKey(deviceId)) { |
| 158 | log.warn("Device {} is not present", deviceId); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 159 | } else { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 160 | stopDevice(deviceId, remove); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 164 | private void stopDevice(DeviceId deviceId, boolean remove) { |
| 165 | netconfDeviceMap.get(deviceId).disconnect(); |
| 166 | netconfDeviceMap.remove(deviceId); |
| 167 | if (remove) { |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 168 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 169 | l.deviceRemoved(deviceId); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void removeDevice(DeviceId deviceId) { |
| 176 | if (!netconfDeviceMap.containsKey(deviceId)) { |
| 177 | log.warn("Device {} is not present", deviceId); |
| 178 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 179 | l.deviceRemoved(deviceId); |
| 180 | } |
| 181 | } else { |
| 182 | netconfDeviceMap.remove(deviceId); |
| 183 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 184 | l.deviceRemoved(deviceId); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 189 | private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException { |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 190 | NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo); |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 191 | netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 192 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 193 | l.deviceAdded(deviceInfo.getDeviceId()); |
| 194 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 195 | return netconfDevice; |
| 196 | } |
| 197 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 198 | |
| 199 | @Override |
| 200 | public Map<DeviceId, NetconfDevice> getDevicesMap() { |
| 201 | return netconfDeviceMap; |
| 202 | } |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 203 | |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 204 | @Override |
| 205 | public Set<DeviceId> getNetconfDevices() { |
| 206 | return netconfDeviceMap.keySet(); |
| 207 | } |
| 208 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 209 | |
| 210 | |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 211 | //Device factory for the specific NetconfDeviceImpl |
| 212 | private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory { |
| 213 | |
| 214 | @Override |
| 215 | public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException { |
| 216 | return new DefaultNetconfDevice(netconfDeviceInfo); |
| 217 | } |
| 218 | } |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 219 | |
| 220 | //Listener for closed session with devices, gets triggered whe devices goes down |
| 221 | // or sends the endpattern ]]>]]> |
| 222 | private class DeviceDownEventListener implements NetconfDeviceOutputEventListener { |
| 223 | |
| 224 | @Override |
| 225 | public void event(NetconfDeviceOutputEvent event) { |
| 226 | if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 227 | removeDevice(event.getDeviceInfo().getDeviceId()); |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public boolean isRelevant(NetconfDeviceOutputEvent event) { |
| 233 | return getDevicesMap().containsKey(event.getDeviceInfo().getDeviceId()); |
| 234 | } |
| 235 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 236 | } |