Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 | |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 19 | import org.onlab.packet.Ip6Address; |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.VlanId; |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 23 | import org.onlab.util.Tools; |
| 24 | import org.onosproject.cfg.ComponentConfigService; |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.Host; |
| 28 | import org.onosproject.net.HostId; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 29 | import org.onosproject.net.HostLocation; |
Rafał Szalecki | de5cf84 | 2018-11-17 13:30:01 +0100 | [diff] [blame] | 30 | import org.onosproject.net.config.Config; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | import org.onosproject.net.config.NetworkConfigEvent; |
| 32 | import org.onosproject.net.config.NetworkConfigListener; |
| 33 | import org.onosproject.net.config.NetworkConfigService; |
| 34 | import org.onosproject.net.config.basics.BasicHostConfig; |
Rafał Szalecki | de5cf84 | 2018-11-17 13:30:01 +0100 | [diff] [blame] | 35 | import org.onosproject.net.config.basics.HostAnnotationConfig; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.device.DeviceService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | import org.onosproject.net.edge.EdgePortService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.host.HostAdminService; |
| 39 | import org.onosproject.net.host.HostDescription; |
| 40 | import org.onosproject.net.host.HostEvent; |
| 41 | import org.onosproject.net.host.HostListener; |
| 42 | import org.onosproject.net.host.HostProvider; |
| 43 | import org.onosproject.net.host.HostProviderRegistry; |
| 44 | import org.onosproject.net.host.HostProviderService; |
| 45 | import org.onosproject.net.host.HostService; |
| 46 | import org.onosproject.net.host.HostStore; |
| 47 | import org.onosproject.net.host.HostStoreDelegate; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 48 | import org.onosproject.net.intf.Interface; |
| 49 | import org.onosproject.net.intf.InterfaceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.packet.PacketService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 51 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.net.provider.AbstractProviderService; |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 53 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 54 | import org.osgi.service.component.annotations.Activate; |
| 55 | import org.osgi.service.component.annotations.Component; |
| 56 | import org.osgi.service.component.annotations.Deactivate; |
| 57 | import org.osgi.service.component.annotations.Modified; |
| 58 | import org.osgi.service.component.annotations.Reference; |
| 59 | import org.osgi.service.component.annotations.ReferenceCardinality; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 60 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 61 | |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 62 | import java.util.Dictionary; |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 63 | import java.util.Map; |
Rafał Szalecki | de5cf84 | 2018-11-17 13:30:01 +0100 | [diff] [blame] | 64 | import java.util.Optional; |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 65 | import java.util.Set; |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 66 | import java.util.concurrent.ConcurrentHashMap; |
| 67 | import java.util.concurrent.Executors; |
| 68 | import java.util.concurrent.ScheduledExecutorService; |
| 69 | import java.util.concurrent.TimeUnit; |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 70 | |
| 71 | import static com.google.common.base.Preconditions.checkNotNull; |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 72 | import static com.google.common.base.Strings.isNullOrEmpty; |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 73 | import static org.onlab.packet.IPv6.getLinkLocalAddress; |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 74 | import static org.onlab.util.Tools.get; |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 75 | import static org.onosproject.net.OsgiPropertyConstants.HM_ALLOW_DUPLICATE_IPS; |
| 76 | import static org.onosproject.net.OsgiPropertyConstants.HM_ALLOW_DUPLICATE_IPS_DEFAULT; |
| 77 | import static org.onosproject.net.OsgiPropertyConstants.HM_GREEDY_LEARNING_IPV6; |
| 78 | import static org.onosproject.net.OsgiPropertyConstants.HM_GREEDY_LEARNING_IPV6_DEFAULT; |
| 79 | import static org.onosproject.net.OsgiPropertyConstants.HM_MONITOR_HOSTS; |
| 80 | import static org.onosproject.net.OsgiPropertyConstants.HM_MONITOR_HOSTS_DEFAULT; |
| 81 | import static org.onosproject.net.OsgiPropertyConstants.HM_PROBE_RATE; |
| 82 | import static org.onosproject.net.OsgiPropertyConstants.HM_PROBE_RATE_DEFAULT; |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 83 | import static org.onosproject.net.OsgiPropertyConstants.HM_HOST_MOVED_THRESHOLD_IN_MILLIS; |
| 84 | import static org.onosproject.net.OsgiPropertyConstants.HM_HOST_MOVED_THRESHOLD_IN_MILLIS_DEFAULT; |
| 85 | import static org.onosproject.net.OsgiPropertyConstants.HM_HOST_MOVE_COUNTER; |
| 86 | import static org.onosproject.net.OsgiPropertyConstants.HM_HOST_MOVE_COUNTER_DEFAULT; |
| 87 | import static org.onosproject.net.OsgiPropertyConstants.HM_HOST_MOVE_TRACKER_ENABLE; |
| 88 | import static org.onosproject.net.OsgiPropertyConstants.HM_HOST_MOVE_TRACKER_ENABLE_DEFAULT; |
| 89 | import static org.onosproject.net.OsgiPropertyConstants.HM_OFFENDING_HOST_EXPIRY_IN_MINS; |
| 90 | import static org.onosproject.net.OsgiPropertyConstants.HM_OFFENDING_HOST_EXPIRY_IN_MINS_DEFAULT; |
| 91 | import static org.onosproject.net.OsgiPropertyConstants.HM_OFFENDING_HOST_THREADS_POOL_SIZE; |
| 92 | import static org.onosproject.net.OsgiPropertyConstants.HM_OFFENDING_HOST_THREADS_POOL_SIZE_DEFAULT; |
| 93 | |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 94 | import static org.onosproject.security.AppGuard.checkPermission; |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 95 | import static org.onosproject.security.AppPermission.Type.HOST_EVENT; |
| 96 | import static org.onosproject.security.AppPermission.Type.HOST_READ; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 97 | import static org.slf4j.LoggerFactory.getLogger; |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 98 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Provides basic implementation of the host SB & NB APIs. |
| 101 | */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 102 | @Component( |
| 103 | immediate = true, |
| 104 | service = { |
| 105 | HostService.class, |
| 106 | HostAdminService.class, |
| 107 | HostProviderRegistry.class |
| 108 | }, |
| 109 | property = { |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 110 | HM_ALLOW_DUPLICATE_IPS + ":Boolean=" + HM_ALLOW_DUPLICATE_IPS_DEFAULT, |
| 111 | HM_MONITOR_HOSTS + ":Boolean=" + HM_MONITOR_HOSTS_DEFAULT, |
| 112 | HM_PROBE_RATE + ":Integer=" + HM_PROBE_RATE_DEFAULT, |
| 113 | HM_GREEDY_LEARNING_IPV6 + ":Boolean=" + HM_GREEDY_LEARNING_IPV6_DEFAULT, |
| 114 | HM_HOST_MOVE_TRACKER_ENABLE + ":Boolean=" + HM_HOST_MOVE_TRACKER_ENABLE_DEFAULT, |
| 115 | HM_HOST_MOVED_THRESHOLD_IN_MILLIS + ":Integer=" + HM_HOST_MOVED_THRESHOLD_IN_MILLIS_DEFAULT, |
| 116 | HM_HOST_MOVE_COUNTER + ":Integer=" + HM_HOST_MOVE_COUNTER_DEFAULT, |
| 117 | HM_OFFENDING_HOST_EXPIRY_IN_MINS + ":Long=" + HM_OFFENDING_HOST_EXPIRY_IN_MINS_DEFAULT, |
| 118 | HM_OFFENDING_HOST_THREADS_POOL_SIZE + ":Integer=" + HM_OFFENDING_HOST_THREADS_POOL_SIZE_DEFAULT |
| 119 | |
| 120 | |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 121 | } |
| 122 | ) |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 123 | public class HostManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 124 | extends AbstractListenerProviderRegistry<HostEvent, HostListener, HostProvider, HostProviderService> |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 125 | implements HostService, HostAdminService, HostProviderRegistry { |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 126 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 127 | private final Logger log = getLogger(getClass()); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 128 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 129 | public static final String HOST_ID_NULL = "Host ID cannot be null"; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 130 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 131 | private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener(); |
| 132 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 133 | private HostStoreDelegate delegate = new InternalStoreDelegate(); |
| 134 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 136 | protected HostStore store; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 137 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 138 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 139 | protected DeviceService deviceService; |
| 140 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 141 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 142 | protected PacketService packetService; |
| 143 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 144 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 145 | protected NetworkConfigService networkConfigService; |
| 146 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 147 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 148 | protected InterfaceService interfaceService; |
| 149 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 150 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | fb32a6e | 2015-09-01 12:12:14 +0200 | [diff] [blame] | 151 | protected EdgePortService edgePortService; |
| 152 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 153 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 154 | protected ComponentConfigService cfgService; |
| 155 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 156 | /** Enable removal of duplicate ip address. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 157 | private boolean allowDuplicateIps = HM_ALLOW_DUPLICATE_IPS_DEFAULT; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 158 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 159 | /** Enable/Disable monitoring of hosts. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 160 | private boolean monitorHosts = HM_MONITOR_HOSTS_DEFAULT; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 161 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 162 | /** Set the probe Rate in milli seconds. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 163 | private long probeRate = HM_PROBE_RATE_DEFAULT; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 164 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 165 | /** Enable/Disable greedy learning of IPv6 link local address. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 166 | private boolean greedyLearningIpv6 = HM_GREEDY_LEARNING_IPV6_DEFAULT; |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 167 | |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 168 | /** Enable/Disable tracking of rogue host moves. */ |
| 169 | private boolean hostMoveTrackerEnabled = HM_HOST_MOVE_TRACKER_ENABLE_DEFAULT; |
| 170 | |
| 171 | /** Host move threshold in milli seconds. */ |
| 172 | private int hostMoveThresholdInMillis = HM_HOST_MOVED_THRESHOLD_IN_MILLIS_DEFAULT; |
| 173 | |
| 174 | /** If the host move happening within given threshold then increment the host move counter. */ |
| 175 | private int hostMoveCounter = HM_HOST_MOVE_COUNTER_DEFAULT; |
| 176 | |
| 177 | /** Max value of the counter after which the host will not be considered as offending host. */ |
| 178 | private long offendingHostExpiryInMins = HM_OFFENDING_HOST_EXPIRY_IN_MINS_DEFAULT; |
| 179 | |
| 180 | /** Default pool size of offending host clear executor thread. */ |
| 181 | private int offendingHostClearThreadPool = HM_OFFENDING_HOST_THREADS_POOL_SIZE_DEFAULT; |
| 182 | |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 183 | private HostMonitor monitor; |
Rafał Szalecki | de5cf84 | 2018-11-17 13:30:01 +0100 | [diff] [blame] | 184 | private HostAnnotationOperator hostAnnotationOperator; |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 185 | private ScheduledExecutorService offendingHostUnblockExecutor = null; |
| 186 | private Map<HostId, HostMoveTracker> hostMoveTracker = new ConcurrentHashMap<>(); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 187 | |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 188 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 189 | @Activate |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 190 | public void activate(ComponentContext context) { |
Rafał Szalecki | de5cf84 | 2018-11-17 13:30:01 +0100 | [diff] [blame] | 191 | hostAnnotationOperator = new HostAnnotationOperator(networkConfigService); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 192 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 193 | eventDispatcher.addSink(HostEvent.class, listenerRegistry); |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 194 | cfgService.registerProperties(getClass()); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 195 | networkConfigService.addListener(networkConfigListener); |
Charles Chan | f5c6f28 | 2022-01-04 16:39:44 -0800 | [diff] [blame^] | 196 | monitor = new HostMonitor(packetService, this, interfaceService, edgePortService, deviceService); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 197 | monitor.setProbeRate(probeRate); |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 198 | monitor.start(); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 199 | cfgService.registerProperties(getClass()); |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 200 | modified(context); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 201 | log.info("Started"); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 202 | } |
| 203 | |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 204 | @Deactivate |
| 205 | public void deactivate() { |
| 206 | store.unsetDelegate(delegate); |
| 207 | eventDispatcher.removeSink(HostEvent.class); |
| 208 | networkConfigService.removeListener(networkConfigListener); |
| 209 | cfgService.unregisterProperties(getClass(), false); |
| 210 | monitor.shutdown(); |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 211 | if (offendingHostUnblockExecutor != null) { |
| 212 | offendingHostUnblockExecutor.shutdown(); |
| 213 | } |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 214 | log.info("Stopped"); |
| 215 | } |
| 216 | |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 217 | @Modified |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 218 | public void modified(ComponentContext context) { |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 219 | boolean oldValue = monitorHosts; |
| 220 | readComponentConfiguration(context); |
| 221 | if (probeRate > 0) { |
| 222 | monitor.setProbeRate(probeRate); |
| 223 | } else { |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 224 | log.warn("ProbeRate cannot be less than 0"); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | if (oldValue != monitorHosts) { |
| 228 | if (monitorHosts) { |
| 229 | startMonitoring(); |
| 230 | } else { |
| 231 | stopMonitoring(); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Extracts properties from the component configuration context. |
| 238 | * |
| 239 | * @param context the component context |
| 240 | */ |
| 241 | private void readComponentConfiguration(ComponentContext context) { |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 242 | Dictionary<?, ?> properties = context.getProperties(); |
| 243 | Boolean flag; |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 244 | int newHostMoveThresholdInMillis; |
| 245 | int newHostMoveCounter; |
| 246 | int newOffendinghostPoolSize; |
| 247 | long newOffendingHostExpiryInMins; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 248 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 249 | flag = Tools.isPropertyEnabled(properties, HM_MONITOR_HOSTS); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 250 | if (flag == null) { |
| 251 | log.info("monitorHosts is not enabled " + |
| 252 | "using current value of {}", monitorHosts); |
| 253 | } else { |
| 254 | monitorHosts = flag; |
| 255 | log.info("Configured. monitorHosts {}", |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 256 | monitorHosts ? "enabled" : "disabled"); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 259 | Long longValue = Tools.getLongProperty(properties, HM_PROBE_RATE); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 260 | if (longValue == null || longValue == 0) { |
| 261 | log.info("probeRate is not set sing default value of {}", probeRate); |
| 262 | } else { |
| 263 | probeRate = longValue; |
| 264 | log.info("Configured. probeRate {}", probeRate); |
| 265 | } |
| 266 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 267 | flag = Tools.isPropertyEnabled(properties, HM_ALLOW_DUPLICATE_IPS); |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 268 | if (flag == null) { |
| 269 | log.info("Removal of duplicate ip address is not configured"); |
| 270 | } else { |
| 271 | allowDuplicateIps = flag; |
| 272 | log.info("Removal of duplicate ip address is {}", |
| 273 | allowDuplicateIps ? "disabled" : "enabled"); |
| 274 | } |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 275 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 276 | flag = Tools.isPropertyEnabled(properties, HM_GREEDY_LEARNING_IPV6); |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 277 | if (flag == null) { |
| 278 | log.info("greedy learning is not enabled " + |
| 279 | "using current value of {}", greedyLearningIpv6); |
| 280 | } else { |
| 281 | greedyLearningIpv6 = flag; |
| 282 | log.info("Configured. greedyLearningIpv6 {}", |
| 283 | greedyLearningIpv6 ? "enabled" : "disabled"); |
| 284 | } |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 285 | flag = Tools.isPropertyEnabled(properties, HM_HOST_MOVE_TRACKER_ENABLE); |
| 286 | if (flag == null) { |
| 287 | log.info("Host move tracker is not configured " + |
| 288 | "using current value of {}", hostMoveTrackerEnabled); |
| 289 | } else { |
| 290 | hostMoveTrackerEnabled = flag; |
| 291 | log.info("Configured. hostMoveTrackerEnabled {}", |
| 292 | hostMoveTrackerEnabled ? "enabled" : "disabled"); |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 293 | |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 294 | //On enable cfg ,sets default configuration vales added , else use the default values |
| 295 | properties = context.getProperties(); |
| 296 | try { |
| 297 | String s = get(properties, HM_HOST_MOVED_THRESHOLD_IN_MILLIS); |
| 298 | newHostMoveThresholdInMillis = isNullOrEmpty(s) ? |
| 299 | hostMoveThresholdInMillis : Integer.parseInt(s.trim()); |
| 300 | |
| 301 | s = get(properties, HM_HOST_MOVE_COUNTER); |
| 302 | newHostMoveCounter = isNullOrEmpty(s) ? hostMoveCounter : Integer.parseInt(s.trim()); |
| 303 | |
| 304 | s = get(properties, HM_OFFENDING_HOST_EXPIRY_IN_MINS); |
| 305 | newOffendingHostExpiryInMins = isNullOrEmpty(s) ? |
| 306 | offendingHostExpiryInMins : Integer.parseInt(s.trim()); |
| 307 | |
| 308 | s = get(properties, HM_OFFENDING_HOST_THREADS_POOL_SIZE); |
| 309 | newOffendinghostPoolSize = isNullOrEmpty(s) ? |
| 310 | offendingHostClearThreadPool : Integer.parseInt(s.trim()); |
| 311 | } catch (NumberFormatException | ClassCastException e) { |
| 312 | newHostMoveThresholdInMillis = HM_HOST_MOVED_THRESHOLD_IN_MILLIS_DEFAULT; |
| 313 | newHostMoveCounter = HM_HOST_MOVE_COUNTER_DEFAULT; |
| 314 | newOffendingHostExpiryInMins = HM_OFFENDING_HOST_EXPIRY_IN_MINS_DEFAULT; |
| 315 | newOffendinghostPoolSize = HM_OFFENDING_HOST_THREADS_POOL_SIZE_DEFAULT; |
| 316 | } |
| 317 | if (newHostMoveThresholdInMillis != hostMoveThresholdInMillis) { |
| 318 | hostMoveThresholdInMillis = newHostMoveThresholdInMillis; |
| 319 | } |
| 320 | if (newHostMoveCounter != hostMoveCounter) { |
| 321 | hostMoveCounter = newHostMoveCounter; |
| 322 | } |
| 323 | if (newOffendingHostExpiryInMins != offendingHostExpiryInMins) { |
| 324 | offendingHostExpiryInMins = newOffendingHostExpiryInMins; |
| 325 | } |
| 326 | if (hostMoveTrackerEnabled && offendingHostUnblockExecutor == null) { |
| 327 | setupThreadPool(); |
| 328 | } else if (newOffendinghostPoolSize != offendingHostClearThreadPool |
| 329 | && offendingHostUnblockExecutor != null) { |
| 330 | offendingHostClearThreadPool = newOffendinghostPoolSize; |
| 331 | offendingHostUnblockExecutor.shutdown(); |
| 332 | offendingHostUnblockExecutor = null; |
| 333 | setupThreadPool(); |
| 334 | } else if (!hostMoveTrackerEnabled && offendingHostUnblockExecutor != null) { |
| 335 | offendingHostUnblockExecutor.shutdown(); |
| 336 | offendingHostUnblockExecutor = null; |
| 337 | } |
| 338 | if (newOffendinghostPoolSize != offendingHostClearThreadPool) { |
| 339 | offendingHostClearThreadPool = newOffendinghostPoolSize; |
| 340 | } |
| 341 | |
| 342 | log.debug("modified hostMoveThresholdInMillis: {}, hostMoveCounter: {}, " + |
| 343 | "offendingHostExpiryInMins: {} ", hostMoveThresholdInMillis, |
| 344 | hostMoveCounter, offendingHostExpiryInMins); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | private synchronized void setupThreadPool() { |
| 349 | offendingHostUnblockExecutor = Executors.newScheduledThreadPool(offendingHostClearThreadPool); |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 350 | } |
| 351 | |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 352 | /** |
| 353 | * Starts monitoring the hosts by IP Address. |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 354 | */ |
| 355 | private void startMonitoring() { |
| 356 | store.getHosts().forEach(host -> { |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 357 | host.ipAddresses().forEach(ip -> { |
| 358 | monitor.addMonitoringFor(ip); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 359 | }); |
| 360 | }); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Stops monitoring the hosts by IP Address. |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 365 | */ |
| 366 | private void stopMonitoring() { |
| 367 | store.getHosts().forEach(host -> { |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 368 | host.ipAddresses().forEach(ip -> { |
| 369 | monitor.stopMonitoring(ip); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 370 | }); |
| 371 | }); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | @Override |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 375 | protected HostProviderService createProviderService(HostProvider provider) { |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 376 | monitor.registerHostProvider(provider); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 377 | return new InternalHostProviderService(provider); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 378 | } |
| 379 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 380 | @Override |
| 381 | public int getHostCount() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 382 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 383 | return store.getHostCount(); |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public Iterable<Host> getHosts() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 388 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 389 | return store.getHosts(); |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public Host getHost(HostId hostId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 394 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 395 | checkNotNull(hostId, HOST_ID_NULL); |
| 396 | return store.getHost(hostId); |
| 397 | } |
| 398 | |
| 399 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 400 | public Set<Host> getHostsByVlan(VlanId vlanId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 401 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 402 | return store.getHosts(vlanId); |
| 403 | } |
| 404 | |
| 405 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 406 | public Set<Host> getHostsByMac(MacAddress mac) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 407 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 408 | checkNotNull(mac, "MAC address cannot be null"); |
| 409 | return store.getHosts(mac); |
| 410 | } |
| 411 | |
| 412 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 413 | public Set<Host> getHostsByIp(IpAddress ip) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 414 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 415 | checkNotNull(ip, "IP address cannot be null"); |
| 416 | return store.getHosts(ip); |
| 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Charles Chan | b51e844 | 2020-03-08 17:41:11 -0700 | [diff] [blame] | 421 | return getConnectedHosts(connectPoint, false); |
| 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint, boolean matchAuxLocations) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 426 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 427 | checkNotNull(connectPoint, "Connection point cannot be null"); |
Charles Chan | b51e844 | 2020-03-08 17:41:11 -0700 | [diff] [blame] | 428 | return store.getConnectedHosts(connectPoint, matchAuxLocations); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 433 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 434 | checkNotNull(deviceId, "Device ID cannot be null"); |
| 435 | return store.getConnectedHosts(deviceId); |
| 436 | } |
| 437 | |
| 438 | @Override |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 439 | public void startMonitoringIp(IpAddress ip) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 440 | checkPermission(HOST_EVENT); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 441 | monitor.addMonitoringFor(ip); |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | @Override |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 445 | public void stopMonitoringIp(IpAddress ip) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 446 | checkPermission(HOST_EVENT); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 447 | monitor.stopMonitoring(ip); |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public void requestMac(IpAddress ip) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 452 | // FIXME!!!! Auto-generated method stub |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 453 | } |
| 454 | |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 455 | @Override |
| 456 | public void removeHost(HostId hostId) { |
| 457 | checkNotNull(hostId, HOST_ID_NULL); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 458 | store.removeHost(hostId); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 459 | } |
| 460 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 461 | // Personalized host provider service issued to the supplied provider. |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 462 | private class InternalHostProviderService |
| 463 | extends AbstractProviderService<HostProvider> |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 464 | implements HostProviderService { |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 465 | InternalHostProviderService(HostProvider provider) { |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 466 | super(provider); |
| 467 | } |
| 468 | |
| 469 | @Override |
Ray Milkey | 718e438 | 2018-12-05 09:08:01 -0800 | [diff] [blame] | 470 | public void hostDetected(HostId hostId, HostDescription initialHostDescription, boolean replaceIps) { |
Andrea Campanella | 852a736 | 2020-09-23 11:42:21 +0200 | [diff] [blame] | 471 | log.debug("Host Detected {}, {}", hostId, initialHostDescription); |
Ray Milkey | 718e438 | 2018-12-05 09:08:01 -0800 | [diff] [blame] | 472 | HostDescription hostDescription = initialHostDescription; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 473 | checkNotNull(hostId, HOST_ID_NULL); |
| 474 | checkValidity(); |
Rafał Szalecki | de5cf84 | 2018-11-17 13:30:01 +0100 | [diff] [blame] | 475 | |
| 476 | BasicHostConfig cfg = networkConfigService.getConfig(hostId, BasicHostConfig.class); |
| 477 | if (!isAllowed(cfg)) { |
| 478 | log.warn("Host {} is not allowed to be added into the contol domain", hostId); |
| 479 | return; |
| 480 | } |
| 481 | |
Ray Milkey | 718e438 | 2018-12-05 09:08:01 -0800 | [diff] [blame] | 482 | hostDescription = BasicHostOperator.combine(cfg, initialHostDescription); |
Andrea Campanella | 852a736 | 2020-09-23 11:42:21 +0200 | [diff] [blame] | 483 | |
| 484 | if (!allowDuplicateIps) { |
| 485 | removeDuplicates(hostId, hostDescription); |
| 486 | } |
| 487 | |
Rafał Szalecki | de5cf84 | 2018-11-17 13:30:01 +0100 | [diff] [blame] | 488 | HostAnnotationConfig annoConfig = networkConfigService.getConfig(hostId, HostAnnotationConfig.class); |
| 489 | if (annoConfig != null) { |
| 490 | hostDescription = hostAnnotationOperator.combine(hostId, hostDescription, Optional.of(annoConfig)); |
| 491 | } |
| 492 | |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 493 | if (!hostMoveTrackerEnabled) { |
| 494 | store.createOrUpdateHost(provider().id(), hostId, |
| 495 | hostDescription, replaceIps); |
| 496 | } else if (!shouldBlock(hostId, hostDescription.locations())) { |
| 497 | log.debug("Host move is allowed for host with Id: {} ", hostId); |
| 498 | store.createOrUpdateHost(provider().id(), hostId, |
| 499 | hostDescription, replaceIps); |
| 500 | } else { |
| 501 | log.info("Host move is NOT allowed for host with Id: {} , removing from host store ", hostId); |
| 502 | } |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 503 | |
| 504 | if (monitorHosts) { |
| 505 | hostDescription.ipAddress().forEach(ip -> { |
| 506 | monitor.addMonitoringFor(ip); |
| 507 | }); |
| 508 | } |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 509 | |
| 510 | // Greedy learning of IPv6 host. We have to disable the greedy |
| 511 | // learning of configured hosts. Validate hosts each time will |
Charles Chan | 82fac58 | 2017-09-12 12:09:22 -0700 | [diff] [blame] | 512 | // overwrite the learnt information with the configured information. |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 513 | if (greedyLearningIpv6) { |
| 514 | // Auto-generation of the IPv6 link local address |
| 515 | // using the mac address |
| 516 | Ip6Address targetIp6Address = Ip6Address.valueOf( |
| 517 | getLinkLocalAddress(hostId.mac().toBytes()) |
| 518 | ); |
| 519 | // If we already know this guy we don't need to do other |
| 520 | if (!hostDescription.ipAddress().contains(targetIp6Address)) { |
| 521 | Host host = store.getHost(hostId); |
| 522 | // Configured host, skip it. |
| 523 | if (host != null && host.configured()) { |
| 524 | return; |
| 525 | } |
| 526 | // Host does not exist in the store or the target is not known |
| 527 | if ((host == null || !host.ipAddresses().contains(targetIp6Address))) { |
Charles Chan | 82fac58 | 2017-09-12 12:09:22 -0700 | [diff] [blame] | 528 | // Use DAD to probe if interface MAC is not specified |
| 529 | MacAddress probeMac = interfaceService.getInterfacesByPort(hostDescription.location()) |
| 530 | .stream().map(Interface::mac).findFirst().orElse(MacAddress.ONOS); |
| 531 | Ip6Address probeIp = !probeMac.equals(MacAddress.ONOS) ? |
| 532 | Ip6Address.valueOf(getLinkLocalAddress(probeMac.toBytes())) : |
| 533 | Ip6Address.ZERO; |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 534 | // We send a probe using the monitoring service |
| 535 | monitor.sendProbe( |
| 536 | hostDescription.location(), |
| 537 | targetIp6Address, |
Charles Chan | 82fac58 | 2017-09-12 12:09:22 -0700 | [diff] [blame] | 538 | probeIp, |
| 539 | probeMac, |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 540 | hostId.vlanId() |
| 541 | ); |
| 542 | } |
| 543 | } |
| 544 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 545 | } |
| 546 | |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 547 | // When a new IP is detected, remove that IP on other hosts if it exists |
| 548 | public void removeDuplicates(HostId hostId, HostDescription desc) { |
| 549 | desc.ipAddress().forEach(ip -> { |
| 550 | Set<Host> allHosts = store.getHosts(ip); |
| 551 | allHosts.forEach(eachHost -> { |
| 552 | if (!(eachHost.id().equals(hostId))) { |
| 553 | log.info("Duplicate ip {} found on host {} and {}", ip, |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 554 | hostId.toString(), eachHost.id().toString()); |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 555 | store.removeIp(eachHost.id(), ip); |
| 556 | } |
| 557 | }); |
| 558 | }); |
Jonathan Hart | 38feb6e | 2016-08-29 22:54:16 +0000 | [diff] [blame] | 559 | } |
| 560 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 561 | @Override |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 562 | public void hostVanished(HostId hostId) { |
| 563 | checkNotNull(hostId, HOST_ID_NULL); |
| 564 | checkValidity(); |
pierventre | f9a00a5 | 2021-10-25 18:49:09 +0200 | [diff] [blame] | 565 | // TODO SDFAB-718 rethink HostStore APIs to allow atomic operations |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 566 | Host host = store.getHost(hostId); |
Charles Chan | 29ecdee | 2017-02-22 18:46:56 -0800 | [diff] [blame] | 567 | |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 568 | if (!allowedToChange(hostId)) { |
| 569 | log.info("Request to remove {} is ignored due to provider mismatch", hostId); |
Charles Chan | 29ecdee | 2017-02-22 18:46:56 -0800 | [diff] [blame] | 570 | return; |
| 571 | } |
| 572 | |
pierventre | f9a00a5 | 2021-10-25 18:49:09 +0200 | [diff] [blame] | 573 | if (host == null) { |
| 574 | log.info("Request to remove {} is ignored due to host not present in the store", hostId); |
| 575 | return; |
| 576 | } |
| 577 | |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 578 | if (monitorHosts) { |
| 579 | host.ipAddresses().forEach(ip -> { |
| 580 | monitor.stopMonitoring(ip); |
| 581 | }); |
| 582 | } |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 583 | store.removeHost(hostId); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 584 | } |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 585 | |
| 586 | @Override |
| 587 | public void removeIpFromHost(HostId hostId, IpAddress ipAddress) { |
| 588 | checkNotNull(hostId, HOST_ID_NULL); |
| 589 | checkValidity(); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 590 | |
| 591 | if (!allowedToChange(hostId)) { |
| 592 | log.info("Request to remove {} from {} is ignored due to provider mismatch", |
| 593 | ipAddress, hostId); |
| 594 | return; |
| 595 | } |
| 596 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 597 | store.removeIp(hostId, ipAddress); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 598 | } |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 599 | |
| 600 | @Override |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 601 | public void addLocationToHost(HostId hostId, HostLocation location) { |
| 602 | checkNotNull(hostId, HOST_ID_NULL); |
| 603 | checkValidity(); |
| 604 | |
| 605 | if (!allowedToChange(hostId)) { |
Charles Chan | b928b3f | 2018-07-23 15:37:08 -0700 | [diff] [blame] | 606 | log.info("Request to add {} to {} is ignored due to provider mismatch", |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 607 | location, hostId); |
| 608 | return; |
| 609 | } |
| 610 | |
| 611 | store.appendLocation(hostId, location); |
| 612 | } |
| 613 | |
| 614 | @Override |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 615 | public void removeLocationFromHost(HostId hostId, HostLocation location) { |
| 616 | checkNotNull(hostId, HOST_ID_NULL); |
| 617 | checkValidity(); |
| 618 | |
| 619 | if (!allowedToChange(hostId)) { |
| 620 | log.info("Request to remove {} from {} is ignored due to provider mismatch", |
| 621 | location, hostId); |
| 622 | return; |
| 623 | } |
| 624 | |
| 625 | store.removeLocation(hostId, location); |
| 626 | } |
| 627 | |
Charles Chan | d0c147a | 2017-09-14 14:00:10 -0700 | [diff] [blame] | 628 | /** |
| 629 | * Providers should only be able to remove a host that is provided by itself, |
| 630 | * or a host that is not configured. |
| 631 | */ |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 632 | private boolean allowedToChange(HostId hostId) { |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 633 | Host host = store.getHost(hostId); |
Charles Chan | d0c147a | 2017-09-14 14:00:10 -0700 | [diff] [blame] | 634 | return host == null || !host.configured() || host.providerId().equals(provider().id()); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 635 | } |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 636 | |
| 637 | |
| 638 | /** |
| 639 | * Deny host move if happening within the threshold time, |
| 640 | * track moved host to identify offending hosts. |
| 641 | * |
| 642 | * @param hostId host identifier |
| 643 | * @param locations host locations |
| 644 | */ |
| 645 | private boolean shouldBlock(HostId hostId, Set<HostLocation> locations) { |
| 646 | Host host = store.getHost(hostId); |
| 647 | // If host is not present in host store means host added for hte first time. |
| 648 | if (host != null) { |
| 649 | if (host.suspended()) { |
| 650 | // Checks host is marked as offending in other onos cluster instance/local instance |
| 651 | log.debug("Host id {} is moving frequently hence host moving " + |
| 652 | "processing is ignored", hostId); |
| 653 | return true; |
| 654 | } |
| 655 | } else { |
| 656 | //host added for the first time. |
| 657 | return false; |
| 658 | } |
| 659 | HostMoveTracker hostMove = hostMoveTracker.computeIfAbsent(hostId, id -> new HostMoveTracker(locations)); |
| 660 | if (Sets.difference(hostMove.getLocations(), locations).isEmpty() && |
| 661 | Sets.difference(locations, hostMove.getLocations()).isEmpty()) { |
| 662 | log.debug("Not hostmove scenario: Host id: {}, Old Host Location: {}, New host Location: {}", |
| 663 | hostId, hostMove.getLocations(), locations); |
| 664 | return false; // It is not a host move scenario |
| 665 | } else if (hostMove.getCounter() >= hostMoveCounter && System.currentTimeMillis() - hostMove.getTimeStamp() |
| 666 | < hostMoveThresholdInMillis) { |
| 667 | //Check host move is crossed the threshold, then to mark as offending Host |
| 668 | log.debug("Host id {} is identified as offending host and entry is added in cache", hostId); |
| 669 | hostMove.resetHostMoveTracker(locations); |
| 670 | store.suspend(hostId); |
| 671 | //Set host suspended flag to false after given offendingHostExpiryInMins |
| 672 | offendingHostUnblockExecutor.schedule(new UnblockOffendingHost(hostId), |
| 673 | offendingHostExpiryInMins, |
| 674 | TimeUnit.MINUTES); |
| 675 | return true; |
| 676 | } else if (System.currentTimeMillis() - hostMove.getTimeStamp() |
| 677 | < hostMoveThresholdInMillis) { |
| 678 | //Increment the host move count as hostmove occured within the hostMoveThresholdInMillis time |
| 679 | hostMove.updateHostMoveTracker(locations); |
| 680 | log.debug("Updated the tracker with the host move registered for host: {}", hostId); |
| 681 | } else if (System.currentTimeMillis() - hostMove.getTimeStamp() |
| 682 | > hostMoveThresholdInMillis) { |
| 683 | //Hostmove is happened after hostMoveThresholdInMillis time so remove from host tracker. |
| 684 | hostMove.resetHostMoveTracker(locations); |
| 685 | store.unsuspend(hostId); |
| 686 | log.debug("Reset the tracker with the host move registered for host: {}", hostId); |
| 687 | } |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | // Set host suspended flag to false after given offendingHostExpiryInMins. |
| 692 | private final class UnblockOffendingHost implements Runnable { |
| 693 | private HostId hostId; |
| 694 | |
| 695 | UnblockOffendingHost(HostId hostId) { |
| 696 | this.hostId = hostId; |
| 697 | } |
| 698 | |
| 699 | @Override |
| 700 | public void run() { |
| 701 | // Set the host suspended flag to false |
| 702 | try { |
| 703 | store.unsuspend(hostId); |
| 704 | log.debug("Host {}: Marked host as unsuspended", hostId); |
| 705 | } catch (Exception ex) { |
| 706 | log.debug("Host {}: not present in host list", hostId); |
| 707 | } |
| 708 | } |
| 709 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 710 | } |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 711 | |
jobin | d19b714 | 2019-05-17 09:46:52 -0400 | [diff] [blame] | 712 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 713 | // Store delegate to re-post events emitted from the store. |
| 714 | private class InternalStoreDelegate implements HostStoreDelegate { |
| 715 | @Override |
| 716 | public void notify(HostEvent event) { |
| 717 | post(event); |
| 718 | } |
| 719 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 720 | |
| 721 | // listens for NetworkConfigEvents of type BasicHostConfig and removes |
| 722 | // links that the config does not allow |
| 723 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 724 | @Override |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 725 | public boolean isRelevant(NetworkConfigEvent event) { |
| 726 | return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED |
| 727 | || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) |
Rafał Szalecki | de5cf84 | 2018-11-17 13:30:01 +0100 | [diff] [blame] | 728 | && (event.configClass().equals(BasicHostConfig.class) |
| 729 | || event.configClass().equals(HostAnnotationConfig.class)); |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | @Override |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 733 | public void event(NetworkConfigEvent event) { |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 734 | log.debug("Detected host network config event {}", event.type()); |
| 735 | HostEvent he = null; |
| 736 | |
| 737 | HostId hostId = (HostId) event.subject(); |
| 738 | BasicHostConfig cfg = |
| 739 | networkConfigService.getConfig(hostId, BasicHostConfig.class); |
| 740 | |
| 741 | if (!isAllowed(cfg)) { |
| 742 | kickOutBadHost(hostId); |
Rafał Szalecki | de5cf84 | 2018-11-17 13:30:01 +0100 | [diff] [blame] | 743 | } else if (event.configClass().equals(BasicHostConfig.class)) { |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 744 | Host host = getHost(hostId); |
| 745 | HostDescription desc = |
| 746 | (host == null) ? null : BasicHostOperator.descriptionOf(host); |
| 747 | desc = BasicHostOperator.combine(cfg, desc); |
| 748 | if (desc != null) { |
| 749 | he = store.createOrUpdateHost(host.providerId(), hostId, desc, false); |
| 750 | } |
Rafał Szalecki | de5cf84 | 2018-11-17 13:30:01 +0100 | [diff] [blame] | 751 | } else if (event.configClass().equals(HostAnnotationConfig.class)) { |
| 752 | Host host = getHost(hostId); |
Ray Milkey | 718e438 | 2018-12-05 09:08:01 -0800 | [diff] [blame] | 753 | HostProvider hp = (host == null) ? null : getProvider(host.providerId()); |
Rafał Szalecki | de5cf84 | 2018-11-17 13:30:01 +0100 | [diff] [blame] | 754 | HostDescription desc = (host == null) ? null : BasicHostOperator.descriptionOf(host); |
| 755 | Optional<Config> prevConfig = event.prevConfig(); |
| 756 | log.debug("Host annotations: {} prevconfig {} desc {}", hostId, prevConfig, desc); |
| 757 | desc = hostAnnotationOperator.combine(hostId, desc, prevConfig); |
| 758 | if (desc != null && hp != null) { |
| 759 | log.debug("Host annotations update - updated host description :{}", desc.toString()); |
| 760 | he = store.createOrUpdateHost(hp.id(), hostId, desc, false); |
| 761 | if (he != null && he.subject() != null) { |
| 762 | log.debug("Host annotations update - Host Event : {}", he.subject().annotations()); |
| 763 | } |
| 764 | } |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | if (he != null) { |
| 768 | post(he); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 773 | // by default allowed, otherwise check flag |
| 774 | private boolean isAllowed(BasicHostConfig cfg) { |
| 775 | return (cfg == null || cfg.isAllowed()); |
| 776 | } |
| 777 | |
| 778 | // removes the specified host, if it exists |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 779 | private void kickOutBadHost(HostId hostId) { |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 780 | Host badHost = getHost(hostId); |
| 781 | if (badHost != null) { |
| 782 | removeHost(hostId); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 783 | } |
| 784 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 785 | } |