blob: 6b5f2f4af30fa2aa9cbca7138c6e288a5abcbfab [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Jonathan Hart335ef462014-10-16 08:20:46 -070016package org.onlab.onos.sdnip;
17
Pingping3855f312014-10-22 12:50:37 -070018import java.util.Collection;
Pingping3855f312014-10-22 12:50:37 -070019import java.util.HashSet;
20import java.util.Iterator;
21import java.util.LinkedList;
22import java.util.List;
Pingping3855f312014-10-22 12:50:37 -070023import java.util.Set;
24import java.util.concurrent.BlockingQueue;
Pingping3855f312014-10-22 12:50:37 -070025import java.util.concurrent.ExecutorService;
26import java.util.concurrent.Executors;
27import java.util.concurrent.LinkedBlockingQueue;
Pingping3855f312014-10-22 12:50:37 -070028
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070029import org.onlab.onos.core.ApplicationId;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070030import org.onlab.onos.net.ConnectPoint;
31import org.onlab.onos.net.Host;
32import org.onlab.onos.net.flow.DefaultTrafficSelector;
33import org.onlab.onos.net.flow.DefaultTrafficTreatment;
34import org.onlab.onos.net.flow.TrafficSelector;
35import org.onlab.onos.net.flow.TrafficTreatment;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070036import org.onlab.onos.net.host.HostEvent;
37import org.onlab.onos.net.host.HostListener;
38import org.onlab.onos.net.host.HostService;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070039import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
40import org.onlab.onos.sdnip.config.BgpPeer;
41import org.onlab.onos.sdnip.config.Interface;
42import org.onlab.onos.sdnip.config.SdnIpConfigService;
43import org.onlab.packet.Ethernet;
44import org.onlab.packet.IpAddress;
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080045import org.onlab.packet.Ip4Address;
46import org.onlab.packet.Ip4Prefix;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070047import org.onlab.packet.MacAddress;
48import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
Pingping3855f312014-10-22 12:50:37 -070051import com.google.common.collect.HashMultimap;
52import com.google.common.collect.Multimaps;
53import com.google.common.collect.SetMultimap;
54import com.google.common.util.concurrent.ThreadFactoryBuilder;
55import com.googlecode.concurrenttrees.common.KeyValuePair;
56import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory;
57import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
58import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
Jonathan Hart335ef462014-10-16 08:20:46 -070059
Jonathan Hart335ef462014-10-16 08:20:46 -070060/**
61 * This class processes BGP route update, translates each update into a intent
62 * and submits the intent.
Jonathan Hart335ef462014-10-16 08:20:46 -070063 */
64public class Router implements RouteListener {
65
66 private static final Logger log = LoggerFactory.getLogger(Router.class);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080067 // For routes announced by local BGP daemon in SDN network,
68 // the next hop will be 0.0.0.0.
69 private static final Ip4Address LOCAL_NEXT_HOP =
70 Ip4Address.valueOf("0.0.0.0");
Jonathan Hart335ef462014-10-16 08:20:46 -070071
Jonathan Hart0b04bed2014-10-16 16:39:19 -070072 // Store all route updates in a radix tree.
73 // The key in this tree is the binary string of prefix of the route.
Jonathan Hart335ef462014-10-16 08:20:46 -070074 private InvertedRadixTree<RouteEntry> bgpRoutes;
75
76 // Stores all incoming route updates in a queue.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080077 private final BlockingQueue<RouteUpdate> routeUpdates;
Jonathan Hart335ef462014-10-16 08:20:46 -070078
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080079 // The Ip4Address is the next hop address of each route update.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080080 private final SetMultimap<Ip4Address, RouteEntry> routesWaitingOnArp;
Jonathan Hart335ef462014-10-16 08:20:46 -070081
Thomas Vachuskab97cf282014-10-20 23:31:12 -070082 private final ApplicationId appId;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080083 private final IntentSynchronizer intentSynchronizer;
84 private final HostService hostService;
85 private final SdnIpConfigService configService;
86 private final InterfaceService interfaceService;
87 private final ExecutorService bgpUpdatesExecutor;
88 private final HostListener hostListener;
Jonathan Hart335ef462014-10-16 08:20:46 -070089
90 /**
91 * Class constructor.
92 *
Jonathan Hart31582d12014-10-22 13:52:41 -070093 * @param appId the application ID
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080094 * @param intentSynchronizer the intent synchronizer
Thomas Vachuskab97cf282014-10-20 23:31:12 -070095 * @param hostService the host service
Jonathan Hart31582d12014-10-22 13:52:41 -070096 * @param configService the configuration service
Thomas Vachuskab97cf282014-10-20 23:31:12 -070097 * @param interfaceService the interface service
Jonathan Hart335ef462014-10-16 08:20:46 -070098 */
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080099 public Router(ApplicationId appId, IntentSynchronizer intentSynchronizer,
Jonathan Hart31582d12014-10-22 13:52:41 -0700100 HostService hostService, SdnIpConfigService configService,
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700101 InterfaceService interfaceService) {
102 this.appId = appId;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800103 this.intentSynchronizer = intentSynchronizer;
Jonathan Hart335ef462014-10-16 08:20:46 -0700104 this.hostService = hostService;
Jonathan Hart31582d12014-10-22 13:52:41 -0700105 this.configService = configService;
Jonathan Hart335ef462014-10-16 08:20:46 -0700106 this.interfaceService = interfaceService;
107
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800108 this.hostListener = new InternalHostListener();
109
Jonathan Hart335ef462014-10-16 08:20:46 -0700110 bgpRoutes = new ConcurrentInvertedRadixTree<>(
111 new DefaultByteArrayNodeFactory());
112 routeUpdates = new LinkedBlockingQueue<>();
113 routesWaitingOnArp = Multimaps.synchronizedSetMultimap(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800114 HashMultimap.<Ip4Address, RouteEntry>create());
Jonathan Hart335ef462014-10-16 08:20:46 -0700115
116 bgpUpdatesExecutor = Executors.newSingleThreadExecutor(
117 new ThreadFactoryBuilder().setNameFormat("bgp-updates-%d").build());
Jonathan Hart335ef462014-10-16 08:20:46 -0700118 }
119
120 /**
Jonathan Hart739c8352014-10-29 17:49:26 -0700121 * Starts the router.
Jonathan Hart335ef462014-10-16 08:20:46 -0700122 */
123 public void start() {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800124 this.hostService.addListener(hostListener);
125
Jonathan Hart335ef462014-10-16 08:20:46 -0700126 bgpUpdatesExecutor.execute(new Runnable() {
127 @Override
128 public void run() {
129 doUpdatesThread();
130 }
131 });
Jonathan Hart335ef462014-10-16 08:20:46 -0700132 }
133
Jonathan Hart739c8352014-10-29 17:49:26 -0700134 /**
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800135 * Stops the router.
Jonathan Hart739c8352014-10-29 17:49:26 -0700136 */
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800137 public void stop() {
138 this.hostService.removeListener(hostListener);
139
140 // Stop the thread(s)
Jonathan Hart739c8352014-10-29 17:49:26 -0700141 bgpUpdatesExecutor.shutdownNow();
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800142
143 synchronized (this) {
144 // Cleanup all local state
145 bgpRoutes = new ConcurrentInvertedRadixTree<>(
146 new DefaultByteArrayNodeFactory());
147 routeUpdates.clear();
148 routesWaitingOnArp.clear();
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800149 }
Jonathan Hart739c8352014-10-29 17:49:26 -0700150 }
151
Jonathan Hart335ef462014-10-16 08:20:46 -0700152 @Override
153 public void update(RouteUpdate routeUpdate) {
Jonathan Hart31582d12014-10-22 13:52:41 -0700154 log.debug("Received new route update: {}", routeUpdate);
Jonathan Hart335ef462014-10-16 08:20:46 -0700155
156 try {
157 routeUpdates.put(routeUpdate);
158 } catch (InterruptedException e) {
159 log.debug("Interrupted while putting on routeUpdates queue", e);
160 Thread.currentThread().interrupt();
161 }
162 }
163
164 /**
Jonathan Hart335ef462014-10-16 08:20:46 -0700165 * Thread for handling route updates.
166 */
167 private void doUpdatesThread() {
168 boolean interrupted = false;
169 try {
170 while (!interrupted) {
171 try {
172 RouteUpdate update = routeUpdates.take();
173 switch (update.type()) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700174 case UPDATE:
175 processRouteAdd(update.routeEntry());
176 break;
177 case DELETE:
178 processRouteDelete(update.routeEntry());
179 break;
180 default:
181 log.error("Unknown update Type: {}", update.type());
182 break;
Jonathan Hart335ef462014-10-16 08:20:46 -0700183 }
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 /**
Jonathan Hart335ef462014-10-16 08:20:46 -0700199 * Processes adding a route entry.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700200 * <p>
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700201 * Put new route entry into the radix tree. If there was an existing
202 * next hop for this prefix, but the next hop was different, then execute
Jonathan Hart335ef462014-10-16 08:20:46 -0700203 * deleting old route entry. If the next hop is the SDN domain, we do not
204 * handle it at the moment. Otherwise, execute adding a route.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700205 * </p>
Jonathan Hart335ef462014-10-16 08:20:46 -0700206 *
207 * @param routeEntry the route entry to add
208 */
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800209 void processRouteAdd(RouteEntry routeEntry) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700210 synchronized (this) {
211 log.debug("Processing route add: {}", routeEntry);
212
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800213 Ip4Prefix prefix = routeEntry.prefix();
214 Ip4Address nextHop = null;
Jonathan Hart335ef462014-10-16 08:20:46 -0700215 RouteEntry foundRouteEntry =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700216 bgpRoutes.put(RouteEntry.createBinaryString(prefix),
217 routeEntry);
Jonathan Hart335ef462014-10-16 08:20:46 -0700218 if (foundRouteEntry != null) {
219 nextHop = foundRouteEntry.nextHop();
220 }
221
222 if (nextHop != null && !nextHop.equals(routeEntry.nextHop())) {
223 // There was an existing nexthop for this prefix. This update
224 // supersedes that, so we need to remove the old flows for this
225 // prefix from the switches
226 executeRouteDelete(routeEntry);
227 }
228 if (nextHop != null && nextHop.equals(routeEntry.nextHop())) {
229 return;
230 }
231
232 if (routeEntry.nextHop().equals(LOCAL_NEXT_HOP)) {
233 // Route originated by SDN domain
234 // We don't handle these at the moment
235 log.debug("Own route {} to {}",
236 routeEntry.prefix(), routeEntry.nextHop());
237 return;
238 }
239
240 executeRouteAdd(routeEntry);
241 }
242 }
243
244 /**
245 * Executes adding a route entry.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700246 * <p>
Jonathan Hart335ef462014-10-16 08:20:46 -0700247 * Find out the egress Interface and MAC address of next hop router for
248 * this route entry. If the MAC address can not be found in ARP cache,
249 * then this prefix will be put in routesWaitingOnArp queue. Otherwise,
250 * new route intent will be created and installed.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700251 * </p>
Jonathan Hart335ef462014-10-16 08:20:46 -0700252 *
253 * @param routeEntry the route entry to add
254 */
255 private void executeRouteAdd(RouteEntry routeEntry) {
256 log.debug("Executing route add: {}", routeEntry);
257
Jonathan Hart31582d12014-10-22 13:52:41 -0700258 // Monitor the IP address so we'll get notified of updates to the MAC
259 // address.
260 hostService.startMonitoringIp(routeEntry.nextHop());
261
Jonathan Hart335ef462014-10-16 08:20:46 -0700262 // See if we know the MAC address of the next hop
Jonathan Hart335ef462014-10-16 08:20:46 -0700263 MacAddress nextHopMacAddress = null;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700264 Set<Host> hosts = hostService.getHostsByIp(routeEntry.nextHop());
Jonathan Hart335ef462014-10-16 08:20:46 -0700265 if (!hosts.isEmpty()) {
266 // TODO how to handle if multiple hosts are returned?
267 nextHopMacAddress = hosts.iterator().next().mac();
268 }
269
270 if (nextHopMacAddress == null) {
271 routesWaitingOnArp.put(routeEntry.nextHop(), routeEntry);
Jonathan Hart335ef462014-10-16 08:20:46 -0700272 return;
273 }
274
275 addRouteIntentToNextHop(routeEntry.prefix(),
276 routeEntry.nextHop(),
277 nextHopMacAddress);
278 }
279
280 /**
281 * Adds a route intent given a prefix and a next hop IP address. This
282 * method will find the egress interface for the intent.
283 *
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700284 * @param prefix IP prefix of the route to add
285 * @param nextHopIpAddress IP address of the next hop
Jonathan Hart335ef462014-10-16 08:20:46 -0700286 * @param nextHopMacAddress MAC address of the next hop
287 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800288 private void addRouteIntentToNextHop(Ip4Prefix prefix,
289 Ip4Address nextHopIpAddress,
Jonathan Hart335ef462014-10-16 08:20:46 -0700290 MacAddress nextHopMacAddress) {
291
292 // Find the attachment point (egress interface) of the next hop
293 Interface egressInterface;
Jonathan Hart31582d12014-10-22 13:52:41 -0700294 if (configService.getBgpPeers().containsKey(nextHopIpAddress)) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700295 // Route to a peer
296 log.debug("Route to peer {}", nextHopIpAddress);
297 BgpPeer peer =
Jonathan Hart31582d12014-10-22 13:52:41 -0700298 configService.getBgpPeers().get(nextHopIpAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700299 egressInterface =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700300 interfaceService.getInterface(peer.connectPoint());
Jonathan Hart335ef462014-10-16 08:20:46 -0700301 } else {
302 // Route to non-peer
303 log.debug("Route to non-peer {}", nextHopIpAddress);
304 egressInterface =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700305 interfaceService.getMatchingInterface(nextHopIpAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700306 if (egressInterface == null) {
307 log.warn("No outgoing interface found for {}",
308 nextHopIpAddress);
309 return;
310 }
311 }
312
313 doAddRouteIntent(prefix, egressInterface, nextHopMacAddress);
314 }
315
316 /**
317 * Installs a route intent for a prefix.
318 * <p/>
319 * Intent will match dst IP prefix and rewrite dst MAC address at all other
320 * border switches, then forward packets according to dst MAC address.
321 *
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700322 * @param prefix IP prefix from route
323 * @param egressInterface egress Interface connected to next hop router
Jonathan Hart335ef462014-10-16 08:20:46 -0700324 * @param nextHopMacAddress MAC address of next hop router
325 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800326 private void doAddRouteIntent(Ip4Prefix prefix, Interface egressInterface,
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700327 MacAddress nextHopMacAddress) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700328 log.debug("Adding intent for prefix {}, next hop mac {}",
329 prefix, nextHopMacAddress);
330
Jonathan Hart335ef462014-10-16 08:20:46 -0700331 Set<ConnectPoint> ingressPorts = new HashSet<>();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800332 ConnectPoint egressPort = egressInterface.connectPoint();
Jonathan Hart335ef462014-10-16 08:20:46 -0700333
334 for (Interface intf : interfaceService.getInterfaces()) {
Jonathan Hart2e3eef32014-11-12 11:05:40 -0800335 if (!intf.connectPoint().equals(egressInterface.connectPoint())) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700336 ConnectPoint srcPort = intf.connectPoint();
337 ingressPorts.add(srcPort);
338 }
339 }
340
341 // Match the destination IP prefix at the first hop
Jonathan Hart335ef462014-10-16 08:20:46 -0700342 TrafficSelector selector = DefaultTrafficSelector.builder()
343 .matchEthType(Ethernet.TYPE_IPV4)
344 .matchIPDst(prefix)
345 .build();
346
347 // Rewrite the destination MAC address
Jonathan Hart335ef462014-10-16 08:20:46 -0700348 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
349 .setEthDst(nextHopMacAddress)
350 .build();
351
352 MultiPointToSinglePointIntent intent =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700353 new MultiPointToSinglePointIntent(appId, selector, treatment,
354 ingressPorts, egressPort);
Jonathan Hart335ef462014-10-16 08:20:46 -0700355
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800356 intentSynchronizer.submitRouteIntent(prefix, intent);
Jonathan Hart335ef462014-10-16 08:20:46 -0700357 }
358
359 /**
360 * Executes deleting a route entry.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700361 * <p>
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700362 * Removes prefix from radix tree, and if successful, then try to delete
363 * the related intent.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700364 * </p>
Jonathan Hart335ef462014-10-16 08:20:46 -0700365 *
366 * @param routeEntry the route entry to delete
367 */
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800368 void processRouteDelete(RouteEntry routeEntry) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700369 synchronized (this) {
370 log.debug("Processing route delete: {}", routeEntry);
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800371 Ip4Prefix prefix = routeEntry.prefix();
Jonathan Hart335ef462014-10-16 08:20:46 -0700372
Jonathan Hart335ef462014-10-16 08:20:46 -0700373 if (bgpRoutes.remove(RouteEntry.createBinaryString(prefix))) {
374 //
375 // Only delete flows if an entry was actually removed from the
376 // tree. If no entry was removed, the <prefix, nexthop> wasn't
377 // there so it's probably already been removed and we don't
378 // need to do anything.
379 //
380 executeRouteDelete(routeEntry);
381 }
382
383 routesWaitingOnArp.remove(routeEntry.nextHop(), routeEntry);
384 // TODO cancel the request in the ARP manager as well
385 }
386 }
387
388 /**
389 * Executed deleting a route entry.
390 *
391 * @param routeEntry the route entry to delete
392 */
393 private void executeRouteDelete(RouteEntry routeEntry) {
394 log.debug("Executing route delete: {}", routeEntry);
395
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800396 intentSynchronizer.withdrawRouteIntent(routeEntry.prefix());
Jonathan Hart335ef462014-10-16 08:20:46 -0700397 }
398
399 /**
Jonathan Hart31582d12014-10-22 13:52:41 -0700400 * Signals the Router that the MAC to IP mapping has potentially been
401 * updated. This has the effect of updating the MAC address for any
402 * installed prefixes if it has changed, as well as installing any pending
403 * prefixes that were waiting for MAC resolution.
Jonathan Hart335ef462014-10-16 08:20:46 -0700404 *
Jonathan Hart31582d12014-10-22 13:52:41 -0700405 * @param ipAddress the IP address that an event was received for
406 * @param macAddress the most recently known MAC address for the IP address
Jonathan Hart335ef462014-10-16 08:20:46 -0700407 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800408 private void updateMac(Ip4Address ipAddress, MacAddress macAddress) {
Jonathan Hart31582d12014-10-22 13:52:41 -0700409 log.debug("Received updated MAC info: {} => {}", ipAddress, macAddress);
410
411 // TODO here we should check whether the next hop for any of our
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800412 // installed prefixes has changed, not just prefixes pending
413 // installation.
Jonathan Hart335ef462014-10-16 08:20:46 -0700414
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700415 // We synchronize on this to prevent changes to the radix tree
416 // while we're pushing intents. If the tree changes, the
417 // tree and intents could get out of sync.
Jonathan Hart335ef462014-10-16 08:20:46 -0700418 synchronized (this) {
419
420 Set<RouteEntry> routesToPush =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700421 routesWaitingOnArp.removeAll(ipAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700422
423 for (RouteEntry routeEntry : routesToPush) {
424 // These will always be adds
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800425 Ip4Prefix prefix = routeEntry.prefix();
Jonathan Hart335ef462014-10-16 08:20:46 -0700426 String binaryString = RouteEntry.createBinaryString(prefix);
427 RouteEntry foundRouteEntry =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700428 bgpRoutes.getValueForExactKey(binaryString);
Jonathan Hart335ef462014-10-16 08:20:46 -0700429 if (foundRouteEntry != null &&
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700430 foundRouteEntry.nextHop().equals(routeEntry.nextHop())) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700431 // We only push prefix flows if the prefix is still in the
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700432 // radix tree and the next hop is the same as our
Jonathan Hart335ef462014-10-16 08:20:46 -0700433 // update.
434 // The prefix could have been removed while we were waiting
435 // for the ARP, or the next hop could have changed.
436 addRouteIntentToNextHop(prefix, ipAddress, macAddress);
437 } else {
Jonathan Hart31582d12014-10-22 13:52:41 -0700438 log.debug("{} has been revoked before the MAC was resolved",
439 routeEntry);
Jonathan Hart335ef462014-10-16 08:20:46 -0700440 }
441 }
442 }
443 }
444
445 /**
446 * Gets the SDN-IP routes.
447 *
448 * @return the SDN-IP routes
449 */
450 public Collection<RouteEntry> getRoutes() {
451 Iterator<KeyValuePair<RouteEntry>> it =
452 bgpRoutes.getKeyValuePairsForKeysStartingWith("").iterator();
453
454 List<RouteEntry> routes = new LinkedList<>();
455
456 while (it.hasNext()) {
457 KeyValuePair<RouteEntry> entry = it.next();
458 routes.add(entry.getValue());
459 }
460
461 return routes;
462 }
463
464 /**
Jonathan Hart335ef462014-10-16 08:20:46 -0700465 * Listener for host events.
466 */
467 class InternalHostListener implements HostListener {
468 @Override
469 public void event(HostEvent event) {
470 if (event.type() == HostEvent.Type.HOST_ADDED ||
471 event.type() == HostEvent.Type.HOST_UPDATED) {
472 Host host = event.subject();
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700473 for (IpAddress ip : host.ipAddresses()) {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800474 Ip4Address ip4Address = ip.getIp4Address();
475 if (ip4Address == null) {
476 // TODO: For now we support only IPv4
477 continue;
478 }
479 updateMac(ip4Address, host.mac());
Jonathan Hart335ef462014-10-16 08:20:46 -0700480 }
481 }
482 }
483 }
484}