blob: b135e802a78c5cddcc658fac878e5402d05ee7fb [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.host.impl;
tomdb0d03f2014-08-27 16:34:15 -070017
Pier Luigi9b1d6262017-02-02 22:31:34 -080018import org.onlab.packet.Ip6Address;
Simon Huntff663742015-05-14 13:33:05 -070019import org.onlab.packet.IpAddress;
20import org.onlab.packet.MacAddress;
21import org.onlab.packet.VlanId;
soumya3e6f05e2016-08-05 15:11:11 -070022import org.onlab.util.Tools;
23import org.onosproject.cfg.ComponentConfigService;
Charles Chan888e20a2017-05-01 15:44:23 -070024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.Host;
27import org.onosproject.net.HostId;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070028import org.onosproject.net.HostLocation;
29import org.onosproject.net.config.NetworkConfigEvent;
30import org.onosproject.net.config.NetworkConfigListener;
31import org.onosproject.net.config.NetworkConfigService;
32import org.onosproject.net.config.basics.BasicHostConfig;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.device.DeviceService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070034import org.onosproject.net.edge.EdgePortService;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.host.HostAdminService;
36import org.onosproject.net.host.HostDescription;
37import org.onosproject.net.host.HostEvent;
38import org.onosproject.net.host.HostListener;
39import org.onosproject.net.host.HostProvider;
40import org.onosproject.net.host.HostProviderRegistry;
41import org.onosproject.net.host.HostProviderService;
42import org.onosproject.net.host.HostService;
43import org.onosproject.net.host.HostStore;
44import org.onosproject.net.host.HostStoreDelegate;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070045import org.onosproject.net.intf.Interface;
46import org.onosproject.net.intf.InterfaceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.packet.PacketService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.provider.AbstractProviderService;
soumya3e6f05e2016-08-05 15:11:11 -070050import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070051import org.osgi.service.component.annotations.Activate;
52import org.osgi.service.component.annotations.Component;
53import org.osgi.service.component.annotations.Deactivate;
54import org.osgi.service.component.annotations.Modified;
55import org.osgi.service.component.annotations.Reference;
56import org.osgi.service.component.annotations.ReferenceCardinality;
tomdb0d03f2014-08-27 16:34:15 -070057import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070058
soumya3e6f05e2016-08-05 15:11:11 -070059import java.util.Dictionary;
Simon Huntff663742015-05-14 13:33:05 -070060import java.util.Set;
61
62import static com.google.common.base.Preconditions.checkNotNull;
Sahil Lele3a0cdd52015-07-21 14:16:31 -070063import static com.google.common.base.Preconditions.checkState;
Pier Luigi9b1d6262017-02-02 22:31:34 -080064import static org.onlab.packet.IPv6.getLinkLocalAddress;
Changhoon Yoon541ef712015-05-23 17:18:34 +090065import static org.onosproject.security.AppGuard.checkPermission;
Simon Huntffbad3b2017-05-16 15:37:51 -070066import static org.onosproject.security.AppPermission.Type.HOST_EVENT;
67import static org.onosproject.security.AppPermission.Type.HOST_READ;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070068import static org.slf4j.LoggerFactory.getLogger;
Simon Huntff663742015-05-14 13:33:05 -070069
tomdb0d03f2014-08-27 16:34:15 -070070/**
71 * Provides basic implementation of the host SB & NB APIs.
72 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070073@Component(immediate = true, service = {HostService.class, HostAdminService.class, HostProviderRegistry.class })
tom202175a2014-09-19 19:00:11 -070074public class HostManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070075 extends AbstractListenerProviderRegistry<HostEvent, HostListener, HostProvider, HostProviderService>
tom89b63c52014-09-16 09:19:51 -070076 implements HostService, HostAdminService, HostProviderRegistry {
tomdb0d03f2014-08-27 16:34:15 -070077
tom5f38b3a2014-08-27 23:50:54 -070078 private final Logger log = getLogger(getClass());
tomdb0d03f2014-08-27 16:34:15 -070079
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070080 public static final String HOST_ID_NULL = "Host ID cannot be null";
tom5f38b3a2014-08-27 23:50:54 -070081
Sahil Lele3a0cdd52015-07-21 14:16:31 -070082 private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener();
83
tomc78acee2014-09-24 15:16:55 -070084 private HostStoreDelegate delegate = new InternalStoreDelegate();
85
Ray Milkeyd84f89b2018-08-17 14:54:17 -070086 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tom5bcc9462014-09-19 10:11:31 -070087 protected HostStore store;
tom7869ad92014-09-09 14:32:08 -070088
Ray Milkeyd84f89b2018-08-17 14:54:17 -070089 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hart70da5122014-10-01 16:37:42 -070090 protected DeviceService deviceService;
91
Ray Milkeyd84f89b2018-08-17 14:54:17 -070092 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hart70da5122014-10-01 16:37:42 -070093 protected PacketService packetService;
94
Ray Milkeyd84f89b2018-08-17 14:54:17 -070095 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sahil Lele3a0cdd52015-07-21 14:16:31 -070096 protected NetworkConfigService networkConfigService;
97
Ray Milkeyd84f89b2018-08-17 14:54:17 -070098 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hart4cb39882015-08-12 23:50:55 -040099 protected InterfaceService interfaceService;
100
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700101 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hartfb32a6e2015-09-01 12:12:14 +0200102 protected EdgePortService edgePortService;
103
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700104 @Reference(cardinality = ReferenceCardinality.MANDATORY)
soumya3e6f05e2016-08-05 15:11:11 -0700105 protected ComponentConfigService cfgService;
106
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700107 //@Property(name = "allowDuplicateIps", boolValue = true,
108 // label = "Enable removal of duplicate ip address")
soumya3e6f05e2016-08-05 15:11:11 -0700109 private boolean allowDuplicateIps = true;
sdn94b00152016-08-30 02:12:32 -0700110
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700111 //@Property(name = "monitorHosts", boolValue = false,
112 // label = "Enable/Disable monitoring of hosts")
sdn94b00152016-08-30 02:12:32 -0700113 private boolean monitorHosts = false;
114
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700115 //@Property(name = "probeRate", longValue = 30000,
116 // label = "Set the probe Rate in milli seconds")
sdn94b00152016-08-30 02:12:32 -0700117 private long probeRate = 30000;
118
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700119 //@Property(name = "greedyLearningIpv6", boolValue = false,
120 // label = "Enable/Disable greedy learning of IPv6 link local address")
Pier Luigi9b1d6262017-02-02 22:31:34 -0800121 private boolean greedyLearningIpv6 = false;
122
Jonathan Hart70da5122014-10-01 16:37:42 -0700123 private HostMonitor monitor;
tomdb0d03f2014-08-27 16:34:15 -0700124
soumya3e6f05e2016-08-05 15:11:11 -0700125
tomdb0d03f2014-08-27 16:34:15 -0700126 @Activate
soumya3e6f05e2016-08-05 15:11:11 -0700127 public void activate(ComponentContext context) {
tomc78acee2014-09-24 15:16:55 -0700128 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700129 eventDispatcher.addSink(HostEvent.class, listenerRegistry);
soumya3e6f05e2016-08-05 15:11:11 -0700130 cfgService.registerProperties(getClass());
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700131 networkConfigService.addListener(networkConfigListener);
Jonathan Hartfb32a6e2015-09-01 12:12:14 +0200132 monitor = new HostMonitor(packetService, this, interfaceService, edgePortService);
sdn94b00152016-08-30 02:12:32 -0700133 monitor.setProbeRate(probeRate);
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700134 monitor.start();
sdn94b00152016-08-30 02:12:32 -0700135 modified(context);
136 cfgService.registerProperties(getClass());
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700137 log.info("Started");
tomdb0d03f2014-08-27 16:34:15 -0700138 }
139
sdn94b00152016-08-30 02:12:32 -0700140 @Deactivate
141 public void deactivate() {
142 store.unsetDelegate(delegate);
143 eventDispatcher.removeSink(HostEvent.class);
144 networkConfigService.removeListener(networkConfigListener);
145 cfgService.unregisterProperties(getClass(), false);
146 monitor.shutdown();
147 log.info("Stopped");
148 }
149
soumya3e6f05e2016-08-05 15:11:11 -0700150 @Modified
Simon Huntffbad3b2017-05-16 15:37:51 -0700151 public void modified(ComponentContext context) {
sdn94b00152016-08-30 02:12:32 -0700152 boolean oldValue = monitorHosts;
153 readComponentConfiguration(context);
154 if (probeRate > 0) {
155 monitor.setProbeRate(probeRate);
156 } else {
157 log.warn("probeRate cannot be lessthan 0");
158 }
159
160 if (oldValue != monitorHosts) {
161 if (monitorHosts) {
162 startMonitoring();
163 } else {
164 stopMonitoring();
165 }
166 }
167 }
168
169 /**
170 * Extracts properties from the component configuration context.
171 *
172 * @param context the component context
173 */
174 private void readComponentConfiguration(ComponentContext context) {
soumya3e6f05e2016-08-05 15:11:11 -0700175 Dictionary<?, ?> properties = context.getProperties();
176 Boolean flag;
sdn94b00152016-08-30 02:12:32 -0700177
178 flag = Tools.isPropertyEnabled(properties, "monitorHosts");
179 if (flag == null) {
180 log.info("monitorHosts is not enabled " +
181 "using current value of {}", monitorHosts);
182 } else {
183 monitorHosts = flag;
184 log.info("Configured. monitorHosts {}",
Simon Huntffbad3b2017-05-16 15:37:51 -0700185 monitorHosts ? "enabled" : "disabled");
sdn94b00152016-08-30 02:12:32 -0700186 }
187
188 Long longValue = Tools.getLongProperty(properties, "probeRate");
189 if (longValue == null || longValue == 0) {
190 log.info("probeRate is not set sing default value of {}", probeRate);
191 } else {
192 probeRate = longValue;
193 log.info("Configured. probeRate {}", probeRate);
194 }
195
soumya3e6f05e2016-08-05 15:11:11 -0700196 flag = Tools.isPropertyEnabled(properties, "allowDuplicateIps");
197 if (flag == null) {
198 log.info("Removal of duplicate ip address is not configured");
199 } else {
200 allowDuplicateIps = flag;
201 log.info("Removal of duplicate ip address is {}",
202 allowDuplicateIps ? "disabled" : "enabled");
203 }
sdn94b00152016-08-30 02:12:32 -0700204
Pier Luigi9b1d6262017-02-02 22:31:34 -0800205 flag = Tools.isPropertyEnabled(properties, "greedyLearningIpv6");
206 if (flag == null) {
207 log.info("greedy learning is not enabled " +
208 "using current value of {}", greedyLearningIpv6);
209 } else {
210 greedyLearningIpv6 = flag;
211 log.info("Configured. greedyLearningIpv6 {}",
212 greedyLearningIpv6 ? "enabled" : "disabled");
213 }
214
soumya3e6f05e2016-08-05 15:11:11 -0700215 }
216
sdn94b00152016-08-30 02:12:32 -0700217 /**
218 * Starts monitoring the hosts by IP Address.
sdn94b00152016-08-30 02:12:32 -0700219 */
220 private void startMonitoring() {
221 store.getHosts().forEach(host -> {
Simon Huntffbad3b2017-05-16 15:37:51 -0700222 host.ipAddresses().forEach(ip -> {
223 monitor.addMonitoringFor(ip);
sdn94b00152016-08-30 02:12:32 -0700224 });
225 });
226 }
227
228 /**
229 * Stops monitoring the hosts by IP Address.
sdn94b00152016-08-30 02:12:32 -0700230 */
231 private void stopMonitoring() {
232 store.getHosts().forEach(host -> {
Simon Huntffbad3b2017-05-16 15:37:51 -0700233 host.ipAddresses().forEach(ip -> {
234 monitor.stopMonitoring(ip);
sdn94b00152016-08-30 02:12:32 -0700235 });
236 });
tomdb0d03f2014-08-27 16:34:15 -0700237 }
238
239 @Override
tom5f38b3a2014-08-27 23:50:54 -0700240 protected HostProviderService createProviderService(HostProvider provider) {
Jonathan Hart70da5122014-10-01 16:37:42 -0700241 monitor.registerHostProvider(provider);
tom5f38b3a2014-08-27 23:50:54 -0700242 return new InternalHostProviderService(provider);
tomdb0d03f2014-08-27 16:34:15 -0700243 }
244
tom7869ad92014-09-09 14:32:08 -0700245 @Override
246 public int getHostCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900247 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700248 return store.getHostCount();
249 }
250
251 @Override
252 public Iterable<Host> getHosts() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900253 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700254 return store.getHosts();
255 }
256
257 @Override
258 public Host getHost(HostId hostId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900259 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700260 checkNotNull(hostId, HOST_ID_NULL);
261 return store.getHost(hostId);
262 }
263
264 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700265 public Set<Host> getHostsByVlan(VlanId vlanId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900266 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700267 return store.getHosts(vlanId);
268 }
269
270 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700271 public Set<Host> getHostsByMac(MacAddress mac) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900272 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700273 checkNotNull(mac, "MAC address cannot be null");
274 return store.getHosts(mac);
275 }
276
277 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700278 public Set<Host> getHostsByIp(IpAddress ip) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900279 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700280 checkNotNull(ip, "IP address cannot be null");
281 return store.getHosts(ip);
282 }
283
284 @Override
285 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900286 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700287 checkNotNull(connectPoint, "Connection point cannot be null");
288 return store.getConnectedHosts(connectPoint);
289 }
290
291 @Override
292 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900293 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700294 checkNotNull(deviceId, "Device ID cannot be null");
295 return store.getConnectedHosts(deviceId);
296 }
297
298 @Override
Jonathan Hartac60c082014-09-23 08:55:17 -0700299 public void startMonitoringIp(IpAddress ip) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900300 checkPermission(HOST_EVENT);
Jonathan Hart70da5122014-10-01 16:37:42 -0700301 monitor.addMonitoringFor(ip);
Jonathan Hartfca736c2014-09-19 17:26:59 -0700302 }
303
304 @Override
Jonathan Hartac60c082014-09-23 08:55:17 -0700305 public void stopMonitoringIp(IpAddress ip) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900306 checkPermission(HOST_EVENT);
Jonathan Hart70da5122014-10-01 16:37:42 -0700307 monitor.stopMonitoring(ip);
Jonathan Hartac60c082014-09-23 08:55:17 -0700308 }
309
310 @Override
311 public void requestMac(IpAddress ip) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700312 // FIXME!!!! Auto-generated method stub
tom7869ad92014-09-09 14:32:08 -0700313 }
314
tom89b63c52014-09-16 09:19:51 -0700315 @Override
316 public void removeHost(HostId hostId) {
317 checkNotNull(hostId, HOST_ID_NULL);
Charles Chan009c3082015-11-10 14:18:04 -0800318 store.removeHost(hostId);
tom89b63c52014-09-16 09:19:51 -0700319 }
320
tomdb0d03f2014-08-27 16:34:15 -0700321 // Personalized host provider service issued to the supplied provider.
tom7869ad92014-09-09 14:32:08 -0700322 private class InternalHostProviderService
323 extends AbstractProviderService<HostProvider>
tomdb0d03f2014-08-27 16:34:15 -0700324 implements HostProviderService {
tomcfde0622014-09-09 11:02:42 -0700325 InternalHostProviderService(HostProvider provider) {
tomdb0d03f2014-08-27 16:34:15 -0700326 super(provider);
327 }
328
329 @Override
Brian O'Connorf107bd72015-09-21 15:31:03 -0700330 public void hostDetected(HostId hostId, HostDescription hostDescription, boolean replaceIps) {
tom7869ad92014-09-09 14:32:08 -0700331 checkNotNull(hostId, HOST_ID_NULL);
332 checkValidity();
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700333 hostDescription = validateHost(hostDescription, hostId);
soumya3e6f05e2016-08-05 15:11:11 -0700334
335 if (!allowDuplicateIps) {
336 removeDuplicates(hostId, hostDescription);
337 }
Charles Chan009c3082015-11-10 14:18:04 -0800338 store.createOrUpdateHost(provider().id(), hostId,
soumya3e6f05e2016-08-05 15:11:11 -0700339 hostDescription, replaceIps);
sdn94b00152016-08-30 02:12:32 -0700340
341 if (monitorHosts) {
342 hostDescription.ipAddress().forEach(ip -> {
343 monitor.addMonitoringFor(ip);
344 });
345 }
Pier Luigi9b1d6262017-02-02 22:31:34 -0800346
347 // Greedy learning of IPv6 host. We have to disable the greedy
348 // learning of configured hosts. Validate hosts each time will
Charles Chan82fac582017-09-12 12:09:22 -0700349 // overwrite the learnt information with the configured information.
Pier Luigi9b1d6262017-02-02 22:31:34 -0800350 if (greedyLearningIpv6) {
351 // Auto-generation of the IPv6 link local address
352 // using the mac address
353 Ip6Address targetIp6Address = Ip6Address.valueOf(
354 getLinkLocalAddress(hostId.mac().toBytes())
355 );
356 // If we already know this guy we don't need to do other
357 if (!hostDescription.ipAddress().contains(targetIp6Address)) {
358 Host host = store.getHost(hostId);
359 // Configured host, skip it.
360 if (host != null && host.configured()) {
361 return;
362 }
363 // Host does not exist in the store or the target is not known
364 if ((host == null || !host.ipAddresses().contains(targetIp6Address))) {
Charles Chan82fac582017-09-12 12:09:22 -0700365 // Use DAD to probe if interface MAC is not specified
366 MacAddress probeMac = interfaceService.getInterfacesByPort(hostDescription.location())
367 .stream().map(Interface::mac).findFirst().orElse(MacAddress.ONOS);
368 Ip6Address probeIp = !probeMac.equals(MacAddress.ONOS) ?
369 Ip6Address.valueOf(getLinkLocalAddress(probeMac.toBytes())) :
370 Ip6Address.ZERO;
Pier Luigi9b1d6262017-02-02 22:31:34 -0800371 // We send a probe using the monitoring service
372 monitor.sendProbe(
373 hostDescription.location(),
374 targetIp6Address,
Charles Chan82fac582017-09-12 12:09:22 -0700375 probeIp,
376 probeMac,
Pier Luigi9b1d6262017-02-02 22:31:34 -0800377 hostId.vlanId()
378 );
379 }
380 }
381 }
tomdb0d03f2014-08-27 16:34:15 -0700382 }
383
soumya3e6f05e2016-08-05 15:11:11 -0700384 // When a new IP is detected, remove that IP on other hosts if it exists
385 public void removeDuplicates(HostId hostId, HostDescription desc) {
386 desc.ipAddress().forEach(ip -> {
387 Set<Host> allHosts = store.getHosts(ip);
388 allHosts.forEach(eachHost -> {
389 if (!(eachHost.id().equals(hostId))) {
390 log.info("Duplicate ip {} found on host {} and {}", ip,
391 hostId.toString(), eachHost.id().toString());
392 store.removeIp(eachHost.id(), ip);
393 }
394 });
395 });
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000396 }
397
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700398 // returns a HostDescription made from the union of the BasicHostConfig
399 // annotations if it exists
400 private HostDescription validateHost(HostDescription hostDescription, HostId hostId) {
401 BasicHostConfig cfg = networkConfigService.getConfig(hostId, BasicHostConfig.class);
402 checkState(cfg == null || cfg.isAllowed(), "Host {} is not allowed", hostId);
Ayaka Koshibe5373e762015-08-06 12:31:44 -0700403
404 return BasicHostOperator.combine(cfg, hostDescription);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700405 }
406
tomdb0d03f2014-08-27 16:34:15 -0700407 @Override
tom7869ad92014-09-09 14:32:08 -0700408 public void hostVanished(HostId hostId) {
409 checkNotNull(hostId, HOST_ID_NULL);
410 checkValidity();
sdn94b00152016-08-30 02:12:32 -0700411 Host host = store.getHost(hostId);
Charles Chan29ecdee2017-02-22 18:46:56 -0800412
Charles Chan888e20a2017-05-01 15:44:23 -0700413 if (!allowedToChange(hostId)) {
414 log.info("Request to remove {} is ignored due to provider mismatch", hostId);
Charles Chan29ecdee2017-02-22 18:46:56 -0800415 return;
416 }
417
sdn94b00152016-08-30 02:12:32 -0700418 if (monitorHosts) {
419 host.ipAddresses().forEach(ip -> {
420 monitor.stopMonitoring(ip);
421 });
422 }
Charles Chan009c3082015-11-10 14:18:04 -0800423 store.removeHost(hostId);
tomdb0d03f2014-08-27 16:34:15 -0700424 }
samanwita palc40e5ed2015-09-24 11:01:51 -0700425
426 @Override
427 public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
428 checkNotNull(hostId, HOST_ID_NULL);
429 checkValidity();
Charles Chan888e20a2017-05-01 15:44:23 -0700430
431 if (!allowedToChange(hostId)) {
432 log.info("Request to remove {} from {} is ignored due to provider mismatch",
433 ipAddress, hostId);
434 return;
435 }
436
Charles Chan009c3082015-11-10 14:18:04 -0800437 store.removeIp(hostId, ipAddress);
samanwita palc40e5ed2015-09-24 11:01:51 -0700438 }
Charles Chan888e20a2017-05-01 15:44:23 -0700439
440 @Override
Charles Chanff79dd92018-06-01 16:33:48 -0700441 public void addLocationToHost(HostId hostId, HostLocation location) {
442 checkNotNull(hostId, HOST_ID_NULL);
443 checkValidity();
444
445 if (!allowedToChange(hostId)) {
Charles Chanb928b3f2018-07-23 15:37:08 -0700446 log.info("Request to add {} to {} is ignored due to provider mismatch",
Charles Chanff79dd92018-06-01 16:33:48 -0700447 location, hostId);
448 return;
449 }
450
451 store.appendLocation(hostId, location);
452 }
453
454 @Override
Charles Chan888e20a2017-05-01 15:44:23 -0700455 public void removeLocationFromHost(HostId hostId, HostLocation location) {
456 checkNotNull(hostId, HOST_ID_NULL);
457 checkValidity();
458
459 if (!allowedToChange(hostId)) {
460 log.info("Request to remove {} from {} is ignored due to provider mismatch",
461 location, hostId);
462 return;
463 }
464
465 store.removeLocation(hostId, location);
466 }
467
Charles Chand0c147a2017-09-14 14:00:10 -0700468 /**
469 * Providers should only be able to remove a host that is provided by itself,
470 * or a host that is not configured.
471 */
Charles Chan888e20a2017-05-01 15:44:23 -0700472 private boolean allowedToChange(HostId hostId) {
Charles Chan888e20a2017-05-01 15:44:23 -0700473 Host host = store.getHost(hostId);
Charles Chand0c147a2017-09-14 14:00:10 -0700474 return host == null || !host.configured() || host.providerId().equals(provider().id());
Charles Chan888e20a2017-05-01 15:44:23 -0700475 }
tomdb0d03f2014-08-27 16:34:15 -0700476 }
tom7869ad92014-09-09 14:32:08 -0700477
tomc78acee2014-09-24 15:16:55 -0700478 // Store delegate to re-post events emitted from the store.
479 private class InternalStoreDelegate implements HostStoreDelegate {
480 @Override
481 public void notify(HostEvent event) {
482 post(event);
483 }
484 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700485
486 // listens for NetworkConfigEvents of type BasicHostConfig and removes
487 // links that the config does not allow
488 private class InternalNetworkConfigListener implements NetworkConfigListener {
489 @Override
Simon Huntffbad3b2017-05-16 15:37:51 -0700490 public boolean isRelevant(NetworkConfigEvent event) {
491 return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED
492 || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)
493 && (event.configClass().equals(BasicHostConfig.class));
494 }
495
496 @Override
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700497 public void event(NetworkConfigEvent event) {
Simon Huntffbad3b2017-05-16 15:37:51 -0700498 log.debug("Detected host network config event {}", event.type());
499 HostEvent he = null;
500
501 HostId hostId = (HostId) event.subject();
502 BasicHostConfig cfg =
503 networkConfigService.getConfig(hostId, BasicHostConfig.class);
504
505 if (!isAllowed(cfg)) {
506 kickOutBadHost(hostId);
507 } else {
508 Host host = getHost(hostId);
509 HostDescription desc =
510 (host == null) ? null : BasicHostOperator.descriptionOf(host);
511 desc = BasicHostOperator.combine(cfg, desc);
512 if (desc != null) {
513 he = store.createOrUpdateHost(host.providerId(), hostId, desc, false);
514 }
515 }
516
517 if (he != null) {
518 post(he);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700519 }
520 }
521 }
522
Simon Huntffbad3b2017-05-16 15:37:51 -0700523 // by default allowed, otherwise check flag
524 private boolean isAllowed(BasicHostConfig cfg) {
525 return (cfg == null || cfg.isAllowed());
526 }
527
528 // removes the specified host, if it exists
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700529 private void kickOutBadHost(HostId hostId) {
Simon Huntffbad3b2017-05-16 15:37:51 -0700530 Host badHost = getHost(hostId);
531 if (badHost != null) {
532 removeHost(hostId);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700533 }
534 }
tomdb0d03f2014-08-27 16:34:15 -0700535}