8782046eeef3c13da933bc01ab8d68476b0a0664
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / impl / test / RpcProviderRegistryTest.java
1 package org.opendaylight.controller.md.sal.binding.impl.test;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertNull;
6 import static org.junit.Assert.fail;
7 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_BAR_KEY;
8 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
9 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.path;
10
11 import java.util.Set;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.TimeoutException;
15
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mockito;
19 import org.opendaylight.controller.md.sal.binding.test.AssertCollections;
20 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
21 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
24 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
25 import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
26 import org.opendaylight.controller.sal.binding.codegen.RpcIsNotRoutedException;
27 import org.opendaylight.controller.sal.binding.impl.RpcProviderRegistryImpl;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.OpendaylightTestRpcServiceService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.OpendaylightTestRoutedRpcService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.TestContext;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33
34 import com.google.common.util.concurrent.SettableFuture;
35
36
37 public class RpcProviderRegistryTest {
38
39     private static InstanceIdentifier<TopLevelList> FOO_PATH = path(TOP_FOO_KEY);
40     private static InstanceIdentifier<TopLevelList> BAR_PATH = path(TOP_BAR_KEY);
41     private static RpcContextIdentifier ROUTING_CONTEXT = RpcContextIdentifier.contextFor(OpendaylightTestRoutedRpcService.class, TestContext.class);
42
43     private RpcProviderRegistryImpl rpcRegistry;
44
45     @Before
46     public void setup() {
47         rpcRegistry = new RpcProviderRegistryImpl("test");
48     }
49
50     private static class TestListener implements RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>> {
51
52         final SettableFuture<RouteChange<RpcContextIdentifier, InstanceIdentifier<?>>> event = SettableFuture.create();
53         @Override
54         public void onRouteChange(
55                 final RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> change) {
56             event.set(change);
57         }
58     }
59
60     @Test
61     public void testGlobalRpcRegistrations() throws Exception {
62         OpendaylightTestRpcServiceService one = Mockito.mock(OpendaylightTestRpcServiceService.class);
63         OpendaylightTestRpcServiceService two = Mockito.mock(OpendaylightTestRpcServiceService.class);
64
65         RpcRegistration<OpendaylightTestRpcServiceService> regOne = rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, one);
66         assertNotNull(regOne);
67
68         try {
69             rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, two);
70         fail("Second call for registration of same RPC must throw IllegalStateException");
71         } catch (IllegalStateException e) {
72             assertNotNull(e.getMessage());
73         }
74
75         regOne.close();
76
77         RpcRegistration<OpendaylightTestRpcServiceService> regTwo = rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, two);
78         assertNotNull(regTwo);
79     }
80
81     @Test
82     public void routedRpcRegisteredUsingGlobalAsDefaultInstance() throws Exception {
83         OpendaylightTestRoutedRpcService def = Mockito.mock(OpendaylightTestRoutedRpcService.class);
84         rpcRegistry.addRpcImplementation(OpendaylightTestRoutedRpcService.class, def);
85         RpcRouter<OpendaylightTestRoutedRpcService> router = rpcRegistry.getRpcRouter(OpendaylightTestRoutedRpcService.class);
86         assertEquals(def, router.getDefaultService());
87     }
88
89     @Test
90     public void nonRoutedRegisteredAsRouted() {
91         OpendaylightTestRpcServiceService one = Mockito.mock(OpendaylightTestRpcServiceService.class);
92         try {
93             rpcRegistry.addRoutedRpcImplementation(OpendaylightTestRpcServiceService.class, one);
94             fail("RpcIsNotRoutedException should be thrown");
95         } catch (RpcIsNotRoutedException e) {
96             assertNotNull(e.getMessage());
97         } catch (Exception e) {
98             fail("RpcIsNotRoutedException should be thrown");
99         }
100
101     }
102
103     @Test
104     public void testRpcRouterInstance() throws Exception  {
105         OpendaylightTestRoutedRpcService def = Mockito.mock(OpendaylightTestRoutedRpcService.class);
106
107         RpcRouter<OpendaylightTestRoutedRpcService> router = rpcRegistry.getRpcRouter(OpendaylightTestRoutedRpcService.class);
108
109         assertEquals(OpendaylightTestRoutedRpcService.class, router.getServiceType());
110         assertNotNull(router.getInvocationProxy());
111         assertNull(router.getDefaultService());
112
113         AssertCollections.assertContains(router.getContexts(), TestContext.class);
114
115         RpcRegistration<OpendaylightTestRoutedRpcService> regDef = router.registerDefaultService(def);
116         assertNotNull(regDef);
117         assertEquals(OpendaylightTestRoutedRpcService.class,regDef.getServiceType());
118         assertEquals(def,regDef.getInstance());
119         assertEquals(def, router.getDefaultService());
120
121         regDef.close();
122         assertNull("Default instance should be null after closing registration",  router.getDefaultService());
123     }
124
125     @Test
126     public void testRoutedRpcPathChangeEvents() throws InterruptedException, TimeoutException, ExecutionException {
127         OpendaylightTestRoutedRpcService one = Mockito.mock(OpendaylightTestRoutedRpcService.class);
128         OpendaylightTestRoutedRpcService two = Mockito.mock(OpendaylightTestRoutedRpcService.class);
129         RoutedRpcRegistration<OpendaylightTestRoutedRpcService> regOne = rpcRegistry.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, one);
130         RoutedRpcRegistration<OpendaylightTestRoutedRpcService> regTwo = rpcRegistry.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, two);
131         assertNotNull(regOne);
132         assertNotNull(regTwo);
133
134         final TestListener addListener = new TestListener();
135         rpcRegistry.registerRouteChangeListener(addListener);
136         regOne.registerPath(TestContext.class, FOO_PATH);
137
138         RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> fooAddEvent = addListener.event.get(500, TimeUnit.MILLISECONDS);
139         Set<InstanceIdentifier<?>> announce = fooAddEvent.getAnnouncements().get(ROUTING_CONTEXT);
140         assertNotNull(announce);
141         AssertCollections.assertContains(announce, FOO_PATH);
142         AssertCollections.assertNotContains(announce, BAR_PATH);
143
144
145
146         final TestListener removeListener = new TestListener();
147         rpcRegistry.registerRouteChangeListener(removeListener);
148
149         regOne.unregisterPath(TestContext.class, FOO_PATH);
150
151         RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> fooRemoveEvent = removeListener.event.get(500, TimeUnit.MILLISECONDS);
152         Set<InstanceIdentifier<?>> removal = fooRemoveEvent.getRemovals().get(ROUTING_CONTEXT);
153         assertNotNull(removal);
154         AssertCollections.assertContains(removal, FOO_PATH);
155         AssertCollections.assertNotContains(removal, BAR_PATH);
156
157
158     }
159
160 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.