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