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