Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 16 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 17 | package org.onosproject.provider.netconf.device.impl; |
| 18 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.onlab.packet.ChassisId; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.incubator.net.config.basics.ConfigException; |
| 29 | import org.onosproject.net.DefaultAnnotations; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 30 | import org.onosproject.net.Device; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.MastershipRole; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 33 | import org.onosproject.net.SparseAnnotations; |
| 34 | import org.onosproject.net.config.ConfigFactory; |
| 35 | import org.onosproject.net.config.NetworkConfigEvent; |
| 36 | import org.onosproject.net.config.NetworkConfigListener; |
| 37 | import org.onosproject.net.config.NetworkConfigRegistry; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 38 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 39 | import org.onosproject.net.device.DeviceDescription; |
| 40 | import org.onosproject.net.device.DeviceProvider; |
| 41 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 42 | import org.onosproject.net.device.DeviceProviderService; |
| 43 | import org.onosproject.net.device.DeviceService; |
| 44 | import org.onosproject.net.provider.AbstractProvider; |
| 45 | import org.onosproject.net.provider.ProviderId; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 46 | import org.onosproject.netconf.NetconfController; |
| 47 | import org.onosproject.netconf.NetconfDevice; |
| 48 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 49 | import org.onosproject.netconf.NetconfDeviceListener; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame^] | 52 | import java.io.IOException; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 53 | import java.util.Map; |
| 54 | |
| 55 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 56 | import static org.slf4j.LoggerFactory.getLogger; |
| 57 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 58 | /** |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 59 | * Provider which uses an NETCONF controller to detect device. |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 60 | */ |
| 61 | @Component(immediate = true) |
| 62 | public class NetconfDeviceProvider extends AbstractProvider |
| 63 | implements DeviceProvider { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 64 | private final Logger log = getLogger(getClass()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected DeviceProviderRegistry providerRegistry; |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected DeviceService deviceService; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected NetconfController controller; //where is initiated ? |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 75 | protected NetworkConfigRegistry cfgService; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 76 | |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 78 | protected CoreService coreService; |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 79 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 80 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 81 | private DeviceProviderService providerService; |
| 82 | private NetconfDeviceListener innerNodeListener = new InnerNetconfDeviceListener(); |
| 83 | protected static final String ISNOTNULL = "NetconfDeviceInfo is not null"; |
| 84 | private static final String UNKNOWN = "unknown"; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 85 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 86 | private final ConfigFactory factory = |
| 87 | new ConfigFactory<ApplicationId, NetconfProviderConfig>(APP_SUBJECT_FACTORY, |
| 88 | NetconfProviderConfig.class, |
| 89 | "devices", |
| 90 | true) { |
| 91 | @Override |
| 92 | public NetconfProviderConfig createConfig() { |
| 93 | return new NetconfProviderConfig(); |
| 94 | } |
| 95 | }; |
| 96 | private final NetworkConfigListener cfgLister = new InternalNetworkConfigListener(); |
| 97 | private ApplicationId appId; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 98 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 99 | |
| 100 | @Activate |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 101 | public void activate() { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 102 | providerService = providerRegistry.register(this); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 103 | cfgService.registerConfigFactory(factory); |
| 104 | cfgService.addListener(cfgLister); |
| 105 | controller.addDeviceListener(innerNodeListener); |
| 106 | connectExistingDevices(); |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 107 | log.info("Started"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 108 | } |
| 109 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 110 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 111 | @Deactivate |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 112 | public void deactivate() { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 113 | providerRegistry.unregister(this); |
| 114 | providerService = null; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 115 | cfgService.unregisterConfigFactory(factory); |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 116 | log.info("Stopped"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 117 | } |
| 118 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 119 | public NetconfDeviceProvider() { |
| 120 | super(new ProviderId("netconf", "org.onosproject.netconf.provider.device")); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void triggerProbe(DeviceId deviceId) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 125 | // TODO: This will be implemented later. |
| 126 | log.info("Triggering probe on device {}", deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 131 | // TODO: This will be implemented later. |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public boolean isReachable(DeviceId deviceId) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 136 | Map<DeviceId, NetconfDevice> devices = controller.getDevicesMap(); |
| 137 | |
| 138 | NetconfDevice netconfDevice = null; |
| 139 | for (DeviceId key : devices.keySet()) { |
| 140 | if (key.equals(deviceId)) { |
| 141 | netconfDevice = controller.getDevicesMap().get(key); |
| 142 | } |
| 143 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 144 | if (netconfDevice == null) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 145 | log.warn("BAD REQUEST: the requested device id: " |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 146 | + deviceId.toString() |
| 147 | + " is not associated to any NETCONF Device"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 148 | return false; |
| 149 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 150 | return netconfDevice.isActive(); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 151 | } |
| 152 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 153 | private class InnerNetconfDeviceListener implements NetconfDeviceListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 154 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 155 | @Override |
| 156 | public void deviceAdded(NetconfDeviceInfo nodeId) { |
| 157 | Preconditions.checkNotNull(nodeId, ISNOTNULL); |
| 158 | DeviceId deviceId = nodeId.getDeviceId(); |
| 159 | //TODO filter for not netconf devices |
| 160 | //Netconf configuration object |
| 161 | ChassisId cid = new ChassisId(); |
| 162 | String ipAddress = nodeId.ip().toString(); |
| 163 | SparseAnnotations annotations = DefaultAnnotations.builder() |
| 164 | .set("ipaddress", ipAddress).build(); |
| 165 | DeviceDescription deviceDescription = new DefaultDeviceDescription( |
| 166 | deviceId.uri(), |
| 167 | Device.Type.SWITCH, |
| 168 | UNKNOWN, UNKNOWN, |
| 169 | UNKNOWN, UNKNOWN, |
| 170 | cid, |
| 171 | annotations); |
| 172 | providerService.deviceConnected(deviceId, deviceDescription); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 173 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 177 | public void deviceRemoved(NetconfDeviceInfo nodeId) { |
| 178 | Preconditions.checkNotNull(nodeId, ISNOTNULL); |
| 179 | DeviceId deviceId = nodeId.getDeviceId(); |
| 180 | providerService.deviceDisconnected(deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 181 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | private void connectExistingDevices() { |
| 186 | //TODO consolidate |
| 187 | appId = coreService.registerApplication("org.onosproject.netconf"); |
| 188 | connectDevices(); |
| 189 | } |
| 190 | |
| 191 | private void connectDevices() { |
| 192 | NetconfProviderConfig cfg = cfgService.getConfig(appId, NetconfProviderConfig.class); |
| 193 | if (cfg != null) { |
| 194 | log.info("cfg {}", cfg); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 195 | try { |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame^] | 196 | cfg.getDevicesAddresses().stream() |
| 197 | .forEach(addr -> { |
| 198 | try { |
| 199 | controller.connectDevice( |
| 200 | new NetconfDeviceInfo(addr.name(), |
| 201 | addr.password(), |
| 202 | addr.ip(), |
| 203 | addr.port())); |
| 204 | } catch (IOException e) { |
| 205 | log.warn("Can't connect to NETCONF " + |
| 206 | "device on {}:{}", |
| 207 | addr.ip(), |
| 208 | addr.port()); |
| 209 | } |
| 210 | } |
| 211 | ); |
| 212 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 213 | } catch (ConfigException e) { |
| 214 | log.error("Cannot read config error " + e); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 215 | } |
| 216 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 217 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 218 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 219 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 220 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 221 | |
| 222 | @Override |
| 223 | public void event(NetworkConfigEvent event) { |
| 224 | connectDevices(); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 225 | } |
| 226 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 227 | @Override |
| 228 | public boolean isRelevant(NetworkConfigEvent event) { |
| 229 | //TODO refactor |
| 230 | return event.configClass().equals(NetconfProviderConfig.class) && |
| 231 | (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 232 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |