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; |
| 27 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 28 | import org.onosproject.netconf.NetconfDeviceListener; |
| 29 | import org.osgi.service.component.ComponentContext; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import java.io.IOException; |
| 34 | import java.util.Map; |
| 35 | import java.util.Set; |
| 36 | import java.util.concurrent.ConcurrentHashMap; |
| 37 | import java.util.concurrent.CopyOnWriteArraySet; |
| 38 | |
| 39 | /** |
| 40 | * The implementation of NetconfController. |
| 41 | */ |
| 42 | @Component(immediate = true) |
| 43 | @Service |
| 44 | public class NetconfControllerImpl implements NetconfController { |
| 45 | |
| 46 | public static final Logger log = LoggerFactory |
| 47 | .getLogger(NetconfControllerImpl.class); |
| 48 | |
| 49 | public Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>(); |
| 50 | |
| 51 | protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>(); |
| 52 | |
| 53 | @Activate |
| 54 | public void activate(ComponentContext context) { |
| 55 | log.info("Started"); |
| 56 | } |
| 57 | |
| 58 | @Deactivate |
| 59 | public void deactivate() { |
| 60 | netconfDeviceMap.clear(); |
| 61 | log.info("Stopped"); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void addDeviceListener(NetconfDeviceListener listener) { |
| 66 | if (!netconfDeviceListeners.contains(listener)) { |
| 67 | netconfDeviceListeners.add(listener); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void removeDeviceListener(NetconfDeviceListener listener) { |
| 73 | netconfDeviceListeners.remove(listener); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public NetconfDevice getNetconfDevice(DeviceId deviceInfo) { |
| 78 | return netconfDeviceMap.get(deviceInfo); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public NetconfDevice getNetconfDevice(IpAddress ip, int port) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 83 | for (DeviceId info : netconfDeviceMap.keySet()) { |
| 84 | if (IpAddress.valueOf(info.uri().getHost()).equals(ip) && |
| 85 | info.uri().getPort() == port) { |
| 86 | return netconfDeviceMap.get(info); |
| 87 | } |
| 88 | } |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 89 | return null; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 93 | public NetconfDevice connectDevice(NetconfDeviceInfo deviceInfo) throws IOException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 94 | if (netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) { |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 95 | log.warn("Device {} is already present", deviceInfo); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 96 | return netconfDeviceMap.get(deviceInfo.getDeviceId()); |
| 97 | } else { |
| 98 | log.info("Creating NETCONF device {}", deviceInfo); |
| 99 | return createDevice(deviceInfo); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void removeDevice(NetconfDeviceInfo deviceInfo) { |
| 105 | if (netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) { |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 106 | log.warn("Device {} is not present", deviceInfo); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 107 | } else { |
| 108 | stopDevice(deviceInfo); |
| 109 | } |
| 110 | } |
| 111 | |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 112 | private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws IOException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 113 | NetconfDevice netconfDevice = null; |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 114 | netconfDevice = new NetconfDeviceImpl(deviceInfo); |
| 115 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 116 | l.deviceAdded(deviceInfo); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 117 | } |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 118 | netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 119 | return netconfDevice; |
| 120 | } |
| 121 | |
| 122 | private void stopDevice(NetconfDeviceInfo deviceInfo) { |
| 123 | netconfDeviceMap.get(deviceInfo.getDeviceId()).disconnect(); |
| 124 | netconfDeviceMap.remove(deviceInfo.getDeviceId()); |
| 125 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 126 | l.deviceRemoved(deviceInfo); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public Map<DeviceId, NetconfDevice> getDevicesMap() { |
| 132 | return netconfDeviceMap; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | } |