blob: 12b9351f335e1a532df1f7f175356792b7a5107a [file] [log] [blame]
Sanjay Se8dcfee2015-04-23 10:07:08 +05301/*
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 */
andreaeb70a942015-10-16 21:34:46 -070016
Sanjay Se8dcfee2015-04-23 10:07:08 +053017package org.onosproject.provider.netconf.device.impl;
18
andreaeb70a942015-10-16 21:34:46 -070019import com.google.common.base.Preconditions;
Sanjay Se8dcfee2015-04-23 10:07:08 +053020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
Sanjay Se8dcfee2015-04-23 10:07:08 +053023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.onlab.packet.ChassisId;
andreaeb70a942015-10-16 21:34:46 -070026import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
28import org.onosproject.incubator.net.config.basics.ConfigException;
29import org.onosproject.net.DefaultAnnotations;
Sanjay Se8dcfee2015-04-23 10:07:08 +053030import org.onosproject.net.Device;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.MastershipRole;
andreaeb70a942015-10-16 21:34:46 -070033import org.onosproject.net.SparseAnnotations;
34import org.onosproject.net.config.ConfigFactory;
35import org.onosproject.net.config.NetworkConfigEvent;
36import org.onosproject.net.config.NetworkConfigListener;
37import org.onosproject.net.config.NetworkConfigRegistry;
Sanjay Se8dcfee2015-04-23 10:07:08 +053038import org.onosproject.net.device.DefaultDeviceDescription;
39import org.onosproject.net.device.DeviceDescription;
40import org.onosproject.net.device.DeviceProvider;
41import org.onosproject.net.device.DeviceProviderRegistry;
42import org.onosproject.net.device.DeviceProviderService;
43import org.onosproject.net.device.DeviceService;
44import org.onosproject.net.provider.AbstractProvider;
45import org.onosproject.net.provider.ProviderId;
andreaeb70a942015-10-16 21:34:46 -070046import org.onosproject.netconf.NetconfController;
47import org.onosproject.netconf.NetconfDevice;
48import org.onosproject.netconf.NetconfDeviceInfo;
49import org.onosproject.netconf.NetconfDeviceListener;
Sanjay Se8dcfee2015-04-23 10:07:08 +053050import org.slf4j.Logger;
51
Andrea Campanella087ceb92015-12-07 09:58:34 -080052import java.io.IOException;
andreaeb70a942015-10-16 21:34:46 -070053import java.util.Map;
54
55import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
56import static org.slf4j.LoggerFactory.getLogger;
57
Sanjay Se8dcfee2015-04-23 10:07:08 +053058/**
andreaeb70a942015-10-16 21:34:46 -070059 * Provider which uses an NETCONF controller to detect device.
Sanjay Se8dcfee2015-04-23 10:07:08 +053060 */
61@Component(immediate = true)
62public class NetconfDeviceProvider extends AbstractProvider
63 implements DeviceProvider {
andreaeb70a942015-10-16 21:34:46 -070064 private final Logger log = getLogger(getClass());
Sanjay Se8dcfee2015-04-23 10:07:08 +053065
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected DeviceProviderRegistry providerRegistry;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected DeviceService deviceService;
andreaeb70a942015-10-16 21:34:46 -070071 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected NetconfController controller; //where is initiated ?
Sanjay Se8dcfee2015-04-23 10:07:08 +053073
74 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -070075 protected NetworkConfigRegistry cfgService;
Sanjay Se8dcfee2015-04-23 10:07:08 +053076
Thomas Vachuskad6811712015-04-29 21:37:04 -070077 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -070078 protected CoreService coreService;
Thomas Vachuskad6811712015-04-29 21:37:04 -070079
Sanjay Se8dcfee2015-04-23 10:07:08 +053080
andreaeb70a942015-10-16 21:34:46 -070081 private DeviceProviderService providerService;
82 private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener();
83 protected static final String ISNOTNULL = "NetconfDeviceInfo is not null";
84 private static final String UNKNOWN = "unknown";
Sanjay Se8dcfee2015-04-23 10:07:08 +053085
andreaeb70a942015-10-16 21:34:46 -070086 private final ConfigFactory factory =
87 new ConfigFactory<ApplicationId, NetconfProviderConfig>(APP_SUBJECT_FACTORY,
88 NetconfProviderConfig.class,
89 "devices",
90 true) {
91 @Override
92 public NetconfProviderConfig createConfig() {
93 return new NetconfProviderConfig();
94 }
95 };
96 private final NetworkConfigListener cfgLister = new InternalNetworkConfigListener();
97 private ApplicationId appId;
Sanjay Se8dcfee2015-04-23 10:07:08 +053098
Sanjay Se8dcfee2015-04-23 10:07:08 +053099
100 @Activate
andreaeb70a942015-10-16 21:34:46 -0700101 public void activate() {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530102 providerService = providerRegistry.register(this);
andreaeb70a942015-10-16 21:34:46 -0700103 cfgService.registerConfigFactory(factory);
104 cfgService.addListener(cfgLister);
105 controller.addDeviceListener(innerNodeListener);
106 connectExistingDevices();
Thomas Vachuskad6811712015-04-29 21:37:04 -0700107 log.info("Started");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530108 }
109
andreaeb70a942015-10-16 21:34:46 -0700110
Sanjay Se8dcfee2015-04-23 10:07:08 +0530111 @Deactivate
andreaeb70a942015-10-16 21:34:46 -0700112 public void deactivate() {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530113 providerRegistry.unregister(this);
114 providerService = null;
andreaeb70a942015-10-16 21:34:46 -0700115 cfgService.unregisterConfigFactory(factory);
Sanjay Seb5eebb2015-04-24 15:44:50 +0530116 log.info("Stopped");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530117 }
118
andreaeb70a942015-10-16 21:34:46 -0700119 public NetconfDeviceProvider() {
120 super(new ProviderId("netconf", "org.onosproject.netconf.provider.device"));
Sanjay Se8dcfee2015-04-23 10:07:08 +0530121 }
122
123 @Override
124 public void triggerProbe(DeviceId deviceId) {
andreaeb70a942015-10-16 21:34:46 -0700125 // TODO: This will be implemented later.
126 log.info("Triggering probe on device {}", deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530127 }
128
129 @Override
130 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
andreaeb70a942015-10-16 21:34:46 -0700131 // TODO: This will be implemented later.
Sanjay Se8dcfee2015-04-23 10:07:08 +0530132 }
133
134 @Override
135 public boolean isReachable(DeviceId deviceId) {
andreaeb70a942015-10-16 21:34:46 -0700136 Map<DeviceId, NetconfDevice> devices = controller.getDevicesMap();
137
138 NetconfDevice netconfDevice = null;
139 for (DeviceId key : devices.keySet()) {
140 if (key.equals(deviceId)) {
141 netconfDevice = controller.getDevicesMap().get(key);
142 }
143 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530144 if (netconfDevice == null) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530145 log.warn("BAD REQUEST: the requested device id: "
andreaeb70a942015-10-16 21:34:46 -0700146 + deviceId.toString()
147 + " is not associated to any NETCONF Device");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530148 return false;
149 }
andreaeb70a942015-10-16 21:34:46 -0700150 return netconfDevice.isActive();
Sanjay Se8dcfee2015-04-23 10:07:08 +0530151 }
152
andreaeb70a942015-10-16 21:34:46 -0700153 private class InnerNetconfDeviceListener implements NetconfDeviceListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530154
andreaeb70a942015-10-16 21:34:46 -0700155 @Override
156 public void deviceAdded(NetconfDeviceInfo nodeId) {
157 Preconditions.checkNotNull(nodeId, ISNOTNULL);
158 DeviceId deviceId = nodeId.getDeviceId();
159 //TODO filter for not netconf devices
160 //Netconf configuration object
161 ChassisId cid = new ChassisId();
162 String ipAddress = nodeId.ip().toString();
163 SparseAnnotations annotations = DefaultAnnotations.builder()
164 .set("ipaddress", ipAddress).build();
165 DeviceDescription deviceDescription = new DefaultDeviceDescription(
166 deviceId.uri(),
167 Device.Type.SWITCH,
168 UNKNOWN, UNKNOWN,
169 UNKNOWN, UNKNOWN,
170 cid,
171 annotations);
172 providerService.deviceConnected(deviceId, deviceDescription);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530173
Sanjay Se8dcfee2015-04-23 10:07:08 +0530174 }
175
176 @Override
andreaeb70a942015-10-16 21:34:46 -0700177 public void deviceRemoved(NetconfDeviceInfo nodeId) {
178 Preconditions.checkNotNull(nodeId, ISNOTNULL);
179 DeviceId deviceId = nodeId.getDeviceId();
180 providerService.deviceDisconnected(deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530181
andreaeb70a942015-10-16 21:34:46 -0700182 }
183 }
184
185 private void connectExistingDevices() {
186 //TODO consolidate
187 appId = coreService.registerApplication("org.onosproject.netconf");
188 connectDevices();
189 }
190
191 private void connectDevices() {
192 NetconfProviderConfig cfg = cfgService.getConfig(appId, NetconfProviderConfig.class);
193 if (cfg != null) {
194 log.info("cfg {}", cfg);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530195 try {
Andrea Campanella087ceb92015-12-07 09:58:34 -0800196 cfg.getDevicesAddresses().stream()
197 .forEach(addr -> {
198 try {
199 controller.connectDevice(
200 new NetconfDeviceInfo(addr.name(),
201 addr.password(),
202 addr.ip(),
203 addr.port()));
204 } catch (IOException e) {
205 log.warn("Can't connect to NETCONF " +
206 "device on {}:{}",
207 addr.ip(),
208 addr.port());
209 }
210 }
211 );
212
andreaeb70a942015-10-16 21:34:46 -0700213 } catch (ConfigException e) {
214 log.error("Cannot read config error " + e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530215 }
216 }
andreaeb70a942015-10-16 21:34:46 -0700217 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530218
andreaeb70a942015-10-16 21:34:46 -0700219 private class InternalNetworkConfigListener implements NetworkConfigListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530220
andreaeb70a942015-10-16 21:34:46 -0700221
222 @Override
223 public void event(NetworkConfigEvent event) {
224 connectDevices();
Sanjay Se8dcfee2015-04-23 10:07:08 +0530225 }
226
andreaeb70a942015-10-16 21:34:46 -0700227 @Override
228 public boolean isRelevant(NetworkConfigEvent event) {
229 //TODO refactor
230 return event.configClass().equals(NetconfProviderConfig.class) &&
231 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
232 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530233 }
234 }
235}