blob: b442b095f78a192d251ce4e75c39cf1437a889fc [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(
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800117 new ThreadFactoryBuilder()
118 .setNameFormat("sdnip-bgp-updates-%d").build());
Jonathan Hart335ef462014-10-16 08:20:46 -0700119 }
120
121 /**
Jonathan Hart739c8352014-10-29 17:49:26 -0700122 * Starts the router.
Jonathan Hart335ef462014-10-16 08:20:46 -0700123 */
124 public void start() {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800125 this.hostService.addListener(hostListener);
126
Jonathan Hart335ef462014-10-16 08:20:46 -0700127 bgpUpdatesExecutor.execute(new Runnable() {
128 @Override
129 public void run() {
130 doUpdatesThread();
131 }
132 });
Jonathan Hart335ef462014-10-16 08:20:46 -0700133 }
134
Jonathan Hart739c8352014-10-29 17:49:26 -0700135 /**
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800136 * Stops the router.
Jonathan Hart739c8352014-10-29 17:49:26 -0700137 */
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800138 public void stop() {
139 this.hostService.removeListener(hostListener);
140
141 // Stop the thread(s)
Jonathan Hart739c8352014-10-29 17:49:26 -0700142 bgpUpdatesExecutor.shutdownNow();
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800143
144 synchronized (this) {
145 // Cleanup all local state
146 bgpRoutes = new ConcurrentInvertedRadixTree<>(
147 new DefaultByteArrayNodeFactory());
148 routeUpdates.clear();
149 routesWaitingOnArp.clear();
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800150 }
Jonathan Hart739c8352014-10-29 17:49:26 -0700151 }
152
Jonathan Hart335ef462014-10-16 08:20:46 -0700153 @Override
154 public void update(RouteUpdate routeUpdate) {
Jonathan Hart31582d12014-10-22 13:52:41 -0700155 log.debug("Received new route update: {}", routeUpdate);
Jonathan Hart335ef462014-10-16 08:20:46 -0700156
157 try {
158 routeUpdates.put(routeUpdate);
159 } catch (InterruptedException e) {
160 log.debug("Interrupted while putting on routeUpdates queue", e);
161 Thread.currentThread().interrupt();
162 }
163 }
164
165 /**
Jonathan Hart335ef462014-10-16 08:20:46 -0700166 * Thread for handling route updates.
167 */
168 private void doUpdatesThread() {
169 boolean interrupted = false;
170 try {
171 while (!interrupted) {
172 try {
173 RouteUpdate update = routeUpdates.take();
174 switch (update.type()) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700175 case UPDATE:
176 processRouteAdd(update.routeEntry());
177 break;
178 case DELETE:
179 processRouteDelete(update.routeEntry());
180 break;
181 default:
182 log.error("Unknown update Type: {}", update.type());
183 break;
Jonathan Hart335ef462014-10-16 08:20:46 -0700184 }
185 } catch (InterruptedException e) {
186 log.debug("Interrupted while taking from updates queue", e);
187 interrupted = true;
188 } catch (Exception e) {
189 log.debug("exception", e);
190 }
191 }
192 } finally {
193 if (interrupted) {
194 Thread.currentThread().interrupt();
195 }
196 }
197 }
198
199 /**
Jonathan Hart335ef462014-10-16 08:20:46 -0700200 * Processes adding a route entry.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700201 * <p>
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700202 * Put new route entry into the radix tree. If there was an existing
203 * next hop for this prefix, but the next hop was different, then execute
Jonathan Hart335ef462014-10-16 08:20:46 -0700204 * deleting old route entry. If the next hop is the SDN domain, we do not
205 * handle it at the moment. Otherwise, execute adding a route.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700206 * </p>
Jonathan Hart335ef462014-10-16 08:20:46 -0700207 *
208 * @param routeEntry the route entry to add
209 */
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800210 void processRouteAdd(RouteEntry routeEntry) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700211 synchronized (this) {
212 log.debug("Processing route add: {}", routeEntry);
213
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800214 Ip4Prefix prefix = routeEntry.prefix();
215 Ip4Address nextHop = null;
Jonathan Hart335ef462014-10-16 08:20:46 -0700216 RouteEntry foundRouteEntry =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700217 bgpRoutes.put(RouteEntry.createBinaryString(prefix),
218 routeEntry);
Jonathan Hart335ef462014-10-16 08:20:46 -0700219 if (foundRouteEntry != null) {
220 nextHop = foundRouteEntry.nextHop();
221 }
222
223 if (nextHop != null && !nextHop.equals(routeEntry.nextHop())) {
224 // There was an existing nexthop for this prefix. This update
225 // supersedes that, so we need to remove the old flows for this
226 // prefix from the switches
227 executeRouteDelete(routeEntry);
228 }
229 if (nextHop != null && nextHop.equals(routeEntry.nextHop())) {
230 return;
231 }
232
233 if (routeEntry.nextHop().equals(LOCAL_NEXT_HOP)) {
234 // Route originated by SDN domain
235 // We don't handle these at the moment
236 log.debug("Own route {} to {}",
237 routeEntry.prefix(), routeEntry.nextHop());
238 return;
239 }
240
241 executeRouteAdd(routeEntry);
242 }
243 }
244
245 /**
246 * Executes adding a route entry.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700247 * <p>
Jonathan Hart335ef462014-10-16 08:20:46 -0700248 * Find out the egress Interface and MAC address of next hop router for
249 * this route entry. If the MAC address can not be found in ARP cache,
250 * then this prefix will be put in routesWaitingOnArp queue. Otherwise,
251 * new route intent will be created and installed.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700252 * </p>
Jonathan Hart335ef462014-10-16 08:20:46 -0700253 *
254 * @param routeEntry the route entry to add
255 */
256 private void executeRouteAdd(RouteEntry routeEntry) {
257 log.debug("Executing route add: {}", routeEntry);
258
Jonathan Hart31582d12014-10-22 13:52:41 -0700259 // Monitor the IP address so we'll get notified of updates to the MAC
260 // address.
261 hostService.startMonitoringIp(routeEntry.nextHop());
262
Jonathan Hart335ef462014-10-16 08:20:46 -0700263 // See if we know the MAC address of the next hop
Jonathan Hart335ef462014-10-16 08:20:46 -0700264 MacAddress nextHopMacAddress = null;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700265 Set<Host> hosts = hostService.getHostsByIp(routeEntry.nextHop());
Jonathan Hart335ef462014-10-16 08:20:46 -0700266 if (!hosts.isEmpty()) {
267 // TODO how to handle if multiple hosts are returned?
268 nextHopMacAddress = hosts.iterator().next().mac();
269 }
270
271 if (nextHopMacAddress == null) {
272 routesWaitingOnArp.put(routeEntry.nextHop(), routeEntry);
Jonathan Hart335ef462014-10-16 08:20:46 -0700273 return;
274 }
275
276 addRouteIntentToNextHop(routeEntry.prefix(),
277 routeEntry.nextHop(),
278 nextHopMacAddress);
279 }
280
281 /**
282 * Adds a route intent given a prefix and a next hop IP address. This
283 * method will find the egress interface for the intent.
284 *
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700285 * @param prefix IP prefix of the route to add
286 * @param nextHopIpAddress IP address of the next hop
Jonathan Hart335ef462014-10-16 08:20:46 -0700287 * @param nextHopMacAddress MAC address of the next hop
288 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800289 private void addRouteIntentToNextHop(Ip4Prefix prefix,
290 Ip4Address nextHopIpAddress,
Jonathan Hart335ef462014-10-16 08:20:46 -0700291 MacAddress nextHopMacAddress) {
292
293 // Find the attachment point (egress interface) of the next hop
294 Interface egressInterface;
Jonathan Hart31582d12014-10-22 13:52:41 -0700295 if (configService.getBgpPeers().containsKey(nextHopIpAddress)) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700296 // Route to a peer
297 log.debug("Route to peer {}", nextHopIpAddress);
298 BgpPeer peer =
Jonathan Hart31582d12014-10-22 13:52:41 -0700299 configService.getBgpPeers().get(nextHopIpAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700300 egressInterface =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700301 interfaceService.getInterface(peer.connectPoint());
Jonathan Hart335ef462014-10-16 08:20:46 -0700302 } else {
303 // Route to non-peer
304 log.debug("Route to non-peer {}", nextHopIpAddress);
305 egressInterface =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700306 interfaceService.getMatchingInterface(nextHopIpAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700307 if (egressInterface == null) {
308 log.warn("No outgoing interface found for {}",
309 nextHopIpAddress);
310 return;
311 }
312 }
313
314 doAddRouteIntent(prefix, egressInterface, nextHopMacAddress);
315 }
316
317 /**
318 * Installs a route intent for a prefix.
319 * <p/>
320 * Intent will match dst IP prefix and rewrite dst MAC address at all other
321 * border switches, then forward packets according to dst MAC address.
322 *
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700323 * @param prefix IP prefix from route
324 * @param egressInterface egress Interface connected to next hop router
Jonathan Hart335ef462014-10-16 08:20:46 -0700325 * @param nextHopMacAddress MAC address of next hop router
326 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800327 private void doAddRouteIntent(Ip4Prefix prefix, Interface egressInterface,
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700328 MacAddress nextHopMacAddress) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700329 log.debug("Adding intent for prefix {}, next hop mac {}",
330 prefix, nextHopMacAddress);
331
Jonathan Hart335ef462014-10-16 08:20:46 -0700332 Set<ConnectPoint> ingressPorts = new HashSet<>();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800333 ConnectPoint egressPort = egressInterface.connectPoint();
Jonathan Hart335ef462014-10-16 08:20:46 -0700334
335 for (Interface intf : interfaceService.getInterfaces()) {
Jonathan Hart2e3eef32014-11-12 11:05:40 -0800336 if (!intf.connectPoint().equals(egressInterface.connectPoint())) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700337 ConnectPoint srcPort = intf.connectPoint();
338 ingressPorts.add(srcPort);
339 }
340 }
341
342 // Match the destination IP prefix at the first hop
Jonathan Hart335ef462014-10-16 08:20:46 -0700343 TrafficSelector selector = DefaultTrafficSelector.builder()
344 .matchEthType(Ethernet.TYPE_IPV4)
345 .matchIPDst(prefix)
346 .build();
347
348 // Rewrite the destination MAC address
Jonathan Hart335ef462014-10-16 08:20:46 -0700349 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
350 .setEthDst(nextHopMacAddress)
351 .build();
352
353 MultiPointToSinglePointIntent intent =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700354 new MultiPointToSinglePointIntent(appId, selector, treatment,
355 ingressPorts, egressPort);
Jonathan Hart335ef462014-10-16 08:20:46 -0700356
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800357 intentSynchronizer.submitRouteIntent(prefix, intent);
Jonathan Hart335ef462014-10-16 08:20:46 -0700358 }
359
360 /**
361 * Executes deleting a route entry.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700362 * <p>
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700363 * Removes prefix from radix tree, and if successful, then try to delete
364 * the related intent.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700365 * </p>
Jonathan Hart335ef462014-10-16 08:20:46 -0700366 *
367 * @param routeEntry the route entry to delete
368 */
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800369 void processRouteDelete(RouteEntry routeEntry) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700370 synchronized (this) {
371 log.debug("Processing route delete: {}", routeEntry);
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800372 Ip4Prefix prefix = routeEntry.prefix();
Jonathan Hart335ef462014-10-16 08:20:46 -0700373
Jonathan Hart335ef462014-10-16 08:20:46 -0700374 if (bgpRoutes.remove(RouteEntry.createBinaryString(prefix))) {
375 //
376 // Only delete flows if an entry was actually removed from the
377 // tree. If no entry was removed, the <prefix, nexthop> wasn't
378 // there so it's probably already been removed and we don't
379 // need to do anything.
380 //
381 executeRouteDelete(routeEntry);
382 }
383
384 routesWaitingOnArp.remove(routeEntry.nextHop(), routeEntry);
385 // TODO cancel the request in the ARP manager as well
386 }
387 }
388
389 /**
390 * Executed deleting a route entry.
391 *
392 * @param routeEntry the route entry to delete
393 */
394 private void executeRouteDelete(RouteEntry routeEntry) {
395 log.debug("Executing route delete: {}", routeEntry);
396
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800397 intentSynchronizer.withdrawRouteIntent(routeEntry.prefix());
Jonathan Hart335ef462014-10-16 08:20:46 -0700398 }
399
400 /**
Jonathan Hart31582d12014-10-22 13:52:41 -0700401 * Signals the Router that the MAC to IP mapping has potentially been
402 * updated. This has the effect of updating the MAC address for any
403 * installed prefixes if it has changed, as well as installing any pending
404 * prefixes that were waiting for MAC resolution.
Jonathan Hart335ef462014-10-16 08:20:46 -0700405 *
Jonathan Hart31582d12014-10-22 13:52:41 -0700406 * @param ipAddress the IP address that an event was received for
407 * @param macAddress the most recently known MAC address for the IP address
Jonathan Hart335ef462014-10-16 08:20:46 -0700408 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800409 private void updateMac(Ip4Address ipAddress, MacAddress macAddress) {
Jonathan Hart31582d12014-10-22 13:52:41 -0700410 log.debug("Received updated MAC info: {} => {}", ipAddress, macAddress);
411
412 // TODO here we should check whether the next hop for any of our
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800413 // installed prefixes has changed, not just prefixes pending
414 // installation.
Jonathan Hart335ef462014-10-16 08:20:46 -0700415
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700416 // We synchronize on this to prevent changes to the radix tree
417 // while we're pushing intents. If the tree changes, the
418 // tree and intents could get out of sync.
Jonathan Hart335ef462014-10-16 08:20:46 -0700419 synchronized (this) {
420
421 Set<RouteEntry> routesToPush =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700422 routesWaitingOnArp.removeAll(ipAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700423
424 for (RouteEntry routeEntry : routesToPush) {
425 // These will always be adds
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800426 Ip4Prefix prefix = routeEntry.prefix();
Jonathan Hart335ef462014-10-16 08:20:46 -0700427 String binaryString = RouteEntry.createBinaryString(prefix);
428 RouteEntry foundRouteEntry =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700429 bgpRoutes.getValueForExactKey(binaryString);
Jonathan Hart335ef462014-10-16 08:20:46 -0700430 if (foundRouteEntry != null &&
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700431 foundRouteEntry.nextHop().equals(routeEntry.nextHop())) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700432 // We only push prefix flows if the prefix is still in the
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700433 // radix tree and the next hop is the same as our
Jonathan Hart335ef462014-10-16 08:20:46 -0700434 // update.
435 // The prefix could have been removed while we were waiting
436 // for the ARP, or the next hop could have changed.
437 addRouteIntentToNextHop(prefix, ipAddress, macAddress);
438 } else {
Jonathan Hart31582d12014-10-22 13:52:41 -0700439 log.debug("{} has been revoked before the MAC was resolved",
440 routeEntry);
Jonathan Hart335ef462014-10-16 08:20:46 -0700441 }
442 }
443 }
444 }
445
446 /**
447 * Gets the SDN-IP routes.
448 *
449 * @return the SDN-IP routes
450 */
451 public Collection<RouteEntry> getRoutes() {
452 Iterator<KeyValuePair<RouteEntry>> it =
453 bgpRoutes.getKeyValuePairsForKeysStartingWith("").iterator();
454
455 List<RouteEntry> routes = new LinkedList<>();
456
457 while (it.hasNext()) {
458 KeyValuePair<RouteEntry> entry = it.next();
459 routes.add(entry.getValue());
460 }
461
462 return routes;
463 }
464
465 /**
Jonathan Hart335ef462014-10-16 08:20:46 -0700466 * Listener for host events.
467 */
468 class InternalHostListener implements HostListener {
469 @Override
470 public void event(HostEvent event) {
471 if (event.type() == HostEvent.Type.HOST_ADDED ||
472 event.type() == HostEvent.Type.HOST_UPDATED) {
473 Host host = event.subject();
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700474 for (IpAddress ip : host.ipAddresses()) {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800475 Ip4Address ip4Address = ip.getIp4Address();
476 if (ip4Address == null) {
477 // TODO: For now we support only IPv4
478 continue;
479 }
480 updateMac(ip4Address, host.mac());
Jonathan Hart335ef462014-10-16 08:20:46 -0700481 }
482 }
483 }
484 }
485}