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