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.impl.RpcProviderRegistryImpl;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.OpendaylightTestRpcServiceService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.OpendaylightTestRoutedRpcService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.TestContext;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import com.google.common.util.concurrent.SettableFuture;
36 public class RpcProviderRegistryTest {
38 private static InstanceIdentifier<TopLevelList> FOO_PATH = path(TOP_FOO_KEY);
39 private static InstanceIdentifier<TopLevelList> BAR_PATH = path(TOP_BAR_KEY);
40 private static RpcContextIdentifier ROUTING_CONTEXT = RpcContextIdentifier.contextFor(OpendaylightTestRoutedRpcService.class, TestContext.class);
42 private RpcProviderRegistryImpl rpcRegistry;
46 rpcRegistry = new RpcProviderRegistryImpl("test");
49 private static class TestListener implements RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>> {
51 final SettableFuture<RouteChange<RpcContextIdentifier, InstanceIdentifier<?>>> event = SettableFuture.create();
53 public void onRouteChange(
54 final RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> change) {
60 public void testGlobalRpcRegistrations() throws Exception {
61 OpendaylightTestRpcServiceService one = Mockito.mock(OpendaylightTestRpcServiceService.class);
62 OpendaylightTestRpcServiceService two = Mockito.mock(OpendaylightTestRpcServiceService.class);
64 RpcRegistration<OpendaylightTestRpcServiceService> regOne = rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, one);
65 assertNotNull(regOne);
68 rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, two);
69 fail("Second call for registration of same RPC must throw IllegalStateException");
70 } catch (IllegalStateException e) {
71 assertNotNull(e.getMessage());
76 RpcRegistration<OpendaylightTestRpcServiceService> regTwo = rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, two);
77 assertNotNull(regTwo);
81 public void testRpcRouterInstance() throws Exception {
82 OpendaylightTestRoutedRpcService def = Mockito.mock(OpendaylightTestRoutedRpcService.class);
84 RpcRouter<OpendaylightTestRoutedRpcService> router = rpcRegistry.getRpcRouter(OpendaylightTestRoutedRpcService.class);
86 assertEquals(OpendaylightTestRoutedRpcService.class, router.getServiceType());
87 assertNotNull(router.getInvocationProxy());
88 assertNull(router.getDefaultService());
90 AssertCollections.assertContains(router.getContexts(), TestContext.class);
92 RpcRegistration<OpendaylightTestRoutedRpcService> regDef = router.registerDefaultService(def);
93 assertNotNull(regDef);
94 assertEquals(OpendaylightTestRoutedRpcService.class,regDef.getServiceType());
95 assertEquals(def,regDef.getInstance());
96 assertEquals(def, router.getDefaultService());
99 assertNull("Default instance should be null after closing registration", router.getDefaultService());
103 public void testRoutedRpcPathChangeEvents() throws InterruptedException, TimeoutException, ExecutionException {
104 OpendaylightTestRoutedRpcService one = Mockito.mock(OpendaylightTestRoutedRpcService.class);
105 OpendaylightTestRoutedRpcService two = Mockito.mock(OpendaylightTestRoutedRpcService.class);
106 RoutedRpcRegistration<OpendaylightTestRoutedRpcService> regOne = rpcRegistry.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, one);
107 RoutedRpcRegistration<OpendaylightTestRoutedRpcService> regTwo = rpcRegistry.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, two);
108 assertNotNull(regOne);
109 assertNotNull(regTwo);
111 final TestListener addListener = new TestListener();
112 rpcRegistry.registerRouteChangeListener(addListener);
113 regOne.registerPath(TestContext.class, FOO_PATH);
115 RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> fooAddEvent = addListener.event.get(500, TimeUnit.MILLISECONDS);
116 Set<InstanceIdentifier<?>> announce = fooAddEvent.getAnnouncements().get(ROUTING_CONTEXT);
117 assertNotNull(announce);
118 AssertCollections.assertContains(announce, FOO_PATH);
119 AssertCollections.assertNotContains(announce, BAR_PATH);
123 final TestListener removeListener = new TestListener();
124 rpcRegistry.registerRouteChangeListener(removeListener);
126 regOne.unregisterPath(TestContext.class, FOO_PATH);
128 RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> fooRemoveEvent = removeListener.event.get(500, TimeUnit.MILLISECONDS);
129 Set<InstanceIdentifier<?>> removal = fooRemoveEvent.getRemovals().get(ROUTING_CONTEXT);
130 assertNotNull(removal);
131 AssertCollections.assertContains(removal, FOO_PATH);
132 AssertCollections.assertNotContains(removal, BAR_PATH);