Jonathan Hart
Committed by Gerrit Code Review

Unit tests for route store.

Change-Id: Ia711c497bb7d0751d692c2461c884ddc5287a2ef
......@@ -20,6 +20,8 @@ import org.onlab.packet.IpAddress;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import java.util.Objects;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
......@@ -84,6 +86,28 @@ public class ResolvedRoute {
}
@Override
public int hashCode() {
return Objects.hash(prefix, nextHop, nextHopMac);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ResolvedRoute)) {
return false;
}
ResolvedRoute that = (ResolvedRoute) other;
return Objects.equals(this.prefix, that.prefix) &&
Objects.equals(this.nextHop, that.nextHop) &&
Objects.equals(this.nextHopMac, that.nextHopMac);
}
@Override
public String toString() {
return toStringHelper(this)
.add("prefix", prefix)
......
......@@ -18,6 +18,8 @@ package org.onosproject.incubator.net.routing;
import org.onosproject.event.AbstractEvent;
import java.util.Objects;
/**
* Describes an event about a route.
*/
......@@ -65,4 +67,24 @@ public class RouteEvent extends AbstractEvent<RouteEvent.Type, ResolvedRoute> {
super(type, subject, time);
}
@Override
public int hashCode() {
return Objects.hash(subject(), type());
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof RouteEvent)) {
return false;
}
RouteEvent that = (RouteEvent) other;
return Objects.equals(this.subject(), that.subject()) &&
Objects.equals(this.type(), that.type());
}
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.net.routing.impl;
import org.onosproject.incubator.net.routing.RouteEvent;
/**
* Queues updates for a route listener to ensure they are received in the
* correct order.
*/
interface ListenerQueue {
/**
* Posts an event to the listener.
*
* @param event event
*/
void post(RouteEvent event);
/**
* Initiates event delivery to the listener.
*/
void start();
/**
* Halts event delivery to the listener.
*/
void stop();
}
......@@ -114,7 +114,7 @@ public class RouteManager implements ListenerService<RouteEvent, RouteListener>,
public void addListener(RouteListener listener) {
synchronized (this) {
log.debug("Synchronizing current routes to new listener");
ListenerQueue l = new ListenerQueue(listener);
ListenerQueue l = createListenerQueue(listener);
routeStore.getRouteTables().forEach(table -> {
Collection<Route> routes = routeStore.getRoutes(table);
if (routes != null) {
......@@ -234,10 +234,19 @@ public class RouteManager implements ListenerService<RouteEvent, RouteListener>,
}
/**
* Queues updates for a route listener to ensure they are received in the
* correct order.
* Creates a new listener queue.
*
* @param listener route listener
* @return listener queue
*/
ListenerQueue createListenerQueue(RouteListener listener) {
return new DefaultListenerQueue(listener);
}
/**
* Default route listener queue.
*/
private class ListenerQueue {
private class DefaultListenerQueue implements ListenerQueue {
private final ExecutorService executorService;
private final BlockingQueue<RouteEvent> queue;
......@@ -248,31 +257,23 @@ public class RouteManager implements ListenerService<RouteEvent, RouteListener>,
*
* @param listener route listener to queue updates for
*/
public ListenerQueue(RouteListener listener) {
public DefaultListenerQueue(RouteListener listener) {
this.listener = listener;
queue = new LinkedBlockingQueue<>();
executorService = newSingleThreadExecutor(threadFactory);
}
/**
* Posts and event to the listener.
*
* @param event event
*/
@Override
public void post(RouteEvent event) {
queue.add(event);
}
/**
* Initiates event delivery to the listener.
*/
@Override
public void start() {
executorService.execute(this::poll);
}
/**
* Halts event delivery to the listener.
*/
@Override
public void stop() {
executorService.shutdown();
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.net.routing.impl;
import com.google.common.collect.Sets;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.Ip4Prefix;
import org.onlab.packet.Ip6Address;
import org.onlab.packet.Ip6Prefix;
import org.onlab.packet.IpAddress;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import org.onlab.packet.VlanId;
import org.onosproject.incubator.net.routing.ResolvedRoute;
import org.onosproject.incubator.net.routing.Route;
import org.onosproject.incubator.net.routing.RouteEvent;
import org.onosproject.incubator.net.routing.RouteListener;
import org.onosproject.incubator.store.routing.impl.LocalRouteStore;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DefaultHost;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Host;
import org.onosproject.net.HostId;
import org.onosproject.net.HostLocation;
import org.onosproject.net.PortNumber;
import org.onosproject.net.host.HostEvent;
import org.onosproject.net.host.HostListener;
import org.onosproject.net.host.HostService;
import org.onosproject.net.host.HostServiceAdapter;
import org.onosproject.net.provider.ProviderId;
import java.util.Collections;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.reset;
import static org.easymock.EasyMock.verify;
/**
* Unit tests for the route manager.
*/
public class RouteManagerTest {
private static final ConnectPoint CP1 = new ConnectPoint(
DeviceId.deviceId("of:0000000000000001"),
PortNumber.portNumber(1));
private static final IpPrefix V4_PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24");
private static final IpPrefix V6_PREFIX1 = Ip6Prefix.valueOf("4000::/64");
private static final IpAddress V4_NEXT_HOP1 = Ip4Address.valueOf("192.168.10.1");
private static final IpAddress V4_NEXT_HOP2 = Ip4Address.valueOf("192.168.20.1");
private static final IpAddress V6_NEXT_HOP1 = Ip6Address.valueOf("1000::1");
private static final IpAddress V6_NEXT_HOP2 = Ip6Address.valueOf("2000::1");
private static final MacAddress MAC1 = MacAddress.valueOf("00:00:00:00:00:01");
private static final MacAddress MAC2 = MacAddress.valueOf("00:00:00:00:00:02");
private static final MacAddress MAC3 = MacAddress.valueOf("00:00:00:00:00:03");
private static final MacAddress MAC4 = MacAddress.valueOf("00:00:00:00:00:04");
private HostService hostService;
private RouteListener routeListener;
private HostListener hostListener;
private RouteManager routeManager;
@Before
public void setUp() throws Exception {
setUpHostService();
routeListener = createMock(RouteListener.class);
routeManager = new TestRouteManager();
routeManager.hostService = hostService;
LocalRouteStore routeStore = new LocalRouteStore();
routeStore.activate();
routeManager.routeStore = routeStore;
routeManager.activate();
routeManager.addListener(routeListener);
}
/**
* Sets up the host service with details of some hosts.
*/
private void setUpHostService() {
hostService = createMock(HostService.class);
hostService.addListener(anyObject(HostListener.class));
expectLastCall().andDelegateTo(new TestHostService()).anyTimes();
Host host1 = createHost(MAC1, V4_NEXT_HOP1);
expectHost(host1);
Host host2 = createHost(MAC2, V4_NEXT_HOP2);
expectHost(host2);
Host host3 = createHost(MAC3, V6_NEXT_HOP1);
expectHost(host3);
Host host4 = createHost(MAC4, V6_NEXT_HOP2);
expectHost(host4);
replay(hostService);
}
/**
* Sets expectations on the host service for a given host.
*
* @param host host
*/
private void expectHost(Host host) {
// Assume the host only has one IP address
IpAddress ip = host.ipAddresses().iterator().next();
expect(hostService.getHostsByIp(ip))
.andReturn(Sets.newHashSet(host)).anyTimes();
hostService.startMonitoringIp(ip);
expectLastCall().anyTimes();
}
/**
* Creates a host with the given parameters.
*
* @param macAddress MAC address
* @param ipAddress IP address
* @return new host
*/
private Host createHost(MacAddress macAddress, IpAddress ipAddress) {
return new DefaultHost(ProviderId.NONE, HostId.NONE, macAddress,
VlanId.NONE, new HostLocation(CP1, 1),
Sets.newHashSet(ipAddress));
}
/**
* Adds a route to the route service without expecting any specific events
* to be sent to the route listeners.
*
* @param route route to add
*/
private void addRoute(Route route) {
reset(routeListener);
routeListener.event(anyObject(RouteEvent.class));
expectLastCall().once();
replay(routeListener);
routeManager.update(Collections.singleton(route));
reset(routeListener);
}
/**
* Tests adding routes to the route manager.
*/
@Test
public void testIpv4RouteAdd() {
Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1);
testRouteAdd(route, resolvedRoute);
route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
resolvedRoute = new ResolvedRoute(route, MAC3);
testRouteAdd(route, resolvedRoute);
}
/**
* Tests adding a new route and verifies that the correct event was sent
* to the route listener.
*
* @param route route to add
* @param resolvedRoute resolved route that should be sent to the route
* listener
*/
private void testRouteAdd(Route route, ResolvedRoute resolvedRoute) {
reset(routeListener);
routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED, resolvedRoute));
replay(routeListener);
routeManager.update(Collections.singleton(route));
verify(routeListener);
}
/**
* Tests updating routes in the route manager.
*/
@Test
public void testRouteUpdate() {
Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Route updatedRoute = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP2);
ResolvedRoute updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2);
testRouteUpdated(route, updatedRoute, updatedResolvedRoute);
route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
updatedRoute = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP2);
updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC4);
testRouteUpdated(route, updatedRoute, updatedResolvedRoute);
}
/**
* Tests updating a route and verifies that the correct events are sent to
* the route listener.
*
* @param original original route
* @param updated updated route
* @param updatedResolvedRoute resolved route that is expected to be sent to
* the routelistener
*/
private void testRouteUpdated(Route original, Route updated,
ResolvedRoute updatedResolvedRoute) {
// First add the original route
addRoute(original);
routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, new ResolvedRoute(original, null)));
expectLastCall().once();
routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED, updatedResolvedRoute));
expectLastCall().once();
replay(routeListener);
routeManager.update(Collections.singleton(updated));
verify(routeListener);
}
/**
* Tests deleting routes from the route manager.
*/
@Test
public void testIpv4RouteDelete() {
Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
testDelete(route);
route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
testDelete(route);
}
/**
* Tests deleting a route and verifies that the correct event is sent to
* the route listener.
*
* @param route route to delete
*/
private void testDelete(Route route) {
addRoute(route);
RouteEvent withdrawRouteEvent = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
new ResolvedRoute(route, null));
reset(routeListener);
routeListener.event(withdrawRouteEvent);
replay(routeListener);
routeManager.withdraw(Collections.singleton(route));
verify(routeListener);
}
/**
* Tests adding a route entry with asynchronous HostService replies.
*/
@Test
public void testAsyncRouteAdd() {
Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
// Host service will reply with no hosts when asked
reset(hostService);
expect(hostService.getHostsByIp(anyObject(IpAddress.class))).andReturn(
Collections.emptySet()).anyTimes();
hostService.startMonitoringIp(V4_NEXT_HOP1);
replay(hostService);
// Initially when we add the route, no route event will be sent because
// the host is not known
replay(routeListener);
routeManager.update(Collections.singleton(route));
verify(routeListener);
// Now when we send the event, we expect the FIB update to be sent
reset(routeListener);
routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
new ResolvedRoute(route, MAC1)));
replay(routeListener);
// Send in the host event
Host host = createHost(MAC1, V4_NEXT_HOP1);
hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host));
verify(routeListener);
}
/**
* Test host service that stores a reference to the host listener.
*/
private class TestHostService extends HostServiceAdapter {
@Override
public void addListener(HostListener listener) {
hostListener = listener;
}
}
/**
* Test route manager that extends the real route manager and injects a test
* listener queue instead of the real listener queue.
*/
private static class TestRouteManager extends RouteManager {
@Override
ListenerQueue createListenerQueue(RouteListener listener) {
return new TestListenerQueue(listener);
}
}
/**
* Test listener queue that simply passes route events straight through to
* the route listener on the caller's thread.
*/
private static class TestListenerQueue implements ListenerQueue {
private final RouteListener listener;
public TestListenerQueue(RouteListener listener) {
this.listener = listener;
}
@Override
public void post(RouteEvent event) {
listener.event(event);
}
@Override
public void start() {
}
@Override
public void stop() {
}
}
}
......@@ -66,7 +66,7 @@ public class LocalRouteStore extends AbstractStore<RouteEvent, RouteStoreDelegat
private Map<IpAddress, MacAddress> nextHops = new ConcurrentHashMap<>();
@Activate
protected void activate() {
public void activate() {
routeTables = new ConcurrentHashMap<>();
routeTables.put(IPV4, new RouteTable());
......