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; |
| 22 | import org.apache.felix.scr.annotations.Service; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.netconf.NetconfController; |
| 26 | import org.onosproject.netconf.NetconfDevice; |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 27 | import org.onosproject.netconf.NetconfDeviceFactory; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 28 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 29 | import org.onosproject.netconf.NetconfDeviceListener; |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 30 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 31 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 32 | import org.onosproject.netconf.NetconfException; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 33 | import org.osgi.service.component.ComponentContext; |
| 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 37 | import java.util.Map; |
| 38 | import java.util.Set; |
| 39 | import java.util.concurrent.ConcurrentHashMap; |
| 40 | import java.util.concurrent.CopyOnWriteArraySet; |
| 41 | |
| 42 | /** |
| 43 | * The implementation of NetconfController. |
| 44 | */ |
| 45 | @Component(immediate = true) |
| 46 | @Service |
| 47 | public class NetconfControllerImpl implements NetconfController { |
| 48 | |
| 49 | public static final Logger log = LoggerFactory |
| 50 | .getLogger(NetconfControllerImpl.class); |
| 51 | |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 52 | private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 53 | |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 54 | private final NetconfDeviceOutputEventListener downListener = new DeviceDownEventListener(); |
| 55 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 56 | protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>(); |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 57 | protected NetconfDeviceFactory deviceFactory = new DefaultNetconfDeviceFactory(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 58 | |
| 59 | @Activate |
| 60 | public void activate(ComponentContext context) { |
| 61 | log.info("Started"); |
| 62 | } |
| 63 | |
| 64 | @Deactivate |
| 65 | public void deactivate() { |
| 66 | netconfDeviceMap.clear(); |
| 67 | log.info("Stopped"); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void addDeviceListener(NetconfDeviceListener listener) { |
| 72 | if (!netconfDeviceListeners.contains(listener)) { |
| 73 | netconfDeviceListeners.add(listener); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void removeDeviceListener(NetconfDeviceListener listener) { |
| 79 | netconfDeviceListeners.remove(listener); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public NetconfDevice getNetconfDevice(DeviceId deviceInfo) { |
| 84 | return netconfDeviceMap.get(deviceInfo); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public NetconfDevice getNetconfDevice(IpAddress ip, int port) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 89 | for (DeviceId info : netconfDeviceMap.keySet()) { |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 90 | if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 91 | return netconfDeviceMap.get(info); |
| 92 | } |
| 93 | } |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 94 | return null; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 98 | public NetconfDevice connectDevice(NetconfDeviceInfo deviceInfo) throws NetconfException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 99 | if (netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 100 | log.info("Device {} is already present", deviceInfo); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 101 | return netconfDeviceMap.get(deviceInfo.getDeviceId()); |
| 102 | } else { |
| 103 | log.info("Creating NETCONF device {}", deviceInfo); |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 104 | NetconfDevice device = createDevice(deviceInfo); |
| 105 | device.getSession().addDeviceOutputListener(downListener); |
| 106 | return device; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | @Override |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame^] | 111 | public void disconnectDevice(NetconfDeviceInfo deviceInfo) { |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 112 | if (!netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) { |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 113 | log.warn("Device {} is not present", deviceInfo); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 114 | } else { |
| 115 | stopDevice(deviceInfo); |
| 116 | } |
| 117 | } |
| 118 | |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame^] | 119 | @Override |
| 120 | public void removeDevice(NetconfDeviceInfo deviceInfo) { |
| 121 | if (!netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) { |
| 122 | log.warn("Device {} is not present", deviceInfo); |
| 123 | } else { |
| 124 | netconfDeviceMap.remove(deviceInfo.getDeviceId()); |
| 125 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 126 | l.deviceRemoved(deviceInfo); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 131 | private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException { |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 132 | NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo); |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 133 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 134 | l.deviceAdded(deviceInfo); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 135 | } |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 136 | netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 137 | return netconfDevice; |
| 138 | } |
| 139 | |
| 140 | private void stopDevice(NetconfDeviceInfo deviceInfo) { |
| 141 | netconfDeviceMap.get(deviceInfo.getDeviceId()).disconnect(); |
| 142 | netconfDeviceMap.remove(deviceInfo.getDeviceId()); |
| 143 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 144 | l.deviceRemoved(deviceInfo); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Map<DeviceId, NetconfDevice> getDevicesMap() { |
| 150 | return netconfDeviceMap; |
| 151 | } |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 152 | |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame^] | 153 | @Override |
| 154 | public Set<DeviceId> getNetconfDevices() { |
| 155 | return netconfDeviceMap.keySet(); |
| 156 | } |
| 157 | |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 158 | //Device factory for the specific NetconfDeviceImpl |
| 159 | private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory { |
| 160 | |
| 161 | @Override |
| 162 | public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException { |
| 163 | return new DefaultNetconfDevice(netconfDeviceInfo); |
| 164 | } |
| 165 | } |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 166 | |
| 167 | //Listener for closed session with devices, gets triggered whe devices goes down |
| 168 | // or sends the endpattern ]]>]]> |
| 169 | private class DeviceDownEventListener implements NetconfDeviceOutputEventListener { |
| 170 | |
| 171 | @Override |
| 172 | public void event(NetconfDeviceOutputEvent event) { |
| 173 | if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) { |
| 174 | removeDevice(event.getDeviceInfo()); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public boolean isRelevant(NetconfDeviceOutputEvent event) { |
| 180 | return getDevicesMap().containsKey(event.getDeviceInfo().getDeviceId()); |
| 181 | } |
| 182 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 183 | } |