blob: 860b1f82bea9e809c41802ca20c8ac709ff35ee9 [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 -070052import java.util.Map;
53
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
Andrea Campanella1cd641b2015-12-07 17:28:34 -080068 // @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69// protected DeviceService deviceService;
andreaeb70a942015-10-16 21:34:46 -070070 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected NetconfController controller; //where is initiated ?
Sanjay Se8dcfee2015-04-23 10:07:08 +053072
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -070074 protected NetworkConfigRegistry cfgService;
Sanjay Se8dcfee2015-04-23 10:07:08 +053075
Thomas Vachuskad6811712015-04-29 21:37:04 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
andreaeb70a942015-10-16 21:34:46 -070077 protected CoreService coreService;
Thomas Vachuskad6811712015-04-29 21:37:04 -070078
Sanjay Se8dcfee2015-04-23 10:07:08 +053079
andreaeb70a942015-10-16 21:34:46 -070080 private DeviceProviderService providerService;
81 private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener();
82 protected static final String ISNOTNULL = "NetconfDeviceInfo is not null";
83 private static final String UNKNOWN = "unknown";
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);
andreaeb70a942015-10-16 21:34:46 -0700102 cfgService.registerConfigFactory(factory);
103 cfgService.addListener(cfgLister);
104 controller.addDeviceListener(innerNodeListener);
105 connectExistingDevices();
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);
Sanjay Seb5eebb2015-04-24 15:44:50 +0530115 log.info("Stopped");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530116 }
117
andreaeb70a942015-10-16 21:34:46 -0700118 public NetconfDeviceProvider() {
119 super(new ProviderId("netconf", "org.onosproject.netconf.provider.device"));
Sanjay Se8dcfee2015-04-23 10:07:08 +0530120 }
121
122 @Override
123 public void triggerProbe(DeviceId deviceId) {
andreaeb70a942015-10-16 21:34:46 -0700124 // TODO: This will be implemented later.
125 log.info("Triggering probe on device {}", deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530126 }
127
128 @Override
129 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
andreaeb70a942015-10-16 21:34:46 -0700130 // TODO: This will be implemented later.
Sanjay Se8dcfee2015-04-23 10:07:08 +0530131 }
132
133 @Override
134 public boolean isReachable(DeviceId deviceId) {
andreaeb70a942015-10-16 21:34:46 -0700135 Map<DeviceId, NetconfDevice> devices = controller.getDevicesMap();
136
137 NetconfDevice netconfDevice = null;
138 for (DeviceId key : devices.keySet()) {
139 if (key.equals(deviceId)) {
140 netconfDevice = controller.getDevicesMap().get(key);
141 }
142 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530143 if (netconfDevice == null) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530144 log.warn("BAD REQUEST: the requested device id: "
andreaeb70a942015-10-16 21:34:46 -0700145 + deviceId.toString()
146 + " is not associated to any NETCONF Device");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530147 return false;
148 }
andreaeb70a942015-10-16 21:34:46 -0700149 return netconfDevice.isActive();
Sanjay Se8dcfee2015-04-23 10:07:08 +0530150 }
151
andreaeb70a942015-10-16 21:34:46 -0700152 private class InnerNetconfDeviceListener implements NetconfDeviceListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530153
andreaeb70a942015-10-16 21:34:46 -0700154 @Override
155 public void deviceAdded(NetconfDeviceInfo nodeId) {
156 Preconditions.checkNotNull(nodeId, ISNOTNULL);
157 DeviceId deviceId = nodeId.getDeviceId();
andreaeb70a942015-10-16 21:34:46 -0700158 //Netconf configuration object
159 ChassisId cid = new ChassisId();
160 String ipAddress = nodeId.ip().toString();
161 SparseAnnotations annotations = DefaultAnnotations.builder()
162 .set("ipaddress", ipAddress).build();
163 DeviceDescription deviceDescription = new DefaultDeviceDescription(
164 deviceId.uri(),
165 Device.Type.SWITCH,
166 UNKNOWN, UNKNOWN,
167 UNKNOWN, UNKNOWN,
168 cid,
169 annotations);
170 providerService.deviceConnected(deviceId, deviceDescription);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530171
Sanjay Se8dcfee2015-04-23 10:07:08 +0530172 }
173
174 @Override
andreaeb70a942015-10-16 21:34:46 -0700175 public void deviceRemoved(NetconfDeviceInfo nodeId) {
176 Preconditions.checkNotNull(nodeId, ISNOTNULL);
177 DeviceId deviceId = nodeId.getDeviceId();
178 providerService.deviceDisconnected(deviceId);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530179
andreaeb70a942015-10-16 21:34:46 -0700180 }
181 }
182
183 private void connectExistingDevices() {
184 //TODO consolidate
185 appId = coreService.registerApplication("org.onosproject.netconf");
186 connectDevices();
187 }
188
189 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 {
196 controller.connectDevice(
197 new NetconfDeviceInfo(addr.name(),
198 addr.password(),
199 addr.ip(),
200 addr.port()));
201 } catch (IOException e) {
202 log.warn("Can't connect to NETCONF " +
203 "device on {}:{}",
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800204 addr.ip(),
205 addr.port());
Andrea Campanella087ceb92015-12-07 09:58:34 -0800206 }
207 }
208 );
209
andreaeb70a942015-10-16 21:34:46 -0700210 } catch (ConfigException e) {
211 log.error("Cannot read config error " + e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530212 }
213 }
andreaeb70a942015-10-16 21:34:46 -0700214 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530215
andreaeb70a942015-10-16 21:34:46 -0700216 private class InternalNetworkConfigListener implements NetworkConfigListener {
Sanjay Se8dcfee2015-04-23 10:07:08 +0530217
andreaeb70a942015-10-16 21:34:46 -0700218
219 @Override
220 public void event(NetworkConfigEvent event) {
221 connectDevices();
Sanjay Se8dcfee2015-04-23 10:07:08 +0530222 }
223
andreaeb70a942015-10-16 21:34:46 -0700224 @Override
225 public boolean isRelevant(NetworkConfigEvent event) {
226 //TODO refactor
227 return event.configClass().equals(NetconfProviderConfig.class) &&
228 (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
229 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530230 }
231 }
232}