blob: 988bce46715d571925931cc498bf3ad06b9d5739 [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 Campanella101417d2015-12-11 17:58:07 -080030import org.onosproject.netconf.NetconfException;
andreaeb70a942015-10-16 21:34:46 -070031import org.osgi.service.component.ComponentContext;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
andreaeb70a942015-10-16 21:34:46 -070035import java.util.Map;
36import java.util.Set;
37import java.util.concurrent.ConcurrentHashMap;
38import java.util.concurrent.CopyOnWriteArraySet;
39
40/**
41 * The implementation of NetconfController.
42 */
43@Component(immediate = true)
44@Service
45public class NetconfControllerImpl implements NetconfController {
46
47 public static final Logger log = LoggerFactory
48 .getLogger(NetconfControllerImpl.class);
49
Andrea Campanella8b1cb672016-01-25 13:58:58 -080050 private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
andreaeb70a942015-10-16 21:34:46 -070051
52 protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>();
Andrea Campanella950310c2016-02-12 18:14:38 -080053 protected NetconfDeviceFactory deviceFactory = new DefaultNetconfDeviceFactory();
andreaeb70a942015-10-16 21:34:46 -070054
55 @Activate
56 public void activate(ComponentContext context) {
57 log.info("Started");
58 }
59
60 @Deactivate
61 public void deactivate() {
62 netconfDeviceMap.clear();
63 log.info("Stopped");
64 }
65
66 @Override
67 public void addDeviceListener(NetconfDeviceListener listener) {
68 if (!netconfDeviceListeners.contains(listener)) {
69 netconfDeviceListeners.add(listener);
70 }
71 }
72
73 @Override
74 public void removeDeviceListener(NetconfDeviceListener listener) {
75 netconfDeviceListeners.remove(listener);
76 }
77
78 @Override
79 public NetconfDevice getNetconfDevice(DeviceId deviceInfo) {
80 return netconfDeviceMap.get(deviceInfo);
81 }
82
83 @Override
84 public NetconfDevice getNetconfDevice(IpAddress ip, int port) {
andreaeb70a942015-10-16 21:34:46 -070085 for (DeviceId info : netconfDeviceMap.keySet()) {
Andrea Campanella57efbb22016-02-11 14:21:41 -080086 if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) {
andreaeb70a942015-10-16 21:34:46 -070087 return netconfDeviceMap.get(info);
88 }
89 }
Andrea Campanella1cd641b2015-12-07 17:28:34 -080090 return null;
andreaeb70a942015-10-16 21:34:46 -070091 }
92
93 @Override
Andrea Campanella101417d2015-12-11 17:58:07 -080094 public NetconfDevice connectDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
andreaeb70a942015-10-16 21:34:46 -070095 if (netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
Andrea Campanella101417d2015-12-11 17:58:07 -080096 log.info("Device {} is already present", deviceInfo);
andreaeb70a942015-10-16 21:34:46 -070097 return netconfDeviceMap.get(deviceInfo.getDeviceId());
98 } else {
99 log.info("Creating NETCONF device {}", deviceInfo);
100 return createDevice(deviceInfo);
101 }
102 }
103
104 @Override
105 public void removeDevice(NetconfDeviceInfo deviceInfo) {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800106 if (!netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800107 log.warn("Device {} is not present", deviceInfo);
andreaeb70a942015-10-16 21:34:46 -0700108 } else {
109 stopDevice(deviceInfo);
110 }
111 }
112
Andrea Campanella101417d2015-12-11 17:58:07 -0800113 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
Andrea Campanella950310c2016-02-12 18:14:38 -0800114 NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo);
Andrea Campanella087ceb92015-12-07 09:58:34 -0800115 for (NetconfDeviceListener l : netconfDeviceListeners) {
116 l.deviceAdded(deviceInfo);
andreaeb70a942015-10-16 21:34:46 -0700117 }
Andrea Campanella087ceb92015-12-07 09:58:34 -0800118 netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
andreaeb70a942015-10-16 21:34:46 -0700119 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 }
Andrea Campanella950310c2016-02-12 18:14:38 -0800134
135 //Device factory for the specific NetconfDeviceImpl
136 private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory {
137
138 @Override
139 public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException {
140 return new DefaultNetconfDevice(netconfDeviceInfo);
141 }
142 }
andreaeb70a942015-10-16 21:34:46 -0700143}