2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.binding.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.verify;
17 import java.util.ArrayList;
18 import java.util.List;
20 import javassist.ClassPool;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
25 import org.opendaylight.controller.sal.binding.api.rpc.RpcRoutingTable;
26 import org.opendaylight.controller.sal.binding.codegen.impl.RuntimeCodeGenerator;
27 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory;
28 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory.NotificationInvoker;
29 import org.opendaylight.controller.sal.binding.test.mock.BarListener;
30 import org.opendaylight.controller.sal.binding.test.mock.BarUpdate;
31 import org.opendaylight.controller.sal.binding.test.mock.FlowDelete;
32 import org.opendaylight.controller.sal.binding.test.mock.FooListener;
33 import org.opendaylight.controller.sal.binding.test.mock.FooService;
34 import org.opendaylight.controller.sal.binding.test.mock.FooUpdate;
35 import org.opendaylight.controller.sal.binding.test.mock.ReferencableObject;
36 import org.opendaylight.controller.sal.binding.test.mock.ReferencableObjectKey;
37 import org.opendaylight.controller.sal.binding.test.mock.SimpleInput;
38 import org.opendaylight.yangtools.yang.binding.Augmentation;
39 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
40 import org.opendaylight.yangtools.yang.binding.DataContainer;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 public class RuntimeCodeGeneratorTest {
46 private RuntimeCodeGenerator codeGenerator;
47 private NotificationInvokerFactory invokerFactory;
50 public void initialize() {
51 this.codeGenerator = new RuntimeCodeGenerator(ClassPool.getDefault());
52 this.invokerFactory = codeGenerator.getInvokerFactory();
56 public void testGenerateDirectProxy() {
57 FooService product = codeGenerator.getDirectProxyFor(FooService.class);
58 assertNotNull(product);
62 public void testGenerateRouter() throws Exception {
63 RpcRouter<FooService> product = codeGenerator.getRouterFor(FooService.class,"test");
64 assertNotNull(product);
65 assertNotNull(product.getInvocationProxy());
67 assertEquals("2 fields should be generated.", 2, product.getInvocationProxy().getClass().getFields().length);
69 verifyRouting(product);
73 public void testInvoker() throws Exception {
75 FooListenerImpl fooListener = new FooListenerImpl();
77 NotificationInvoker invokerFoo = invokerFactory.invokerFor(fooListener);
80 assertSame(fooListener,invokerFoo.getDelegate());
81 assertNotNull(invokerFoo.getSupportedNotifications());
82 assertEquals(1, invokerFoo.getSupportedNotifications().size());
83 assertNotNull(invokerFoo.getInvocationProxy());
85 FooUpdateImpl fooOne = new FooUpdateImpl();
86 invokerFoo.getInvocationProxy().onNotification(fooOne);
88 assertEquals(1, fooListener.receivedFoos.size());
89 assertSame(fooOne, fooListener.receivedFoos.get(0));
91 CompositeListenerImpl composite = new CompositeListenerImpl();
93 NotificationInvoker invokerComposite = invokerFactory.invokerFor(composite);
95 assertNotNull(invokerComposite.getSupportedNotifications());
96 assertEquals(3, invokerComposite.getSupportedNotifications().size());
97 assertNotNull(invokerComposite.getInvocationProxy());
99 invokerComposite.getInvocationProxy().onNotification(fooOne);
101 assertEquals(1, composite.receivedFoos.size());
102 assertSame(fooOne, composite.receivedFoos.get(0));
104 assertEquals(0, composite.receivedBars.size());
106 BarUpdateImpl barOne = new BarUpdateImpl();
108 invokerComposite.getInvocationProxy().onNotification(barOne);
110 assertEquals(1, composite.receivedFoos.size());
111 assertEquals(1, composite.receivedBars.size());
112 assertSame(barOne, composite.receivedBars.get(0));
116 private void verifyRouting(final RpcRouter<FooService> product) {
117 assertNotNull("Routing table should be initialized", product.getRoutingTable(BaseIdentity.class));
119 RpcRoutingTable<BaseIdentity, FooService> routingTable = product.getRoutingTable(BaseIdentity.class);
121 int servicesCount = 2;
122 int instancesPerService = 3;
124 InstanceIdentifier<?>[][] identifiers = identifiers(servicesCount, instancesPerService);
125 FooService service[] = new FooService[] { mock(FooService.class, "Instance 0"),
126 mock(FooService.class, "Instance 1") };
128 for (int i = 0; i < service.length; i++) {
129 for (InstanceIdentifier<?> instance : identifiers[i]) {
130 routingTable.updateRoute(instance, service[i]);
134 assertEquals("All instances should be registered.", servicesCount * instancesPerService, routingTable
135 .getRoutes().size());
137 SimpleInput[] instance_0_input = new SimpleInputImpl[] { new SimpleInputImpl(identifiers[0][0]),
138 new SimpleInputImpl(identifiers[0][1]), new SimpleInputImpl(identifiers[0][2]) };
140 SimpleInput[] instance_1_input = new SimpleInputImpl[] { new SimpleInputImpl(identifiers[1][0]),
141 new SimpleInputImpl(identifiers[1][1]), new SimpleInputImpl(identifiers[1][2]) };
143 // We test sending mock messages
145 product.getInvocationProxy().simple(instance_0_input[0]);
146 verify(service[0]).simple(instance_0_input[0]);
148 product.getInvocationProxy().simple(instance_0_input[1]);
149 product.getInvocationProxy().simple(instance_0_input[2]);
151 verify(service[0]).simple(instance_0_input[1]);
152 verify(service[0]).simple(instance_0_input[2]);
154 product.getInvocationProxy().simple(instance_1_input[0]);
156 // We should have call to instance 1
157 verify(service[1]).simple(instance_1_input[0]);
160 * Generated RPC service should throw illegalArgumentException
161 * with message if rpc input is null.
164 product.getInvocationProxy().simple(null);
165 fail("Generated RPC router should throw IllegalArgumentException on null input");
166 } catch (IllegalArgumentException e){
167 assertNotNull(e.getMessage());
172 * Generated RPC service should throw illegalArgumentException
173 * with message if rpc route is null.
176 SimpleInput withoutValue = new SimpleInputImpl(null);
177 product.getInvocationProxy().simple(withoutValue);
178 fail("Generated RPC router should throw IllegalArgumentException on null value for route");
179 } catch (IllegalArgumentException e){
180 assertNotNull(e.getMessage());
185 private InstanceIdentifier<?>[][] identifiers(final int serviceSize, final int instancesPerService) {
186 InstanceIdentifier<?>[][] ret = new InstanceIdentifier[serviceSize][];
188 for (int i = 0; i < serviceSize; i++) {
190 InstanceIdentifier<?>[] instanceIdentifiers = new InstanceIdentifier[instancesPerService];
191 ret[i] = instanceIdentifiers;
192 for (int id = 0; id < instancesPerService; id++) {
193 instanceIdentifiers[id] = referencableIdentifier(service * instancesPerService + id);
201 private InstanceIdentifier<?> referencableIdentifier(final int i) {
202 return InstanceIdentifier.builder(ReferencableObject.class, new ReferencableObjectKey(i)).build();
205 private static class SimpleInputImpl implements SimpleInput {
206 private final InstanceIdentifier<?> identifier;
208 public SimpleInputImpl(final InstanceIdentifier<?> _identifier) {
209 this.identifier = _identifier;
213 public <E extends Augmentation<SimpleInput>> E getAugmentation(final Class<E> augmentationType) {
218 public InstanceIdentifier<?> getIdentifier() {
219 return this.identifier;
223 public Class<? extends DataObject> getImplementedInterface() {
224 return SimpleInput.class;
228 private static class FooUpdateImpl implements FooUpdate {
230 public Class<? extends DataContainer> getImplementedInterface() {
231 return FooUpdate.class;
235 private static class BarUpdateImpl implements BarUpdate {
237 public Class<? extends DataContainer> getImplementedInterface() {
238 return BarUpdate.class;
242 public InstanceIdentifier<?> getInheritedIdentifier() {
247 private static class FooListenerImpl implements FooListener {
249 List<FooUpdate> receivedFoos = new ArrayList<>();
252 public void onFooUpdate(final FooUpdate notification) {
253 receivedFoos.add(notification);
258 private static class CompositeListenerImpl extends FooListenerImpl implements BarListener {
260 List<BarUpdate> receivedBars = new ArrayList<>();
261 List<FlowDelete> receivedDeletes = new ArrayList<>();
264 public void onBarUpdate(final BarUpdate notification) {
265 receivedBars.add(notification);
269 public void onFlowDelete(final FlowDelete notification) {
270 receivedDeletes.add(notification);