blob: 11369514cdd9b5338e49e8867c3b1eb4b5b0ae2c [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;
Ray Milkeyd04e2272018-10-16 18:20:18 -070065import static org.onosproject.net.OsgiPropertyConstants.HM_ALLOW_DUPLICATE_IPS;
66import static org.onosproject.net.OsgiPropertyConstants.HM_ALLOW_DUPLICATE_IPS_DEFAULT;
67import static org.onosproject.net.OsgiPropertyConstants.HM_GREEDY_LEARNING_IPV6;
68import static org.onosproject.net.OsgiPropertyConstants.HM_GREEDY_LEARNING_IPV6_DEFAULT;
69import static org.onosproject.net.OsgiPropertyConstants.HM_MONITOR_HOSTS;
70import static org.onosproject.net.OsgiPropertyConstants.HM_MONITOR_HOSTS_DEFAULT;
71import static org.onosproject.net.OsgiPropertyConstants.HM_PROBE_RATE;
72import static org.onosproject.net.OsgiPropertyConstants.HM_PROBE_RATE_DEFAULT;
Changhoon Yoon541ef712015-05-23 17:18:34 +090073import static org.onosproject.security.AppGuard.checkPermission;
Simon Huntffbad3b2017-05-16 15:37:51 -070074import static org.onosproject.security.AppPermission.Type.HOST_EVENT;
75import static org.onosproject.security.AppPermission.Type.HOST_READ;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070076import static org.slf4j.LoggerFactory.getLogger;
Simon Huntff663742015-05-14 13:33:05 -070077
tomdb0d03f2014-08-27 16:34:15 -070078/**
79 * Provides basic implementation of the host SB & NB APIs.
80 */
Ray Milkeyd04e2272018-10-16 18:20:18 -070081@Component(
82 immediate = true,
83 service = {
84 HostService.class,
85 HostAdminService.class,
86 HostProviderRegistry.class
87 },
88 property = {
Ray Milkey2d7bca12018-10-17 14:51:52 -070089 HM_ALLOW_DUPLICATE_IPS + ":Boolean=" + HM_ALLOW_DUPLICATE_IPS_DEFAULT,
90 HM_MONITOR_HOSTS + ":Boolean=" + HM_MONITOR_HOSTS_DEFAULT,
91 HM_PROBE_RATE + ":Integer=" + HM_PROBE_RATE_DEFAULT,
92 HM_GREEDY_LEARNING_IPV6 + ":Boolean=" + HM_GREEDY_LEARNING_IPV6_DEFAULT
Ray Milkeyd04e2272018-10-16 18:20:18 -070093 }
94)
tom202175a2014-09-19 19:00:11 -070095public class HostManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070096 extends AbstractListenerProviderRegistry<HostEvent, HostListener, HostProvider, HostProviderService>
tom89b63c52014-09-16 09:19:51 -070097 implements HostService, HostAdminService, HostProviderRegistry {
tomdb0d03f2014-08-27 16:34:15 -070098
tom5f38b3a2014-08-27 23:50:54 -070099 private final Logger log = getLogger(getClass());
tomdb0d03f2014-08-27 16:34:15 -0700100
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700101 public static final String HOST_ID_NULL = "Host ID cannot be null";
tom5f38b3a2014-08-27 23:50:54 -0700102
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700103 private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener();
104
tomc78acee2014-09-24 15:16:55 -0700105 private HostStoreDelegate delegate = new InternalStoreDelegate();
106
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700107 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tom5bcc9462014-09-19 10:11:31 -0700108 protected HostStore store;
tom7869ad92014-09-09 14:32:08 -0700109
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700110 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hart70da5122014-10-01 16:37:42 -0700111 protected DeviceService deviceService;
112
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700113 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hart70da5122014-10-01 16:37:42 -0700114 protected PacketService packetService;
115
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700116 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700117 protected NetworkConfigService networkConfigService;
118
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700119 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hart4cb39882015-08-12 23:50:55 -0400120 protected InterfaceService interfaceService;
121
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700122 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hartfb32a6e2015-09-01 12:12:14 +0200123 protected EdgePortService edgePortService;
124
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700125 @Reference(cardinality = ReferenceCardinality.MANDATORY)
soumya3e6f05e2016-08-05 15:11:11 -0700126 protected ComponentConfigService cfgService;
127
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700128 /** Enable removal of duplicate ip address. */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700129 private boolean allowDuplicateIps = HM_ALLOW_DUPLICATE_IPS_DEFAULT;
sdn94b00152016-08-30 02:12:32 -0700130
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700131 /** Enable/Disable monitoring of hosts. */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700132 private boolean monitorHosts = HM_MONITOR_HOSTS_DEFAULT;
sdn94b00152016-08-30 02:12:32 -0700133
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700134 /** Set the probe Rate in milli seconds. */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700135 private long probeRate = HM_PROBE_RATE_DEFAULT;
sdn94b00152016-08-30 02:12:32 -0700136
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700137 /** Enable/Disable greedy learning of IPv6 link local address. */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700138 private boolean greedyLearningIpv6 = HM_GREEDY_LEARNING_IPV6_DEFAULT;
Pier Luigi9b1d6262017-02-02 22:31:34 -0800139
Jonathan Hart70da5122014-10-01 16:37:42 -0700140 private HostMonitor monitor;
tomdb0d03f2014-08-27 16:34:15 -0700141
soumya3e6f05e2016-08-05 15:11:11 -0700142
tomdb0d03f2014-08-27 16:34:15 -0700143 @Activate
soumya3e6f05e2016-08-05 15:11:11 -0700144 public void activate(ComponentContext context) {
tomc78acee2014-09-24 15:16:55 -0700145 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700146 eventDispatcher.addSink(HostEvent.class, listenerRegistry);
soumya3e6f05e2016-08-05 15:11:11 -0700147 cfgService.registerProperties(getClass());
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700148 networkConfigService.addListener(networkConfigListener);
Jonathan Hartfb32a6e2015-09-01 12:12:14 +0200149 monitor = new HostMonitor(packetService, this, interfaceService, edgePortService);
sdn94b00152016-08-30 02:12:32 -0700150 monitor.setProbeRate(probeRate);
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700151 monitor.start();
sdn94b00152016-08-30 02:12:32 -0700152 modified(context);
153 cfgService.registerProperties(getClass());
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700154 log.info("Started");
tomdb0d03f2014-08-27 16:34:15 -0700155 }
156
sdn94b00152016-08-30 02:12:32 -0700157 @Deactivate
158 public void deactivate() {
159 store.unsetDelegate(delegate);
160 eventDispatcher.removeSink(HostEvent.class);
161 networkConfigService.removeListener(networkConfigListener);
162 cfgService.unregisterProperties(getClass(), false);
163 monitor.shutdown();
164 log.info("Stopped");
165 }
166
soumya3e6f05e2016-08-05 15:11:11 -0700167 @Modified
Simon Huntffbad3b2017-05-16 15:37:51 -0700168 public void modified(ComponentContext context) {
sdn94b00152016-08-30 02:12:32 -0700169 boolean oldValue = monitorHosts;
170 readComponentConfiguration(context);
171 if (probeRate > 0) {
172 monitor.setProbeRate(probeRate);
173 } else {
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700174 log.warn("ProbeRate cannot be less than 0");
sdn94b00152016-08-30 02:12:32 -0700175 }
176
177 if (oldValue != monitorHosts) {
178 if (monitorHosts) {
179 startMonitoring();
180 } else {
181 stopMonitoring();
182 }
183 }
184 }
185
186 /**
187 * Extracts properties from the component configuration context.
188 *
189 * @param context the component context
190 */
191 private void readComponentConfiguration(ComponentContext context) {
soumya3e6f05e2016-08-05 15:11:11 -0700192 Dictionary<?, ?> properties = context.getProperties();
193 Boolean flag;
sdn94b00152016-08-30 02:12:32 -0700194
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700195 flag = Tools.isPropertyEnabled(properties, HM_MONITOR_HOSTS);
sdn94b00152016-08-30 02:12:32 -0700196 if (flag == null) {
197 log.info("monitorHosts is not enabled " +
198 "using current value of {}", monitorHosts);
199 } else {
200 monitorHosts = flag;
201 log.info("Configured. monitorHosts {}",
Simon Huntffbad3b2017-05-16 15:37:51 -0700202 monitorHosts ? "enabled" : "disabled");
sdn94b00152016-08-30 02:12:32 -0700203 }
204
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700205 Long longValue = Tools.getLongProperty(properties, HM_PROBE_RATE);
sdn94b00152016-08-30 02:12:32 -0700206 if (longValue == null || longValue == 0) {
207 log.info("probeRate is not set sing default value of {}", probeRate);
208 } else {
209 probeRate = longValue;
210 log.info("Configured. probeRate {}", probeRate);
211 }
212
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700213 flag = Tools.isPropertyEnabled(properties, HM_ALLOW_DUPLICATE_IPS);
soumya3e6f05e2016-08-05 15:11:11 -0700214 if (flag == null) {
215 log.info("Removal of duplicate ip address is not configured");
216 } else {
217 allowDuplicateIps = flag;
218 log.info("Removal of duplicate ip address is {}",
219 allowDuplicateIps ? "disabled" : "enabled");
220 }
sdn94b00152016-08-30 02:12:32 -0700221
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700222 flag = Tools.isPropertyEnabled(properties, HM_GREEDY_LEARNING_IPV6);
Pier Luigi9b1d6262017-02-02 22:31:34 -0800223 if (flag == null) {
224 log.info("greedy learning is not enabled " +
225 "using current value of {}", greedyLearningIpv6);
226 } else {
227 greedyLearningIpv6 = flag;
228 log.info("Configured. greedyLearningIpv6 {}",
229 greedyLearningIpv6 ? "enabled" : "disabled");
230 }
231
soumya3e6f05e2016-08-05 15:11:11 -0700232 }
233
sdn94b00152016-08-30 02:12:32 -0700234 /**
235 * Starts monitoring the hosts by IP Address.
sdn94b00152016-08-30 02:12:32 -0700236 */
237 private void startMonitoring() {
238 store.getHosts().forEach(host -> {
Simon Huntffbad3b2017-05-16 15:37:51 -0700239 host.ipAddresses().forEach(ip -> {
240 monitor.addMonitoringFor(ip);
sdn94b00152016-08-30 02:12:32 -0700241 });
242 });
243 }
244
245 /**
246 * Stops monitoring the hosts by IP Address.
sdn94b00152016-08-30 02:12:32 -0700247 */
248 private void stopMonitoring() {
249 store.getHosts().forEach(host -> {
Simon Huntffbad3b2017-05-16 15:37:51 -0700250 host.ipAddresses().forEach(ip -> {
251 monitor.stopMonitoring(ip);
sdn94b00152016-08-30 02:12:32 -0700252 });
253 });
tomdb0d03f2014-08-27 16:34:15 -0700254 }
255
256 @Override
tom5f38b3a2014-08-27 23:50:54 -0700257 protected HostProviderService createProviderService(HostProvider provider) {
Jonathan Hart70da5122014-10-01 16:37:42 -0700258 monitor.registerHostProvider(provider);
tom5f38b3a2014-08-27 23:50:54 -0700259 return new InternalHostProviderService(provider);
tomdb0d03f2014-08-27 16:34:15 -0700260 }
261
tom7869ad92014-09-09 14:32:08 -0700262 @Override
263 public int getHostCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900264 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700265 return store.getHostCount();
266 }
267
268 @Override
269 public Iterable<Host> getHosts() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900270 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700271 return store.getHosts();
272 }
273
274 @Override
275 public Host getHost(HostId hostId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900276 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700277 checkNotNull(hostId, HOST_ID_NULL);
278 return store.getHost(hostId);
279 }
280
281 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700282 public Set<Host> getHostsByVlan(VlanId vlanId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900283 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700284 return store.getHosts(vlanId);
285 }
286
287 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700288 public Set<Host> getHostsByMac(MacAddress mac) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900289 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700290 checkNotNull(mac, "MAC address cannot be null");
291 return store.getHosts(mac);
292 }
293
294 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700295 public Set<Host> getHostsByIp(IpAddress ip) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900296 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700297 checkNotNull(ip, "IP address cannot be null");
298 return store.getHosts(ip);
299 }
300
301 @Override
302 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900303 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700304 checkNotNull(connectPoint, "Connection point cannot be null");
305 return store.getConnectedHosts(connectPoint);
306 }
307
308 @Override
309 public Set<Host> getConnectedHosts(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900310 checkPermission(HOST_READ);
tom7869ad92014-09-09 14:32:08 -0700311 checkNotNull(deviceId, "Device ID cannot be null");
312 return store.getConnectedHosts(deviceId);
313 }
314
315 @Override
Jonathan Hartac60c082014-09-23 08:55:17 -0700316 public void startMonitoringIp(IpAddress ip) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900317 checkPermission(HOST_EVENT);
Jonathan Hart70da5122014-10-01 16:37:42 -0700318 monitor.addMonitoringFor(ip);
Jonathan Hartfca736c2014-09-19 17:26:59 -0700319 }
320
321 @Override
Jonathan Hartac60c082014-09-23 08:55:17 -0700322 public void stopMonitoringIp(IpAddress ip) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900323 checkPermission(HOST_EVENT);
Jonathan Hart70da5122014-10-01 16:37:42 -0700324 monitor.stopMonitoring(ip);
Jonathan Hartac60c082014-09-23 08:55:17 -0700325 }
326
327 @Override
328 public void requestMac(IpAddress ip) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700329 // FIXME!!!! Auto-generated method stub
tom7869ad92014-09-09 14:32:08 -0700330 }
331
tom89b63c52014-09-16 09:19:51 -0700332 @Override
333 public void removeHost(HostId hostId) {
334 checkNotNull(hostId, HOST_ID_NULL);
Charles Chan009c3082015-11-10 14:18:04 -0800335 store.removeHost(hostId);
tom89b63c52014-09-16 09:19:51 -0700336 }
337
tomdb0d03f2014-08-27 16:34:15 -0700338 // Personalized host provider service issued to the supplied provider.
tom7869ad92014-09-09 14:32:08 -0700339 private class InternalHostProviderService
340 extends AbstractProviderService<HostProvider>
tomdb0d03f2014-08-27 16:34:15 -0700341 implements HostProviderService {
tomcfde0622014-09-09 11:02:42 -0700342 InternalHostProviderService(HostProvider provider) {
tomdb0d03f2014-08-27 16:34:15 -0700343 super(provider);
344 }
345
346 @Override
Brian O'Connorf107bd72015-09-21 15:31:03 -0700347 public void hostDetected(HostId hostId, HostDescription hostDescription, boolean replaceIps) {
tom7869ad92014-09-09 14:32:08 -0700348 checkNotNull(hostId, HOST_ID_NULL);
349 checkValidity();
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700350 hostDescription = validateHost(hostDescription, hostId);
soumya3e6f05e2016-08-05 15:11:11 -0700351
352 if (!allowDuplicateIps) {
353 removeDuplicates(hostId, hostDescription);
354 }
Charles Chan009c3082015-11-10 14:18:04 -0800355 store.createOrUpdateHost(provider().id(), hostId,
soumya3e6f05e2016-08-05 15:11:11 -0700356 hostDescription, replaceIps);
sdn94b00152016-08-30 02:12:32 -0700357
358 if (monitorHosts) {
359 hostDescription.ipAddress().forEach(ip -> {
360 monitor.addMonitoringFor(ip);
361 });
362 }
Pier Luigi9b1d6262017-02-02 22:31:34 -0800363
364 // Greedy learning of IPv6 host. We have to disable the greedy
365 // learning of configured hosts. Validate hosts each time will
Charles Chan82fac582017-09-12 12:09:22 -0700366 // overwrite the learnt information with the configured information.
Pier Luigi9b1d6262017-02-02 22:31:34 -0800367 if (greedyLearningIpv6) {
368 // Auto-generation of the IPv6 link local address
369 // using the mac address
370 Ip6Address targetIp6Address = Ip6Address.valueOf(
371 getLinkLocalAddress(hostId.mac().toBytes())
372 );
373 // If we already know this guy we don't need to do other
374 if (!hostDescription.ipAddress().contains(targetIp6Address)) {
375 Host host = store.getHost(hostId);
376 // Configured host, skip it.
377 if (host != null && host.configured()) {
378 return;
379 }
380 // Host does not exist in the store or the target is not known
381 if ((host == null || !host.ipAddresses().contains(targetIp6Address))) {
Charles Chan82fac582017-09-12 12:09:22 -0700382 // Use DAD to probe if interface MAC is not specified
383 MacAddress probeMac = interfaceService.getInterfacesByPort(hostDescription.location())
384 .stream().map(Interface::mac).findFirst().orElse(MacAddress.ONOS);
385 Ip6Address probeIp = !probeMac.equals(MacAddress.ONOS) ?
386 Ip6Address.valueOf(getLinkLocalAddress(probeMac.toBytes())) :
387 Ip6Address.ZERO;
Pier Luigi9b1d6262017-02-02 22:31:34 -0800388 // We send a probe using the monitoring service
389 monitor.sendProbe(
390 hostDescription.location(),
391 targetIp6Address,
Charles Chan82fac582017-09-12 12:09:22 -0700392 probeIp,
393 probeMac,
Pier Luigi9b1d6262017-02-02 22:31:34 -0800394 hostId.vlanId()
395 );
396 }
397 }
398 }
tomdb0d03f2014-08-27 16:34:15 -0700399 }
400
soumya3e6f05e2016-08-05 15:11:11 -0700401 // When a new IP is detected, remove that IP on other hosts if it exists
402 public void removeDuplicates(HostId hostId, HostDescription desc) {
403 desc.ipAddress().forEach(ip -> {
404 Set<Host> allHosts = store.getHosts(ip);
405 allHosts.forEach(eachHost -> {
406 if (!(eachHost.id().equals(hostId))) {
407 log.info("Duplicate ip {} found on host {} and {}", ip,
408 hostId.toString(), eachHost.id().toString());
409 store.removeIp(eachHost.id(), ip);
410 }
411 });
412 });
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000413 }
414
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700415 // returns a HostDescription made from the union of the BasicHostConfig
416 // annotations if it exists
417 private HostDescription validateHost(HostDescription hostDescription, HostId hostId) {
418 BasicHostConfig cfg = networkConfigService.getConfig(hostId, BasicHostConfig.class);
419 checkState(cfg == null || cfg.isAllowed(), "Host {} is not allowed", hostId);
Ayaka Koshibe5373e762015-08-06 12:31:44 -0700420
421 return BasicHostOperator.combine(cfg, hostDescription);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700422 }
423
tomdb0d03f2014-08-27 16:34:15 -0700424 @Override
tom7869ad92014-09-09 14:32:08 -0700425 public void hostVanished(HostId hostId) {
426 checkNotNull(hostId, HOST_ID_NULL);
427 checkValidity();
sdn94b00152016-08-30 02:12:32 -0700428 Host host = store.getHost(hostId);
Charles Chan29ecdee2017-02-22 18:46:56 -0800429
Charles Chan888e20a2017-05-01 15:44:23 -0700430 if (!allowedToChange(hostId)) {
431 log.info("Request to remove {} is ignored due to provider mismatch", hostId);
Charles Chan29ecdee2017-02-22 18:46:56 -0800432 return;
433 }
434
sdn94b00152016-08-30 02:12:32 -0700435 if (monitorHosts) {
436 host.ipAddresses().forEach(ip -> {
437 monitor.stopMonitoring(ip);
438 });
439 }
Charles Chan009c3082015-11-10 14:18:04 -0800440 store.removeHost(hostId);
tomdb0d03f2014-08-27 16:34:15 -0700441 }
samanwita palc40e5ed2015-09-24 11:01:51 -0700442
443 @Override
444 public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
445 checkNotNull(hostId, HOST_ID_NULL);
446 checkValidity();
Charles Chan888e20a2017-05-01 15:44:23 -0700447
448 if (!allowedToChange(hostId)) {
449 log.info("Request to remove {} from {} is ignored due to provider mismatch",
450 ipAddress, hostId);
451 return;
452 }
453
Charles Chan009c3082015-11-10 14:18:04 -0800454 store.removeIp(hostId, ipAddress);
samanwita palc40e5ed2015-09-24 11:01:51 -0700455 }
Charles Chan888e20a2017-05-01 15:44:23 -0700456
457 @Override
Charles Chanff79dd92018-06-01 16:33:48 -0700458 public void addLocationToHost(HostId hostId, HostLocation location) {
459 checkNotNull(hostId, HOST_ID_NULL);
460 checkValidity();
461
462 if (!allowedToChange(hostId)) {
Charles Chanb928b3f2018-07-23 15:37:08 -0700463 log.info("Request to add {} to {} is ignored due to provider mismatch",
Charles Chanff79dd92018-06-01 16:33:48 -0700464 location, hostId);
465 return;
466 }
467
468 store.appendLocation(hostId, location);
469 }
470
471 @Override
Charles Chan888e20a2017-05-01 15:44:23 -0700472 public void removeLocationFromHost(HostId hostId, HostLocation location) {
473 checkNotNull(hostId, HOST_ID_NULL);
474 checkValidity();
475
476 if (!allowedToChange(hostId)) {
477 log.info("Request to remove {} from {} is ignored due to provider mismatch",
478 location, hostId);
479 return;
480 }
481
482 store.removeLocation(hostId, location);
483 }
484
Charles Chand0c147a2017-09-14 14:00:10 -0700485 /**
486 * Providers should only be able to remove a host that is provided by itself,
487 * or a host that is not configured.
488 */
Charles Chan888e20a2017-05-01 15:44:23 -0700489 private boolean allowedToChange(HostId hostId) {
Charles Chan888e20a2017-05-01 15:44:23 -0700490 Host host = store.getHost(hostId);
Charles Chand0c147a2017-09-14 14:00:10 -0700491 return host == null || !host.configured() || host.providerId().equals(provider().id());
Charles Chan888e20a2017-05-01 15:44:23 -0700492 }
tomdb0d03f2014-08-27 16:34:15 -0700493 }
tom7869ad92014-09-09 14:32:08 -0700494
tomc78acee2014-09-24 15:16:55 -0700495 // Store delegate to re-post events emitted from the store.
496 private class InternalStoreDelegate implements HostStoreDelegate {
497 @Override
498 public void notify(HostEvent event) {
499 post(event);
500 }
501 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700502
503 // listens for NetworkConfigEvents of type BasicHostConfig and removes
504 // links that the config does not allow
505 private class InternalNetworkConfigListener implements NetworkConfigListener {
506 @Override
Simon Huntffbad3b2017-05-16 15:37:51 -0700507 public boolean isRelevant(NetworkConfigEvent event) {
508 return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED
509 || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)
510 && (event.configClass().equals(BasicHostConfig.class));
511 }
512
513 @Override
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700514 public void event(NetworkConfigEvent event) {
Simon Huntffbad3b2017-05-16 15:37:51 -0700515 log.debug("Detected host network config event {}", event.type());
516 HostEvent he = null;
517
518 HostId hostId = (HostId) event.subject();
519 BasicHostConfig cfg =
520 networkConfigService.getConfig(hostId, BasicHostConfig.class);
521
522 if (!isAllowed(cfg)) {
523 kickOutBadHost(hostId);
524 } else {
525 Host host = getHost(hostId);
526 HostDescription desc =
527 (host == null) ? null : BasicHostOperator.descriptionOf(host);
528 desc = BasicHostOperator.combine(cfg, desc);
529 if (desc != null) {
530 he = store.createOrUpdateHost(host.providerId(), hostId, desc, false);
531 }
532 }
533
534 if (he != null) {
535 post(he);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700536 }
537 }
538 }
539
Simon Huntffbad3b2017-05-16 15:37:51 -0700540 // by default allowed, otherwise check flag
541 private boolean isAllowed(BasicHostConfig cfg) {
542 return (cfg == null || cfg.isAllowed());
543 }
544
545 // removes the specified host, if it exists
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700546 private void kickOutBadHost(HostId hostId) {
Simon Huntffbad3b2017-05-16 15:37:51 -0700547 Host badHost = getHost(hostId);
548 if (badHost != null) {
549 removeHost(hostId);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700550 }
551 }
tomdb0d03f2014-08-27 16:34:15 -0700552}