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 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 29 | import org.onosproject.net.Host; |
| 30 | import org.onosproject.net.HostId; |
| 31 | import org.onosproject.net.HostLocation; |
| 32 | import org.onosproject.net.config.NetworkConfigEvent; |
| 33 | import org.onosproject.net.config.NetworkConfigListener; |
| 34 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 35 | import org.onosproject.net.config.basics.BasicHostConfig; |
| 36 | import org.onosproject.net.host.DefaultHostDescription; |
| 37 | import org.onosproject.net.host.HostDescription; |
| 38 | import org.onosproject.net.host.HostProvider; |
| 39 | import org.onosproject.net.host.HostProviderRegistry; |
| 40 | import org.onosproject.net.host.HostProviderService; |
| 41 | import org.onosproject.net.provider.AbstractProvider; |
| 42 | import org.onosproject.net.provider.ProviderId; |
| 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 45 | |
| 46 | import java.util.Collections; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 47 | import java.util.Set; |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 48 | import java.util.stream.Collectors; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Host provider that uses network config service to discover hosts. |
| 52 | */ |
| 53 | @Component(immediate = true) |
| 54 | public class NetworkConfigHostProvider extends AbstractProvider implements HostProvider { |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected CoreService coreService; |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected HostProviderRegistry providerRegistry; |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected NetworkConfigRegistry networkConfigRegistry; |
| 63 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 64 | private ApplicationId appId; |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 65 | private static final String APP_NAME = "org.onosproject.netcfghost"; |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 66 | private static final ProviderId PROVIDER_ID = new ProviderId("host", APP_NAME); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 67 | protected HostProviderService providerService; |
| 68 | |
| 69 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 70 | private final InternalNetworkConfigListener networkConfigListener = |
| 71 | new InternalNetworkConfigListener(); |
| 72 | |
| 73 | /** |
| 74 | * Creates an network config host location provider. |
| 75 | */ |
| 76 | public NetworkConfigHostProvider() { |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 77 | super(PROVIDER_ID); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Activate |
| 81 | protected void activate() { |
| 82 | appId = coreService.registerApplication(APP_NAME); |
| 83 | providerService = providerRegistry.register(this); |
| 84 | networkConfigRegistry.addListener(networkConfigListener); |
| 85 | readInitialConfig(); |
| 86 | log.info("Started"); |
| 87 | } |
| 88 | |
| 89 | @Deactivate |
| 90 | protected void deactivate() { |
| 91 | networkConfigRegistry.removeListener(networkConfigListener); |
| 92 | providerRegistry.unregister(this); |
| 93 | providerService = null; |
| 94 | log.info("Stopped"); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void triggerProbe(Host host) { |
| 99 | /* |
Charles Chan | b3007e1 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 100 | * Note: All hosts are configured in network config host provider. |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 101 | * Therefore no probe is required. |
| 102 | */ |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Adds host information. |
| 107 | * IP information will be appended if host exists. |
| 108 | * |
| 109 | * @param mac MAC address of the host |
| 110 | * @param vlan VLAN ID of the host |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 111 | * @param locations Location of the host |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 112 | * @param ips Set of IP addresses of the host |
| 113 | */ |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 114 | 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] | 115 | HostId hid = HostId.hostId(mac, vlan); |
Charles Chan | 7e0e2e6 | 2016-04-15 16:30:41 -0700 | [diff] [blame] | 116 | HostDescription desc = (ips != null) ? |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 117 | new DefaultHostDescription(mac, vlan, locations, ips, true) : |
| 118 | new DefaultHostDescription(mac, vlan, locations, Collections.emptySet(), true); |
Charles Chan | b1e9924 | 2017-07-07 14:11:09 -0700 | [diff] [blame] | 119 | providerService.hostDetected(hid, desc, true); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Updates host information. |
| 124 | * IP information will be replaced if host exists. |
| 125 | * |
| 126 | * @param mac MAC address of the host |
| 127 | * @param vlan VLAN ID of the host |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 128 | * @param locations Location of the host |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 129 | * @param ips Set of IP addresses of the host |
| 130 | */ |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 131 | 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] | 132 | HostId hid = HostId.hostId(mac, vlan); |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 133 | HostDescription desc = new DefaultHostDescription(mac, vlan, locations, ips, true); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 134 | providerService.hostDetected(hid, desc, true); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Removes host information. |
| 139 | * |
| 140 | * @param mac MAC address of the host |
| 141 | * @param vlan VLAN ID of the host |
| 142 | */ |
| 143 | protected void removeHost(MacAddress mac, VlanId vlan) { |
| 144 | HostId hid = HostId.hostId(mac, vlan); |
| 145 | providerService.hostVanished(hid); |
| 146 | } |
| 147 | |
| 148 | private void readInitialConfig() { |
| 149 | networkConfigRegistry.getSubjects(HostId.class).forEach(hostId -> { |
| 150 | MacAddress mac = hostId.mac(); |
| 151 | VlanId vlan = hostId.vlanId(); |
| 152 | BasicHostConfig hostConfig = |
| 153 | networkConfigRegistry.getConfig(hostId, BasicHostConfig.class); |
| 154 | Set<IpAddress> ipAddresses = hostConfig.ipAddresses(); |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 155 | Set<HostLocation> locations = hostConfig.locations().stream() |
| 156 | .map(hostLocation -> new HostLocation(hostLocation, System.currentTimeMillis())) |
| 157 | .collect(Collectors.toSet()); |
| 158 | addHost(mac, vlan, locations, ipAddresses); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 159 | }); |
| 160 | } |
| 161 | |
| 162 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 163 | @Override |
| 164 | public void event(NetworkConfigEvent event) { |
| 165 | // Do not process non-host, register and unregister events |
| 166 | if (!event.configClass().equals(BasicHostConfig.class) || |
| 167 | event.type() == NetworkConfigEvent.Type.CONFIG_REGISTERED || |
| 168 | event.type() == NetworkConfigEvent.Type.CONFIG_UNREGISTERED) { |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | HostId hostId = (HostId) event.subject(); |
| 173 | MacAddress mac = hostId.mac(); |
| 174 | VlanId vlan = hostId.vlanId(); |
| 175 | BasicHostConfig hostConfig = |
| 176 | networkConfigRegistry.getConfig(hostId, BasicHostConfig.class); |
| 177 | Set<IpAddress> ipAddresses = null; |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 178 | Set<HostLocation> locations = null; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 179 | |
| 180 | // Note: There will be no config presented in the CONFIG_REMOVE case |
| 181 | if (hostConfig != null) { |
| 182 | ipAddresses = hostConfig.ipAddresses(); |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 183 | locations = hostConfig.locations().stream() |
| 184 | .map(hostLocation -> new HostLocation(hostLocation, System.currentTimeMillis())) |
| 185 | .collect(Collectors.toSet()); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | switch (event.type()) { |
| 189 | case CONFIG_ADDED: |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 190 | addHost(mac, vlan, locations, ipAddresses); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 191 | break; |
| 192 | case CONFIG_UPDATED: |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 193 | updateHost(mac, vlan, locations, ipAddresses); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 194 | break; |
| 195 | case CONFIG_REMOVED: |
| 196 | removeHost(mac, vlan); |
| 197 | break; |
| 198 | default: |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |