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