Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.sdnip; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 17 | |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 19 | import java.util.HashSet; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.LinkedList; |
| 22 | import java.util.List; |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 23 | import java.util.Map; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 24 | import java.util.Set; |
| 25 | import java.util.concurrent.BlockingQueue; |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 26 | import java.util.concurrent.ConcurrentHashMap; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 27 | import java.util.concurrent.ExecutorService; |
| 28 | import java.util.concurrent.Executors; |
| 29 | import java.util.concurrent.LinkedBlockingQueue; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 30 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 31 | import org.apache.commons.lang3.tuple.Pair; |
Jonathan Hart | 7dd4a33 | 2014-12-04 17:37:05 -0800 | [diff] [blame] | 32 | import org.onlab.packet.Ethernet; |
Jonathan Hart | 7dd4a33 | 2014-12-04 17:37:05 -0800 | [diff] [blame] | 33 | import org.onlab.packet.IpAddress; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 34 | import org.onlab.packet.IpPrefix; |
| 35 | import org.onlab.packet.Ip4Address; |
Jonathan Hart | 7dd4a33 | 2014-12-04 17:37:05 -0800 | [diff] [blame] | 36 | import org.onlab.packet.MacAddress; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.core.ApplicationId; |
| 38 | import org.onosproject.net.ConnectPoint; |
| 39 | import org.onosproject.net.Host; |
| 40 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 41 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 42 | import org.onosproject.net.flow.TrafficSelector; |
| 43 | import org.onosproject.net.flow.TrafficTreatment; |
| 44 | import org.onosproject.net.host.HostEvent; |
| 45 | import org.onosproject.net.host.HostListener; |
| 46 | import org.onosproject.net.host.HostService; |
| 47 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 48 | import org.onosproject.sdnip.config.BgpPeer; |
| 49 | import org.onosproject.sdnip.config.Interface; |
| 50 | import org.onosproject.sdnip.config.SdnIpConfigurationService; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 51 | import org.slf4j.Logger; |
| 52 | import org.slf4j.LoggerFactory; |
| 53 | |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 54 | import com.google.common.collect.HashMultimap; |
| 55 | import com.google.common.collect.Multimaps; |
| 56 | import com.google.common.collect.SetMultimap; |
| 57 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 58 | import com.googlecode.concurrenttrees.common.KeyValuePair; |
| 59 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 60 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 61 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 62 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 63 | /** |
| 64 | * This class processes BGP route update, translates each update into a intent |
| 65 | * and submits the intent. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 66 | */ |
| 67 | public class Router implements RouteListener { |
| 68 | |
| 69 | private static final Logger log = LoggerFactory.getLogger(Router.class); |
| 70 | |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 71 | // Store all route updates in a radix tree. |
| 72 | // The key in this tree is the binary string of prefix of the route. |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 73 | private InvertedRadixTree<RouteEntry> ribTable4; |
| 74 | private InvertedRadixTree<RouteEntry> ribTable6; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 75 | |
| 76 | // Stores all incoming route updates in a queue. |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 77 | private final BlockingQueue<Collection<RouteUpdate>> routeUpdatesQueue; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 78 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 79 | // The IpAddress is the next hop address of each route update. |
| 80 | private final SetMultimap<IpAddress, RouteEntry> routesWaitingOnArp; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 81 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 82 | // The IPv4 address to MAC address mapping |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 83 | private final Map<IpAddress, MacAddress> ip2Mac; |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 84 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 85 | private final ApplicationId appId; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 86 | private final IntentSynchronizer intentSynchronizer; |
| 87 | private final HostService hostService; |
Jonathan Hart | 9965d77 | 2014-12-02 10:28:34 -0800 | [diff] [blame] | 88 | private final SdnIpConfigurationService configService; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 89 | private final InterfaceService interfaceService; |
| 90 | private final ExecutorService bgpUpdatesExecutor; |
| 91 | private final HostListener hostListener; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Class constructor. |
| 95 | * |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 96 | * @param appId the application ID |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 97 | * @param intentSynchronizer the intent synchronizer |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 98 | * @param configService the configuration service |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 99 | * @param interfaceService the interface service |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 100 | * @param hostService the host service |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 101 | */ |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 102 | public Router(ApplicationId appId, IntentSynchronizer intentSynchronizer, |
Jonathan Hart | 9965d77 | 2014-12-02 10:28:34 -0800 | [diff] [blame] | 103 | SdnIpConfigurationService configService, |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 104 | InterfaceService interfaceService, |
| 105 | HostService hostService) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 106 | this.appId = appId; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 107 | this.intentSynchronizer = intentSynchronizer; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 108 | this.configService = configService; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 109 | this.interfaceService = interfaceService; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 110 | this.hostService = hostService; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 111 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 112 | this.hostListener = new InternalHostListener(); |
| 113 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 114 | ribTable4 = new ConcurrentInvertedRadixTree<>( |
| 115 | new DefaultByteArrayNodeFactory()); |
| 116 | ribTable6 = new ConcurrentInvertedRadixTree<>( |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 117 | new DefaultByteArrayNodeFactory()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 118 | routeUpdatesQueue = new LinkedBlockingQueue<>(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 119 | routesWaitingOnArp = Multimaps.synchronizedSetMultimap( |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 120 | HashMultimap.<IpAddress, RouteEntry>create()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 121 | ip2Mac = new ConcurrentHashMap<>(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 122 | |
| 123 | bgpUpdatesExecutor = Executors.newSingleThreadExecutor( |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 124 | new ThreadFactoryBuilder() |
| 125 | .setNameFormat("sdnip-bgp-updates-%d").build()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 129 | * Starts the router. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 130 | */ |
| 131 | public void start() { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 132 | this.hostService.addListener(hostListener); |
| 133 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 134 | bgpUpdatesExecutor.execute(new Runnable() { |
| 135 | @Override |
| 136 | public void run() { |
| 137 | doUpdatesThread(); |
| 138 | } |
| 139 | }); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 142 | /** |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 143 | * Stops the router. |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 144 | */ |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 145 | public void stop() { |
| 146 | this.hostService.removeListener(hostListener); |
| 147 | |
| 148 | // Stop the thread(s) |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 149 | bgpUpdatesExecutor.shutdownNow(); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 150 | |
| 151 | synchronized (this) { |
| 152 | // Cleanup all local state |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 153 | ribTable4 = new ConcurrentInvertedRadixTree<>( |
| 154 | new DefaultByteArrayNodeFactory()); |
| 155 | ribTable6 = new ConcurrentInvertedRadixTree<>( |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 156 | new DefaultByteArrayNodeFactory()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 157 | routeUpdatesQueue.clear(); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 158 | routesWaitingOnArp.clear(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 159 | ip2Mac.clear(); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 160 | } |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 163 | @Override |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 164 | public void update(Collection<RouteUpdate> routeUpdates) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 165 | try { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 166 | routeUpdatesQueue.put(routeUpdates); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 167 | } catch (InterruptedException e) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 168 | log.debug("Interrupted while putting on routeUpdatesQueue", e); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 169 | Thread.currentThread().interrupt(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 174 | * Thread for handling route updates. |
| 175 | */ |
| 176 | private void doUpdatesThread() { |
| 177 | boolean interrupted = false; |
| 178 | try { |
| 179 | while (!interrupted) { |
| 180 | try { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 181 | Collection<RouteUpdate> routeUpdates = |
| 182 | routeUpdatesQueue.take(); |
| 183 | processRouteUpdates(routeUpdates); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 184 | } catch (InterruptedException e) { |
| 185 | log.debug("Interrupted while taking from updates queue", e); |
| 186 | interrupted = true; |
| 187 | } catch (Exception e) { |
| 188 | log.debug("exception", e); |
| 189 | } |
| 190 | } |
| 191 | } finally { |
| 192 | if (interrupted) { |
| 193 | Thread.currentThread().interrupt(); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 199 | * Gets all IPv4 routes from the RIB. |
| 200 | * |
| 201 | * @return all IPv4 routes from the RIB |
| 202 | */ |
| 203 | public Collection<RouteEntry> getRoutes4() { |
| 204 | Iterator<KeyValuePair<RouteEntry>> it = |
| 205 | ribTable4.getKeyValuePairsForKeysStartingWith("").iterator(); |
| 206 | |
| 207 | List<RouteEntry> routes = new LinkedList<>(); |
| 208 | |
| 209 | while (it.hasNext()) { |
| 210 | KeyValuePair<RouteEntry> entry = it.next(); |
| 211 | routes.add(entry.getValue()); |
| 212 | } |
| 213 | |
| 214 | return routes; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Gets all IPv6 routes from the RIB. |
| 219 | * |
| 220 | * @return all IPv6 routes from the RIB |
| 221 | */ |
| 222 | public Collection<RouteEntry> getRoutes6() { |
| 223 | Iterator<KeyValuePair<RouteEntry>> it = |
| 224 | ribTable6.getKeyValuePairsForKeysStartingWith("").iterator(); |
| 225 | |
| 226 | List<RouteEntry> routes = new LinkedList<>(); |
| 227 | |
| 228 | while (it.hasNext()) { |
| 229 | KeyValuePair<RouteEntry> entry = it.next(); |
| 230 | routes.add(entry.getValue()); |
| 231 | } |
| 232 | |
| 233 | return routes; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Finds a route in the RIB for a prefix. The prefix can be either IPv4 or |
| 238 | * IPv6. |
| 239 | * |
| 240 | * @param prefix the prefix to use |
| 241 | * @return the route if found, otherwise null |
| 242 | */ |
| 243 | RouteEntry findRibRoute(IpPrefix prefix) { |
| 244 | String binaryString = RouteEntry.createBinaryString(prefix); |
| 245 | if (prefix.version() == Ip4Address.VERSION) { |
| 246 | // IPv4 |
| 247 | return ribTable4.getValueForExactKey(binaryString); |
| 248 | } |
| 249 | // IPv6 |
| 250 | return ribTable6.getValueForExactKey(binaryString); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Adds a route to the RIB. The route can be either IPv4 or IPv6. |
| 255 | * |
| 256 | * @param routeEntry the route entry to use |
| 257 | */ |
| 258 | void addRibRoute(RouteEntry routeEntry) { |
| 259 | if (routeEntry.prefix().version() == Ip4Address.VERSION) { |
| 260 | // IPv4 |
| 261 | ribTable4.put(RouteEntry.createBinaryString(routeEntry.prefix()), |
| 262 | routeEntry); |
| 263 | } else { |
| 264 | // IPv6 |
| 265 | ribTable6.put(RouteEntry.createBinaryString(routeEntry.prefix()), |
| 266 | routeEntry); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Removes a route for a prefix from the RIB. The prefix can be either IPv4 |
| 272 | * or IPv6. |
| 273 | * |
| 274 | * @param prefix the prefix to use |
| 275 | * @return true if the rotue was found and removed, otherwise false |
| 276 | */ |
| 277 | boolean removeRibRoute(IpPrefix prefix) { |
| 278 | if (prefix.version() == Ip4Address.VERSION) { |
| 279 | // IPv4 |
| 280 | return ribTable4.remove(RouteEntry.createBinaryString(prefix)); |
| 281 | } |
| 282 | // IPv6 |
| 283 | return ribTable6.remove(RouteEntry.createBinaryString(prefix)); |
| 284 | } |
| 285 | |
| 286 | /** |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 287 | * Processes route updates. |
| 288 | * |
| 289 | * @param routeUpdates the route updates to process |
| 290 | */ |
| 291 | void processRouteUpdates(Collection<RouteUpdate> routeUpdates) { |
| 292 | synchronized (this) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 293 | Collection<Pair<IpPrefix, MultiPointToSinglePointIntent>> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 294 | submitIntents = new LinkedList<>(); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 295 | Collection<IpPrefix> withdrawPrefixes = new LinkedList<>(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 296 | MultiPointToSinglePointIntent intent; |
| 297 | |
| 298 | for (RouteUpdate update : routeUpdates) { |
| 299 | switch (update.type()) { |
| 300 | case UPDATE: |
| 301 | intent = processRouteAdd(update.routeEntry(), |
| 302 | withdrawPrefixes); |
| 303 | if (intent != null) { |
| 304 | submitIntents.add(Pair.of(update.routeEntry().prefix(), |
| 305 | intent)); |
| 306 | } |
| 307 | break; |
| 308 | case DELETE: |
| 309 | processRouteDelete(update.routeEntry(), withdrawPrefixes); |
| 310 | break; |
| 311 | default: |
| 312 | log.error("Unknown update Type: {}", update.type()); |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | intentSynchronizer.updateRouteIntents(submitIntents, |
| 318 | withdrawPrefixes); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 323 | * Processes adding a route entry. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 324 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 325 | * The route entry is added to the radix tree. If there was an existing |
| 326 | * next hop for this prefix, but the next hop was different, then the |
| 327 | * old route entry is deleted. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 328 | * </p> |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 329 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 330 | * NOTE: Currently, we don't handle routes if the next hop is within the |
| 331 | * SDN domain. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 332 | * </p> |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 333 | * |
| 334 | * @param routeEntry the route entry to add |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 335 | * @param withdrawPrefixes the collection of accumulated prefixes whose |
| 336 | * intents will be withdrawn |
| 337 | * @return the corresponding intent that should be submitted, or null |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 338 | */ |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 339 | private MultiPointToSinglePointIntent processRouteAdd( |
| 340 | RouteEntry routeEntry, |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 341 | Collection<IpPrefix> withdrawPrefixes) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 342 | log.debug("Processing route add: {}", routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 343 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 344 | // Find the old next-hop if we are updating an old route entry |
| 345 | IpAddress oldNextHop = null; |
| 346 | RouteEntry oldRouteEntry = findRibRoute(routeEntry.prefix()); |
| 347 | if (oldRouteEntry != null) { |
| 348 | oldNextHop = oldRouteEntry.nextHop(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 351 | // Add the new route to the RIB |
| 352 | addRibRoute(routeEntry); |
| 353 | |
| 354 | if (oldNextHop != null) { |
| 355 | if (oldNextHop.equals(routeEntry.nextHop())) { |
| 356 | return null; // No change |
| 357 | } |
| 358 | // |
| 359 | // Update an existing nexthop for the prefix. |
| 360 | // We need to remove the old flows for this prefix from the |
| 361 | // switches before the new flows are added. |
| 362 | // |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 363 | withdrawPrefixes.add(routeEntry.prefix()); |
| 364 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 365 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 366 | if (routeEntry.nextHop().isZero()) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 367 | // Route originated by SDN domain |
| 368 | // We don't handle these at the moment |
| 369 | log.debug("Own route {} to {}", |
| 370 | routeEntry.prefix(), routeEntry.nextHop()); |
| 371 | return null; |
| 372 | } |
| 373 | |
| 374 | // |
| 375 | // Find the MAC address of next hop router for this route entry. |
| 376 | // If the MAC address can not be found in ARP cache, then this prefix |
| 377 | // will be put in routesWaitingOnArp queue. Otherwise, generate |
| 378 | // a new route intent. |
| 379 | // |
| 380 | |
| 381 | // Monitor the IP address for updates of the MAC address |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 382 | hostService.startMonitoringIp(routeEntry.nextHop()); |
| 383 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 384 | // Check if we know the MAC address of the next hop |
| 385 | MacAddress nextHopMacAddress = ip2Mac.get(routeEntry.nextHop()); |
| 386 | if (nextHopMacAddress == null) { |
| 387 | Set<Host> hosts = hostService.getHostsByIp(routeEntry.nextHop()); |
| 388 | if (!hosts.isEmpty()) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 389 | nextHopMacAddress = hosts.iterator().next().mac(); |
| 390 | } |
| 391 | if (nextHopMacAddress != null) { |
| 392 | ip2Mac.put(routeEntry.nextHop(), nextHopMacAddress); |
| 393 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 394 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 395 | if (nextHopMacAddress == null) { |
| 396 | routesWaitingOnArp.put(routeEntry.nextHop(), routeEntry); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 397 | return null; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 398 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 399 | return generateRouteIntent(routeEntry.prefix(), routeEntry.nextHop(), |
| 400 | nextHopMacAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /** |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 404 | * Generates a route intent for a prefix, the next hop IP address, and |
| 405 | * the next hop MAC address. |
| 406 | * <p/> |
| 407 | * This method will find the egress interface for the intent. |
| 408 | * Intent will match dst IP prefix and rewrite dst MAC address at all other |
| 409 | * border switches, then forward packets according to dst MAC address. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 410 | * |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 411 | * @param prefix IP prefix of the route to add |
| 412 | * @param nextHopIpAddress IP address of the next hop |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 413 | * @param nextHopMacAddress MAC address of the next hop |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 414 | * @return the generated intent, or null if no intent should be submitted |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 415 | */ |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 416 | private MultiPointToSinglePointIntent generateRouteIntent( |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 417 | IpPrefix prefix, |
| 418 | IpAddress nextHopIpAddress, |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 419 | MacAddress nextHopMacAddress) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 420 | |
| 421 | // Find the attachment point (egress interface) of the next hop |
| 422 | Interface egressInterface; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 423 | if (configService.getBgpPeers().containsKey(nextHopIpAddress)) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 424 | // Route to a peer |
| 425 | log.debug("Route to peer {}", nextHopIpAddress); |
| 426 | BgpPeer peer = |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 427 | configService.getBgpPeers().get(nextHopIpAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 428 | egressInterface = |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 429 | interfaceService.getInterface(peer.connectPoint()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 430 | } else { |
| 431 | // Route to non-peer |
| 432 | log.debug("Route to non-peer {}", nextHopIpAddress); |
| 433 | egressInterface = |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 434 | interfaceService.getMatchingInterface(nextHopIpAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 435 | if (egressInterface == null) { |
| 436 | log.warn("No outgoing interface found for {}", |
| 437 | nextHopIpAddress); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 438 | return null; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 442 | // |
| 443 | // Generate the intent itself |
| 444 | // |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 445 | Set<ConnectPoint> ingressPorts = new HashSet<>(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 446 | ConnectPoint egressPort = egressInterface.connectPoint(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 447 | log.debug("Generating intent for prefix {}, next hop mac {}", |
| 448 | prefix, nextHopMacAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 449 | |
| 450 | for (Interface intf : interfaceService.getInterfaces()) { |
Jonathan Hart | 2e3eef3 | 2014-11-12 11:05:40 -0800 | [diff] [blame] | 451 | if (!intf.connectPoint().equals(egressInterface.connectPoint())) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 452 | ConnectPoint srcPort = intf.connectPoint(); |
| 453 | ingressPorts.add(srcPort); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // Match the destination IP prefix at the first hop |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 458 | TrafficSelector selector; |
| 459 | if (prefix.version() == Ip4Address.VERSION) { |
| 460 | selector = DefaultTrafficSelector.builder() |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 461 | .matchEthType(Ethernet.TYPE_IPV4) |
| 462 | .matchIPDst(prefix) |
| 463 | .build(); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 464 | } else { |
| 465 | selector = DefaultTrafficSelector.builder() |
| 466 | .matchEthType(Ethernet.TYPE_IPV6) |
| 467 | .matchIPDst(prefix) |
| 468 | .build(); |
| 469 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 470 | |
| 471 | // Rewrite the destination MAC address |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 472 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 473 | .setEthDst(nextHopMacAddress) |
| 474 | .build(); |
| 475 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 476 | return new MultiPointToSinglePointIntent(appId, selector, treatment, |
| 477 | ingressPorts, egressPort); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /** |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 481 | * Processes the deletion of a route entry. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 482 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 483 | * The prefix for the routing entry is removed from radix tree. |
| 484 | * If the operation is successful, the prefix is added to the collection |
| 485 | * of prefixes whose intents that will be withdrawn. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 486 | * </p> |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 487 | * |
| 488 | * @param routeEntry the route entry to delete |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 489 | * @param withdrawPrefixes the collection of accumulated prefixes whose |
| 490 | * intents will be withdrawn |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 491 | */ |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 492 | private void processRouteDelete(RouteEntry routeEntry, |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 493 | Collection<IpPrefix> withdrawPrefixes) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 494 | log.debug("Processing route delete: {}", routeEntry); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 495 | boolean isRemoved = removeRibRoute(routeEntry.prefix()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 496 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 497 | if (isRemoved) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 498 | // |
| 499 | // Only withdraw intents if an entry was actually removed from the |
| 500 | // tree. If no entry was removed, the <prefix, nexthop> wasn't |
| 501 | // there so it's probably already been removed and we don't |
| 502 | // need to do anything. |
| 503 | // |
| 504 | withdrawPrefixes.add(routeEntry.prefix()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 505 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 506 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 507 | routesWaitingOnArp.remove(routeEntry.nextHop(), routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /** |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 511 | * Signals the Router that the MAC to IP mapping has potentially been |
| 512 | * updated. This has the effect of updating the MAC address for any |
| 513 | * installed prefixes if it has changed, as well as installing any pending |
| 514 | * prefixes that were waiting for MAC resolution. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 515 | * |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 516 | * @param ipAddress the IP address that an event was received for |
| 517 | * @param macAddress the most recently known MAC address for the IP address |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 518 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 519 | private void updateMac(IpAddress ipAddress, MacAddress macAddress) { |
| 520 | log.debug("Received updated MAC info: {} => {}", ipAddress, |
| 521 | macAddress); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 522 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 523 | // |
| 524 | // We synchronize on "this" to prevent changes to the Radix tree |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 525 | // while we're pushing intents. If the tree changes, the |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 526 | // tree and the intents could get out of sync. |
| 527 | // |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 528 | synchronized (this) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 529 | Collection<Pair<IpPrefix, MultiPointToSinglePointIntent>> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 530 | submitIntents = new LinkedList<>(); |
| 531 | MultiPointToSinglePointIntent intent; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 532 | |
| 533 | Set<RouteEntry> routesToPush = |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 534 | routesWaitingOnArp.removeAll(ipAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 535 | |
| 536 | for (RouteEntry routeEntry : routesToPush) { |
| 537 | // These will always be adds |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 538 | RouteEntry foundRouteEntry = findRibRoute(routeEntry.prefix()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 539 | if (foundRouteEntry != null && |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 540 | foundRouteEntry.nextHop().equals(routeEntry.nextHop())) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 541 | // We only push prefix flows if the prefix is still in the |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 542 | // radix tree and the next hop is the same as our |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 543 | // update. |
| 544 | // The prefix could have been removed while we were waiting |
| 545 | // for the ARP, or the next hop could have changed. |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 546 | intent = generateRouteIntent(routeEntry.prefix(), |
| 547 | ipAddress, macAddress); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 548 | if (intent != null) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 549 | submitIntents.add(Pair.of(routeEntry.prefix(), |
| 550 | intent)); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 551 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 552 | } else { |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 553 | log.debug("{} has been revoked before the MAC was resolved", |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 554 | routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 555 | } |
| 556 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 557 | |
| 558 | if (!submitIntents.isEmpty()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 559 | Collection<IpPrefix> withdrawPrefixes = new LinkedList<>(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 560 | intentSynchronizer.updateRouteIntents(submitIntents, |
| 561 | withdrawPrefixes); |
| 562 | } |
| 563 | |
| 564 | ip2Mac.put(ipAddress, macAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
| 568 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 569 | * Listener for host events. |
| 570 | */ |
| 571 | class InternalHostListener implements HostListener { |
| 572 | @Override |
| 573 | public void event(HostEvent event) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 574 | log.debug("Received HostEvent {}", event); |
| 575 | |
| 576 | Host host = event.subject(); |
| 577 | switch (event.type()) { |
| 578 | case HOST_ADDED: |
| 579 | // FALLTHROUGH |
| 580 | case HOST_UPDATED: |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 581 | for (IpAddress ipAddress : host.ipAddresses()) { |
| 582 | updateMac(ipAddress, host.mac()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 583 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 584 | break; |
| 585 | case HOST_REMOVED: |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame^] | 586 | for (IpAddress ipAddress : host.ipAddresses()) { |
| 587 | ip2Mac.remove(ipAddress); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 588 | } |
| 589 | break; |
| 590 | default: |
| 591 | break; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | } |
| 595 | } |