blob: 0d839785d79b967c65efadd4c2a2ce7f111b02e6 [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;
27import org.onosproject.netconf.NetconfDeviceInfo;
28import org.onosproject.netconf.NetconfDeviceListener;
Andrea Campanella101417d2015-12-11 17:58:07 -080029import org.onosproject.netconf.NetconfException;
andreaeb70a942015-10-16 21:34:46 -070030import org.osgi.service.component.ComponentContext;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
andreaeb70a942015-10-16 21:34:46 -070034import java.util.Map;
35import java.util.Set;
36import java.util.concurrent.ConcurrentHashMap;
37import java.util.concurrent.CopyOnWriteArraySet;
38
39/**
40 * The implementation of NetconfController.
41 */
42@Component(immediate = true)
43@Service
44public class NetconfControllerImpl implements NetconfController {
45
46 public static final Logger log = LoggerFactory
47 .getLogger(NetconfControllerImpl.class);
48
Andrea Campanella8b1cb672016-01-25 13:58:58 -080049 private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
andreaeb70a942015-10-16 21:34:46 -070050
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) {
andreaeb70a942015-10-16 21:34:46 -070083 for (DeviceId info : netconfDeviceMap.keySet()) {
Andrea Campanella57efbb22016-02-11 14:21:41 -080084 if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) {
andreaeb70a942015-10-16 21:34:46 -070085 return netconfDeviceMap.get(info);
86 }
87 }
Andrea Campanella1cd641b2015-12-07 17:28:34 -080088 return null;
andreaeb70a942015-10-16 21:34:46 -070089 }
90
91 @Override
Andrea Campanella101417d2015-12-11 17:58:07 -080092 public NetconfDevice connectDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
andreaeb70a942015-10-16 21:34:46 -070093 if (netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
Andrea Campanella101417d2015-12-11 17:58:07 -080094 log.info("Device {} is already present", deviceInfo);
andreaeb70a942015-10-16 21:34:46 -070095 return netconfDeviceMap.get(deviceInfo.getDeviceId());
96 } else {
97 log.info("Creating NETCONF device {}", deviceInfo);
98 return createDevice(deviceInfo);
99 }
100 }
101
102 @Override
103 public void removeDevice(NetconfDeviceInfo deviceInfo) {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800104 if (!netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800105 log.warn("Device {} is not present", deviceInfo);
andreaeb70a942015-10-16 21:34:46 -0700106 } else {
107 stopDevice(deviceInfo);
108 }
109 }
110
Andrea Campanella101417d2015-12-11 17:58:07 -0800111 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
112 NetconfDevice netconfDevice = new NetconfDeviceImpl(deviceInfo);
Andrea Campanella087ceb92015-12-07 09:58:34 -0800113 for (NetconfDeviceListener l : netconfDeviceListeners) {
114 l.deviceAdded(deviceInfo);
andreaeb70a942015-10-16 21:34:46 -0700115 }
Andrea Campanella087ceb92015-12-07 09:58:34 -0800116 netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
andreaeb70a942015-10-16 21:34:46 -0700117 return netconfDevice;
118 }
119
120 private void stopDevice(NetconfDeviceInfo deviceInfo) {
121 netconfDeviceMap.get(deviceInfo.getDeviceId()).disconnect();
122 netconfDeviceMap.remove(deviceInfo.getDeviceId());
123 for (NetconfDeviceListener l : netconfDeviceListeners) {
124 l.deviceRemoved(deviceInfo);
125 }
126 }
127
128 @Override
129 public Map<DeviceId, NetconfDevice> getDevicesMap() {
130 return netconfDeviceMap;
131 }
andreaeb70a942015-10-16 21:34:46 -0700132}