blob: 0bf3493d28ec4db8ff5649f2e3b19597d72d148d [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;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080034import org.onosproject.net.behaviour.PortDiscovery;
andreaeb70a942015-10-16 21:34:46 -070035import org.onosproject.net.config.ConfigFactory;
36import org.onosproject.net.config.NetworkConfigEvent;
37import org.onosproject.net.config.NetworkConfigListener;
38import org.onosproject.net.config.NetworkConfigRegistry;
Sanjay Se8dcfee2015-04-23 10:07:08 +053039import org.onosproject.net.device.DefaultDeviceDescription;
40import org.onosproject.net.device.DeviceDescription;
41import org.onosproject.net.device.DeviceProvider;
42import org.onosproject.net.device.DeviceProviderRegistry;
43import org.onosproject.net.device.DeviceProviderService;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080044import org.onosproject.net.device.DeviceService;
45import org.onosproject.net.driver.DriverService;
Sanjay Se8dcfee2015-04-23 10:07:08 +053046import org.onosproject.net.provider.AbstractProvider;
47import org.onosproject.net.provider.ProviderId;
andreaeb70a942015-10-16 21:34:46 -070048import org.onosproject.netconf.NetconfController;
49import org.onosproject.netconf.NetconfDevice;
50import org.onosproject.netconf.NetconfDeviceInfo;
51import org.onosproject.netconf.NetconfDeviceListener;
Andrea Campanella8b1cb672016-01-25 13:58:58 -080052import org.onosproject.netconf.NetconfException;
Sanjay Se8dcfee2015-04-23 10:07:08 +053053import org.slf4j.Logger;
54
Andrea Campanella087ceb92015-12-07 09:58:34 -080055import java.io.IOException;
andreaeb70a942015-10-16 21:34:46 -070056
57import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
58import static org.slf4j.LoggerFactory.getLogger;
59
Sanjay Se8dcfee2015-04-23 10:07:08 +053060/**
andreaeb70a942015-10-16 21:34:46 -070061 * Provider which uses an NETCONF controller to detect device.
Sanjay Se8dcfee2015-04-23 10:07:08 +053062 */
63@Component(immediate = true)
64public class NetconfDeviceProvider extends AbstractProvider
65 implements DeviceProvider {
andreaeb70a942015-10-16 21:34:46 -070066 private final Logger log = getLogger(getClass());
Sanjay Se8dcfee2015-04-23 10:07:08 +053067
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected DeviceProviderRegistry providerRegistry;
70
andreaeb70a942015-10-16 21:34:46 -070071 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella101417d2015-12-11 17:58:07 -080072 protected NetconfController controller;
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
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080080 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected DriverService driverService;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected DeviceService deviceService;
85
Andrea Campanella101417d2015-12-11 17:58:07 -080086 private static final String APP_NAME = "org.onosproject.netconf";
87 private static final String SCHEME_NAME = "netconf";
88 private static final String DEVICE_PROVIDER_PACKAGE = "org.onosproject.netconf.provider.device";
89 private static final String UNKNOWN = "unknown";
Sanjay Se8dcfee2015-04-23 10:07:08 +053090
andreaeb70a942015-10-16 21:34:46 -070091 private DeviceProviderService providerService;
92 private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener();
Sanjay Se8dcfee2015-04-23 10:07:08 +053093
andreaeb70a942015-10-16 21:34:46 -070094 private final ConfigFactory factory =
95 new ConfigFactory<ApplicationId, NetconfProviderConfig>(APP_SUBJECT_FACTORY,
96 NetconfProviderConfig.class,
97 "devices",
98 true) {
99 @Override
100 public NetconfProviderConfig createConfig() {
101 return new NetconfProviderConfig();
102 }
103 };
104 private final NetworkConfigListener cfgLister = new InternalNetworkConfigListener();
105 private ApplicationId appId;
Sanjay Se8dcfee2015-04-23 10:07:08 +0530106
Sanjay Se8dcfee2015-04-23 10:07:08 +0530107
108 @Activate
andreaeb70a942015-10-16 21:34:46 -0700109 public void activate() {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530110 providerService = providerRegistry.register(this);
Andrea Campanella101417d2015-12-11 17:58:07 -0800111 appId = coreService.registerApplication(APP_NAME);
andreaeb70a942015-10-16 21:34:46 -0700112 cfgService.registerConfigFactory(factory);
113 cfgService.addListener(cfgLister);
114 controller.addDeviceListener(innerNodeListener);
Andrea Campanella8e290c52016-01-12 15:54:50 -0800115 connectDevices();
Thomas Vachuskad6811712015-04-29 21:37:04 -0700116 log.info("Started");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530117 }
118
andreaeb70a942015-10-16 21:34:46 -0700119
Sanjay Se8dcfee2015-04-23 10:07:08 +0530120 @Deactivate
andreaeb70a942015-10-16 21:34:46 -0700121 public void deactivate() {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530122 providerRegistry.unregister(this);
123 providerService = null;
andreaeb70a942015-10-16 21:34:46 -0700124 cfgService.unregisterConfigFactory(factory);
Andrea Campanella101417d2015-12-11 17:58:07 -0800125 controller.removeDeviceListener(innerNodeListener);
Sanjay Seb5eebb2015-04-24 15:44:50 +0530126 log.info("Stopped");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530127 }
128
andreaeb70a942015-10-16 21:34:46 -0700129 public NetconfDeviceProvider() {
Andrea Campanella101417d2015-12-11 17:58:07 -0800130 super(new ProviderId(SCHEME_NAME, DEVICE_PROVIDER_PACKAGE));
Sanjay Se8dcfee2015-04-23 10:07:08 +0530131 }
132
133 @Override
134 public void triggerProbe(DeviceId deviceId) {
andreaeb70a942015-10-16 21:34:46 -0700135 // TODO: This will be implemented later.
136 log.info("Triggering probe on device {}", deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530137 }
138
139 @Override
140 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
andreaeb70a942015-10-16 21:34:46 -0700141 // TODO: This will be implemented later.
Sanjay Se8dcfee2015-04-23 10:07:08 +0530142 }
143
144 @Override
145 public boolean isReachable(DeviceId deviceId) {
Andrea Campanella8e290c52016-01-12 15:54:50 -0800146 NetconfDevice netconfDevice = controller.getNetconfDevice(deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530147 if (netconfDevice == null) {
Andrea Campanella2464dc32016-02-17 17:54:53 -0800148 log.debug("Requested device id: "
andreaeb70a942015-10-16 21:34:46 -0700149 + deviceId.toString()
150 + " is not associated to any NETCONF Device");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530151 return false;
152 }
andreaeb70a942015-10-16 21:34:46 -0700153 return netconfDevice.isActive();
Sanjay Se8dcfee2015-04-23 10:07:08 +0530154 }
155
andreaeb70a942015-10-16 21:34:46 -0700156 private class InnerNetconfDeviceListener implements NetconfDeviceListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530157
Andrea Campanella101417d2015-12-11 17:58:07 -0800158 private static final String IPADDRESS = "ipaddress";
159 protected static final String ISNULL = "NetconfDeviceInfo is null";
160
andreaeb70a942015-10-16 21:34:46 -0700161 @Override
162 public void deviceAdded(NetconfDeviceInfo nodeId) {
Andrea Campanella101417d2015-12-11 17:58:07 -0800163 Preconditions.checkNotNull(nodeId, ISNULL);
andreaeb70a942015-10-16 21:34:46 -0700164 DeviceId deviceId = nodeId.getDeviceId();
andreaeb70a942015-10-16 21:34:46 -0700165 //Netconf configuration object
166 ChassisId cid = new ChassisId();
167 String ipAddress = nodeId.ip().toString();
168 SparseAnnotations annotations = DefaultAnnotations.builder()
Andrea Campanella101417d2015-12-11 17:58:07 -0800169 .set(IPADDRESS, ipAddress).build();
andreaeb70a942015-10-16 21:34:46 -0700170 DeviceDescription deviceDescription = new DefaultDeviceDescription(
171 deviceId.uri(),
172 Device.Type.SWITCH,
173 UNKNOWN, UNKNOWN,
174 UNKNOWN, UNKNOWN,
175 cid,
176 annotations);
177 providerService.deviceConnected(deviceId, deviceDescription);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530178 }
179
180 @Override
andreaeb70a942015-10-16 21:34:46 -0700181 public void deviceRemoved(NetconfDeviceInfo nodeId) {
Andrea Campanella101417d2015-12-11 17:58:07 -0800182 Preconditions.checkNotNull(nodeId, ISNULL);
andreaeb70a942015-10-16 21:34:46 -0700183 DeviceId deviceId = nodeId.getDeviceId();
184 providerService.deviceDisconnected(deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530185
andreaeb70a942015-10-16 21:34:46 -0700186 }
187 }
188
andreaeb70a942015-10-16 21:34:46 -0700189 private void connectDevices() {
190 NetconfProviderConfig cfg = cfgService.getConfig(appId, NetconfProviderConfig.class);
191 if (cfg != null) {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530192 try {
Andrea Campanella087ceb92015-12-07 09:58:34 -0800193 cfg.getDevicesAddresses().stream()
194 .forEach(addr -> {
195 try {
Aaron Kruglikov17b4c852016-01-15 16:37:04 -0800196 NetconfDeviceInfo netconf = new NetconfDeviceInfo(addr.name(),
197 addr.password(),
198 addr.ip(),
199 addr.port());
200 controller.connectDevice(netconf);
201 Device device = deviceService.getDevice(netconf.getDeviceId());
202 if (device.is(PortDiscovery.class)) {
203 PortDiscovery portConfig = device.as(PortDiscovery.class);
204 if (portConfig != null) {
205 providerService.updatePorts(netconf.getDeviceId(),
206 portConfig.getPorts());
207 }
208 } else {
209 log.warn("No portGetter behaviour for device {}", netconf.getDeviceId());
210 }
211
Andrea Campanella087ceb92015-12-07 09:58:34 -0800212 } catch (IOException e) {
Andrea Campanella8b1cb672016-01-25 13:58:58 -0800213 throw new RuntimeException(
214 new NetconfException(
215 "Can't connect to NETCONF " +
216 "device on " + addr.ip() +
217 ":" + addr.port(), e));
Andrea Campanella087ceb92015-12-07 09:58:34 -0800218 }
219 }
220 );
221
andreaeb70a942015-10-16 21:34:46 -0700222 } catch (ConfigException e) {
223 log.error("Cannot read config error " + e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530224 }
225 }
andreaeb70a942015-10-16 21:34:46 -0700226 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530227
andreaeb70a942015-10-16 21:34:46 -0700228 private class InternalNetworkConfigListener implements NetworkConfigListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530229
andreaeb70a942015-10-16 21:34:46 -0700230
231 @Override
232 public void event(NetworkConfigEvent event) {
233 connectDevices();
Sanjay Se8dcfee2015-04-23 10:07:08 +0530234 }
235
andreaeb70a942015-10-16 21:34:46 -0700236 @Override
237 public boolean isRelevant(NetworkConfigEvent event) {
238 //TODO refactor
239 return event.configClass().equals(NetconfProviderConfig.class) &&
240 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
241 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530242 }
243 }
244}