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