blob: afe791c965a4be51649bd2e251fd70cb661212e2 [file] [log] [blame]
andreaeb70a942015-10-16 21:34:46 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
andreaeb70a942015-10-16 21:34:46 -07003 *
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;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030022import org.apache.felix.scr.annotations.Modified;
23import org.apache.felix.scr.annotations.Property;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
andreaeb70a942015-10-16 21:34:46 -070026import org.apache.felix.scr.annotations.Service;
27import org.onlab.packet.IpAddress;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030028import org.onosproject.cfg.ComponentConfigService;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060029import org.onosproject.net.AnnotationKeys;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070030import org.onosproject.net.Device;
andreaeb70a942015-10-16 21:34:46 -070031import org.onosproject.net.DeviceId;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070032import org.onosproject.net.device.DeviceService;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060033import org.onosproject.net.key.DeviceKey;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070034import org.onosproject.net.key.DeviceKeyId;
35import org.onosproject.net.key.DeviceKeyService;
36import org.onosproject.net.key.UsernamePassword;
andreaeb70a942015-10-16 21:34:46 -070037import org.onosproject.netconf.NetconfController;
38import org.onosproject.netconf.NetconfDevice;
Andrea Campanella950310c2016-02-12 18:14:38 -080039import org.onosproject.netconf.NetconfDeviceFactory;
andreaeb70a942015-10-16 21:34:46 -070040import org.onosproject.netconf.NetconfDeviceInfo;
41import org.onosproject.netconf.NetconfDeviceListener;
Andrea Campanella2464dc32016-02-17 17:54:53 -080042import org.onosproject.netconf.NetconfDeviceOutputEvent;
43import org.onosproject.netconf.NetconfDeviceOutputEventListener;
Andrea Campanella101417d2015-12-11 17:58:07 -080044import org.onosproject.netconf.NetconfException;
andreaeb70a942015-10-16 21:34:46 -070045import org.osgi.service.component.ComponentContext;
46import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
48
Andrea Campanella7e6200a2016-03-21 09:48:40 -070049import java.util.Arrays;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030050import java.util.Dictionary;
andreaeb70a942015-10-16 21:34:46 -070051import java.util.Map;
52import java.util.Set;
53import java.util.concurrent.ConcurrentHashMap;
54import java.util.concurrent.CopyOnWriteArraySet;
55
Andreas Papazois4752cfa2016-04-25 14:52:12 +030056import static com.google.common.base.Strings.isNullOrEmpty;
57import static org.onlab.util.Tools.get;
58
andreaeb70a942015-10-16 21:34:46 -070059/**
60 * The implementation of NetconfController.
61 */
62@Component(immediate = true)
63@Service
64public class NetconfControllerImpl implements NetconfController {
Sean Condon334ad692016-12-13 17:56:56 +000065 private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 5;
66 private static final String PROP_NETCONF_CONNECT_TIMEOUT = "netconfConnectTimeout";
67 @Property(name = PROP_NETCONF_CONNECT_TIMEOUT, intValue = DEFAULT_CONNECT_TIMEOUT_SECONDS,
68 label = "Time (in seconds) to wait for a NETCONF connect.")
69 protected static int netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS;
70
Andreas Papazois4752cfa2016-04-25 14:52:12 +030071 private static final String PROP_NETCONF_REPLY_TIMEOUT = "netconfReplyTimeout";
72 private static final int DEFAULT_REPLY_TIMEOUT_SECONDS = 5;
73 @Property(name = PROP_NETCONF_REPLY_TIMEOUT, intValue = DEFAULT_REPLY_TIMEOUT_SECONDS,
74 label = "Time (in seconds) waiting for a NetConf reply")
75 protected static int netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS;
76
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected ComponentConfigService cfgService;
79
Andrea Campanella7e6200a2016-03-21 09:48:40 -070080 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected DeviceService deviceService;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected DeviceKeyService deviceKeyService;
andreaeb70a942015-10-16 21:34:46 -070085
86 public static final Logger log = LoggerFactory
87 .getLogger(NetconfControllerImpl.class);
88
Andrea Campanella8b1cb672016-01-25 13:58:58 -080089 private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
andreaeb70a942015-10-16 21:34:46 -070090
Andrea Campanella2464dc32016-02-17 17:54:53 -080091 private final NetconfDeviceOutputEventListener downListener = new DeviceDownEventListener();
92
andreaeb70a942015-10-16 21:34:46 -070093 protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>();
Andrea Campanella950310c2016-02-12 18:14:38 -080094 protected NetconfDeviceFactory deviceFactory = new DefaultNetconfDeviceFactory();
andreaeb70a942015-10-16 21:34:46 -070095
96 @Activate
97 public void activate(ComponentContext context) {
Andreas Papazois4752cfa2016-04-25 14:52:12 +030098 cfgService.registerProperties(getClass());
99 modified(context);
andreaeb70a942015-10-16 21:34:46 -0700100 log.info("Started");
101 }
102
103 @Deactivate
104 public void deactivate() {
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300105 cfgService.unregisterProperties(getClass(), false);
andreaeb70a942015-10-16 21:34:46 -0700106 netconfDeviceMap.clear();
107 log.info("Stopped");
108 }
109
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300110 @Modified
111 public void modified(ComponentContext context) {
112 if (context == null) {
113 netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS;
Sean Condon334ad692016-12-13 17:56:56 +0000114 netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS;
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300115 log.info("No component configuration");
116 return;
117 }
118
119 Dictionary<?, ?> properties = context.getProperties();
120
121 int newNetconfReplyTimeout;
Sean Condon334ad692016-12-13 17:56:56 +0000122 int newNetconfConnectTimeout;
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300123 try {
124 String s = get(properties, PROP_NETCONF_REPLY_TIMEOUT);
125 newNetconfReplyTimeout = isNullOrEmpty(s) ?
126 netconfReplyTimeout : Integer.parseInt(s.trim());
Sean Condon334ad692016-12-13 17:56:56 +0000127
128 s = get(properties, PROP_NETCONF_CONNECT_TIMEOUT);
129 newNetconfConnectTimeout = isNullOrEmpty(s) ?
130 netconfConnectTimeout : Integer.parseInt(s.trim());
131
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300132 } catch (NumberFormatException e) {
133 log.warn("Component configuration had invalid value", e);
134 return;
135 }
136
Sean Condon334ad692016-12-13 17:56:56 +0000137 if (newNetconfConnectTimeout < 0) {
138 log.warn("netconfConnectTimeout is invalid - less than 0");
139 return;
140 } else if (newNetconfReplyTimeout <= 0) {
141 log.warn("netconfReplyTimeout is invalid - 0 or less.");
142 return;
143 }
144
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300145 netconfReplyTimeout = newNetconfReplyTimeout;
Sean Condon334ad692016-12-13 17:56:56 +0000146 netconfConnectTimeout = newNetconfConnectTimeout;
147 log.info("Settings: {} = {}, {} = {}",
148 PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout, PROP_NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300149 }
150
andreaeb70a942015-10-16 21:34:46 -0700151 @Override
152 public void addDeviceListener(NetconfDeviceListener listener) {
153 if (!netconfDeviceListeners.contains(listener)) {
154 netconfDeviceListeners.add(listener);
155 }
156 }
157
158 @Override
159 public void removeDeviceListener(NetconfDeviceListener listener) {
160 netconfDeviceListeners.remove(listener);
161 }
162
163 @Override
164 public NetconfDevice getNetconfDevice(DeviceId deviceInfo) {
165 return netconfDeviceMap.get(deviceInfo);
166 }
167
168 @Override
169 public NetconfDevice getNetconfDevice(IpAddress ip, int port) {
andreaeb70a942015-10-16 21:34:46 -0700170 for (DeviceId info : netconfDeviceMap.keySet()) {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800171 if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) {
andreaeb70a942015-10-16 21:34:46 -0700172 return netconfDeviceMap.get(info);
173 }
174 }
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800175 return null;
andreaeb70a942015-10-16 21:34:46 -0700176 }
177
178 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700179 public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException {
180 if (netconfDeviceMap.containsKey(deviceId)) {
181 log.debug("Device {} is already present", deviceId);
182 return netconfDeviceMap.get(deviceId);
andreaeb70a942015-10-16 21:34:46 -0700183 } else {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700184 log.debug("Creating NETCONF device {}", deviceId);
185 Device device = deviceService.getDevice(deviceId);
186 String ip;
187 int port;
188 if (device != null) {
189 ip = device.annotations().value("ipaddress");
190 port = Integer.parseInt(device.annotations().value("port"));
191 } else {
192 String[] info = deviceId.toString().split(":");
193 if (info.length == 3) {
194 ip = info[1];
195 port = Integer.parseInt(info[2]);
196 } else {
197 ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0])
198 && !el.equals(info[info.length - 1]))
199 .reduce((t, u) -> t + ":" + u)
200 .get();
201 log.debug("ip v6 {}", ip);
202 port = Integer.parseInt(info[info.length - 1]);
203 }
204 }
205 try {
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600206 DeviceKey deviceKey = deviceKeyService.getDeviceKey(
207 DeviceKeyId.deviceKeyId(deviceId.toString()));
208 NetconfDeviceInfo deviceInfo = null;
209 if (deviceKey.type() == DeviceKey.Type.USERNAME_PASSWORD) {
210 UsernamePassword usernamepasswd = deviceKey.asUsernamePassword();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700211
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600212 deviceInfo = new NetconfDeviceInfo(usernamepasswd.username(),
213 usernamepasswd.password(),
214 IpAddress.valueOf(ip),
215 port);
216
217 } else if (deviceKey.type() == DeviceKey.Type.SSL_KEY) {
218 String username = deviceKey.annotations().value(AnnotationKeys.USERNAME);
219 String password = deviceKey.annotations().value(AnnotationKeys.PASSWORD);
220 String sshkey = deviceKey.annotations().value(AnnotationKeys.SSHKEY);
221
222 deviceInfo = new NetconfDeviceInfo(username,
223 password,
224 IpAddress.valueOf(ip),
225 port,
226 sshkey);
227 } else {
228 log.error("Unknown device key for device {}", deviceId);
229 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700230 NetconfDevice netconfDevicedevice = createDevice(deviceInfo);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700231 netconfDevicedevice.getSession().addDeviceOutputListener(downListener);
232 return netconfDevicedevice;
233 } catch (NullPointerException e) {
234 throw new NetconfException("No Device Key for device " + deviceId, e);
235 }
andreaeb70a942015-10-16 21:34:46 -0700236 }
237 }
238
239 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700240 public void disconnectDevice(DeviceId deviceId, boolean remove) {
241 if (!netconfDeviceMap.containsKey(deviceId)) {
242 log.warn("Device {} is not present", deviceId);
andreaeb70a942015-10-16 21:34:46 -0700243 } else {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700244 stopDevice(deviceId, remove);
andreaeb70a942015-10-16 21:34:46 -0700245 }
246 }
247
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700248 private void stopDevice(DeviceId deviceId, boolean remove) {
249 netconfDeviceMap.get(deviceId).disconnect();
250 netconfDeviceMap.remove(deviceId);
251 if (remove) {
Andrea Campanella86294db2016-03-07 11:42:49 -0800252 for (NetconfDeviceListener l : netconfDeviceListeners) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700253 l.deviceRemoved(deviceId);
254 }
255 }
256 }
257
258 @Override
259 public void removeDevice(DeviceId deviceId) {
260 if (!netconfDeviceMap.containsKey(deviceId)) {
261 log.warn("Device {} is not present", deviceId);
262 for (NetconfDeviceListener l : netconfDeviceListeners) {
263 l.deviceRemoved(deviceId);
264 }
265 } else {
266 netconfDeviceMap.remove(deviceId);
267 for (NetconfDeviceListener l : netconfDeviceListeners) {
268 l.deviceRemoved(deviceId);
Andrea Campanella86294db2016-03-07 11:42:49 -0800269 }
270 }
271 }
272
Andrea Campanella101417d2015-12-11 17:58:07 -0800273 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
Andrea Campanella950310c2016-02-12 18:14:38 -0800274 NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo);
Andrea Campanella087ceb92015-12-07 09:58:34 -0800275 netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700276 for (NetconfDeviceListener l : netconfDeviceListeners) {
277 l.deviceAdded(deviceInfo.getDeviceId());
278 }
andreaeb70a942015-10-16 21:34:46 -0700279 return netconfDevice;
280 }
281
andreaeb70a942015-10-16 21:34:46 -0700282
283 @Override
284 public Map<DeviceId, NetconfDevice> getDevicesMap() {
285 return netconfDeviceMap;
286 }
Andrea Campanella950310c2016-02-12 18:14:38 -0800287
Andrea Campanella86294db2016-03-07 11:42:49 -0800288 @Override
289 public Set<DeviceId> getNetconfDevices() {
290 return netconfDeviceMap.keySet();
291 }
292
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700293
294
Andrea Campanella950310c2016-02-12 18:14:38 -0800295 //Device factory for the specific NetconfDeviceImpl
296 private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory {
297
298 @Override
299 public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException {
300 return new DefaultNetconfDevice(netconfDeviceInfo);
301 }
302 }
Andrea Campanella2464dc32016-02-17 17:54:53 -0800303
304 //Listener for closed session with devices, gets triggered whe devices goes down
305 // or sends the endpattern ]]>]]>
306 private class DeviceDownEventListener implements NetconfDeviceOutputEventListener {
307
308 @Override
309 public void event(NetconfDeviceOutputEvent event) {
310 if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700311 removeDevice(event.getDeviceInfo().getDeviceId());
Andrea Campanella2464dc32016-02-17 17:54:53 -0800312 }
313 }
314
315 @Override
316 public boolean isRelevant(NetconfDeviceOutputEvent event) {
317 return getDevicesMap().containsKey(event.getDeviceInfo().getDeviceId());
318 }
319 }
andreaeb70a942015-10-16 21:34:46 -0700320}