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 |
| 161 | public void addSink(McastRoute route, Set<ConnectPoint> sinks) { |
| 162 | checkNotNull(route, "Route cannot be null"); |
| 163 | checkNotNull(sinks, "Sinks cannot be null"); |
| 164 | if (checkRoute(route)) { |
| 165 | store.addSinks(route, sinks); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void removeSinks(McastRoute route) { |
| 171 | checkNotNull(route, "Route cannot be null"); |
| 172 | if (checkRoute(route)) { |
| 173 | store.removeSinks(route); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void removeSink(McastRoute route, HostId hostId) { |
| 179 | checkNotNull(route, "Route cannot be null"); |
| 180 | checkNotNull(hostId, "Host cannot be null"); |
| 181 | if (checkRoute(route)) { |
| 182 | store.removeSink(route, hostId); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | @Override |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 187 | public void removeSinks(McastRoute route, Set<ConnectPoint> connectPoints) { |
| 188 | checkNotNull(route, "Route cannot be null"); |
| 189 | if (checkRoute(route)) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame^] | 190 | store.removeSinks(route, connectPoints); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public McastRouteData routeData(McastRoute route) { |
| 196 | checkNotNull(route, "Route cannot be null"); |
| 197 | return checkRoute(route) ? store.getRouteData(route) : null; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public Set<ConnectPoint> sources(McastRoute route) { |
| 202 | checkNotNull(route, "Route cannot be null"); |
| 203 | return checkRoute(route) ? store.sourcesFor(route) : ImmutableSet.of(); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public Set<ConnectPoint> sinks(McastRoute route) { |
| 208 | checkNotNull(route, "Route cannot be null"); |
| 209 | return checkRoute(route) ? store.sinksFor(route) : ImmutableSet.of(); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public Set<ConnectPoint> sinks(McastRoute route, HostId hostId) { |
| 214 | checkNotNull(route, "Route cannot be null"); |
| 215 | return checkRoute(route) ? store.sinksFor(route, hostId) : ImmutableSet.of(); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public Set<ConnectPoint> nonHostSinks(McastRoute route) { |
| 220 | checkNotNull(route, "Route cannot be null"); |
| 221 | return checkRoute(route) ? store.sinksFor(route, HostId.NONE) : ImmutableSet.of(); |
| 222 | } |
| 223 | |
| 224 | private class InternalMcastStoreDelegate implements McastStoreDelegate { |
| 225 | @Override |
| 226 | public void notify(McastEvent event) { |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame^] | 227 | log.debug("Notify event: {}", event); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 228 | post(event); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | private boolean checkRoute(McastRoute route) { |
| 233 | if (store.getRoutes().contains(route)) { |
| 234 | return true; |
| 235 | } else { |
| 236 | log.warn("Route {} is not present in the store, please add it", route); |
| 237 | } |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | private class InternalHostListener implements HostListener { |
| 242 | |
| 243 | @Override |
| 244 | public void event(HostEvent event) { |
| 245 | HostId hostId = event.subject().id(); |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame^] | 246 | log.debug("Host event: {}", event); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 247 | switch (event.type()) { |
| 248 | case HOST_ADDED: |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame^] | 249 | //the host is added, if it already comes with some locations let's use them |
| 250 | eventAddSinks(hostId, event.subject().locations()); |
| 251 | break; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 252 | case HOST_MOVED: |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame^] | 253 | //both subjects must be null or the system is in an incoherent state |
| 254 | if ((event.prevSubject() != null && event.subject() != null)) { |
| 255 | //we compute the difference between old locations and new ones and remove the previous |
| 256 | Set<HostLocation> removedSinks = Sets.difference(event.prevSubject().locations(), |
| 257 | event.subject().locations()).immutableCopy(); |
| 258 | if (!removedSinks.isEmpty()) { |
| 259 | eventRemoveSinks(hostId, removedSinks); |
| 260 | } |
| 261 | Set<HostLocation> addedSinks = Sets.difference(event.subject().locations(), |
| 262 | event.prevSubject().locations()).immutableCopy(); |
| 263 | //if the host now has some new locations we add them to the sinks set |
| 264 | if (!addedSinks.isEmpty()) { |
| 265 | eventAddSinks(hostId, addedSinks); |
| 266 | } |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 267 | } |
| 268 | break; |
| 269 | case HOST_REMOVED: |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame^] | 270 | // Removing all the sinks for that specific host |
| 271 | // even if the locations are 0 we keep |
| 272 | // the host information in the route in case it shows up again |
| 273 | eventRemoveSinks(event.subject().id(), event.subject().locations()); |
| 274 | break; |
| 275 | case HOST_UPDATED: |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 276 | default: |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame^] | 277 | log.debug("Host event {} not handled", event.type()); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame^] | 281 | |
| 282 | //Adds sinks for a given host event |
| 283 | private void eventRemoveSinks(HostId hostId, Set<HostLocation> removedSinks) { |
| 284 | Set<ConnectPoint> sinks = new HashSet<>(); |
| 285 | // Build sink using host location |
| 286 | sinks.addAll(removedSinks); |
| 287 | // Filter by host id and then remove from each route the provided sinks |
| 288 | store.getRoutes().stream().filter(mcastRoute -> store.getRouteData(mcastRoute) |
| 289 | .sinks().get(hostId) != null) |
| 290 | .forEach(route -> store.removeSinks(route, hostId, sinks)); |
| 291 | } |
| 292 | |
| 293 | //Removes the sinks for a given host event |
| 294 | private void eventAddSinks(HostId hostId, Set<HostLocation> addedSinks) { |
| 295 | Set<ConnectPoint> sinks = new HashSet<>(); |
| 296 | // Build sink using host location |
| 297 | sinks.addAll(addedSinks); |
| 298 | // Filter by host id and then add to each route the provided sinks |
| 299 | store.getRoutes().stream().filter(mcastRoute -> store.getRouteData(mcastRoute) |
| 300 | .sinks().get(hostId) != null) |
| 301 | .forEach(route -> store.addSink(route, hostId, sinks)); |
| 302 | } |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 303 | } |