Bug 1280: Added option to automaticly create parents
[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.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;
32
33 import com.google.common.util.concurrent.SettableFuture;
34
35
36 public class RpcProviderRegistryTest {
37
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);
41
42     private RpcProviderRegistryImpl rpcRegistry;
43
44     @Before
45     public void setup() {
46         rpcRegistry = new RpcProviderRegistryImpl("test");
47     }
48
49     private static class TestListener implements RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>> {
50
51         final SettableFuture<RouteChange<RpcContextIdentifier, InstanceIdentifier<?>>> event = SettableFuture.create();
52         @Override
53         public void onRouteChange(
54                 final RouteChange<RpcContextIdentifier, InstanceIdentifier<?>> change) {
55             event.set(change);
56         }
57     }
58
59     @Test
60     public void testGlobalRpcRegistrations() throws Exception {
61         OpendaylightTestRpcServiceService one = Mockito.mock(OpendaylightTestRpcServiceService.class);
62         OpendaylightTestRpcServiceService two = Mockito.mock(OpendaylightTestRpcServiceService.class);
63
64         RpcRegistration<OpendaylightTestRpcServiceService> regOne = rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, one);
65         assertNotNull(regOne);
66
67         try {
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());
72         }
73
74         regOne.close();
75
76         RpcRegistration<OpendaylightTestRpcServiceService> regTwo = rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, two);
77         assertNotNull(regTwo);
78     }
79
80     @Test
81     public void testRpcRouterInstance() throws Exception  {
82         OpendaylightTestRoutedRpcService def = Mockito.mock(OpendaylightTestRoutedRpcService.class);
83
84         RpcRouter<OpendaylightTestRoutedRpcService> router = rpcRegistry.getRpcRouter(OpendaylightTestRoutedRpcService.class);
85
86         assertEquals(OpendaylightTestRoutedRpcService.class, router.getServiceType());
87         assertNotNull(router.getInvocationProxy());
88         assertNull(router.getDefaultService());
89
90         AssertCollections.assertContains(router.getContexts(), TestContext.class);
91
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());
97
98         regDef.close();
99         assertNull("Default instance should be null after closing registration",  router.getDefaultService());
100     }
101
102     @Test
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);
110
111         final TestListener addListener = new TestListener();
112         rpcRegistry.registerRouteChangeListener(addListener);
113         regOne.registerPath(TestContext.class, FOO_PATH);
114
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);
120
121
122
123         final TestListener removeListener = new TestListener();
124         rpcRegistry.registerRouteChangeListener(removeListener);
125
126         regOne.unregisterPath(TestContext.class, FOO_PATH);
127
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);
133
134
135     }
136
137 }