1 package org.opendaylight.controller.md.sal.binding.impl.test;
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;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.TimeoutException;
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;
34 import com.google.common.util.concurrent.SettableFuture;
37 public class RpcProviderRegistryTest {
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);
43 private RpcProviderRegistryImpl rpcRegistry;
47 rpcRegistry = new RpcProviderRegistryImpl("test");
50 private static class TestListener implements RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>> {
52 final SettableFuture<RouteChange<RpcContextIdentifier, InstanceIdentifier<?>>> event = SettableFuture.create();
54 public void onRouteChange(
55 final RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> change) {
61 public void testGlobalRpcRegistrations() throws Exception {
62 OpendaylightTestRpcServiceService one = Mockito.mock(OpendaylightTestRpcServiceService.class);
63 OpendaylightTestRpcServiceService two = Mockito.mock(OpendaylightTestRpcServiceService.class);
65 RpcRegistration<OpendaylightTestRpcServiceService> regOne = rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, one);
66 assertNotNull(regOne);
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());
77 RpcRegistration<OpendaylightTestRpcServiceService> regTwo = rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, two);
78 assertNotNull(regTwo);
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());
90 public void nonRoutedRegisteredAsRouted() {
91 OpendaylightTestRpcServiceService one = Mockito.mock(OpendaylightTestRpcServiceService.class);
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");
104 public void testRpcRouterInstance() throws Exception {
105 OpendaylightTestRoutedRpcService def = Mockito.mock(OpendaylightTestRoutedRpcService.class);
107 RpcRouter<OpendaylightTestRoutedRpcService> router = rpcRegistry.getRpcRouter(OpendaylightTestRoutedRpcService.class);
109 assertEquals(OpendaylightTestRoutedRpcService.class, router.getServiceType());
110 assertNotNull(router.getInvocationProxy());
111 assertNull(router.getDefaultService());
113 AssertCollections.assertContains(router.getContexts(), TestContext.class);
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());
122 assertNull("Default instance should be null after closing registration", router.getDefaultService());
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);
134 final TestListener addListener = new TestListener();
135 rpcRegistry.registerRouteChangeListener(addListener);
136 regOne.registerPath(TestContext.class, FOO_PATH);
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);
146 final TestListener removeListener = new TestListener();
147 rpcRegistry.registerRouteChangeListener(removeListener);
149 regOne.unregisterPath(TestContext.class, FOO_PATH);
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);