2 * Copyright (c) 2015 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.openflowplugin.impl.rpc;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
24 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
28 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
29 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
30 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
31 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
32 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.binding.RpcService;
42 @RunWith(MockitoJUnitRunner.class)
43 public class RpcContextImplTest {
45 private static final int MAX_REQUESTS = 5;
46 private RpcContextImpl rpcContext;
50 private RpcProviderRegistry rpcProviderRegistry;
52 private DeviceState deviceState;
54 private MessageSpy messageSpy;
56 private DeviceContext deviceContext;
58 private BindingAwareBroker.RoutedRpcRegistration<TestRpcService> routedRpcReg;
61 private NotificationPublishService notificationPublishService;
63 private TestRpcService serviceInstance;
65 private DeviceInfo deviceInfo;
67 private ExtensionConverterProvider extensionConverterProvider;
69 private ConvertorExecutor convertorExecutor;
71 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
75 final NodeId nodeId = new NodeId("openflow:1");
76 nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
78 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
79 when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
80 when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
81 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
83 rpcContext = new RpcContextImpl(
87 extensionConverterProvider,
89 notificationPublishService, true);
91 when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance))
92 .thenReturn(routedRpcReg);
96 public void testStoreOrFail() throws Exception {
97 try (RpcContext rpcContext = new RpcContextImpl(
101 extensionConverterProvider,
103 notificationPublishService, true)) {
104 final RequestContext<?> requestContext = rpcContext.createRequestContext();
105 assertNotNull(requestContext);
110 public void testStoreOrFailThatFails() throws Exception {
111 try (RpcContext rpcContext = new RpcContextImpl(
115 extensionConverterProvider,
117 notificationPublishService, true)) {
118 final RequestContext<?> requestContext = rpcContext.createRequestContext();
119 assertNull(requestContext);
124 public void testStoreAndCloseOrFail() throws Exception {
125 try (RpcContext rpcContext = new RpcContextImpl(
129 extensionConverterProvider,
131 notificationPublishService, true)) {
132 final RequestContext<?> requestContext = rpcContext.createRequestContext();
133 assertNotNull(requestContext);
134 requestContext.close();
135 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
139 public void testRegisterRpcServiceImplementation() {
140 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
141 verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
142 verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
143 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
147 public void testLookupRpcService() {
148 when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
149 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
150 TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
151 assertEquals(serviceInstance,temp);
155 public void testClose() {
156 Class<TestRpcService> serviceClass = TestRpcService.class;
157 when(routedRpcReg.getServiceType()).thenReturn(serviceClass);
158 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
160 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
164 * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned.
167 public void testCreateRequestContext1() throws InterruptedException {
168 when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(null);
169 assertEquals(rpcContext.createRequestContext(),null);
173 * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned.
177 public void testCreateRequestContext2() throws InterruptedException {
178 RequestContext temp = rpcContext.createRequestContext();
180 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
184 public void testUnregisterRpcServiceImpl() {
185 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
186 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
187 rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
188 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
191 //Stub for RpcService class.
192 public class TestRpcService implements RpcService {}