blob: cc53e37f65372555f488dd492bbf610e599a5841 [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;
29import org.osgi.service.component.ComponentContext;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.io.IOException;
34import 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
49 public Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
50
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) {
83 NetconfDevice device = null;
84 for (DeviceId info : netconfDeviceMap.keySet()) {
85 if (IpAddress.valueOf(info.uri().getHost()).equals(ip) &&
86 info.uri().getPort() == port) {
87 return netconfDeviceMap.get(info);
88 }
89 }
90 return device;
91 }
92
93 @Override
Andrea Campanella087ceb92015-12-07 09:58:34 -080094 public NetconfDevice connectDevice(NetconfDeviceInfo deviceInfo) throws IOException {
andreaeb70a942015-10-16 21:34:46 -070095 if (netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
Andrea Campanella087ceb92015-12-07 09:58:34 -080096 log.info("Device {} is already present");
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) {
106 if (netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
107 log.warn("Device {} is not present");
108 } else {
109 stopDevice(deviceInfo);
110 }
111 }
112
Andrea Campanella087ceb92015-12-07 09:58:34 -0800113 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws IOException {
andreaeb70a942015-10-16 21:34:46 -0700114 NetconfDevice netconfDevice = null;
Andrea Campanella087ceb92015-12-07 09:58:34 -0800115 netconfDevice = new NetconfDeviceImpl(deviceInfo);
116 for (NetconfDeviceListener l : netconfDeviceListeners) {
117 l.deviceAdded(deviceInfo);
andreaeb70a942015-10-16 21:34:46 -0700118 }
Andrea Campanella087ceb92015-12-07 09:58:34 -0800119 netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
andreaeb70a942015-10-16 21:34:46 -0700120 return netconfDevice;
121 }
122
123 private void stopDevice(NetconfDeviceInfo deviceInfo) {
124 netconfDeviceMap.get(deviceInfo.getDeviceId()).disconnect();
125 netconfDeviceMap.remove(deviceInfo.getDeviceId());
126 for (NetconfDeviceListener l : netconfDeviceListeners) {
127 l.deviceRemoved(deviceInfo);
128 }
129 }
130
131 @Override
132 public Map<DeviceId, NetconfDevice> getDevicesMap() {
133 return netconfDeviceMap;
134 }
135
136
137}