blob: a572a2bc0f9bc7a2865b89a50b59572ab98e3009 [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
94 public NetconfDevice connectDevice(NetconfDeviceInfo deviceInfo) {
95 if (netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
96 log.warn("Device {} is already present");
97 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
113 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) {
114 NetconfDevice netconfDevice = null;
115 try {
116 netconfDevice = new NetconfDeviceImpl(deviceInfo);
117 for (NetconfDeviceListener l : netconfDeviceListeners) {
118 l.deviceAdded(deviceInfo);
119 }
120 netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
121 } catch (IOException e) {
122 throw new IllegalStateException("Cannot create NETCONF device " +
123 "with device Info: " +
124 deviceInfo + " \n" + e);
125 }
126 return netconfDevice;
127 }
128
129 private void stopDevice(NetconfDeviceInfo deviceInfo) {
130 netconfDeviceMap.get(deviceInfo.getDeviceId()).disconnect();
131 netconfDeviceMap.remove(deviceInfo.getDeviceId());
132 for (NetconfDeviceListener l : netconfDeviceListeners) {
133 l.deviceRemoved(deviceInfo);
134 }
135 }
136
137 @Override
138 public Map<DeviceId, NetconfDevice> getDevicesMap() {
139 return netconfDeviceMap;
140 }
141
142
143}