blob: ea3bfcc727c93ab20fca132eba02f5f0dae21eb1 [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;
Sanjay Se8dcfee2015-04-23 10:07:08 +053049import org.slf4j.Logger;
50
Andrea Campanella087ceb92015-12-07 09:58:34 -080051import java.io.IOException;
andreaeb70a942015-10-16 21:34:46 -070052
53import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
54import static org.slf4j.LoggerFactory.getLogger;
55
Sanjay Se8dcfee2015-04-23 10:07:08 +053056/**
andreaeb70a942015-10-16 21:34:46 -070057 * Provider which uses an NETCONF controller to detect device.
Sanjay Se8dcfee2015-04-23 10:07:08 +053058 */
59@Component(immediate = true)
60public class NetconfDeviceProvider extends AbstractProvider
61 implements DeviceProvider {
andreaeb70a942015-10-16 21:34:46 -070062 private final Logger log = getLogger(getClass());
Sanjay Se8dcfee2015-04-23 10:07:08 +053063
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected DeviceProviderRegistry providerRegistry;
66
andreaeb70a942015-10-16 21:34:46 -070067 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andrea Campanella101417d2015-12-11 17:58:07 -080068 protected NetconfController controller;
Sanjay Se8dcfee2015-04-23 10:07:08 +053069
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -070071 protected NetworkConfigRegistry cfgService;
Sanjay Se8dcfee2015-04-23 10:07:08 +053072
Thomas Vachuskad6811712015-04-29 21:37:04 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -070074 protected CoreService coreService;
Thomas Vachuskad6811712015-04-29 21:37:04 -070075
Andrea Campanella101417d2015-12-11 17:58:07 -080076 private static final String APP_NAME = "org.onosproject.netconf";
77 private static final String SCHEME_NAME = "netconf";
78 private static final String DEVICE_PROVIDER_PACKAGE = "org.onosproject.netconf.provider.device";
79 private static final String UNKNOWN = "unknown";
Sanjay Se8dcfee2015-04-23 10:07:08 +053080
andreaeb70a942015-10-16 21:34:46 -070081 private DeviceProviderService providerService;
82 private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener();
Sanjay Se8dcfee2015-04-23 10:07:08 +053083
andreaeb70a942015-10-16 21:34:46 -070084 private final ConfigFactory factory =
85 new ConfigFactory<ApplicationId, NetconfProviderConfig>(APP_SUBJECT_FACTORY,
86 NetconfProviderConfig.class,
87 "devices",
88 true) {
89 @Override
90 public NetconfProviderConfig createConfig() {
91 return new NetconfProviderConfig();
92 }
93 };
94 private final NetworkConfigListener cfgLister = new InternalNetworkConfigListener();
95 private ApplicationId appId;
Sanjay Se8dcfee2015-04-23 10:07:08 +053096
Sanjay Se8dcfee2015-04-23 10:07:08 +053097
98 @Activate
andreaeb70a942015-10-16 21:34:46 -070099 public void activate() {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530100 providerService = providerRegistry.register(this);
Andrea Campanella101417d2015-12-11 17:58:07 -0800101 appId = coreService.registerApplication(APP_NAME);
andreaeb70a942015-10-16 21:34:46 -0700102 cfgService.registerConfigFactory(factory);
103 cfgService.addListener(cfgLister);
104 controller.addDeviceListener(innerNodeListener);
Andrea Campanella8e290c52016-01-12 15:54:50 -0800105 connectDevices();
Thomas Vachuskad6811712015-04-29 21:37:04 -0700106 log.info("Started");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530107 }
108
andreaeb70a942015-10-16 21:34:46 -0700109
Sanjay Se8dcfee2015-04-23 10:07:08 +0530110 @Deactivate
andreaeb70a942015-10-16 21:34:46 -0700111 public void deactivate() {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530112 providerRegistry.unregister(this);
113 providerService = null;
andreaeb70a942015-10-16 21:34:46 -0700114 cfgService.unregisterConfigFactory(factory);
Andrea Campanella101417d2015-12-11 17:58:07 -0800115 controller.removeDeviceListener(innerNodeListener);
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() {
Andrea Campanella101417d2015-12-11 17:58:07 -0800120 super(new ProviderId(SCHEME_NAME, DEVICE_PROVIDER_PACKAGE));
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) {
Andrea Campanella8e290c52016-01-12 15:54:50 -0800136 NetconfDevice netconfDevice = controller.getNetconfDevice(deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530137 if (netconfDevice == null) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530138 log.warn("BAD REQUEST: the requested device id: "
andreaeb70a942015-10-16 21:34:46 -0700139 + deviceId.toString()
140 + " is not associated to any NETCONF Device");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530141 return false;
142 }
andreaeb70a942015-10-16 21:34:46 -0700143 return netconfDevice.isActive();
Sanjay Se8dcfee2015-04-23 10:07:08 +0530144 }
145
andreaeb70a942015-10-16 21:34:46 -0700146 private class InnerNetconfDeviceListener implements NetconfDeviceListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530147
Andrea Campanella101417d2015-12-11 17:58:07 -0800148 private static final String IPADDRESS = "ipaddress";
149 protected static final String ISNULL = "NetconfDeviceInfo is null";
150
andreaeb70a942015-10-16 21:34:46 -0700151 @Override
152 public void deviceAdded(NetconfDeviceInfo nodeId) {
Andrea Campanella101417d2015-12-11 17:58:07 -0800153 Preconditions.checkNotNull(nodeId, ISNULL);
andreaeb70a942015-10-16 21:34:46 -0700154 DeviceId deviceId = nodeId.getDeviceId();
andreaeb70a942015-10-16 21:34:46 -0700155 //Netconf configuration object
156 ChassisId cid = new ChassisId();
157 String ipAddress = nodeId.ip().toString();
158 SparseAnnotations annotations = DefaultAnnotations.builder()
Andrea Campanella101417d2015-12-11 17:58:07 -0800159 .set(IPADDRESS, ipAddress).build();
andreaeb70a942015-10-16 21:34:46 -0700160 DeviceDescription deviceDescription = new DefaultDeviceDescription(
161 deviceId.uri(),
162 Device.Type.SWITCH,
163 UNKNOWN, UNKNOWN,
164 UNKNOWN, UNKNOWN,
165 cid,
166 annotations);
167 providerService.deviceConnected(deviceId, deviceDescription);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530168
Sanjay Se8dcfee2015-04-23 10:07:08 +0530169 }
170
171 @Override
andreaeb70a942015-10-16 21:34:46 -0700172 public void deviceRemoved(NetconfDeviceInfo nodeId) {
Andrea Campanella101417d2015-12-11 17:58:07 -0800173 Preconditions.checkNotNull(nodeId, ISNULL);
andreaeb70a942015-10-16 21:34:46 -0700174 DeviceId deviceId = nodeId.getDeviceId();
175 providerService.deviceDisconnected(deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530176
andreaeb70a942015-10-16 21:34:46 -0700177 }
178 }
179
andreaeb70a942015-10-16 21:34:46 -0700180 private void connectDevices() {
181 NetconfProviderConfig cfg = cfgService.getConfig(appId, NetconfProviderConfig.class);
182 if (cfg != null) {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530183 try {
Andrea Campanella087ceb92015-12-07 09:58:34 -0800184 cfg.getDevicesAddresses().stream()
185 .forEach(addr -> {
186 try {
187 controller.connectDevice(
188 new NetconfDeviceInfo(addr.name(),
189 addr.password(),
190 addr.ip(),
191 addr.port()));
192 } catch (IOException e) {
Andrea Campanella101417d2015-12-11 17:58:07 -0800193 log.info("Can't connect to NETCONF " +
Andrea Campanella087ceb92015-12-07 09:58:34 -0800194 "device on {}:{}",
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800195 addr.ip(),
196 addr.port());
Andrea Campanella087ceb92015-12-07 09:58:34 -0800197 }
198 }
199 );
200
andreaeb70a942015-10-16 21:34:46 -0700201 } catch (ConfigException e) {
202 log.error("Cannot read config error " + e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530203 }
204 }
andreaeb70a942015-10-16 21:34:46 -0700205 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530206
andreaeb70a942015-10-16 21:34:46 -0700207 private class InternalNetworkConfigListener implements NetworkConfigListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530208
andreaeb70a942015-10-16 21:34:46 -0700209
210 @Override
211 public void event(NetworkConfigEvent event) {
212 connectDevices();
Sanjay Se8dcfee2015-04-23 10:07:08 +0530213 }
214
andreaeb70a942015-10-16 21:34:46 -0700215 @Override
216 public boolean isRelevant(NetworkConfigEvent event) {
217 //TODO refactor
218 return event.configClass().equals(NetconfProviderConfig.class) &&
219 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
220 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530221 }
222 }
223}