blob: cebe012901d104c40cf7e7ca154dd3ab8f35b343 [file] [log] [blame]
andreaeb70a942015-10-16 21:34:46 -07001/*
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
17package org.onosproject.netconf.ctl;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Service;
23import org.onlab.packet.IpAddress;
24import org.onosproject.net.DeviceId;
25import org.onosproject.netconf.NetconfController;
26import org.onosproject.netconf.NetconfDevice;
Andrea Campanella950310c2016-02-12 18:14:38 -080027import org.onosproject.netconf.NetconfDeviceFactory;
andreaeb70a942015-10-16 21:34:46 -070028import org.onosproject.netconf.NetconfDeviceInfo;
29import org.onosproject.netconf.NetconfDeviceListener;
Andrea Campanella2464dc32016-02-17 17:54:53 -080030import org.onosproject.netconf.NetconfDeviceOutputEvent;
31import org.onosproject.netconf.NetconfDeviceOutputEventListener;
Andrea Campanella101417d2015-12-11 17:58:07 -080032import org.onosproject.netconf.NetconfException;
andreaeb70a942015-10-16 21:34:46 -070033import org.osgi.service.component.ComponentContext;
34import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
andreaeb70a942015-10-16 21:34:46 -070037import java.util.Map;
38import java.util.Set;
39import java.util.concurrent.ConcurrentHashMap;
40import java.util.concurrent.CopyOnWriteArraySet;
41
42/**
43 * The implementation of NetconfController.
44 */
45@Component(immediate = true)
46@Service
47public class NetconfControllerImpl implements NetconfController {
48
49 public static final Logger log = LoggerFactory
50 .getLogger(NetconfControllerImpl.class);
51
Andrea Campanella8b1cb672016-01-25 13:58:58 -080052 private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
andreaeb70a942015-10-16 21:34:46 -070053
Andrea Campanella2464dc32016-02-17 17:54:53 -080054 private final NetconfDeviceOutputEventListener downListener = new DeviceDownEventListener();
55
andreaeb70a942015-10-16 21:34:46 -070056 protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>();
Andrea Campanella950310c2016-02-12 18:14:38 -080057 protected NetconfDeviceFactory deviceFactory = new DefaultNetconfDeviceFactory();
andreaeb70a942015-10-16 21:34:46 -070058
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) {
andreaeb70a942015-10-16 21:34:46 -070089 for (DeviceId info : netconfDeviceMap.keySet()) {
Andrea Campanella57efbb22016-02-11 14:21:41 -080090 if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) {
andreaeb70a942015-10-16 21:34:46 -070091 return netconfDeviceMap.get(info);
92 }
93 }
Andrea Campanella1cd641b2015-12-07 17:28:34 -080094 return null;
andreaeb70a942015-10-16 21:34:46 -070095 }
96
97 @Override
Andrea Campanella101417d2015-12-11 17:58:07 -080098 public NetconfDevice connectDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
andreaeb70a942015-10-16 21:34:46 -070099 if (netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
Andrea Campanella101417d2015-12-11 17:58:07 -0800100 log.info("Device {} is already present", deviceInfo);
andreaeb70a942015-10-16 21:34:46 -0700101 return netconfDeviceMap.get(deviceInfo.getDeviceId());
102 } else {
103 log.info("Creating NETCONF device {}", deviceInfo);
Andrea Campanella2464dc32016-02-17 17:54:53 -0800104 NetconfDevice device = createDevice(deviceInfo);
105 device.getSession().addDeviceOutputListener(downListener);
106 return device;
andreaeb70a942015-10-16 21:34:46 -0700107 }
108 }
109
110 @Override
111 public void removeDevice(NetconfDeviceInfo deviceInfo) {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800112 if (!netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800113 log.warn("Device {} is not present", deviceInfo);
andreaeb70a942015-10-16 21:34:46 -0700114 } else {
115 stopDevice(deviceInfo);
116 }
117 }
118
Andrea Campanella101417d2015-12-11 17:58:07 -0800119 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
Andrea Campanella950310c2016-02-12 18:14:38 -0800120 NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo);
Andrea Campanella087ceb92015-12-07 09:58:34 -0800121 for (NetconfDeviceListener l : netconfDeviceListeners) {
122 l.deviceAdded(deviceInfo);
andreaeb70a942015-10-16 21:34:46 -0700123 }
Andrea Campanella087ceb92015-12-07 09:58:34 -0800124 netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
andreaeb70a942015-10-16 21:34:46 -0700125 return netconfDevice;
126 }
127
128 private void stopDevice(NetconfDeviceInfo deviceInfo) {
129 netconfDeviceMap.get(deviceInfo.getDeviceId()).disconnect();
130 netconfDeviceMap.remove(deviceInfo.getDeviceId());
131 for (NetconfDeviceListener l : netconfDeviceListeners) {
132 l.deviceRemoved(deviceInfo);
133 }
134 }
135
136 @Override
137 public Map<DeviceId, NetconfDevice> getDevicesMap() {
138 return netconfDeviceMap;
139 }
Andrea Campanella950310c2016-02-12 18:14:38 -0800140
141 //Device factory for the specific NetconfDeviceImpl
142 private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory {
143
144 @Override
145 public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException {
146 return new DefaultNetconfDevice(netconfDeviceInfo);
147 }
148 }
Andrea Campanella2464dc32016-02-17 17:54:53 -0800149
150 //Listener for closed session with devices, gets triggered whe devices goes down
151 // or sends the endpattern ]]>]]>
152 private class DeviceDownEventListener implements NetconfDeviceOutputEventListener {
153
154 @Override
155 public void event(NetconfDeviceOutputEvent event) {
156 if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) {
157 removeDevice(event.getDeviceInfo());
158 }
159 }
160
161 @Override
162 public boolean isRelevant(NetconfDeviceOutputEvent event) {
163 return getDevicesMap().containsKey(event.getDeviceInfo().getDeviceId());
164 }
165 }
andreaeb70a942015-10-16 21:34:46 -0700166}