Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package org.onosproject.provider.netcfghost; |
| 18 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.osgi.service.component.annotations.Activate; |
| 20 | import org.osgi.service.component.annotations.Component; |
| 21 | import org.osgi.service.component.annotations.Deactivate; |
| 22 | import org.osgi.service.component.annotations.Reference; |
| 23 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 24 | import org.onlab.packet.EthType; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.VlanId; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreService; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 30 | import org.onosproject.net.Host; |
| 31 | import org.onosproject.net.HostId; |
| 32 | import org.onosproject.net.HostLocation; |
| 33 | import org.onosproject.net.config.NetworkConfigEvent; |
| 34 | import org.onosproject.net.config.NetworkConfigListener; |
| 35 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 36 | import org.onosproject.net.config.basics.BasicHostConfig; |
| 37 | import org.onosproject.net.host.DefaultHostDescription; |
| 38 | import org.onosproject.net.host.HostDescription; |
| 39 | import org.onosproject.net.host.HostProvider; |
| 40 | import org.onosproject.net.host.HostProviderRegistry; |
| 41 | import org.onosproject.net.host.HostProviderService; |
| 42 | import org.onosproject.net.provider.AbstractProvider; |
| 43 | import org.onosproject.net.provider.ProviderId; |
| 44 | import org.slf4j.Logger; |
| 45 | import org.slf4j.LoggerFactory; |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 46 | |
| 47 | import java.util.Collections; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 48 | import java.util.Set; |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 49 | import java.util.stream.Collectors; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Host provider that uses network config service to discover hosts. |
| 53 | */ |
| 54 | @Component(immediate = true) |
| 55 | public class NetworkConfigHostProvider extends AbstractProvider implements HostProvider { |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 57 | protected CoreService coreService; |
| 58 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 60 | protected HostProviderRegistry providerRegistry; |
| 61 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 63 | protected NetworkConfigRegistry networkConfigRegistry; |
| 64 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 65 | private ApplicationId appId; |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 66 | private static final String APP_NAME = "org.onosproject.netcfghost"; |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 67 | private static final ProviderId PROVIDER_ID = new ProviderId("host", APP_NAME); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 68 | protected HostProviderService providerService; |
| 69 | |
| 70 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 71 | private final InternalNetworkConfigListener networkConfigListener = |
| 72 | new InternalNetworkConfigListener(); |
| 73 | |
| 74 | /** |
| 75 | * Creates an network config host location provider. |
| 76 | */ |
| 77 | public NetworkConfigHostProvider() { |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 78 | super(PROVIDER_ID); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Activate |
| 82 | protected void activate() { |
| 83 | appId = coreService.registerApplication(APP_NAME); |
| 84 | providerService = providerRegistry.register(this); |
| 85 | networkConfigRegistry.addListener(networkConfigListener); |
| 86 | readInitialConfig(); |
| 87 | log.info("Started"); |
| 88 | } |
| 89 | |
| 90 | @Deactivate |
| 91 | protected void deactivate() { |
| 92 | networkConfigRegistry.removeListener(networkConfigListener); |
| 93 | providerRegistry.unregister(this); |
| 94 | providerService = null; |
| 95 | log.info("Stopped"); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void triggerProbe(Host host) { |
| 100 | /* |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 101 | * Note: All hosts are configured in network config host provider. |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 102 | * Therefore no probe is required. |
| 103 | */ |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Adds host information. |
| 108 | * IP information will be appended if host exists. |
| 109 | * |
| 110 | * @param mac MAC address of the host |
| 111 | * @param vlan VLAN ID of the host |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 112 | * @param locations Location of the host |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 113 | * @param ips Set of IP addresses of the host |
| 114 | */ |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 115 | protected void addHost(MacAddress mac, VlanId vlan, Set<HostLocation> locations, Set<IpAddress> ips) { |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 116 | HostId hid = HostId.hostId(mac, vlan); |
Charles Chan | 7e0e2e6 | 2016-04-15 16:30:41 -0700 | [diff] [blame] | 117 | HostDescription desc = (ips != null) ? |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 118 | new DefaultHostDescription(mac, vlan, locations, ips, true) : |
| 119 | new DefaultHostDescription(mac, vlan, locations, Collections.emptySet(), true); |
Charles Chan | b1e9924 | 2017-07-07 14:11:09 -0700 | [diff] [blame] | 120 | providerService.hostDetected(hid, desc, true); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 124 | * Adds host information. |
| 125 | * IP information will be appended if host exists. |
| 126 | * |
| 127 | * @param mac MAC address of the host |
| 128 | * @param vlan VLAN ID of the host |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 129 | * @param locations Locations of the host |
| 130 | * @param auxLocations auxiliary locations of the host |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 131 | * @param ips Set of IP addresses of the host |
| 132 | * @param innerVlan host inner VLAN identifier |
| 133 | * @param outerTpid outer TPID of a host |
| 134 | */ |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 135 | protected void addHost(MacAddress mac, VlanId vlan, Set<HostLocation> locations, Set<HostLocation> auxLocations, |
| 136 | Set<IpAddress> ips, VlanId innerVlan, EthType outerTpid) { |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 137 | HostId hid = HostId.hostId(mac, vlan); |
| 138 | HostDescription desc = (ips != null) ? |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 139 | new DefaultHostDescription(mac, vlan, locations, auxLocations, ips, |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 140 | innerVlan, outerTpid, true) : |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 141 | new DefaultHostDescription(mac, vlan, locations, auxLocations, Collections.emptySet(), |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 142 | innerVlan, outerTpid, true); |
| 143 | providerService.hostDetected(hid, desc, true); |
| 144 | } |
| 145 | |
| 146 | /** |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 147 | * Updates host information. |
| 148 | * IP information will be replaced if host exists. |
| 149 | * |
| 150 | * @param mac MAC address of the host |
| 151 | * @param vlan VLAN ID of the host |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 152 | * @param locations Location of the host |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 153 | * @param ips Set of IP addresses of the host |
| 154 | */ |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 155 | protected void updateHost(MacAddress mac, VlanId vlan, Set<HostLocation> locations, Set<IpAddress> ips) { |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 156 | HostId hid = HostId.hostId(mac, vlan); |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 157 | HostDescription desc = new DefaultHostDescription(mac, vlan, locations, ips, true); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 158 | providerService.hostDetected(hid, desc, true); |
| 159 | } |
| 160 | |
| 161 | /** |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 162 | * Updates host information. |
| 163 | * IP information will be replaced if host exists. |
| 164 | * |
| 165 | * @param mac MAC address of the host |
| 166 | * @param vlan VLAN ID of the host |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 167 | * @param locations Locations of the host |
| 168 | * @param auxLocations auxiliary locations of the host |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 169 | * @param ips Set of IP addresses of the host |
| 170 | * @param innerVlan host inner VLAN identifier |
| 171 | * @param outerTpid outer TPID of a host |
| 172 | */ |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 173 | protected void updateHost(MacAddress mac, VlanId vlan, Set<HostLocation> locations, Set<HostLocation> auxLocations, |
| 174 | Set<IpAddress> ips, VlanId innerVlan, EthType outerTpid) { |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 175 | HostId hid = HostId.hostId(mac, vlan); |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 176 | HostDescription desc = new DefaultHostDescription(mac, vlan, locations, auxLocations, ips, |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 177 | innerVlan, outerTpid, true); |
| 178 | providerService.hostDetected(hid, desc, true); |
| 179 | } |
| 180 | |
| 181 | /** |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 182 | * Removes host information. |
| 183 | * |
| 184 | * @param mac MAC address of the host |
| 185 | * @param vlan VLAN ID of the host |
| 186 | */ |
| 187 | protected void removeHost(MacAddress mac, VlanId vlan) { |
| 188 | HostId hid = HostId.hostId(mac, vlan); |
| 189 | providerService.hostVanished(hid); |
| 190 | } |
| 191 | |
| 192 | private void readInitialConfig() { |
| 193 | networkConfigRegistry.getSubjects(HostId.class).forEach(hostId -> { |
| 194 | MacAddress mac = hostId.mac(); |
| 195 | VlanId vlan = hostId.vlanId(); |
| 196 | BasicHostConfig hostConfig = |
| 197 | networkConfigRegistry.getConfig(hostId, BasicHostConfig.class); |
| 198 | Set<IpAddress> ipAddresses = hostConfig.ipAddresses(); |
Thomas Vachuska | 53cdbaa | 2018-05-01 11:23:29 -0700 | [diff] [blame] | 199 | Set<HostLocation> locs = hostConfig.locations(); |
| 200 | if (locs != null) { |
| 201 | Set<HostLocation> locations = locs.stream() |
| 202 | .map(hostLocation -> new HostLocation(hostLocation, System.currentTimeMillis())) |
| 203 | .collect(Collectors.toSet()); |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 204 | |
| 205 | // auxLocations allows to be null |
| 206 | Set<HostLocation> auxLocations = hostConfig.auxLocations(); |
| 207 | if (auxLocations != null) { |
| 208 | auxLocations = auxLocations.stream() |
| 209 | .map(auxLocation -> new HostLocation(auxLocation, System.currentTimeMillis())) |
| 210 | .collect(Collectors.toSet()); |
| 211 | } |
| 212 | |
Thomas Vachuska | 53cdbaa | 2018-05-01 11:23:29 -0700 | [diff] [blame] | 213 | VlanId innerVlan = hostConfig.innerVlan(); |
| 214 | EthType outerTpid = hostConfig.outerTpid(); |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 215 | addHost(mac, vlan, locations, auxLocations, ipAddresses, innerVlan, outerTpid); |
Thomas Vachuska | 53cdbaa | 2018-05-01 11:23:29 -0700 | [diff] [blame] | 216 | } else { |
| 217 | log.warn("Host {} configuration {} is missing locations", hostId, hostConfig); |
| 218 | } |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 219 | }); |
| 220 | } |
| 221 | |
| 222 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 223 | @Override |
| 224 | public void event(NetworkConfigEvent event) { |
| 225 | // Do not process non-host, register and unregister events |
| 226 | if (!event.configClass().equals(BasicHostConfig.class) || |
| 227 | event.type() == NetworkConfigEvent.Type.CONFIG_REGISTERED || |
| 228 | event.type() == NetworkConfigEvent.Type.CONFIG_UNREGISTERED) { |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | HostId hostId = (HostId) event.subject(); |
| 233 | MacAddress mac = hostId.mac(); |
| 234 | VlanId vlan = hostId.vlanId(); |
Charles Chan | b34a0bd | 2017-09-12 13:32:41 -0700 | [diff] [blame] | 235 | BasicHostConfig hostConfig = networkConfigRegistry.getConfig(hostId, BasicHostConfig.class); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 236 | Set<IpAddress> ipAddresses = null; |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 237 | Set<HostLocation> locations = null; |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 238 | Set<HostLocation> auxLocations = null; |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 239 | VlanId innerVlan = VlanId.NONE; |
| 240 | EthType outerTpid = EthType.EtherType.UNKNOWN.ethType(); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 241 | |
| 242 | // Note: There will be no config presented in the CONFIG_REMOVE case |
| 243 | if (hostConfig != null) { |
| 244 | ipAddresses = hostConfig.ipAddresses(); |
Charles Chan | b34a0bd | 2017-09-12 13:32:41 -0700 | [diff] [blame] | 245 | locations = hostConfig.locations(); |
| 246 | if (locations == null || locations.size() < 1) { |
| 247 | log.debug("Ignore network config event without host locations {}", event); |
| 248 | return; |
| 249 | } |
| 250 | locations = locations.stream() |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 251 | .map(hostLocation -> new HostLocation(hostLocation, System.currentTimeMillis())) |
| 252 | .collect(Collectors.toSet()); |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 253 | |
| 254 | // auxLocations allows to be null |
| 255 | auxLocations = hostConfig.auxLocations(); |
| 256 | if (auxLocations != null) { |
| 257 | auxLocations = auxLocations.stream() |
| 258 | .map(auxLocation -> new HostLocation(auxLocation, System.currentTimeMillis())) |
| 259 | .collect(Collectors.toSet()); |
| 260 | } |
| 261 | |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 262 | innerVlan = hostConfig.innerVlan(); |
| 263 | outerTpid = hostConfig.outerTpid(); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | switch (event.type()) { |
| 267 | case CONFIG_ADDED: |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 268 | addHost(mac, vlan, locations, auxLocations, ipAddresses, innerVlan, outerTpid); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 269 | break; |
| 270 | case CONFIG_UPDATED: |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 271 | updateHost(mac, vlan, locations, auxLocations, ipAddresses, innerVlan, outerTpid); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 272 | break; |
| 273 | case CONFIG_REMOVED: |
| 274 | removeHost(mac, vlan); |
| 275 | break; |
| 276 | default: |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |