blob: f7415ef1e8cbb2390251208a3e0da6b896c261fc [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;
Sanjay Se8dcfee2015-04-23 10:07:08 +053043import org.onosproject.net.provider.AbstractProvider;
44import org.onosproject.net.provider.ProviderId;
andreaeb70a942015-10-16 21:34:46 -070045import org.onosproject.netconf.NetconfController;
46import org.onosproject.netconf.NetconfDevice;
47import org.onosproject.netconf.NetconfDeviceInfo;
48import org.onosproject.netconf.NetconfDeviceListener;
Andrea Campanella8b1cb672016-01-25 13:58:58 -080049import org.onosproject.netconf.NetconfException;
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 -070053
54import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
55import static org.slf4j.LoggerFactory.getLogger;
56
Sanjay Se8dcfee2015-04-23 10:07:08 +053057/**
andreaeb70a942015-10-16 21:34:46 -070058 * Provider which uses an NETCONF controller to detect device.
Sanjay Se8dcfee2015-04-23 10:07:08 +053059 */
60@Component(immediate = true)
61public class NetconfDeviceProvider extends AbstractProvider
62 implements DeviceProvider {
andreaeb70a942015-10-16 21:34:46 -070063 private final Logger log = getLogger(getClass());
Sanjay Se8dcfee2015-04-23 10:07:08 +053064
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected DeviceProviderRegistry providerRegistry;
67
andreaeb70a942015-10-16 21:34:46 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella101417d2015-12-11 17:58:07 -080069 protected NetconfController controller;
Sanjay Se8dcfee2015-04-23 10:07:08 +053070
71 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -070072 protected NetworkConfigRegistry cfgService;
Sanjay Se8dcfee2015-04-23 10:07:08 +053073
Thomas Vachuskad6811712015-04-29 21:37:04 -070074 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -070075 protected CoreService coreService;
Thomas Vachuskad6811712015-04-29 21:37:04 -070076
Andrea Campanella101417d2015-12-11 17:58:07 -080077 private static final String APP_NAME = "org.onosproject.netconf";
78 private static final String SCHEME_NAME = "netconf";
79 private static final String DEVICE_PROVIDER_PACKAGE = "org.onosproject.netconf.provider.device";
80 private static final String UNKNOWN = "unknown";
Sanjay Se8dcfee2015-04-23 10:07:08 +053081
andreaeb70a942015-10-16 21:34:46 -070082 private DeviceProviderService providerService;
83 private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener();
Sanjay Se8dcfee2015-04-23 10:07:08 +053084
andreaeb70a942015-10-16 21:34:46 -070085 private final ConfigFactory factory =
86 new ConfigFactory<ApplicationId, NetconfProviderConfig>(APP_SUBJECT_FACTORY,
87 NetconfProviderConfig.class,
88 "devices",
89 true) {
90 @Override
91 public NetconfProviderConfig createConfig() {
92 return new NetconfProviderConfig();
93 }
94 };
95 private final NetworkConfigListener cfgLister = new InternalNetworkConfigListener();
96 private ApplicationId appId;
Sanjay Se8dcfee2015-04-23 10:07:08 +053097
Sanjay Se8dcfee2015-04-23 10:07:08 +053098
99 @Activate
andreaeb70a942015-10-16 21:34:46 -0700100 public void activate() {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530101 providerService = providerRegistry.register(this);
Andrea Campanella101417d2015-12-11 17:58:07 -0800102 appId = coreService.registerApplication(APP_NAME);
andreaeb70a942015-10-16 21:34:46 -0700103 cfgService.registerConfigFactory(factory);
104 cfgService.addListener(cfgLister);
105 controller.addDeviceListener(innerNodeListener);
Andrea Campanella8e290c52016-01-12 15:54:50 -0800106 connectDevices();
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);
Andrea Campanella101417d2015-12-11 17:58:07 -0800116 controller.removeDeviceListener(innerNodeListener);
Sanjay Seb5eebb2015-04-24 15:44:50 +0530117 log.info("Stopped");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530118 }
119
andreaeb70a942015-10-16 21:34:46 -0700120 public NetconfDeviceProvider() {
Andrea Campanella101417d2015-12-11 17:58:07 -0800121 super(new ProviderId(SCHEME_NAME, DEVICE_PROVIDER_PACKAGE));
Sanjay Se8dcfee2015-04-23 10:07:08 +0530122 }
123
124 @Override
125 public void triggerProbe(DeviceId deviceId) {
andreaeb70a942015-10-16 21:34:46 -0700126 // TODO: This will be implemented later.
127 log.info("Triggering probe on device {}", deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530128 }
129
130 @Override
131 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
andreaeb70a942015-10-16 21:34:46 -0700132 // TODO: This will be implemented later.
Sanjay Se8dcfee2015-04-23 10:07:08 +0530133 }
134
135 @Override
136 public boolean isReachable(DeviceId deviceId) {
Andrea Campanella8e290c52016-01-12 15:54:50 -0800137 NetconfDevice netconfDevice = controller.getNetconfDevice(deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530138 if (netconfDevice == null) {
Andrea Campanella2464dc32016-02-17 17:54:53 -0800139 log.debug("Requested device id: "
andreaeb70a942015-10-16 21:34:46 -0700140 + deviceId.toString()
141 + " is not associated to any NETCONF Device");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530142 return false;
143 }
andreaeb70a942015-10-16 21:34:46 -0700144 return netconfDevice.isActive();
Sanjay Se8dcfee2015-04-23 10:07:08 +0530145 }
146
andreaeb70a942015-10-16 21:34:46 -0700147 private class InnerNetconfDeviceListener implements NetconfDeviceListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530148
Andrea Campanella101417d2015-12-11 17:58:07 -0800149 private static final String IPADDRESS = "ipaddress";
150 protected static final String ISNULL = "NetconfDeviceInfo is null";
151
andreaeb70a942015-10-16 21:34:46 -0700152 @Override
153 public void deviceAdded(NetconfDeviceInfo nodeId) {
Andrea Campanella101417d2015-12-11 17:58:07 -0800154 Preconditions.checkNotNull(nodeId, ISNULL);
andreaeb70a942015-10-16 21:34:46 -0700155 DeviceId deviceId = nodeId.getDeviceId();
andreaeb70a942015-10-16 21:34:46 -0700156 //Netconf configuration object
157 ChassisId cid = new ChassisId();
158 String ipAddress = nodeId.ip().toString();
159 SparseAnnotations annotations = DefaultAnnotations.builder()
Andrea Campanella101417d2015-12-11 17:58:07 -0800160 .set(IPADDRESS, ipAddress).build();
andreaeb70a942015-10-16 21:34:46 -0700161 DeviceDescription deviceDescription = new DefaultDeviceDescription(
162 deviceId.uri(),
163 Device.Type.SWITCH,
164 UNKNOWN, UNKNOWN,
165 UNKNOWN, UNKNOWN,
166 cid,
167 annotations);
168 providerService.deviceConnected(deviceId, deviceDescription);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530169
Sanjay Se8dcfee2015-04-23 10:07:08 +0530170 }
171
172 @Override
andreaeb70a942015-10-16 21:34:46 -0700173 public void deviceRemoved(NetconfDeviceInfo nodeId) {
Andrea Campanella101417d2015-12-11 17:58:07 -0800174 Preconditions.checkNotNull(nodeId, ISNULL);
andreaeb70a942015-10-16 21:34:46 -0700175 DeviceId deviceId = nodeId.getDeviceId();
176 providerService.deviceDisconnected(deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530177
andreaeb70a942015-10-16 21:34:46 -0700178 }
179 }
180
andreaeb70a942015-10-16 21:34:46 -0700181 private void connectDevices() {
182 NetconfProviderConfig cfg = cfgService.getConfig(appId, NetconfProviderConfig.class);
183 if (cfg != null) {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530184 try {
Andrea Campanella087ceb92015-12-07 09:58:34 -0800185 cfg.getDevicesAddresses().stream()
186 .forEach(addr -> {
187 try {
188 controller.connectDevice(
189 new NetconfDeviceInfo(addr.name(),
190 addr.password(),
191 addr.ip(),
192 addr.port()));
193 } catch (IOException e) {
Andrea Campanella8b1cb672016-01-25 13:58:58 -0800194 throw new RuntimeException(
195 new NetconfException(
196 "Can't connect to NETCONF " +
197 "device on " + addr.ip() +
198 ":" + addr.port(), e));
Andrea Campanella087ceb92015-12-07 09:58:34 -0800199 }
200 }
201 );
202
andreaeb70a942015-10-16 21:34:46 -0700203 } catch (ConfigException e) {
204 log.error("Cannot read config error " + e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530205 }
206 }
andreaeb70a942015-10-16 21:34:46 -0700207 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530208
andreaeb70a942015-10-16 21:34:46 -0700209 private class InternalNetworkConfigListener implements NetworkConfigListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530210
andreaeb70a942015-10-16 21:34:46 -0700211
212 @Override
213 public void event(NetworkConfigEvent event) {
214 connectDevices();
Sanjay Se8dcfee2015-04-23 10:07:08 +0530215 }
216
andreaeb70a942015-10-16 21:34:46 -0700217 @Override
218 public boolean isRelevant(NetworkConfigEvent event) {
219 //TODO refactor
220 return event.configClass().equals(NetconfProviderConfig.class) &&
221 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
222 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530223 }
224 }
225}