X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fremoterpc-routingtable%2Fimplementation%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fconnector%2Fremoterpc%2Fimpl%2FRoutingTableImplTest.java;h=1ff49c1543feb3c59b81c027eac81e47f19b3a02;hb=22000c97031a397b247a75ede2383f6ae792efc9;hp=0987df595689176b2d734da6c048ef00a909b2b0;hpb=7eb31665534a5e8c61b4cb4ae6caf4030a3dfd36;p=controller.git diff --git a/opendaylight/md-sal/remoterpc-routingtable/implementation/src/test/java/org/opendaylight/controller/sal/connector/remoterpc/impl/RoutingTableImplTest.java b/opendaylight/md-sal/remoterpc-routingtable/implementation/src/test/java/org/opendaylight/controller/sal/connector/remoterpc/impl/RoutingTableImplTest.java index 0987df5956..1ff49c1543 100644 --- a/opendaylight/md-sal/remoterpc-routingtable/implementation/src/test/java/org/opendaylight/controller/sal/connector/remoterpc/impl/RoutingTableImplTest.java +++ b/opendaylight/md-sal/remoterpc-routingtable/implementation/src/test/java/org/opendaylight/controller/sal/connector/remoterpc/impl/RoutingTableImplTest.java @@ -8,7 +8,22 @@ package org.opendaylight.controller.sal.connector.remoterpc.impl; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.when; + +import java.net.URI; +import java.util.EnumSet; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + import junit.framework.Assert; + import org.apache.felix.dm.Component; import org.junit.After; import org.junit.Before; @@ -17,314 +32,303 @@ import org.opendaylight.controller.clustering.services.IClusterGlobalServices; import org.opendaylight.controller.clustering.services.IClusterServices; import org.opendaylight.controller.sal.connector.api.RpcRouter; import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTable; -import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTableException; -import org.opendaylight.controller.sal.connector.remoterpc.api.SystemException; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; -import java.net.URI; -import java.util.EnumSet; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.Set; -import java.util.concurrent.*; - -import static org.mockito.Mockito.*; - public class RoutingTableImplTest { - private final URI namespace = URI.create("http://cisco.com/example"); - private final QName QNAME = new QName(namespace, "global"); - - private IClusterGlobalServices clusterService; - private RoutingTableImpl, String> routingTable; - ConcurrentMap mockGlobalRpcCache; - ConcurrentMap mockRpcCache; - - @Before - public void setUp() throws Exception{ - clusterService = mock(IClusterGlobalServices.class); - routingTable = new RoutingTableImpl, String>(); - mockGlobalRpcCache = new ConcurrentHashMap<>(); - mockRpcCache = new ConcurrentHashMap<>(); - createRoutingTableCache(); - } - - @After - public void tearDown(){ - reset(clusterService); - mockGlobalRpcCache = null; - mockRpcCache = null; - } + private final URI namespace = URI.create("http://cisco.com/example"); + private final QName QNAME = new QName(namespace, "global"); + + private IClusterGlobalServices clusterService; + private RoutingTableImpl, String> routingTable; + ConcurrentMap mockGlobalRpcCache; + ConcurrentMap mockRpcCache; + + @Before + public void setUp() throws Exception{ + clusterService = mock(IClusterGlobalServices.class); + routingTable = new RoutingTableImpl, String>(); + mockGlobalRpcCache = new ConcurrentHashMap<>(); + mockRpcCache = new ConcurrentHashMap<>(); + createRoutingTableCache(); + } - @Test - public void addGlobalRoute_ValidArguments_ShouldAdd() throws Exception { + @After + public void tearDown(){ + reset(clusterService); + mockGlobalRpcCache = null; + mockRpcCache = null; + } - Assert.assertNotNull(mockGlobalRpcCache); - RpcRouter.RouteIdentifier routeIdentifier = getRouteIdentifier(); + @Test + public void addGlobalRoute_ValidArguments_ShouldAdd() throws Exception { - final String expectedRoute = "172.27.12.1:5000"; - routingTable.addGlobalRoute(routeIdentifier, expectedRoute); + Assert.assertNotNull(mockGlobalRpcCache); + RpcRouter.RouteIdentifier routeIdentifier = getRouteIdentifier(); - ConcurrentMap latestCache = routingTable.getGlobalRpcCache(); - Assert.assertEquals(mockGlobalRpcCache, latestCache); - Assert.assertEquals(expectedRoute, latestCache.get(routeIdentifier)); - } + final String expectedRoute = "172.27.12.1:5000"; + routingTable.addGlobalRoute(routeIdentifier, expectedRoute); - @Test (expected = RoutingTable.DuplicateRouteException.class) - public void addGlobalRoute_DuplicateRoute_ShouldThrow() throws Exception{ + ConcurrentMap latestCache = routingTable.getGlobalRpcCache(); + Assert.assertEquals(mockGlobalRpcCache, latestCache); + Assert.assertEquals(expectedRoute, latestCache.get(routeIdentifier)); + } - Assert.assertNotNull(mockGlobalRpcCache); + @Test (expected = RoutingTable.DuplicateRouteException.class) + public void addGlobalRoute_DuplicateRoute_ShouldThrow() throws Exception{ - RpcRouter.RouteIdentifier routeIdentifier = getRouteIdentifier(); - routingTable.addGlobalRoute(routeIdentifier, new String()); - routingTable.addGlobalRoute(routeIdentifier, new String()); - } + Assert.assertNotNull(mockGlobalRpcCache); - @Test - public void getGlobalRoute_ExistingRouteId_ShouldReturnRoute() throws Exception { + RpcRouter.RouteIdentifier routeIdentifier = getRouteIdentifier(); + routingTable.addGlobalRoute(routeIdentifier, new String()); + routingTable.addGlobalRoute(routeIdentifier, new String()); + } - Assert.assertNotNull(mockGlobalRpcCache); - RpcRouter.RouteIdentifier routeIdentifier = getRouteIdentifier(); - String expectedRoute = "172.27.12.1:5000"; + @Test + public void getGlobalRoute_ExistingRouteId_ShouldReturnRoute() throws Exception { - routingTable.addGlobalRoute(routeIdentifier, expectedRoute); + Assert.assertNotNull(mockGlobalRpcCache); + RpcRouter.RouteIdentifier routeIdentifier = getRouteIdentifier(); + String expectedRoute = "172.27.12.1:5000"; - String actualRoute = (String) routingTable.getGlobalRoute(routeIdentifier); - Assert.assertEquals(expectedRoute, actualRoute); - } + routingTable.addGlobalRoute(routeIdentifier, expectedRoute); - @Test - public void getGlobalRoute_NonExistentRouteId_ShouldReturnNull() throws Exception { + String actualRoute = routingTable.getGlobalRoute(routeIdentifier); + Assert.assertEquals(expectedRoute, actualRoute); + } - Assert.assertNotNull(mockGlobalRpcCache); - RpcRouter.RouteIdentifier routeIdentifier = getRouteIdentifier(); + @Test + public void getGlobalRoute_NonExistentRouteId_ShouldReturnNull() throws Exception { - String actualRoute = (String) routingTable.getGlobalRoute(routeIdentifier); - Assert.assertNull(actualRoute); - } + Assert.assertNotNull(mockGlobalRpcCache); + RpcRouter.RouteIdentifier routeIdentifier = getRouteIdentifier(); - @Test - public void removeGlobalRoute_ExistingRouteId_ShouldRemove() throws Exception { + String actualRoute = routingTable.getGlobalRoute(routeIdentifier); + Assert.assertNull(actualRoute); + } - Assert.assertNotNull(mockGlobalRpcCache); - RpcRouter.RouteIdentifier routeIdentifier = getRouteIdentifier(); + @Test + public void removeGlobalRoute_ExistingRouteId_ShouldRemove() throws Exception { - ConcurrentMap cache = routingTable.getGlobalRpcCache(); - Assert.assertTrue(cache.size() == 0); - routingTable.addGlobalRoute(routeIdentifier, "172.27.12.1:5000"); - Assert.assertTrue(cache.size() == 1); + Assert.assertNotNull(mockGlobalRpcCache); + RpcRouter.RouteIdentifier routeIdentifier = getRouteIdentifier(); - routingTable.removeGlobalRoute(routeIdentifier); - Assert.assertTrue(cache.size() == 0); + ConcurrentMap cache = routingTable.getGlobalRpcCache(); + Assert.assertTrue(cache.size() == 0); + routingTable.addGlobalRoute(routeIdentifier, "172.27.12.1:5000"); + Assert.assertTrue(cache.size() == 1); - } + routingTable.removeGlobalRoute(routeIdentifier); + Assert.assertTrue(cache.size() == 0); - @Test - public void removeGlobalRoute_NonExistentRouteId_ShouldDoNothing() throws Exception { + } - Assert.assertNotNull(mockGlobalRpcCache); - RpcRouter.RouteIdentifier routeIdentifier = getRouteIdentifier(); + @Test + public void removeGlobalRoute_NonExistentRouteId_ShouldDoNothing() throws Exception { - ConcurrentMap cache = routingTable.getGlobalRpcCache(); - Assert.assertTrue(cache.size() == 0); + Assert.assertNotNull(mockGlobalRpcCache); + RpcRouter.RouteIdentifier routeIdentifier = getRouteIdentifier(); - routingTable.removeGlobalRoute(routeIdentifier); - Assert.assertTrue(cache.size() == 0); + ConcurrentMap cache = routingTable.getGlobalRpcCache(); + Assert.assertTrue(cache.size() == 0); - } + routingTable.removeGlobalRoute(routeIdentifier); + Assert.assertTrue(cache.size() == 0); - @Test - public void addRoute_ForNewRouteId_ShouldAddRoute() throws Exception { - Assert.assertTrue(mockRpcCache.size() == 0); + } - RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); + @Test + public void addRoute_ForNewRouteId_ShouldAddRoute() throws Exception { + Assert.assertTrue(mockRpcCache.size() == 0); - routingTable.addRoute(routeId, new String()); - Assert.assertTrue(mockRpcCache.size() == 1); + RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); - Set routes = routingTable.getRoutes(routeId); - Assert.assertEquals(1, routes.size()); - } + routingTable.addRoute(routeId, new String()); + Assert.assertTrue(mockRpcCache.size() == 1); - @Test - public void addRoute_ForExistingRouteId_ShouldAppendRoute() throws Exception { + Set routes = routingTable.getRoutes(routeId); + Assert.assertEquals(1, routes.size()); + } - Assert.assertTrue(mockRpcCache.size() == 0); + @Test + public void addRoute_ForExistingRouteId_ShouldAppendRoute() throws Exception { - RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); + Assert.assertTrue(mockRpcCache.size() == 0); - String route_1 = "10.0.0.1:5955"; - String route_2 = "10.0.0.2:5955"; + RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); - routingTable.addRoute(routeId, route_1); - routingTable.addRoute(routeId, route_2); + String route_1 = "10.0.0.1:5955"; + String route_2 = "10.0.0.2:5955"; - Assert.assertTrue(mockRpcCache.size() == 1); + routingTable.addRoute(routeId, route_1); + routingTable.addRoute(routeId, route_2); - Set routes = routingTable.getRoutes(routeId); - Assert.assertEquals(2, routes.size()); - Assert.assertTrue(routes.contains(route_1)); - Assert.assertTrue(routes.contains(route_2)); - } + Assert.assertTrue(mockRpcCache.size() == 1); - @Test - public void addRoute_UsingMultipleThreads_ShouldNotOverwrite(){ - ExecutorService threadPool = Executors.newCachedThreadPool(); + Set routes = routingTable.getRoutes(routeId); + Assert.assertEquals(2, routes.size()); + Assert.assertTrue(routes.contains(route_1)); + Assert.assertTrue(routes.contains(route_2)); + } - int numOfRoutesToAdd = 100; - String routePrefix_1 = "10.0.0.1:555"; - RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); - threadPool.submit(addRoutes(numOfRoutesToAdd, routePrefix_1, routeId)); - String routePrefix_2 = "10.0.0.1:556"; - threadPool.submit(addRoutes(numOfRoutesToAdd, routePrefix_2, routeId)); + @Test + public void addRoute_UsingMultipleThreads_ShouldNotOverwrite(){ + ExecutorService threadPool = Executors.newCachedThreadPool(); + + int numOfRoutesToAdd = 100; + String routePrefix_1 = "10.0.0.1:555"; + RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); + threadPool.submit(addRoutes(numOfRoutesToAdd, routePrefix_1, routeId)); + String routePrefix_2 = "10.0.0.1:556"; + threadPool.submit(addRoutes(numOfRoutesToAdd, routePrefix_2, routeId)); + + // wait for all tasks to complete; timeout in 10 sec + threadPool.shutdown(); + try { + threadPool.awaitTermination(10, TimeUnit.SECONDS); // + } catch (InterruptedException e) { + e.printStackTrace(); + } - // wait for all tasks to complete; timeout in 10 sec - threadPool.shutdown(); - try { - threadPool.awaitTermination(10, TimeUnit.SECONDS); // - } catch (InterruptedException e) { - e.printStackTrace(); + Assert.assertEquals(2*numOfRoutesToAdd, routingTable.getRoutes(routeId).size()); } - Assert.assertEquals(2*numOfRoutesToAdd, routingTable.getRoutes(routeId).size()); - } - - @Test(expected = NullPointerException.class) - public void addRoute_NullRouteId_shouldThrowNpe() throws Exception { + @Test(expected = NullPointerException.class) + public void addRoute_NullRouteId_shouldThrowNpe() throws Exception { - routingTable.addRoute(null, new String()); - } + routingTable.addRoute(null, new String()); + } - @Test(expected = NullPointerException.class) - public void addRoute_NullRoute_shouldThrowNpe() throws Exception{ + @Test(expected = NullPointerException.class) + public void addRoute_NullRoute_shouldThrowNpe() throws Exception{ - routingTable.addRoute(getRouteIdentifier(), null); - } + routingTable.addRoute(getRouteIdentifier(), null); + } - @Test (expected = UnsupportedOperationException.class) - public void getRoutes_Call_ShouldReturnImmutableCopy() throws Exception{ - Assert.assertNotNull(routingTable); - RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); - routingTable.addRoute(routeId, new String()); + @Test (expected = UnsupportedOperationException.class) + public void getRoutes_Call_ShouldReturnImmutableCopy() throws Exception{ + Assert.assertNotNull(routingTable); + RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); + routingTable.addRoute(routeId, new String()); - Set routes = routingTable.getRoutes(routeId); //returns Immutable Set + Set routes = routingTable.getRoutes(routeId); //returns Immutable Set - routes.add(new String()); //can not be modified; should throw - } + routes.add(new String()); //can not be modified; should throw + } - @Test - public void getRoutes_With2RoutesFor1RouteId_ShouldReturnASetWithSize2() throws Exception{ - Assert.assertNotNull(routingTable); - RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); - routingTable.addRoute(routeId, "10.0.0.1:5555"); - routingTable.addRoute(routeId, "10.0.0.2:5555"); + @Test + public void getRoutes_With2RoutesFor1RouteId_ShouldReturnASetWithSize2() throws Exception{ + Assert.assertNotNull(routingTable); + RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); + routingTable.addRoute(routeId, "10.0.0.1:5555"); + routingTable.addRoute(routeId, "10.0.0.2:5555"); - Set routes = routingTable.getRoutes(routeId); //returns Immutable Set + Set routes = routingTable.getRoutes(routeId); //returns Immutable Set - Assert.assertEquals(2, routes.size()); - } + Assert.assertEquals(2, routes.size()); + } - @Test - public void getLastAddedRoute_WhenMultipleRoutesExists_ShouldReturnLatestRoute() - throws Exception { + @Test + public void getLastAddedRoute_WhenMultipleRoutesExists_ShouldReturnLatestRoute() + throws Exception { - Assert.assertNotNull(routingTable); - RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); - String route_1 = "10.0.0.1:5555"; - String route_2 = "10.0.0.2:5555"; - routingTable.addRoute(routeId, route_1); - routingTable.addRoute(routeId, route_2); + Assert.assertNotNull(routingTable); + RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); + String route_1 = "10.0.0.1:5555"; + String route_2 = "10.0.0.2:5555"; + routingTable.addRoute(routeId, route_1); + routingTable.addRoute(routeId, route_2); - Assert.assertEquals(route_2, routingTable.getLastAddedRoute(routeId)); - } + Assert.assertEquals(route_2, routingTable.getLastAddedRoute(routeId)); + } - @Test - public void removeRoute_WhenMultipleRoutesExist_RemovesGivenRoute() throws Exception{ - Assert.assertNotNull(routingTable); - RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); - String route_1 = "10.0.0.1:5555"; - String route_2 = "10.0.0.2:5555"; + @Test + public void removeRoute_WhenMultipleRoutesExist_RemovesGivenRoute() throws Exception{ + Assert.assertNotNull(routingTable); + RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); + String route_1 = "10.0.0.1:5555"; + String route_2 = "10.0.0.2:5555"; - routingTable.addRoute(routeId, route_1); - routingTable.addRoute(routeId, route_2); + routingTable.addRoute(routeId, route_1); + routingTable.addRoute(routeId, route_2); - Assert.assertEquals(2, routingTable.getRoutes(routeId).size()); + Assert.assertEquals(2, routingTable.getRoutes(routeId).size()); - routingTable.removeRoute(routeId, route_1); - Assert.assertEquals(1, routingTable.getRoutes(routeId).size()); + routingTable.removeRoute(routeId, route_1); + Assert.assertEquals(1, routingTable.getRoutes(routeId).size()); - } + } - @Test - public void removeRoute_WhenOnlyOneRouteExists_RemovesRouteId() throws Exception{ - Assert.assertNotNull(routingTable); - RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); - String route_1 = "10.0.0.1:5555"; + @Test + public void removeRoute_WhenOnlyOneRouteExists_RemovesRouteId() throws Exception{ + Assert.assertNotNull(routingTable); + RpcRouter.RouteIdentifier routeId = getRouteIdentifier(); + String route_1 = "10.0.0.1:5555"; - routingTable.addRoute(routeId, route_1); - Assert.assertEquals(1, routingTable.getRoutes(routeId).size()); + routingTable.addRoute(routeId, route_1); + Assert.assertEquals(1, routingTable.getRoutes(routeId).size()); - routingTable.removeRoute(routeId, route_1); - ConcurrentMap cache = routingTable.getRpcCache(); - Assert.assertFalse(cache.containsKey(routeId)); + routingTable.removeRoute(routeId, route_1); + ConcurrentMap cache = routingTable.getRpcCache(); + Assert.assertFalse(cache.containsKey(routeId)); - } + } - /* - * Private helper methods - */ - private void createRoutingTableCache() throws Exception { + /* + * Private helper methods + */ + private void createRoutingTableCache() throws Exception { - //here init - Component c = mock(Component.class); + //here init + Component c = mock(Component.class); - when(clusterService.existCache( - RoutingTableImpl.GLOBALRPC_CACHE)).thenReturn(false); + when(clusterService.existCache( + RoutingTableImpl.GLOBALRPC_CACHE)).thenReturn(false); - when(clusterService.createCache(RoutingTableImpl.GLOBALRPC_CACHE, - EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))). - thenReturn(mockGlobalRpcCache); + when(clusterService.createCache(RoutingTableImpl.GLOBALRPC_CACHE, + EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))). + thenReturn(mockGlobalRpcCache); - when(clusterService.existCache( - RoutingTableImpl.RPC_CACHE)).thenReturn(false); + when(clusterService.existCache( + RoutingTableImpl.RPC_CACHE)).thenReturn(false); - when(clusterService.createCache(RoutingTableImpl.RPC_CACHE, - EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))). - thenReturn(mockRpcCache); + when(clusterService.createCache(RoutingTableImpl.RPC_CACHE, + EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))). + thenReturn(mockRpcCache); - doNothing().when(clusterService).tbegin(); - doNothing().when(clusterService).tcommit(); + doNothing().when(clusterService).tbegin(); + doNothing().when(clusterService).tcommit(); - routingTable.setClusterGlobalServices(this.clusterService); - routingTable.init(c); + routingTable.setClusterGlobalServices(this.clusterService); + routingTable.init(c); - Assert.assertEquals(mockGlobalRpcCache, routingTable.getGlobalRpcCache()); - Assert.assertEquals(mockRpcCache, routingTable.getRpcCache()); - } + Assert.assertEquals(mockGlobalRpcCache, routingTable.getGlobalRpcCache()); + Assert.assertEquals(mockRpcCache, routingTable.getRpcCache()); + } - private RpcRouter.RouteIdentifier getRouteIdentifier(){ - RpcRouter.RouteIdentifier routeIdentifier = mock(RpcRouter.RouteIdentifier.class); - InstanceIdentifier identifier = mock(InstanceIdentifier.class); - when(routeIdentifier.getType()).thenReturn(QNAME); - when(routeIdentifier.getRoute()).thenReturn(identifier); + private RpcRouter.RouteIdentifier getRouteIdentifier(){ + RpcRouter.RouteIdentifier routeIdentifier = mock(RpcRouter.RouteIdentifier.class); + InstanceIdentifier identifier = mock(InstanceIdentifier.class); + when(routeIdentifier.getType()).thenReturn(QNAME); + when(routeIdentifier.getRoute()).thenReturn(identifier); - return routeIdentifier; - } + return routeIdentifier; + } - private Runnable addRoutes(final int numRoutes, final String routePrefix, final RpcRouter.RouteIdentifier routeId){ - return new Runnable() { - @Override - public void run() { - for (int i=0;i