blob: 0aea67ebe58b1b2a8894870a29726460542e28c4 [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)
68 protected NetconfController controller; //where is initiated ?
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
Sanjay Se8dcfee2015-04-23 10:07:08 +053076
andreaeb70a942015-10-16 21:34:46 -070077 private DeviceProviderService providerService;
78 private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener();
79 protected static final String ISNOTNULL = "NetconfDeviceInfo is not null";
80 private static final String UNKNOWN = "unknown";
Sanjay Se8dcfee2015-04-23 10:07:08 +053081
andreaeb70a942015-10-16 21:34:46 -070082 private final ConfigFactory factory =
83 new ConfigFactory<ApplicationId, NetconfProviderConfig>(APP_SUBJECT_FACTORY,
84 NetconfProviderConfig.class,
85 "devices",
86 true) {
87 @Override
88 public NetconfProviderConfig createConfig() {
89 return new NetconfProviderConfig();
90 }
91 };
92 private final NetworkConfigListener cfgLister = new InternalNetworkConfigListener();
93 private ApplicationId appId;
Sanjay Se8dcfee2015-04-23 10:07:08 +053094
Sanjay Se8dcfee2015-04-23 10:07:08 +053095
96 @Activate
andreaeb70a942015-10-16 21:34:46 -070097 public void activate() {
Sanjay Se8dcfee2015-04-23 10:07:08 +053098 providerService = providerRegistry.register(this);
andreaeb70a942015-10-16 21:34:46 -070099 cfgService.registerConfigFactory(factory);
100 cfgService.addListener(cfgLister);
101 controller.addDeviceListener(innerNodeListener);
Andrea Campanella8e290c52016-01-12 15:54:50 -0800102 appId = coreService.registerApplication("org.onosproject.netconf");
103 connectDevices();
Thomas Vachuskad6811712015-04-29 21:37:04 -0700104 log.info("Started");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530105 }
106
andreaeb70a942015-10-16 21:34:46 -0700107
Sanjay Se8dcfee2015-04-23 10:07:08 +0530108 @Deactivate
andreaeb70a942015-10-16 21:34:46 -0700109 public void deactivate() {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530110 providerRegistry.unregister(this);
111 providerService = null;
andreaeb70a942015-10-16 21:34:46 -0700112 cfgService.unregisterConfigFactory(factory);
Sanjay Seb5eebb2015-04-24 15:44:50 +0530113 log.info("Stopped");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530114 }
115
andreaeb70a942015-10-16 21:34:46 -0700116 public NetconfDeviceProvider() {
117 super(new ProviderId("netconf", "org.onosproject.netconf.provider.device"));
Sanjay Se8dcfee2015-04-23 10:07:08 +0530118 }
119
120 @Override
121 public void triggerProbe(DeviceId deviceId) {
andreaeb70a942015-10-16 21:34:46 -0700122 // TODO: This will be implemented later.
123 log.info("Triggering probe on device {}", deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530124 }
125
126 @Override
127 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
andreaeb70a942015-10-16 21:34:46 -0700128 // TODO: This will be implemented later.
Sanjay Se8dcfee2015-04-23 10:07:08 +0530129 }
130
131 @Override
132 public boolean isReachable(DeviceId deviceId) {
Andrea Campanella8e290c52016-01-12 15:54:50 -0800133 NetconfDevice netconfDevice = controller.getNetconfDevice(deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530134 if (netconfDevice == null) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530135 log.warn("BAD REQUEST: the requested device id: "
andreaeb70a942015-10-16 21:34:46 -0700136 + deviceId.toString()
137 + " is not associated to any NETCONF Device");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530138 return false;
139 }
andreaeb70a942015-10-16 21:34:46 -0700140 return netconfDevice.isActive();
Sanjay Se8dcfee2015-04-23 10:07:08 +0530141 }
142
andreaeb70a942015-10-16 21:34:46 -0700143 private class InnerNetconfDeviceListener implements NetconfDeviceListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530144
andreaeb70a942015-10-16 21:34:46 -0700145 @Override
146 public void deviceAdded(NetconfDeviceInfo nodeId) {
147 Preconditions.checkNotNull(nodeId, ISNOTNULL);
148 DeviceId deviceId = nodeId.getDeviceId();
andreaeb70a942015-10-16 21:34:46 -0700149 //Netconf configuration object
150 ChassisId cid = new ChassisId();
151 String ipAddress = nodeId.ip().toString();
152 SparseAnnotations annotations = DefaultAnnotations.builder()
153 .set("ipaddress", ipAddress).build();
154 DeviceDescription deviceDescription = new DefaultDeviceDescription(
155 deviceId.uri(),
156 Device.Type.SWITCH,
157 UNKNOWN, UNKNOWN,
158 UNKNOWN, UNKNOWN,
159 cid,
160 annotations);
161 providerService.deviceConnected(deviceId, deviceDescription);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530162
Sanjay Se8dcfee2015-04-23 10:07:08 +0530163 }
164
165 @Override
andreaeb70a942015-10-16 21:34:46 -0700166 public void deviceRemoved(NetconfDeviceInfo nodeId) {
167 Preconditions.checkNotNull(nodeId, ISNOTNULL);
168 DeviceId deviceId = nodeId.getDeviceId();
169 providerService.deviceDisconnected(deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530170
andreaeb70a942015-10-16 21:34:46 -0700171 }
172 }
173
andreaeb70a942015-10-16 21:34:46 -0700174 private void connectDevices() {
175 NetconfProviderConfig cfg = cfgService.getConfig(appId, NetconfProviderConfig.class);
176 if (cfg != null) {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530177 try {
Andrea Campanella087ceb92015-12-07 09:58:34 -0800178 cfg.getDevicesAddresses().stream()
179 .forEach(addr -> {
180 try {
181 controller.connectDevice(
182 new NetconfDeviceInfo(addr.name(),
183 addr.password(),
184 addr.ip(),
185 addr.port()));
186 } catch (IOException e) {
187 log.warn("Can't connect to NETCONF " +
188 "device on {}:{}",
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800189 addr.ip(),
190 addr.port());
Andrea Campanella087ceb92015-12-07 09:58:34 -0800191 }
192 }
193 );
194
andreaeb70a942015-10-16 21:34:46 -0700195 } catch (ConfigException e) {
196 log.error("Cannot read config error " + e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530197 }
198 }
andreaeb70a942015-10-16 21:34:46 -0700199 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530200
andreaeb70a942015-10-16 21:34:46 -0700201 private class InternalNetworkConfigListener implements NetworkConfigListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530202
andreaeb70a942015-10-16 21:34:46 -0700203
204 @Override
205 public void event(NetworkConfigEvent event) {
206 connectDevices();
Sanjay Se8dcfee2015-04-23 10:07:08 +0530207 }
208
andreaeb70a942015-10-16 21:34:46 -0700209 @Override
210 public boolean isRelevant(NetworkConfigEvent event) {
211 //TODO refactor
212 return event.configClass().equals(NetconfProviderConfig.class) &&
213 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
214 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530215 }
216 }
217}