2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.sal.connector.remoterpc.impl;
11 import junit.framework.Assert;
12 import org.apache.felix.dm.Component;
13 import org.junit.Test;
14 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
15 import org.opendaylight.controller.clustering.services.IClusterServices;
16 import org.opendaylight.controller.sal.connector.api.RpcRouter;
17 import org.opendaylight.controller.sal.connector.remoterpc.api.RouteChangeListener;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
22 import java.util.EnumSet;
23 import java.util.HashSet;
25 import java.util.concurrent.ConcurrentMap;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
33 public class RoutingTableImplTest {
35 private IClusterGlobalServices ics = mock(IClusterGlobalServices.class);
36 private RoutingTableImpl rti = new RoutingTableImpl();
38 private final URI namespace = URI.create("http://cisco.com/example");
39 private final QName QNAME = new QName(namespace,"global");
41 ConcurrentMap concurrentMapMock = mock(ConcurrentMap.class);
45 public void testAddGlobalRoute() throws Exception {
46 ConcurrentMap concurrentMap = createRoutingTableCache();
48 Assert.assertNotNull(concurrentMap);
49 RpcRouter.RouteIdentifier<QName, QName, InstanceIdentifier> routeIdentifier = mock(RpcRouter.RouteIdentifier.class);
50 InstanceIdentifier identifier = mock(InstanceIdentifier.class);
51 when(routeIdentifier.getType()).thenReturn(QNAME);
52 when(routeIdentifier.getRoute()).thenReturn(identifier);
54 rti.addGlobalRoute(routeIdentifier, "172.27.12.1:5000");
56 Set<String> globalService = new HashSet<String>();
57 globalService.add("172.27.12.1:5000");
59 when(concurrentMap.get(routeIdentifier)).thenReturn(globalService);
60 ConcurrentMap latestCache = rti.getRoutingTableCache();
62 Assert.assertEquals(concurrentMap,latestCache);
64 Set<String> servicesGlobal = (Set<String>)latestCache.get(routeIdentifier);
65 Assert.assertEquals(servicesGlobal.size(),1);
67 Assert.assertEquals(servicesGlobal.iterator().next(),"172.27.12.1:5000");
72 public void testGetRoutes() throws Exception {
73 ConcurrentMap concurrentMap = createRoutingTableCache();
75 Assert.assertNotNull(concurrentMap);
76 RpcRouter.RouteIdentifier<QName, QName, InstanceIdentifier> routeIdentifier = mock(RpcRouter.RouteIdentifier.class);
77 InstanceIdentifier identifier = mock(InstanceIdentifier.class);
78 when(routeIdentifier.getContext()).thenReturn(QNAME);
79 when(routeIdentifier.getRoute()).thenReturn(identifier);
81 rti.addGlobalRoute(routeIdentifier, "172.27.12.1:5000");
83 Set<String> globalService = new HashSet<String>();
84 globalService.add("172.27.12.1:5000");
86 when(concurrentMap.get(routeIdentifier)).thenReturn(globalService);
87 ConcurrentMap latestCache = rti.getRoutingTableCache();
89 Assert.assertEquals(concurrentMap,latestCache);
91 Set<String> servicesGlobal = rti.getRoutes(routeIdentifier);
94 Assert.assertEquals(servicesGlobal.size(),1);
96 Assert.assertEquals(servicesGlobal.iterator().next(),"172.27.12.1:5000");
102 public void testRegisterRouteChangeListener() throws Exception {
103 Assert.assertEquals(rti.getRegisteredRouteChangeListeners().size(),0);
104 rti.registerRouteChangeListener(new RouteChangeListenerImpl());
106 Assert.assertEquals(rti.getRegisteredRouteChangeListeners().size(),1);
110 public void testRemoveGlobalRoute()throws Exception {
112 ConcurrentMap concurrentMap = createRoutingTableCache();
114 Assert.assertNotNull(concurrentMap);
115 RpcRouter.RouteIdentifier<QName, QName, InstanceIdentifier> routeIdentifier = mock(RpcRouter.RouteIdentifier.class);
116 InstanceIdentifier identifier = mock(InstanceIdentifier.class);
117 when(routeIdentifier.getContext()).thenReturn(QNAME);
118 when(routeIdentifier.getRoute()).thenReturn(identifier);
120 rti.addGlobalRoute(routeIdentifier, "172.27.12.1:5000");
122 Set<String> globalService = new HashSet<String>();
123 globalService.add("172.27.12.1:5000");
125 when(concurrentMap.get(routeIdentifier)).thenReturn(globalService);
126 ConcurrentMap latestCache = rti.getRoutingTableCache();
128 Assert.assertEquals(concurrentMap,latestCache);
130 Set<String> servicesGlobal = rti.getRoutes(routeIdentifier);
133 Assert.assertEquals(servicesGlobal.size(),1);
135 Assert.assertEquals(servicesGlobal.iterator().next(),"172.27.12.1:5000");
137 rti.removeGlobalRoute(routeIdentifier);
139 Assert.assertNotNull(rti.getRoutes(routeIdentifier));
144 private ConcurrentMap createRoutingTableCache() throws Exception {
147 Component c = mock(Component.class);
150 RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE)).thenReturn(false);
152 when(ics.createCache(RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(concurrentMapMock);
153 rti.setClusterGlobalServices(this.ics);
156 Assert.assertEquals(concurrentMapMock,rti.getRoutingTableCache() );
157 return concurrentMapMock;
163 public void testCreateRoutingTableCacheReturnExistingCache() throws Exception {
164 ConcurrentMap concurrentMap = createRoutingTableCache();
166 //OK here we should try creating again the cache but this time it should return the existing one
168 RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE)).thenReturn(true);
171 RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE)).thenReturn(concurrentMap);
175 Component c = mock(Component.class);
179 Assert.assertEquals(concurrentMap,rti.getRoutingTableCache());
187 private class RouteChangeListenerImpl<I,R> implements RouteChangeListener<I,R>{
190 public void onRouteUpdated(I key, R new_value) {
191 //To change body of implemented methods use File | Settings | File Templates.
195 public void onRouteDeleted(I key) {
196 //To change body of implemented methods use File | Settings | File Templates.