blob: 432329b2f604c242e86530836338c6c80c5c783d [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;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070022import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
andreaeb70a942015-10-16 21:34:46 -070024import org.apache.felix.scr.annotations.Service;
25import org.onlab.packet.IpAddress;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070026import org.onosproject.net.Device;
andreaeb70a942015-10-16 21:34:46 -070027import org.onosproject.net.DeviceId;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070028import org.onosproject.net.device.DeviceService;
29import org.onosproject.net.key.DeviceKeyId;
30import org.onosproject.net.key.DeviceKeyService;
31import org.onosproject.net.key.UsernamePassword;
andreaeb70a942015-10-16 21:34:46 -070032import org.onosproject.netconf.NetconfController;
33import org.onosproject.netconf.NetconfDevice;
Andrea Campanella950310c2016-02-12 18:14:38 -080034import org.onosproject.netconf.NetconfDeviceFactory;
andreaeb70a942015-10-16 21:34:46 -070035import org.onosproject.netconf.NetconfDeviceInfo;
36import org.onosproject.netconf.NetconfDeviceListener;
Andrea Campanella2464dc32016-02-17 17:54:53 -080037import org.onosproject.netconf.NetconfDeviceOutputEvent;
38import org.onosproject.netconf.NetconfDeviceOutputEventListener;
Andrea Campanella101417d2015-12-11 17:58:07 -080039import org.onosproject.netconf.NetconfException;
andreaeb70a942015-10-16 21:34:46 -070040import org.osgi.service.component.ComponentContext;
41import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
43
Andrea Campanella7e6200a2016-03-21 09:48:40 -070044import java.util.Arrays;
andreaeb70a942015-10-16 21:34:46 -070045import java.util.Map;
46import java.util.Set;
47import java.util.concurrent.ConcurrentHashMap;
48import java.util.concurrent.CopyOnWriteArraySet;
49
50/**
51 * The implementation of NetconfController.
52 */
53@Component(immediate = true)
54@Service
55public class NetconfControllerImpl implements NetconfController {
Andrea Campanella7e6200a2016-03-21 09:48:40 -070056 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected DeviceService deviceService;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected DeviceKeyService deviceKeyService;
andreaeb70a942015-10-16 21:34:46 -070061
62 public static final Logger log = LoggerFactory
63 .getLogger(NetconfControllerImpl.class);
64
Andrea Campanella8b1cb672016-01-25 13:58:58 -080065 private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
andreaeb70a942015-10-16 21:34:46 -070066
Andrea Campanella2464dc32016-02-17 17:54:53 -080067 private final NetconfDeviceOutputEventListener downListener = new DeviceDownEventListener();
68
andreaeb70a942015-10-16 21:34:46 -070069 protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>();
Andrea Campanella950310c2016-02-12 18:14:38 -080070 protected NetconfDeviceFactory deviceFactory = new DefaultNetconfDeviceFactory();
andreaeb70a942015-10-16 21:34:46 -070071
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) {
andreaeb70a942015-10-16 21:34:46 -0700102 for (DeviceId info : netconfDeviceMap.keySet()) {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800103 if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) {
andreaeb70a942015-10-16 21:34:46 -0700104 return netconfDeviceMap.get(info);
105 }
106 }
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800107 return null;
andreaeb70a942015-10-16 21:34:46 -0700108 }
109
110 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700111 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);
andreaeb70a942015-10-16 21:34:46 -0700115 } else {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700116 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 }
andreaeb70a942015-10-16 21:34:46 -0700152 }
153 }
154
155 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700156 public void disconnectDevice(DeviceId deviceId, boolean remove) {
157 if (!netconfDeviceMap.containsKey(deviceId)) {
158 log.warn("Device {} is not present", deviceId);
andreaeb70a942015-10-16 21:34:46 -0700159 } else {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700160 stopDevice(deviceId, remove);
andreaeb70a942015-10-16 21:34:46 -0700161 }
162 }
163
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700164 private void stopDevice(DeviceId deviceId, boolean remove) {
165 netconfDeviceMap.get(deviceId).disconnect();
166 netconfDeviceMap.remove(deviceId);
167 if (remove) {
Andrea Campanella86294db2016-03-07 11:42:49 -0800168 for (NetconfDeviceListener l : netconfDeviceListeners) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700169 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 Campanella86294db2016-03-07 11:42:49 -0800185 }
186 }
187 }
188
Andrea Campanella101417d2015-12-11 17:58:07 -0800189 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
Andrea Campanella950310c2016-02-12 18:14:38 -0800190 NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo);
Andrea Campanella087ceb92015-12-07 09:58:34 -0800191 netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700192 for (NetconfDeviceListener l : netconfDeviceListeners) {
193 l.deviceAdded(deviceInfo.getDeviceId());
194 }
andreaeb70a942015-10-16 21:34:46 -0700195 return netconfDevice;
196 }
197
andreaeb70a942015-10-16 21:34:46 -0700198
199 @Override
200 public Map<DeviceId, NetconfDevice> getDevicesMap() {
201 return netconfDeviceMap;
202 }
Andrea Campanella950310c2016-02-12 18:14:38 -0800203
Andrea Campanella86294db2016-03-07 11:42:49 -0800204 @Override
205 public Set<DeviceId> getNetconfDevices() {
206 return netconfDeviceMap.keySet();
207 }
208
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700209
210
Andrea Campanella950310c2016-02-12 18:14:38 -0800211 //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 Campanella2464dc32016-02-17 17:54:53 -0800219
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 Campanella7e6200a2016-03-21 09:48:40 -0700227 removeDevice(event.getDeviceInfo().getDeviceId());
Andrea Campanella2464dc32016-02-17 17:54:53 -0800228 }
229 }
230
231 @Override
232 public boolean isRelevant(NetconfDeviceOutputEvent event) {
233 return getDevicesMap().containsKey(event.getDeviceInfo().getDeviceId());
234 }
235 }
andreaeb70a942015-10-16 21:34:46 -0700236}