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;
24 import java.util.Iterator;
26 import java.util.concurrent.ConcurrentMap;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
34 public class RoutingTableImplTest {
36 private IClusterGlobalServices ics = mock(IClusterGlobalServices.class);
37 private RoutingTableImpl rti = new RoutingTableImpl();
39 private final URI namespace = URI.create("http://cisco.com/example");
40 private final QName QNAME = new QName(namespace,"global");
42 ConcurrentMap concurrentMapMock = mock(ConcurrentMap.class);
46 public void testAddGlobalRoute() throws Exception {
47 ConcurrentMap concurrentMap = createRoutingTableCache();
49 Assert.assertNotNull(concurrentMap);
50 RpcRouter.RouteIdentifier<QName, QName, InstanceIdentifier> routeIdentifier = mock(RpcRouter.RouteIdentifier.class);
51 InstanceIdentifier identifier = mock(InstanceIdentifier.class);
52 when(routeIdentifier.getType()).thenReturn(QNAME);
53 when(routeIdentifier.getRoute()).thenReturn(identifier);
55 rti.addGlobalRoute(routeIdentifier, "172.27.12.1:5000");
57 Set<String> globalService = new HashSet<String>();
58 globalService.add("172.27.12.1:5000");
60 when(concurrentMap.get(routeIdentifier)).thenReturn(globalService);
61 ConcurrentMap latestCache = rti.getRoutingTableCache();
63 Assert.assertEquals(concurrentMap,latestCache);
65 Set<String> servicesGlobal = (Set<String>)latestCache.get(routeIdentifier);
66 Assert.assertEquals(servicesGlobal.size(),1);
68 Assert.assertEquals(servicesGlobal.iterator().next(),"172.27.12.1:5000");
73 public void testGetRoutes() throws Exception {
74 ConcurrentMap concurrentMap = createRoutingTableCache();
76 Assert.assertNotNull(concurrentMap);
77 RpcRouter.RouteIdentifier<QName, QName, InstanceIdentifier> routeIdentifier = mock(RpcRouter.RouteIdentifier.class);
78 InstanceIdentifier identifier = mock(InstanceIdentifier.class);
79 when(routeIdentifier.getContext()).thenReturn(QNAME);
80 when(routeIdentifier.getRoute()).thenReturn(identifier);
82 rti.addGlobalRoute(routeIdentifier, "172.27.12.1:5000");
84 String globalService = "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);
95 Iterator<String> iterator = servicesGlobal.iterator();
96 while(iterator.hasNext()){
97 Assert.assertEquals(iterator.next(),"172.27.12.1:5000");
103 public void testRegisterRouteChangeListener() throws Exception {
104 Assert.assertEquals(rti.getRegisteredRouteChangeListeners().size(),0);
105 rti.registerRouteChangeListener(new RouteChangeListenerImpl());
107 Assert.assertEquals(rti.getRegisteredRouteChangeListeners().size(),0); //old should not work
108 //what about the new approach - using whiteboard pattern
109 rti.setRouteChangeListener(new RouteChangeListenerImpl());
111 Assert.assertEquals(rti.getRegisteredRouteChangeListeners().size(),1); //should not work
116 public void testRemoveGlobalRoute()throws Exception {
118 ConcurrentMap concurrentMap = createRoutingTableCache();
120 Assert.assertNotNull(concurrentMap);
121 RpcRouter.RouteIdentifier<QName, QName, InstanceIdentifier> routeIdentifier = mock(RpcRouter.RouteIdentifier.class);
122 InstanceIdentifier identifier = mock(InstanceIdentifier.class);
123 when(routeIdentifier.getContext()).thenReturn(QNAME);
124 when(routeIdentifier.getRoute()).thenReturn(identifier);
126 rti.addGlobalRoute(routeIdentifier, "172.27.12.1:5000");
128 String globalService = "172.27.12.1:5000";
130 when(concurrentMap.get(routeIdentifier)).thenReturn(globalService);
131 ConcurrentMap latestCache = rti.getRoutingTableCache();
133 Assert.assertEquals(concurrentMap,latestCache);
135 Set<String> servicesGlobal = rti.getRoutes(routeIdentifier);
138 Assert.assertEquals(servicesGlobal.size(),1);
140 Assert.assertEquals(servicesGlobal.iterator().next(),"172.27.12.1:5000");
142 rti.removeGlobalRoute(routeIdentifier);
144 Assert.assertNotNull(rti.getRoutes(routeIdentifier));
149 private ConcurrentMap createRoutingTableCache() throws Exception {
152 Component c = mock(Component.class);
155 RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE)).thenReturn(false);
157 when(ics.createCache(RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(concurrentMapMock);
158 rti.setClusterGlobalServices(this.ics);
161 Assert.assertEquals(concurrentMapMock,rti.getRoutingTableCache() );
162 return concurrentMapMock;
168 public void testCreateRoutingTableCacheReturnExistingCache() throws Exception {
169 ConcurrentMap concurrentMap = createRoutingTableCache();
171 //OK here we should try creating again the cache but this time it should return the existing one
173 RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE)).thenReturn(true);
176 RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE)).thenReturn(concurrentMap);
180 Component c = mock(Component.class);
184 Assert.assertEquals(concurrentMap,rti.getRoutingTableCache());
192 private class RouteChangeListenerImpl<I,R> implements RouteChangeListener<I,R>{
195 public void onRouteUpdated(I key, R new_value) {
196 //To change body of implemented methods use File | Settings | File Templates.
200 public void onRouteDeleted(I key) {
201 //To change body of implemented methods use File | Settings | File Templates.