Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-present Open Networking Foundation |
| 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 | */ |
| 16 | package org.onosproject.mcast.impl; |
| 17 | |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 18 | import com.google.common.base.Objects; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onosproject.event.AbstractListenerManager; |
| 29 | import org.onosproject.mcast.api.McastEvent; |
| 30 | import org.onosproject.mcast.api.McastListener; |
| 31 | import org.onosproject.mcast.api.McastRoute; |
| 32 | import org.onosproject.mcast.api.McastRouteData; |
| 33 | import org.onosproject.mcast.api.McastStore; |
| 34 | import org.onosproject.mcast.api.McastStoreDelegate; |
| 35 | import org.onosproject.mcast.api.MulticastRouteService; |
| 36 | import org.onosproject.net.ConnectPoint; |
| 37 | import org.onosproject.net.Host; |
| 38 | import org.onosproject.net.HostId; |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 39 | import org.onosproject.net.HostLocation; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 40 | import org.onosproject.net.host.HostEvent; |
| 41 | import org.onosproject.net.host.HostListener; |
| 42 | import org.onosproject.net.host.HostService; |
| 43 | import org.slf4j.Logger; |
| 44 | |
| 45 | import java.util.HashSet; |
| 46 | import java.util.Optional; |
| 47 | import java.util.Set; |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 48 | import java.util.stream.Collectors; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 49 | |
| 50 | import static com.google.common.base.Preconditions.checkNotNull; |
| 51 | import static org.slf4j.LoggerFactory.getLogger; |
| 52 | |
| 53 | /** |
| 54 | * An implementation of a multicast route table. |
| 55 | */ |
| 56 | @Component(immediate = true) |
| 57 | @Service |
| 58 | public class MulticastRouteManager |
| 59 | extends AbstractListenerManager<McastEvent, McastListener> |
| 60 | implements MulticastRouteService { |
| 61 | //TODO: add MulticastRouteAdminService |
| 62 | |
| 63 | private Logger log = getLogger(getClass()); |
| 64 | |
| 65 | private final McastStoreDelegate delegate = new InternalMcastStoreDelegate(); |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected McastStore store; |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected HostService hostService; |
| 72 | |
| 73 | private HostListener hostListener = new InternalHostListener(); |
| 74 | |
| 75 | @Activate |
| 76 | public void activate() { |
| 77 | hostService.addListener(hostListener); |
| 78 | eventDispatcher.addSink(McastEvent.class, listenerRegistry); |
| 79 | store.setDelegate(delegate); |
| 80 | log.info("Started"); |
| 81 | } |
| 82 | |
| 83 | @Deactivate |
| 84 | public void deactivate() { |
| 85 | hostService.removeListener(hostListener); |
| 86 | store.unsetDelegate(delegate); |
| 87 | eventDispatcher.removeSink(McastEvent.class); |
| 88 | log.info("Stopped"); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void add(McastRoute route) { |
| 93 | checkNotNull(route, "Route cannot be null"); |
| 94 | store.storeRoute(route); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void remove(McastRoute route) { |
| 99 | checkNotNull(route, "Route cannot be null"); |
| 100 | if (checkRoute(route)) { |
| 101 | store.removeRoute(route); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public Set<McastRoute> getRoutes() { |
| 107 | return store.getRoutes(); |
| 108 | } |
| 109 | |
| 110 | @Override |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 111 | public Set<McastRoute> getRoute(IpAddress groupIp, IpAddress sourceIp) { |
| 112 | // Let's transform it into an optional |
| 113 | final Optional<IpAddress> source = Optional.ofNullable(sourceIp); |
| 114 | return store.getRoutes().stream() |
| 115 | .filter(route -> route.group().equals(groupIp) && |
| 116 | Objects.equal(route.source(), source)) |
| 117 | .collect(Collectors.toSet()); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void addSources(McastRoute route, Set<ConnectPoint> connectPoints) { |
| 122 | checkNotNull(route, "Route cannot be null"); |
| 123 | checkNotNull(connectPoints, "Source cannot be null"); |
| 124 | if (checkRoute(route)) { |
| 125 | store.storeSources(route, connectPoints); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void removeSources(McastRoute route) { |
| 131 | checkNotNull(route, "Route cannot be null"); |
| 132 | if (checkRoute(route)) { |
| 133 | store.removeSources(route); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void removeSources(McastRoute route, Set<ConnectPoint> sources) { |
| 139 | checkNotNull(route, "Route cannot be null"); |
| 140 | checkNotNull(sources, "Source cannot be null"); |
| 141 | if (checkRoute(route)) { |
| 142 | store.removeSources(route, sources); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void addSink(McastRoute route, HostId hostId) { |
| 148 | if (checkRoute(route)) { |
| 149 | Set<ConnectPoint> sinks = new HashSet<>(); |
| 150 | Host host = hostService.getHost(hostId); |
| 151 | if (host != null) { |
| 152 | host.locations().forEach(hostLocation -> sinks.add( |
| 153 | ConnectPoint.deviceConnectPoint(hostLocation.deviceId() + "/" + hostLocation.port()))); |
| 154 | } |
| 155 | store.addSink(route, hostId, sinks); |
| 156 | } |
| 157 | |
| 158 | } |
| 159 | |
| 160 | @Override |
Andrea Campanella | 644a8a6 | 2018-03-21 19:08:21 -0700 | [diff] [blame^] | 161 | public void addSinks(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) { |
| 162 | if (checkRoute(route)) { |
| 163 | store.addSink(route, hostId, sinks); |
| 164 | } |
| 165 | |
| 166 | } |
| 167 | |
| 168 | @Override |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 169 | public void addSink(McastRoute route, Set<ConnectPoint> sinks) { |
| 170 | checkNotNull(route, "Route cannot be null"); |
| 171 | checkNotNull(sinks, "Sinks cannot be null"); |
| 172 | if (checkRoute(route)) { |
| 173 | store.addSinks(route, sinks); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void removeSinks(McastRoute route) { |
| 179 | checkNotNull(route, "Route cannot be null"); |
| 180 | if (checkRoute(route)) { |
| 181 | store.removeSinks(route); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void removeSink(McastRoute route, HostId hostId) { |
| 187 | checkNotNull(route, "Route cannot be null"); |
| 188 | checkNotNull(hostId, "Host cannot be null"); |
| 189 | if (checkRoute(route)) { |
| 190 | store.removeSink(route, hostId); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | @Override |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 195 | public void removeSinks(McastRoute route, Set<ConnectPoint> connectPoints) { |
| 196 | checkNotNull(route, "Route cannot be null"); |
| 197 | if (checkRoute(route)) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 198 | store.removeSinks(route, connectPoints); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public McastRouteData routeData(McastRoute route) { |
| 204 | checkNotNull(route, "Route cannot be null"); |
| 205 | return checkRoute(route) ? store.getRouteData(route) : null; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public Set<ConnectPoint> sources(McastRoute route) { |
| 210 | checkNotNull(route, "Route cannot be null"); |
| 211 | return checkRoute(route) ? store.sourcesFor(route) : ImmutableSet.of(); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public Set<ConnectPoint> sinks(McastRoute route) { |
| 216 | checkNotNull(route, "Route cannot be null"); |
| 217 | return checkRoute(route) ? store.sinksFor(route) : ImmutableSet.of(); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public Set<ConnectPoint> sinks(McastRoute route, HostId hostId) { |
| 222 | checkNotNull(route, "Route cannot be null"); |
| 223 | return checkRoute(route) ? store.sinksFor(route, hostId) : ImmutableSet.of(); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public Set<ConnectPoint> nonHostSinks(McastRoute route) { |
| 228 | checkNotNull(route, "Route cannot be null"); |
| 229 | return checkRoute(route) ? store.sinksFor(route, HostId.NONE) : ImmutableSet.of(); |
| 230 | } |
| 231 | |
| 232 | private class InternalMcastStoreDelegate implements McastStoreDelegate { |
| 233 | @Override |
| 234 | public void notify(McastEvent event) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 235 | log.debug("Notify event: {}", event); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 236 | post(event); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | private boolean checkRoute(McastRoute route) { |
| 241 | if (store.getRoutes().contains(route)) { |
| 242 | return true; |
| 243 | } else { |
| 244 | log.warn("Route {} is not present in the store, please add it", route); |
| 245 | } |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | private class InternalHostListener implements HostListener { |
| 250 | |
| 251 | @Override |
| 252 | public void event(HostEvent event) { |
| 253 | HostId hostId = event.subject().id(); |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 254 | log.debug("Host event: {}", event); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 255 | switch (event.type()) { |
| 256 | case HOST_ADDED: |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 257 | //the host is added, if it already comes with some locations let's use them |
| 258 | eventAddSinks(hostId, event.subject().locations()); |
| 259 | break; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 260 | case HOST_MOVED: |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 261 | //both subjects must be null or the system is in an incoherent state |
| 262 | if ((event.prevSubject() != null && event.subject() != null)) { |
| 263 | //we compute the difference between old locations and new ones and remove the previous |
| 264 | Set<HostLocation> removedSinks = Sets.difference(event.prevSubject().locations(), |
| 265 | event.subject().locations()).immutableCopy(); |
| 266 | if (!removedSinks.isEmpty()) { |
| 267 | eventRemoveSinks(hostId, removedSinks); |
| 268 | } |
| 269 | Set<HostLocation> addedSinks = Sets.difference(event.subject().locations(), |
| 270 | event.prevSubject().locations()).immutableCopy(); |
| 271 | //if the host now has some new locations we add them to the sinks set |
| 272 | if (!addedSinks.isEmpty()) { |
| 273 | eventAddSinks(hostId, addedSinks); |
| 274 | } |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 275 | } |
| 276 | break; |
| 277 | case HOST_REMOVED: |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 278 | // Removing all the sinks for that specific host |
| 279 | // even if the locations are 0 we keep |
| 280 | // the host information in the route in case it shows up again |
| 281 | eventRemoveSinks(event.subject().id(), event.subject().locations()); |
| 282 | break; |
| 283 | case HOST_UPDATED: |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 284 | default: |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 285 | log.debug("Host event {} not handled", event.type()); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | } |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 289 | |
| 290 | //Adds sinks for a given host event |
| 291 | private void eventRemoveSinks(HostId hostId, Set<HostLocation> removedSinks) { |
| 292 | Set<ConnectPoint> sinks = new HashSet<>(); |
| 293 | // Build sink using host location |
| 294 | sinks.addAll(removedSinks); |
| 295 | // Filter by host id and then remove from each route the provided sinks |
| 296 | store.getRoutes().stream().filter(mcastRoute -> store.getRouteData(mcastRoute) |
| 297 | .sinks().get(hostId) != null) |
| 298 | .forEach(route -> store.removeSinks(route, hostId, sinks)); |
| 299 | } |
| 300 | |
| 301 | //Removes the sinks for a given host event |
| 302 | private void eventAddSinks(HostId hostId, Set<HostLocation> addedSinks) { |
| 303 | Set<ConnectPoint> sinks = new HashSet<>(); |
| 304 | // Build sink using host location |
| 305 | sinks.addAll(addedSinks); |
| 306 | // Filter by host id and then add to each route the provided sinks |
| 307 | store.getRoutes().stream().filter(mcastRoute -> store.getRouteData(mcastRoute) |
| 308 | .sinks().get(hostId) != null) |
| 309 | .forEach(route -> store.addSink(route, hostId, sinks)); |
| 310 | } |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 311 | } |