Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.host.impl; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Service; |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 27 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 28 | import org.onosproject.core.Permission; |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 29 | import org.onosproject.incubator.net.config.NetworkConfigEvent; |
| 30 | import org.onosproject.incubator.net.config.NetworkConfigListener; |
| 31 | import org.onosproject.incubator.net.config.NetworkConfigService; |
| 32 | import org.onosproject.incubator.net.config.basics.BasicHostConfig; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.ConnectPoint; |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 34 | import org.onosproject.net.DefaultAnnotations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.Host; |
| 37 | import org.onosproject.net.HostId; |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 38 | import org.onosproject.net.SparseAnnotations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.device.DeviceService; |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 40 | import org.onosproject.net.host.DefaultHostDescription; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.host.HostAdminService; |
| 42 | import org.onosproject.net.host.HostDescription; |
| 43 | import org.onosproject.net.host.HostEvent; |
| 44 | import org.onosproject.net.host.HostListener; |
| 45 | import org.onosproject.net.host.HostProvider; |
| 46 | import org.onosproject.net.host.HostProviderRegistry; |
| 47 | import org.onosproject.net.host.HostProviderService; |
| 48 | import org.onosproject.net.host.HostService; |
| 49 | import org.onosproject.net.host.HostStore; |
| 50 | import org.onosproject.net.host.HostStoreDelegate; |
| 51 | import org.onosproject.net.host.PortAddresses; |
| 52 | import org.onosproject.net.packet.PacketService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 53 | import org.onosproject.net.provider.AbstractProviderService; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 54 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 55 | |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 56 | import java.util.Set; |
| 57 | |
| 58 | import static com.google.common.base.Preconditions.checkNotNull; |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 59 | import static com.google.common.base.Preconditions.checkState; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 60 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 61 | import static org.slf4j.LoggerFactory.getLogger; |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 62 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Provides basic implementation of the host SB & NB APIs. |
| 65 | */ |
| 66 | @Component(immediate = true) |
| 67 | @Service |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 68 | public class HostManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 69 | extends AbstractListenerProviderRegistry<HostEvent, HostListener, HostProvider, HostProviderService> |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 70 | implements HostService, HostAdminService, HostProviderRegistry { |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 71 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 72 | private final Logger log = getLogger(getClass()); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 73 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 74 | public static final String HOST_ID_NULL = "Host ID cannot be null"; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 75 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 76 | private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener(); |
| 77 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 78 | private HostStoreDelegate delegate = new InternalStoreDelegate(); |
| 79 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected HostStore store; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 82 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 84 | protected DeviceService deviceService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected PacketService packetService; |
| 88 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected NetworkConfigService networkConfigService; |
| 91 | |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 92 | private HostMonitor monitor; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 93 | |
| 94 | @Activate |
| 95 | public void activate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 96 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 97 | eventDispatcher.addSink(HostEvent.class, listenerRegistry); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 98 | networkConfigService.addListener(networkConfigListener); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 99 | monitor = new HostMonitor(deviceService, packetService, this); |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 100 | monitor.start(); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 101 | log.info("Started"); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Deactivate |
| 105 | public void deactivate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 106 | store.unsetDelegate(delegate); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 107 | eventDispatcher.removeSink(HostEvent.class); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 108 | networkConfigService.removeListener(networkConfigListener); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 109 | log.info("Stopped"); |
| 110 | } |
| 111 | |
| 112 | @Override |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 113 | protected HostProviderService createProviderService(HostProvider provider) { |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 114 | monitor.registerHostProvider(provider); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 115 | return new InternalHostProviderService(provider); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 116 | } |
| 117 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 118 | @Override |
| 119 | public int getHostCount() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 120 | checkPermission(Permission.HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 121 | return store.getHostCount(); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Iterable<Host> getHosts() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 126 | checkPermission(Permission.HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 127 | return store.getHosts(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public Host getHost(HostId hostId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 132 | checkPermission(Permission.HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 133 | checkNotNull(hostId, HOST_ID_NULL); |
| 134 | return store.getHost(hostId); |
| 135 | } |
| 136 | |
| 137 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 138 | public Set<Host> getHostsByVlan(VlanId vlanId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 139 | checkPermission(Permission.HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 140 | return store.getHosts(vlanId); |
| 141 | } |
| 142 | |
| 143 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 144 | public Set<Host> getHostsByMac(MacAddress mac) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 145 | checkPermission(Permission.HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 146 | checkNotNull(mac, "MAC address cannot be null"); |
| 147 | return store.getHosts(mac); |
| 148 | } |
| 149 | |
| 150 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 151 | public Set<Host> getHostsByIp(IpAddress ip) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 152 | checkPermission(Permission.HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 153 | checkNotNull(ip, "IP address cannot be null"); |
| 154 | return store.getHosts(ip); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 159 | checkPermission(Permission.HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 160 | checkNotNull(connectPoint, "Connection point cannot be null"); |
| 161 | return store.getConnectedHosts(connectPoint); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 166 | checkPermission(Permission.HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 167 | checkNotNull(deviceId, "Device ID cannot be null"); |
| 168 | return store.getConnectedHosts(deviceId); |
| 169 | } |
| 170 | |
| 171 | @Override |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 172 | public void startMonitoringIp(IpAddress ip) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 173 | checkPermission(Permission.HOST_EVENT); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 174 | monitor.addMonitoringFor(ip); |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 178 | public void stopMonitoringIp(IpAddress ip) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 179 | checkPermission(Permission.HOST_EVENT); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 180 | monitor.stopMonitoring(ip); |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void requestMac(IpAddress ip) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 185 | // FIXME!!!! Auto-generated method stub |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 186 | } |
| 187 | |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 188 | @Override |
| 189 | public void removeHost(HostId hostId) { |
| 190 | checkNotNull(hostId, HOST_ID_NULL); |
| 191 | HostEvent event = store.removeHost(hostId); |
| 192 | if (event != null) { |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 193 | post(event); |
| 194 | } |
| 195 | } |
| 196 | |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 197 | @Override |
Jonathan Hart | 09585c6 | 2014-09-23 16:58:04 -0700 | [diff] [blame] | 198 | public void bindAddressesToPort(PortAddresses addresses) { |
| 199 | store.updateAddressBindings(addresses); |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
Jonathan Hart | 09585c6 | 2014-09-23 16:58:04 -0700 | [diff] [blame] | 203 | public void unbindAddressesFromPort(PortAddresses portAddresses) { |
| 204 | store.removeAddressBindings(portAddresses); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void clearAddresses(ConnectPoint connectPoint) { |
| 209 | store.clearAddressBindings(connectPoint); |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public Set<PortAddresses> getAddressBindings() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 214 | checkPermission(Permission.HOST_READ); |
Jonathan Hart | 43c182c | 2014-09-23 11:13:42 -0700 | [diff] [blame] | 215 | return store.getAddressBindings(); |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Override |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 219 | public Set<PortAddresses> getAddressBindingsForPort(ConnectPoint connectPoint) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 220 | checkPermission(Permission.HOST_READ); |
Jonathan Hart | 43c182c | 2014-09-23 11:13:42 -0700 | [diff] [blame] | 221 | return store.getAddressBindingsForPort(connectPoint); |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 222 | } |
| 223 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 224 | // Personalized host provider service issued to the supplied provider. |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 225 | private class InternalHostProviderService |
| 226 | extends AbstractProviderService<HostProvider> |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 227 | implements HostProviderService { |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 228 | InternalHostProviderService(HostProvider provider) { |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 229 | super(provider); |
| 230 | } |
| 231 | |
| 232 | @Override |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 233 | public void hostDetected(HostId hostId, HostDescription hostDescription) { |
| 234 | checkNotNull(hostId, HOST_ID_NULL); |
| 235 | checkValidity(); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 236 | hostDescription = validateHost(hostDescription, hostId); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 237 | HostEvent event = store.createOrUpdateHost(provider().id(), hostId, |
| 238 | hostDescription); |
| 239 | if (event != null) { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 240 | post(event); |
| 241 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 244 | // returns a HostDescription made from the union of the BasicHostConfig |
| 245 | // annotations if it exists |
| 246 | private HostDescription validateHost(HostDescription hostDescription, HostId hostId) { |
| 247 | BasicHostConfig cfg = networkConfigService.getConfig(hostId, BasicHostConfig.class); |
| 248 | checkState(cfg == null || cfg.isAllowed(), "Host {} is not allowed", hostId); |
| 249 | if (cfg != null) { |
| 250 | SparseAnnotations finalSparse = processAnnotations(cfg, hostDescription); |
| 251 | hostDescription = new DefaultHostDescription(hostId.mac(), |
| 252 | hostDescription.vlan(), |
| 253 | hostDescription.location(), |
| 254 | finalSparse); |
| 255 | } |
| 256 | return hostDescription; |
| 257 | } |
| 258 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 259 | @Override |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 260 | public void hostVanished(HostId hostId) { |
| 261 | checkNotNull(hostId, HOST_ID_NULL); |
| 262 | checkValidity(); |
| 263 | HostEvent event = store.removeHost(hostId); |
| 264 | if (event != null) { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 265 | post(event); |
| 266 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 269 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 270 | // Supplements or replaces hostDescriptions's annotations with BasicHostConfig's |
| 271 | // annotations |
| 272 | private SparseAnnotations processAnnotations(BasicHostConfig cfg, HostDescription hostDescription) { |
| 273 | SparseAnnotations originalAnnotations = hostDescription.annotations(); |
| 274 | DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder(); |
| 275 | if (cfg.name() != null) { |
| 276 | newBuilder.set(cfg.NAME, cfg.name()); |
| 277 | } |
| 278 | if (cfg.latitude() != -1) { |
| 279 | newBuilder.set(cfg.LATITUDE, Double.toString(cfg.latitude())); |
| 280 | } |
| 281 | if (cfg.longitude() != -1) { |
| 282 | newBuilder.set(cfg.LONGITUDE, Double.toString(cfg.longitude())); |
| 283 | } |
| 284 | if (cfg.rackAddress() != null) { |
| 285 | newBuilder.set(cfg.RACK_ADDRESS, cfg.rackAddress()); |
| 286 | } |
| 287 | if (cfg.owner() != null) { |
| 288 | newBuilder.set(cfg.OWNER, cfg.owner()); |
| 289 | } |
| 290 | DefaultAnnotations newAnnotations = newBuilder.build(); |
| 291 | return DefaultAnnotations.union(originalAnnotations, newAnnotations); |
| 292 | } |
| 293 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 294 | // Store delegate to re-post events emitted from the store. |
| 295 | private class InternalStoreDelegate implements HostStoreDelegate { |
| 296 | @Override |
| 297 | public void notify(HostEvent event) { |
| 298 | post(event); |
| 299 | } |
| 300 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 301 | |
| 302 | // listens for NetworkConfigEvents of type BasicHostConfig and removes |
| 303 | // links that the config does not allow |
| 304 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 305 | @Override |
| 306 | public void event(NetworkConfigEvent event) { |
| 307 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 308 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
| 309 | event.configClass().equals(BasicHostConfig.class)) { |
| 310 | log.info("Detected Host network config event {}", event.type()); |
| 311 | kickOutBadHost(((HostId) event.subject())); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // checks if the specified host is allowed by the BasicHostConfig |
| 317 | // and if not, removes it |
| 318 | private void kickOutBadHost(HostId hostId) { |
| 319 | BasicHostConfig cfg = networkConfigService.getConfig(hostId, BasicHostConfig.class); |
| 320 | if (cfg != null && !cfg.isAllowed()) { |
| 321 | Host badHost = getHost(hostId); |
| 322 | if (badHost != null) { |
| 323 | removeHost(hostId); |
| 324 | } else { |
| 325 | log.info("Failed removal: Host {} does not exist", hostId); |
| 326 | } |
| 327 | } |
| 328 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 329 | } |