Bug 2364: Migrated Binding MD-SAL to not use composites nodes
[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.assertNotNull;
4 import static org.junit.Assert.fail;
5 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_BAR_KEY;
6 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
7 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.path;
8
9 import org.opendaylight.controller.md.sal.binding.compat.HeliumRpcProviderRegistry;
10
11 import com.google.common.base.Throwables;
12 import java.util.Arrays;
13 import javassist.ClassPool;
14 import org.junit.Ignore;
15 import org.junit.Test;
16 import org.mockito.Mockito;
17 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMRpcProviderServiceAdapter;
18 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMRpcServiceAdapter;
19 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
20 import org.opendaylight.controller.md.sal.binding.test.AbstractSchemaAwareTest;
21 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMRpcRouter;
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.RpcConsumerRegistry;
25 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
26 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
27 import org.opendaylight.controller.sal.binding.codegen.RpcIsNotRoutedException;
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.binding.data.codec.gen.impl.DataObjectSerializerGenerator;
33 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
34 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
35 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
36 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
39 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41
42
43 public class RpcProviderRegistryTest  extends AbstractSchemaAwareTest {
44
45     private static InstanceIdentifier<TopLevelList> FOO_PATH = path(TOP_FOO_KEY);
46     private static InstanceIdentifier<TopLevelList> BAR_PATH = path(TOP_BAR_KEY);
47     private static RpcContextIdentifier ROUTING_CONTEXT = RpcContextIdentifier.contextFor(OpendaylightTestRoutedRpcService.class, TestContext.class);
48
49     private RpcProviderRegistry rpcRegistry;
50
51
52     @Override
53     protected Iterable<YangModuleInfo> getModuleInfos() {
54         try {
55             return Arrays.asList(
56                     BindingReflections.getModuleInfo(TopLevelList.class),
57                     BindingReflections.getModuleInfo(OpendaylightTestRoutedRpcService.class),
58                     BindingReflections.getModuleInfo(OpendaylightTestRpcServiceService.class));
59         } catch (final Exception e) {
60             throw Throwables.propagate(e);
61         }
62     }
63
64     @Override
65     protected void setupWithSchema(final SchemaContext context) {
66         final DataObjectSerializerGenerator generator = StreamWriterGenerator.create(JavassistUtils.forClassPool(ClassPool.getDefault()));
67         final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(generator);
68         final GeneratedClassLoadingStrategy classLoadingStrategy = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
69         final BindingToNormalizedNodeCodec codec = new BindingToNormalizedNodeCodec(classLoadingStrategy, null, codecRegistry);
70         final DOMRpcRouter domRpcRegistry = new DOMRpcRouter();
71         domRpcRegistry.onGlobalContextUpdated(context);
72         codec.onGlobalContextUpdated(context);
73         final RpcConsumerRegistry consumer = new BindingDOMRpcServiceAdapter(domRpcRegistry, codec);
74         final BindingDOMRpcProviderServiceAdapter provider = new BindingDOMRpcProviderServiceAdapter( domRpcRegistry,codec);
75         rpcRegistry = new HeliumRpcProviderRegistry(consumer,provider);
76     }
77
78     @Test
79     public void testGlobalRpcRegistrations() throws Exception {
80         final OpendaylightTestRpcServiceService one = Mockito.mock(OpendaylightTestRpcServiceService.class);
81         final OpendaylightTestRpcServiceService two = Mockito.mock(OpendaylightTestRpcServiceService.class);
82
83         final RpcRegistration<OpendaylightTestRpcServiceService> regOne = rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, one);
84         assertNotNull(regOne);
85         rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, two);
86         regOne.close();
87         final RpcRegistration<OpendaylightTestRpcServiceService> regTwo = rpcRegistry.addRpcImplementation(OpendaylightTestRpcServiceService.class, two);
88         assertNotNull(regTwo);
89     }
90
91
92     @Test
93     @Ignore
94     public void nonRoutedRegisteredAsRouted() {
95         final OpendaylightTestRpcServiceService one = Mockito.mock(OpendaylightTestRpcServiceService.class);
96         try {
97             final RoutedRpcRegistration<OpendaylightTestRpcServiceService> reg = rpcRegistry.addRoutedRpcImplementation(OpendaylightTestRpcServiceService.class, one);
98             reg.registerPath(null, BAR_PATH);
99             fail("RpcIsNotRoutedException should be thrown");
100         } catch (final RpcIsNotRoutedException e) {
101             assertNotNull(e.getMessage());
102         } catch (final Exception e) {
103             fail("RpcIsNotRoutedException should be thrown");
104         }
105
106     }
107
108 }

©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.