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 | |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 18 | import org.onlab.packet.Ip6Address; |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.packet.VlanId; |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 22 | import org.onlab.util.Tools; |
| 23 | import org.onosproject.cfg.ComponentConfigService; |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.Host; |
| 27 | import org.onosproject.net.HostId; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 28 | import org.onosproject.net.HostLocation; |
| 29 | import org.onosproject.net.config.NetworkConfigEvent; |
| 30 | import org.onosproject.net.config.NetworkConfigListener; |
| 31 | import org.onosproject.net.config.NetworkConfigService; |
| 32 | import org.onosproject.net.config.basics.BasicHostConfig; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.device.DeviceService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 34 | import org.onosproject.net.edge.EdgePortService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.host.HostAdminService; |
| 36 | import org.onosproject.net.host.HostDescription; |
| 37 | import org.onosproject.net.host.HostEvent; |
| 38 | import org.onosproject.net.host.HostListener; |
| 39 | import org.onosproject.net.host.HostProvider; |
| 40 | import org.onosproject.net.host.HostProviderRegistry; |
| 41 | import org.onosproject.net.host.HostProviderService; |
| 42 | import org.onosproject.net.host.HostService; |
| 43 | import org.onosproject.net.host.HostStore; |
| 44 | import org.onosproject.net.host.HostStoreDelegate; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 45 | import org.onosproject.net.intf.Interface; |
| 46 | import org.onosproject.net.intf.InterfaceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 47 | import org.onosproject.net.packet.PacketService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 48 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.net.provider.AbstractProviderService; |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 50 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 51 | import org.osgi.service.component.annotations.Activate; |
| 52 | import org.osgi.service.component.annotations.Component; |
| 53 | import org.osgi.service.component.annotations.Deactivate; |
| 54 | import org.osgi.service.component.annotations.Modified; |
| 55 | import org.osgi.service.component.annotations.Reference; |
| 56 | import org.osgi.service.component.annotations.ReferenceCardinality; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 57 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 58 | |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 59 | import java.util.Dictionary; |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 60 | import java.util.Set; |
| 61 | |
| 62 | import static com.google.common.base.Preconditions.checkNotNull; |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 63 | import static com.google.common.base.Preconditions.checkState; |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 64 | import static org.onlab.packet.IPv6.getLinkLocalAddress; |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 65 | import static org.onosproject.net.OsgiPropertyConstants.HM_ALLOW_DUPLICATE_IPS; |
| 66 | import static org.onosproject.net.OsgiPropertyConstants.HM_ALLOW_DUPLICATE_IPS_DEFAULT; |
| 67 | import static org.onosproject.net.OsgiPropertyConstants.HM_GREEDY_LEARNING_IPV6; |
| 68 | import static org.onosproject.net.OsgiPropertyConstants.HM_GREEDY_LEARNING_IPV6_DEFAULT; |
| 69 | import static org.onosproject.net.OsgiPropertyConstants.HM_MONITOR_HOSTS; |
| 70 | import static org.onosproject.net.OsgiPropertyConstants.HM_MONITOR_HOSTS_DEFAULT; |
| 71 | import static org.onosproject.net.OsgiPropertyConstants.HM_PROBE_RATE; |
| 72 | import static org.onosproject.net.OsgiPropertyConstants.HM_PROBE_RATE_DEFAULT; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 73 | import static org.onosproject.security.AppGuard.checkPermission; |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 74 | import static org.onosproject.security.AppPermission.Type.HOST_EVENT; |
| 75 | import static org.onosproject.security.AppPermission.Type.HOST_READ; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 76 | import static org.slf4j.LoggerFactory.getLogger; |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 77 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Provides basic implementation of the host SB & NB APIs. |
| 80 | */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 81 | @Component( |
| 82 | immediate = true, |
| 83 | service = { |
| 84 | HostService.class, |
| 85 | HostAdminService.class, |
| 86 | HostProviderRegistry.class |
| 87 | }, |
| 88 | property = { |
Ray Milkey | 2d7bca1 | 2018-10-17 14:51:52 -0700 | [diff] [blame] | 89 | HM_ALLOW_DUPLICATE_IPS + ":Boolean=" + HM_ALLOW_DUPLICATE_IPS_DEFAULT, |
| 90 | HM_MONITOR_HOSTS + ":Boolean=" + HM_MONITOR_HOSTS_DEFAULT, |
| 91 | HM_PROBE_RATE + ":Integer=" + HM_PROBE_RATE_DEFAULT, |
| 92 | HM_GREEDY_LEARNING_IPV6 + ":Boolean=" + HM_GREEDY_LEARNING_IPV6_DEFAULT |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 93 | } |
| 94 | ) |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 95 | public class HostManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 96 | extends AbstractListenerProviderRegistry<HostEvent, HostListener, HostProvider, HostProviderService> |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 97 | implements HostService, HostAdminService, HostProviderRegistry { |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 98 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 99 | private final Logger log = getLogger(getClass()); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 100 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 101 | public static final String HOST_ID_NULL = "Host ID cannot be null"; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 102 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 103 | private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener(); |
| 104 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 105 | private HostStoreDelegate delegate = new InternalStoreDelegate(); |
| 106 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 108 | protected HostStore store; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 109 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 111 | protected DeviceService deviceService; |
| 112 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 114 | protected PacketService packetService; |
| 115 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 117 | protected NetworkConfigService networkConfigService; |
| 118 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 120 | protected InterfaceService interfaceService; |
| 121 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | fb32a6e | 2015-09-01 12:12:14 +0200 | [diff] [blame] | 123 | protected EdgePortService edgePortService; |
| 124 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 126 | protected ComponentConfigService cfgService; |
| 127 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame^] | 128 | /** Enable removal of duplicate ip address. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 129 | private boolean allowDuplicateIps = HM_ALLOW_DUPLICATE_IPS_DEFAULT; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 130 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame^] | 131 | /** Enable/Disable monitoring of hosts. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 132 | private boolean monitorHosts = HM_MONITOR_HOSTS_DEFAULT; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 133 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame^] | 134 | /** Set the probe Rate in milli seconds. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 135 | private long probeRate = HM_PROBE_RATE_DEFAULT; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 136 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame^] | 137 | /** Enable/Disable greedy learning of IPv6 link local address. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 138 | private boolean greedyLearningIpv6 = HM_GREEDY_LEARNING_IPV6_DEFAULT; |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 139 | |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 140 | private HostMonitor monitor; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 141 | |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 142 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 143 | @Activate |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 144 | public void activate(ComponentContext context) { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 145 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 146 | eventDispatcher.addSink(HostEvent.class, listenerRegistry); |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 147 | cfgService.registerProperties(getClass()); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 148 | networkConfigService.addListener(networkConfigListener); |
Jonathan Hart | fb32a6e | 2015-09-01 12:12:14 +0200 | [diff] [blame] | 149 | monitor = new HostMonitor(packetService, this, interfaceService, edgePortService); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 150 | monitor.setProbeRate(probeRate); |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 151 | monitor.start(); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 152 | modified(context); |
| 153 | cfgService.registerProperties(getClass()); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 154 | log.info("Started"); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 155 | } |
| 156 | |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 157 | @Deactivate |
| 158 | public void deactivate() { |
| 159 | store.unsetDelegate(delegate); |
| 160 | eventDispatcher.removeSink(HostEvent.class); |
| 161 | networkConfigService.removeListener(networkConfigListener); |
| 162 | cfgService.unregisterProperties(getClass(), false); |
| 163 | monitor.shutdown(); |
| 164 | log.info("Stopped"); |
| 165 | } |
| 166 | |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 167 | @Modified |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 168 | public void modified(ComponentContext context) { |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 169 | boolean oldValue = monitorHosts; |
| 170 | readComponentConfiguration(context); |
| 171 | if (probeRate > 0) { |
| 172 | monitor.setProbeRate(probeRate); |
| 173 | } else { |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame^] | 174 | log.warn("ProbeRate cannot be less than 0"); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | if (oldValue != monitorHosts) { |
| 178 | if (monitorHosts) { |
| 179 | startMonitoring(); |
| 180 | } else { |
| 181 | stopMonitoring(); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Extracts properties from the component configuration context. |
| 188 | * |
| 189 | * @param context the component context |
| 190 | */ |
| 191 | private void readComponentConfiguration(ComponentContext context) { |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 192 | Dictionary<?, ?> properties = context.getProperties(); |
| 193 | Boolean flag; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 194 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame^] | 195 | flag = Tools.isPropertyEnabled(properties, HM_MONITOR_HOSTS); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 196 | if (flag == null) { |
| 197 | log.info("monitorHosts is not enabled " + |
| 198 | "using current value of {}", monitorHosts); |
| 199 | } else { |
| 200 | monitorHosts = flag; |
| 201 | log.info("Configured. monitorHosts {}", |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 202 | monitorHosts ? "enabled" : "disabled"); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame^] | 205 | Long longValue = Tools.getLongProperty(properties, HM_PROBE_RATE); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 206 | if (longValue == null || longValue == 0) { |
| 207 | log.info("probeRate is not set sing default value of {}", probeRate); |
| 208 | } else { |
| 209 | probeRate = longValue; |
| 210 | log.info("Configured. probeRate {}", probeRate); |
| 211 | } |
| 212 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame^] | 213 | flag = Tools.isPropertyEnabled(properties, HM_ALLOW_DUPLICATE_IPS); |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 214 | if (flag == null) { |
| 215 | log.info("Removal of duplicate ip address is not configured"); |
| 216 | } else { |
| 217 | allowDuplicateIps = flag; |
| 218 | log.info("Removal of duplicate ip address is {}", |
| 219 | allowDuplicateIps ? "disabled" : "enabled"); |
| 220 | } |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 221 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame^] | 222 | flag = Tools.isPropertyEnabled(properties, HM_GREEDY_LEARNING_IPV6); |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 223 | if (flag == null) { |
| 224 | log.info("greedy learning is not enabled " + |
| 225 | "using current value of {}", greedyLearningIpv6); |
| 226 | } else { |
| 227 | greedyLearningIpv6 = flag; |
| 228 | log.info("Configured. greedyLearningIpv6 {}", |
| 229 | greedyLearningIpv6 ? "enabled" : "disabled"); |
| 230 | } |
| 231 | |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 232 | } |
| 233 | |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 234 | /** |
| 235 | * Starts monitoring the hosts by IP Address. |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 236 | */ |
| 237 | private void startMonitoring() { |
| 238 | store.getHosts().forEach(host -> { |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 239 | host.ipAddresses().forEach(ip -> { |
| 240 | monitor.addMonitoringFor(ip); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 241 | }); |
| 242 | }); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Stops monitoring the hosts by IP Address. |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 247 | */ |
| 248 | private void stopMonitoring() { |
| 249 | store.getHosts().forEach(host -> { |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 250 | host.ipAddresses().forEach(ip -> { |
| 251 | monitor.stopMonitoring(ip); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 252 | }); |
| 253 | }); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | @Override |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 257 | protected HostProviderService createProviderService(HostProvider provider) { |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 258 | monitor.registerHostProvider(provider); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 259 | return new InternalHostProviderService(provider); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 260 | } |
| 261 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 262 | @Override |
| 263 | public int getHostCount() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 264 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 265 | return store.getHostCount(); |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public Iterable<Host> getHosts() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 270 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 271 | return store.getHosts(); |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public Host getHost(HostId hostId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 276 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 277 | checkNotNull(hostId, HOST_ID_NULL); |
| 278 | return store.getHost(hostId); |
| 279 | } |
| 280 | |
| 281 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 282 | public Set<Host> getHostsByVlan(VlanId vlanId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 283 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 284 | return store.getHosts(vlanId); |
| 285 | } |
| 286 | |
| 287 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 288 | public Set<Host> getHostsByMac(MacAddress mac) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 289 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 290 | checkNotNull(mac, "MAC address cannot be null"); |
| 291 | return store.getHosts(mac); |
| 292 | } |
| 293 | |
| 294 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 295 | public Set<Host> getHostsByIp(IpAddress ip) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 296 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 297 | checkNotNull(ip, "IP address cannot be null"); |
| 298 | return store.getHosts(ip); |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 303 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 304 | checkNotNull(connectPoint, "Connection point cannot be null"); |
| 305 | return store.getConnectedHosts(connectPoint); |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 310 | checkPermission(HOST_READ); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 311 | checkNotNull(deviceId, "Device ID cannot be null"); |
| 312 | return store.getConnectedHosts(deviceId); |
| 313 | } |
| 314 | |
| 315 | @Override |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 316 | public void startMonitoringIp(IpAddress ip) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 317 | checkPermission(HOST_EVENT); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 318 | monitor.addMonitoringFor(ip); |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 322 | public void stopMonitoringIp(IpAddress ip) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 323 | checkPermission(HOST_EVENT); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 324 | monitor.stopMonitoring(ip); |
Jonathan Hart | ac60c08 | 2014-09-23 08:55:17 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public void requestMac(IpAddress ip) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 329 | // FIXME!!!! Auto-generated method stub |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 330 | } |
| 331 | |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 332 | @Override |
| 333 | public void removeHost(HostId hostId) { |
| 334 | checkNotNull(hostId, HOST_ID_NULL); |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 335 | store.removeHost(hostId); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 336 | } |
| 337 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 338 | // Personalized host provider service issued to the supplied provider. |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 339 | private class InternalHostProviderService |
| 340 | extends AbstractProviderService<HostProvider> |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 341 | implements HostProviderService { |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 342 | InternalHostProviderService(HostProvider provider) { |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 343 | super(provider); |
| 344 | } |
| 345 | |
| 346 | @Override |
Brian O'Connor | f107bd7 | 2015-09-21 15:31:03 -0700 | [diff] [blame] | 347 | public void hostDetected(HostId hostId, HostDescription hostDescription, boolean replaceIps) { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 348 | checkNotNull(hostId, HOST_ID_NULL); |
| 349 | checkValidity(); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 350 | hostDescription = validateHost(hostDescription, hostId); |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 351 | |
| 352 | if (!allowDuplicateIps) { |
| 353 | removeDuplicates(hostId, hostDescription); |
| 354 | } |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 355 | store.createOrUpdateHost(provider().id(), hostId, |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 356 | hostDescription, replaceIps); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 357 | |
| 358 | if (monitorHosts) { |
| 359 | hostDescription.ipAddress().forEach(ip -> { |
| 360 | monitor.addMonitoringFor(ip); |
| 361 | }); |
| 362 | } |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 363 | |
| 364 | // Greedy learning of IPv6 host. We have to disable the greedy |
| 365 | // learning of configured hosts. Validate hosts each time will |
Charles Chan | 82fac58 | 2017-09-12 12:09:22 -0700 | [diff] [blame] | 366 | // overwrite the learnt information with the configured information. |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 367 | if (greedyLearningIpv6) { |
| 368 | // Auto-generation of the IPv6 link local address |
| 369 | // using the mac address |
| 370 | Ip6Address targetIp6Address = Ip6Address.valueOf( |
| 371 | getLinkLocalAddress(hostId.mac().toBytes()) |
| 372 | ); |
| 373 | // If we already know this guy we don't need to do other |
| 374 | if (!hostDescription.ipAddress().contains(targetIp6Address)) { |
| 375 | Host host = store.getHost(hostId); |
| 376 | // Configured host, skip it. |
| 377 | if (host != null && host.configured()) { |
| 378 | return; |
| 379 | } |
| 380 | // Host does not exist in the store or the target is not known |
| 381 | if ((host == null || !host.ipAddresses().contains(targetIp6Address))) { |
Charles Chan | 82fac58 | 2017-09-12 12:09:22 -0700 | [diff] [blame] | 382 | // Use DAD to probe if interface MAC is not specified |
| 383 | MacAddress probeMac = interfaceService.getInterfacesByPort(hostDescription.location()) |
| 384 | .stream().map(Interface::mac).findFirst().orElse(MacAddress.ONOS); |
| 385 | Ip6Address probeIp = !probeMac.equals(MacAddress.ONOS) ? |
| 386 | Ip6Address.valueOf(getLinkLocalAddress(probeMac.toBytes())) : |
| 387 | Ip6Address.ZERO; |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 388 | // We send a probe using the monitoring service |
| 389 | monitor.sendProbe( |
| 390 | hostDescription.location(), |
| 391 | targetIp6Address, |
Charles Chan | 82fac58 | 2017-09-12 12:09:22 -0700 | [diff] [blame] | 392 | probeIp, |
| 393 | probeMac, |
Pier Luigi | 9b1d626 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 394 | hostId.vlanId() |
| 395 | ); |
| 396 | } |
| 397 | } |
| 398 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 399 | } |
| 400 | |
soumya | 3e6f05e | 2016-08-05 15:11:11 -0700 | [diff] [blame] | 401 | // When a new IP is detected, remove that IP on other hosts if it exists |
| 402 | public void removeDuplicates(HostId hostId, HostDescription desc) { |
| 403 | desc.ipAddress().forEach(ip -> { |
| 404 | Set<Host> allHosts = store.getHosts(ip); |
| 405 | allHosts.forEach(eachHost -> { |
| 406 | if (!(eachHost.id().equals(hostId))) { |
| 407 | log.info("Duplicate ip {} found on host {} and {}", ip, |
| 408 | hostId.toString(), eachHost.id().toString()); |
| 409 | store.removeIp(eachHost.id(), ip); |
| 410 | } |
| 411 | }); |
| 412 | }); |
Jonathan Hart | 38feb6e | 2016-08-29 22:54:16 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 415 | // returns a HostDescription made from the union of the BasicHostConfig |
| 416 | // annotations if it exists |
| 417 | private HostDescription validateHost(HostDescription hostDescription, HostId hostId) { |
| 418 | BasicHostConfig cfg = networkConfigService.getConfig(hostId, BasicHostConfig.class); |
| 419 | checkState(cfg == null || cfg.isAllowed(), "Host {} is not allowed", hostId); |
Ayaka Koshibe | 5373e76 | 2015-08-06 12:31:44 -0700 | [diff] [blame] | 420 | |
| 421 | return BasicHostOperator.combine(cfg, hostDescription); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 422 | } |
| 423 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 424 | @Override |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 425 | public void hostVanished(HostId hostId) { |
| 426 | checkNotNull(hostId, HOST_ID_NULL); |
| 427 | checkValidity(); |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 428 | Host host = store.getHost(hostId); |
Charles Chan | 29ecdee | 2017-02-22 18:46:56 -0800 | [diff] [blame] | 429 | |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 430 | if (!allowedToChange(hostId)) { |
| 431 | log.info("Request to remove {} is ignored due to provider mismatch", hostId); |
Charles Chan | 29ecdee | 2017-02-22 18:46:56 -0800 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 435 | if (monitorHosts) { |
| 436 | host.ipAddresses().forEach(ip -> { |
| 437 | monitor.stopMonitoring(ip); |
| 438 | }); |
| 439 | } |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 440 | store.removeHost(hostId); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 441 | } |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 442 | |
| 443 | @Override |
| 444 | public void removeIpFromHost(HostId hostId, IpAddress ipAddress) { |
| 445 | checkNotNull(hostId, HOST_ID_NULL); |
| 446 | checkValidity(); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 447 | |
| 448 | if (!allowedToChange(hostId)) { |
| 449 | log.info("Request to remove {} from {} is ignored due to provider mismatch", |
| 450 | ipAddress, hostId); |
| 451 | return; |
| 452 | } |
| 453 | |
Charles Chan | 009c308 | 2015-11-10 14:18:04 -0800 | [diff] [blame] | 454 | store.removeIp(hostId, ipAddress); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 455 | } |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 456 | |
| 457 | @Override |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 458 | public void addLocationToHost(HostId hostId, HostLocation location) { |
| 459 | checkNotNull(hostId, HOST_ID_NULL); |
| 460 | checkValidity(); |
| 461 | |
| 462 | if (!allowedToChange(hostId)) { |
Charles Chan | b928b3f | 2018-07-23 15:37:08 -0700 | [diff] [blame] | 463 | log.info("Request to add {} to {} is ignored due to provider mismatch", |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 464 | location, hostId); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | store.appendLocation(hostId, location); |
| 469 | } |
| 470 | |
| 471 | @Override |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 472 | public void removeLocationFromHost(HostId hostId, HostLocation location) { |
| 473 | checkNotNull(hostId, HOST_ID_NULL); |
| 474 | checkValidity(); |
| 475 | |
| 476 | if (!allowedToChange(hostId)) { |
| 477 | log.info("Request to remove {} from {} is ignored due to provider mismatch", |
| 478 | location, hostId); |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | store.removeLocation(hostId, location); |
| 483 | } |
| 484 | |
Charles Chan | d0c147a | 2017-09-14 14:00:10 -0700 | [diff] [blame] | 485 | /** |
| 486 | * Providers should only be able to remove a host that is provided by itself, |
| 487 | * or a host that is not configured. |
| 488 | */ |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 489 | private boolean allowedToChange(HostId hostId) { |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 490 | Host host = store.getHost(hostId); |
Charles Chan | d0c147a | 2017-09-14 14:00:10 -0700 | [diff] [blame] | 491 | return host == null || !host.configured() || host.providerId().equals(provider().id()); |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 492 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 493 | } |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 494 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 495 | // Store delegate to re-post events emitted from the store. |
| 496 | private class InternalStoreDelegate implements HostStoreDelegate { |
| 497 | @Override |
| 498 | public void notify(HostEvent event) { |
| 499 | post(event); |
| 500 | } |
| 501 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 502 | |
| 503 | // listens for NetworkConfigEvents of type BasicHostConfig and removes |
| 504 | // links that the config does not allow |
| 505 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 506 | @Override |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 507 | public boolean isRelevant(NetworkConfigEvent event) { |
| 508 | return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED |
| 509 | || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) |
| 510 | && (event.configClass().equals(BasicHostConfig.class)); |
| 511 | } |
| 512 | |
| 513 | @Override |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 514 | public void event(NetworkConfigEvent event) { |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 515 | log.debug("Detected host network config event {}", event.type()); |
| 516 | HostEvent he = null; |
| 517 | |
| 518 | HostId hostId = (HostId) event.subject(); |
| 519 | BasicHostConfig cfg = |
| 520 | networkConfigService.getConfig(hostId, BasicHostConfig.class); |
| 521 | |
| 522 | if (!isAllowed(cfg)) { |
| 523 | kickOutBadHost(hostId); |
| 524 | } else { |
| 525 | Host host = getHost(hostId); |
| 526 | HostDescription desc = |
| 527 | (host == null) ? null : BasicHostOperator.descriptionOf(host); |
| 528 | desc = BasicHostOperator.combine(cfg, desc); |
| 529 | if (desc != null) { |
| 530 | he = store.createOrUpdateHost(host.providerId(), hostId, desc, false); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | if (he != null) { |
| 535 | post(he); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 540 | // by default allowed, otherwise check flag |
| 541 | private boolean isAllowed(BasicHostConfig cfg) { |
| 542 | return (cfg == null || cfg.isAllowed()); |
| 543 | } |
| 544 | |
| 545 | // removes the specified host, if it exists |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 546 | private void kickOutBadHost(HostId hostId) { |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 547 | Host badHost = getHost(hostId); |
| 548 | if (badHost != null) { |
| 549 | removeHost(hostId); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 550 | } |
| 551 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 552 | } |