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