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 | |
| 53 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 54 | import static org.slf4j.LoggerFactory.getLogger; |
| 55 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 56 | /** |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 57 | * Provider which uses an NETCONF controller to detect device. |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 58 | */ |
| 59 | @Component(immediate = true) |
| 60 | public class NetconfDeviceProvider extends AbstractProvider |
| 61 | implements DeviceProvider { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 62 | private final Logger log = getLogger(getClass()); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected DeviceProviderRegistry providerRegistry; |
| 66 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected NetconfController controller; //where is initiated ? |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 71 | protected NetworkConfigRegistry cfgService; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 72 | |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 74 | protected CoreService coreService; |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 75 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 76 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 77 | 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 S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 81 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 82 | 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 S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 94 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 95 | |
| 96 | @Activate |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 97 | public void activate() { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 98 | providerService = providerRegistry.register(this); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 99 | cfgService.registerConfigFactory(factory); |
| 100 | cfgService.addListener(cfgLister); |
| 101 | controller.addDeviceListener(innerNodeListener); |
Andrea Campanella | 8e290c5 | 2016-01-12 15:54:50 -0800 | [diff] [blame^] | 102 | appId = coreService.registerApplication("org.onosproject.netconf"); |
| 103 | connectDevices(); |
Thomas Vachuska | d681171 | 2015-04-29 21:37:04 -0700 | [diff] [blame] | 104 | log.info("Started"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 105 | } |
| 106 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 107 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 108 | @Deactivate |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 109 | public void deactivate() { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 110 | providerRegistry.unregister(this); |
| 111 | providerService = null; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 112 | cfgService.unregisterConfigFactory(factory); |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 113 | log.info("Stopped"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 114 | } |
| 115 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 116 | public NetconfDeviceProvider() { |
| 117 | super(new ProviderId("netconf", "org.onosproject.netconf.provider.device")); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void triggerProbe(DeviceId deviceId) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 122 | // TODO: This will be implemented later. |
| 123 | log.info("Triggering probe on device {}", deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 128 | // TODO: This will be implemented later. |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public boolean isReachable(DeviceId deviceId) { |
Andrea Campanella | 8e290c5 | 2016-01-12 15:54:50 -0800 | [diff] [blame^] | 133 | NetconfDevice netconfDevice = controller.getNetconfDevice(deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 134 | if (netconfDevice == null) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 135 | log.warn("BAD REQUEST: the requested device id: " |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 136 | + deviceId.toString() |
| 137 | + " is not associated to any NETCONF Device"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 138 | return false; |
| 139 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 140 | return netconfDevice.isActive(); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 141 | } |
| 142 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 143 | private class InnerNetconfDeviceListener implements NetconfDeviceListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 144 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 145 | @Override |
| 146 | public void deviceAdded(NetconfDeviceInfo nodeId) { |
| 147 | Preconditions.checkNotNull(nodeId, ISNOTNULL); |
| 148 | DeviceId deviceId = nodeId.getDeviceId(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 149 | //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 S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 162 | |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 166 | public void deviceRemoved(NetconfDeviceInfo nodeId) { |
| 167 | Preconditions.checkNotNull(nodeId, ISNOTNULL); |
| 168 | DeviceId deviceId = nodeId.getDeviceId(); |
| 169 | providerService.deviceDisconnected(deviceId); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 170 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 174 | private void connectDevices() { |
| 175 | NetconfProviderConfig cfg = cfgService.getConfig(appId, NetconfProviderConfig.class); |
| 176 | if (cfg != null) { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 177 | try { |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 178 | 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 Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 189 | addr.ip(), |
| 190 | addr.port()); |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | ); |
| 194 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 195 | } catch (ConfigException e) { |
| 196 | log.error("Cannot read config error " + e); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 197 | } |
| 198 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 199 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 200 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 201 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 202 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 203 | |
| 204 | @Override |
| 205 | public void event(NetworkConfigEvent event) { |
| 206 | connectDevices(); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 207 | } |
| 208 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 209 | @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 S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | } |