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