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; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 43 | import org.onosproject.net.provider.AbstractProvider; |
| 44 | import org.onosproject.net.provider.ProviderId; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 45 | import org.onosproject.netconf.NetconfController; |
| 46 | import org.onosproject.netconf.NetconfDevice; |
| 47 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 48 | import org.onosproject.netconf.NetconfDeviceListener; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 49 | import org.slf4j.Logger; |
| 50 | |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 51 | import java.io.IOException; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 52 | import java.util.Map; |
| 53 | |
| 54 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 55 | import static org.slf4j.LoggerFactory.getLogger; |
| 56 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 57 | /** |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 58 | * Provider which uses an NETCONF controller to detect device. |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 59 | */ |
| 60 | @Component(immediate = true) |
| 61 | public class NetconfDeviceProvider extends AbstractProvider |
| 62 | implements DeviceProvider { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 63 | private final Logger log = getLogger(getClass()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected DeviceProviderRegistry providerRegistry; |
| 67 | |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame^] | 68 | // @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | // protected DeviceService deviceService; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected NetconfController controller; //where is initiated ? |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 74 | protected NetworkConfigRegistry cfgService; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 75 | |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 77 | protected CoreService coreService; |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 78 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 79 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 80 | 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 S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 84 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 85 | 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 S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 97 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 98 | |
| 99 | @Activate |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 100 | public void activate() { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 101 | providerService = providerRegistry.register(this); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 102 | cfgService.registerConfigFactory(factory); |
| 103 | cfgService.addListener(cfgLister); |
| 104 | controller.addDeviceListener(innerNodeListener); |
| 105 | connectExistingDevices(); |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 106 | log.info("Started"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 107 | } |
| 108 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 109 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 110 | @Deactivate |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 111 | public void deactivate() { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 112 | providerRegistry.unregister(this); |
| 113 | providerService = null; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 114 | cfgService.unregisterConfigFactory(factory); |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 115 | log.info("Stopped"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 116 | } |
| 117 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 118 | public NetconfDeviceProvider() { |
| 119 | super(new ProviderId("netconf", "org.onosproject.netconf.provider.device")); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void triggerProbe(DeviceId deviceId) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 124 | // TODO: This will be implemented later. |
| 125 | log.info("Triggering probe on device {}", deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 130 | // TODO: This will be implemented later. |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public boolean isReachable(DeviceId deviceId) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 135 | 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 S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 143 | if (netconfDevice == null) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 144 | log.warn("BAD REQUEST: the requested device id: " |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 145 | + deviceId.toString() |
| 146 | + " is not associated to any NETCONF Device"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 147 | return false; |
| 148 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 149 | return netconfDevice.isActive(); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 150 | } |
| 151 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 152 | private class InnerNetconfDeviceListener implements NetconfDeviceListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 153 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 154 | @Override |
| 155 | public void deviceAdded(NetconfDeviceInfo nodeId) { |
| 156 | Preconditions.checkNotNull(nodeId, ISNOTNULL); |
| 157 | DeviceId deviceId = nodeId.getDeviceId(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 158 | //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 S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 171 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 175 | public void deviceRemoved(NetconfDeviceInfo nodeId) { |
| 176 | Preconditions.checkNotNull(nodeId, ISNOTNULL); |
| 177 | DeviceId deviceId = nodeId.getDeviceId(); |
| 178 | providerService.deviceDisconnected(deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 179 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 180 | } |
| 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 S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 192 | try { |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 193 | 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 Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame^] | 204 | addr.ip(), |
| 205 | addr.port()); |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | ); |
| 209 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 210 | } catch (ConfigException e) { |
| 211 | log.error("Cannot read config error " + e); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 212 | } |
| 213 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 214 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 215 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 216 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 217 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 218 | |
| 219 | @Override |
| 220 | public void event(NetworkConfigEvent event) { |
| 221 | connectDevices(); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 222 | } |
| 223 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 224 | @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 S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | } |