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; |
Deepa Vaddireddy | 50ad080 | 2016-08-08 19:27:36 +0530 | [diff] [blame] | 109 | |
| 110 | @Property(name = "monitorHosts", boolValue = false, |
| 111 | label = "Enable/Disable monitoring of hosts") |
| 112 | private boolean monitorHosts = false; |
| 113 | |
| 114 | @Property(name = "probeRate", longValue = 30000, |
| 115 | label = "Set the probe Rate in milli seconds") |
| 116 | private long probeRate = 30000; |
| 117 | |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 118 | private HostMonitor monitor; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 119 | |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 120 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 121 | @Activate |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 122 | public void activate(ComponentContext context) { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 123 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 124 | eventDispatcher.addSink(HostEvent.class, listenerRegistry); |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 125 | cfgService.registerProperties(getClass()); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 126 | networkConfigService.addListener(networkConfigListener); |
Jonathan Hart | fb32a6e | 2015-09-01 12:12:14 +0200 | [diff] [blame] | 127 | monitor = new HostMonitor(packetService, this, interfaceService, edgePortService); |
Deepa Vaddireddy | 50ad080 | 2016-08-08 19:27:36 +0530 | [diff] [blame] | 128 | monitor.setProbeRate(probeRate); |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 129 | monitor.start(); |
Deepa Vaddireddy | 50ad080 | 2016-08-08 19:27:36 +0530 | [diff] [blame] | 130 | modified(context); |
| 131 | cfgService.registerProperties(getClass()); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 132 | log.info("Started"); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Deepa Vaddireddy | 50ad080 | 2016-08-08 19:27:36 +0530 | [diff] [blame] | 135 | @Deactivate |
| 136 | public void deactivate() { |
| 137 | store.unsetDelegate(delegate); |
| 138 | eventDispatcher.removeSink(HostEvent.class); |
| 139 | networkConfigService.removeListener(networkConfigListener); |
| 140 | cfgService.unregisterProperties(getClass(), false); |
| 141 | monitor.shutdown(); |
| 142 | log.info("Stopped"); |
| 143 | } |
| 144 | |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 145 | @Modified |
| 146 | public void modified(ComponentContext context) { |
Deepa Vaddireddy | 50ad080 | 2016-08-08 19:27:36 +0530 | [diff] [blame] | 147 | boolean oldValue = monitorHosts; |
| 148 | readComponentConfiguration(context); |
| 149 | if (probeRate > 0) { |
| 150 | monitor.setProbeRate(probeRate); |
| 151 | } else { |
| 152 | log.warn("probeRate cannot be lessthan 0"); |
| 153 | } |
| 154 | |
| 155 | if (oldValue != monitorHosts) { |
| 156 | if (monitorHosts) { |
| 157 | startMonitoring(); |
| 158 | } else { |
| 159 | stopMonitoring(); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Extracts properties from the component configuration context. |
| 166 | * |
| 167 | * @param context the component context |
| 168 | */ |
| 169 | private void readComponentConfiguration(ComponentContext context) { |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 170 | Dictionary<?, ?> properties = context.getProperties(); |
| 171 | Boolean flag; |
Deepa Vaddireddy | 50ad080 | 2016-08-08 19:27:36 +0530 | [diff] [blame] | 172 | |
| 173 | flag = Tools.isPropertyEnabled(properties, "monitorHosts"); |
| 174 | if (flag == null) { |
| 175 | log.info("monitorHosts is not enabled " + |
| 176 | "using current value of {}", monitorHosts); |
| 177 | } else { |
| 178 | monitorHosts = flag; |
| 179 | log.info("Configured. monitorHosts {}", |
| 180 | monitorHosts ? "enabled" : "disabled"); |
| 181 | } |
| 182 | |
| 183 | Long longValue = Tools.getLongProperty(properties, "probeRate"); |
| 184 | if (longValue == null || longValue == 0) { |
| 185 | log.info("probeRate is not set sing default value of {}", probeRate); |
| 186 | } else { |
| 187 | probeRate = longValue; |
| 188 | log.info("Configured. probeRate {}", probeRate); |
| 189 | } |
| 190 | |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 191 | flag = Tools.isPropertyEnabled(properties, "allowDuplicateIps"); |
| 192 | if (flag == null) { |
| 193 | log.info("Removal of duplicate ip address is not configured"); |
| 194 | } else { |
| 195 | allowDuplicateIps = flag; |
| 196 | log.info("Removal of duplicate ip address is {}", |
| 197 | allowDuplicateIps ? "disabled" : "enabled"); |
| 198 | } |
Deepa Vaddireddy | 50ad080 | 2016-08-08 19:27:36 +0530 | [diff] [blame] | 199 | |
| 200 | |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Deepa Vaddireddy | 50ad080 | 2016-08-08 19:27:36 +0530 | [diff] [blame] | 203 | /** |
| 204 | * Starts monitoring the hosts by IP Address. |
| 205 | * |
| 206 | */ |
| 207 | private void startMonitoring() { |
| 208 | store.getHosts().forEach(host -> { |
| 209 | host.ipAddresses().forEach(ip -> { |
| 210 | monitor.addMonitoringFor(ip); |
| 211 | }); |
| 212 | }); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Stops monitoring the hosts by IP Address. |
| 217 | * |
| 218 | */ |
| 219 | private void stopMonitoring() { |
| 220 | store.getHosts().forEach(host -> { |
| 221 | host.ipAddresses().forEach(ip -> { |
| 222 | monitor.stopMonitoring(ip); |
| 223 | }); |
| 224 | }); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 228 | protected HostProviderService createProviderService(HostProvider provider) { |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 229 | monitor.registerHostProvider(provider); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 230 | return new InternalHostProviderService(provider); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 231 | } |
| 232 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 233 | @Override |
| 234 | public int getHostCount() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 235 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 236 | return store.getHostCount(); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public Iterable<Host> getHosts() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 241 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 242 | return store.getHosts(); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public Host getHost(HostId hostId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 247 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 248 | checkNotNull(hostId, HOST_ID_NULL); |
| 249 | return store.getHost(hostId); |
| 250 | } |
| 251 | |
| 252 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 253 | public Set<Host> getHostsByVlan(VlanId vlanId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 254 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 255 | return store.getHosts(vlanId); |
| 256 | } |
| 257 | |
| 258 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 259 | public Set<Host> getHostsByMac(MacAddress mac) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 260 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 261 | checkNotNull(mac, "MAC address cannot be null"); |
| 262 | return store.getHosts(mac); |
| 263 | } |
| 264 | |
| 265 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 266 | public Set<Host> getHostsByIp(IpAddress ip) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 267 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 268 | checkNotNull(ip, "IP address cannot be null"); |
| 269 | return store.getHosts(ip); |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 274 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 275 | checkNotNull(connectPoint, "Connection point cannot be null"); |
| 276 | return store.getConnectedHosts(connectPoint); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 281 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 282 | checkNotNull(deviceId, "Device ID cannot be null"); |
| 283 | return store.getConnectedHosts(deviceId); |
| 284 | } |
| 285 | |
| 286 | @Override |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 287 | public void startMonitoringIp(IpAddress ip) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 288 | checkPermission(HOST_EVENT); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 289 | monitor.addMonitoringFor(ip); |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | @Override |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 293 | public void stopMonitoringIp(IpAddress ip) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 294 | checkPermission(HOST_EVENT); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 295 | monitor.stopMonitoring(ip); |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public void requestMac(IpAddress ip) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 300 | // FIXME!!!! Auto-generated method stub |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 301 | } |
| 302 | |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 303 | @Override |
| 304 | public void removeHost(HostId hostId) { |
| 305 | checkNotNull(hostId, HOST_ID_NULL); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 306 | store.removeHost(hostId); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 307 | } |
| 308 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 309 | // Personalized host provider service issued to the supplied provider. |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 310 | private class InternalHostProviderService |
| 311 | extends AbstractProviderService<HostProvider> |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 312 | implements HostProviderService { |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 313 | InternalHostProviderService(HostProvider provider) { |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 314 | super(provider); |
| 315 | } |
| 316 | |
| 317 | @Override |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 318 | public void hostDetected(HostId hostId, HostDescription hostDescription, boolean replaceIps) { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 319 | checkNotNull(hostId, HOST_ID_NULL); |
| 320 | checkValidity(); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 321 | hostDescription = validateHost(hostDescription, hostId); |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 322 | |
| 323 | if (!allowDuplicateIps) { |
| 324 | removeDuplicates(hostId, hostDescription); |
| 325 | } |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 326 | store.createOrUpdateHost(provider().id(), hostId, |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 327 | hostDescription, replaceIps); |
Deepa Vaddireddy | 50ad080 | 2016-08-08 19:27:36 +0530 | [diff] [blame] | 328 | |
| 329 | if (monitorHosts) { |
| 330 | hostDescription.ipAddress().forEach(ip -> { |
| 331 | monitor.addMonitoringFor(ip); |
| 332 | }); |
| 333 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Deepa Vaddireddy | 50ad080 | 2016-08-08 19:27:36 +0530 | [diff] [blame] | 336 | |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 337 | // When a new IP is detected, remove that IP on other hosts if it exists |
| 338 | public void removeDuplicates(HostId hostId, HostDescription desc) { |
| 339 | desc.ipAddress().forEach(ip -> { |
| 340 | Set<Host> allHosts = store.getHosts(ip); |
| 341 | allHosts.forEach(eachHost -> { |
| 342 | if (!(eachHost.id().equals(hostId))) { |
| 343 | log.info("Duplicate ip {} found on host {} and {}", ip, |
| 344 | hostId.toString(), eachHost.id().toString()); |
| 345 | store.removeIp(eachHost.id(), ip); |
| 346 | } |
| 347 | }); |
| 348 | }); |
Deepa Vaddireddy | 50ad080 | 2016-08-08 19:27:36 +0530 | [diff] [blame] | 349 | } |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 350 | |
| 351 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 352 | // returns a HostDescription made from the union of the BasicHostConfig |
| 353 | // annotations if it exists |
| 354 | private HostDescription validateHost(HostDescription hostDescription, HostId hostId) { |
| 355 | BasicHostConfig cfg = networkConfigService.getConfig(hostId, BasicHostConfig.class); |
| 356 | checkState(cfg == null || cfg.isAllowed(), "Host {} is not allowed", hostId); |
Ayaka Koshibe | 5373e76 | 2015-08-06 12:31:44 -0700 | [diff] [blame] | 357 | |
| 358 | return BasicHostOperator.combine(cfg, hostDescription); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 359 | } |
| 360 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 361 | @Override |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 362 | public void hostVanished(HostId hostId) { |
| 363 | checkNotNull(hostId, HOST_ID_NULL); |
| 364 | checkValidity(); |
Deepa Vaddireddy | 50ad080 | 2016-08-08 19:27:36 +0530 | [diff] [blame] | 365 | Host host = store.getHost(hostId); |
| 366 | if (monitorHosts) { |
| 367 | host.ipAddresses().forEach(ip -> { |
| 368 | monitor.stopMonitoring(ip); |
| 369 | }); |
| 370 | } |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 371 | store.removeHost(hostId); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 372 | } |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 373 | |
| 374 | @Override |
| 375 | public void removeIpFromHost(HostId hostId, IpAddress ipAddress) { |
| 376 | checkNotNull(hostId, HOST_ID_NULL); |
| 377 | checkValidity(); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 378 | store.removeIp(hostId, ipAddress); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 379 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 380 | } |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 381 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 382 | // Store delegate to re-post events emitted from the store. |
| 383 | private class InternalStoreDelegate implements HostStoreDelegate { |
| 384 | @Override |
| 385 | public void notify(HostEvent event) { |
| 386 | post(event); |
| 387 | } |
| 388 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 389 | |
| 390 | // listens for NetworkConfigEvents of type BasicHostConfig and removes |
| 391 | // links that the config does not allow |
| 392 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 393 | @Override |
| 394 | public void event(NetworkConfigEvent event) { |
| 395 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 396 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
| 397 | event.configClass().equals(BasicHostConfig.class)) { |
Thomas Vachuska | 138de8b | 2016-01-11 21:31:38 -0800 | [diff] [blame] | 398 | log.debug("Detected host network config event {}", event.type()); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 399 | kickOutBadHost(((HostId) event.subject())); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // checks if the specified host is allowed by the BasicHostConfig |
| 405 | // and if not, removes it |
| 406 | private void kickOutBadHost(HostId hostId) { |
| 407 | BasicHostConfig cfg = networkConfigService.getConfig(hostId, BasicHostConfig.class); |
| 408 | if (cfg != null && !cfg.isAllowed()) { |
| 409 | Host badHost = getHost(hostId); |
| 410 | if (badHost != null) { |
| 411 | removeHost(hostId); |
| 412 | } else { |
| 413 | log.info("Failed removal: Host {} does not exist", hostId); |
| 414 | } |
| 415 | } |
| 416 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 417 | } |
Deepa Vaddireddy | 50ad080 | 2016-08-08 19:27:36 +0530 | [diff] [blame] | 418 | |