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.openflowplugin.api.openflow.device.DeviceContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
27 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
28 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
30 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
31 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.RpcService;
41 @RunWith(MockitoJUnitRunner.class)
42 public class RpcContextImplTest {
44 private static final int MAX_REQUESTS = 5;
45 private RpcContextImpl rpcContext;
49 private BindingAwareBroker.ProviderContext rpcProviderRegistry;
51 private DeviceState deviceState;
53 private MessageSpy messageSpy;
55 private DeviceContext deviceContext;
57 private BindingAwareBroker.RoutedRpcRegistration<TestRpcService> routedRpcReg;
59 private Class<TestRpcService> serviceClass;
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(deviceContext.getDeviceState()).thenReturn(deviceState);
79 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
80 when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
81 when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
82 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
84 rpcContext = new RpcContextImpl(
88 extensionConverterProvider,
90 notificationPublishService, true);
92 when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance))
93 .thenReturn(routedRpcReg);
97 public void testStoreOrFail() throws Exception {
98 try (RpcContext rpcContext = new RpcContextImpl(
102 extensionConverterProvider,
104 notificationPublishService, true)) {
105 final RequestContext<?> requestContext = rpcContext.createRequestContext();
106 assertNotNull(requestContext);
111 public void testStoreOrFailThatFails() throws Exception {
112 try (RpcContext rpcContext = new RpcContextImpl(
116 extensionConverterProvider,
118 notificationPublishService, true)) {
119 final RequestContext<?> requestContext = rpcContext.createRequestContext();
120 assertNull(requestContext);
125 public void testStoreAndCloseOrFail() throws Exception {
126 try (RpcContext rpcContext = new RpcContextImpl(
130 extensionConverterProvider,
132 notificationPublishService, true)) {
133 final RequestContext<?> requestContext = rpcContext.createRequestContext();
134 assertNotNull(requestContext);
135 requestContext.close();
136 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
140 public void testRegisterRpcServiceImplementation() {
141 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
142 verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
143 verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
144 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
148 public void testLookupRpcService() {
149 when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
150 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
151 TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
152 assertEquals(serviceInstance,temp);
156 public void testClose() {
157 serviceClass = TestRpcService.class;
158 when(routedRpcReg.getServiceType()).thenReturn(serviceClass);
159 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
161 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
165 * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned.
168 public void testCreateRequestContext1() throws InterruptedException {
169 when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(null);
170 assertEquals(rpcContext.createRequestContext(),null);
174 * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned.
178 public void testCreateRequestContext2() throws InterruptedException {
179 RequestContext temp = rpcContext.createRequestContext();
181 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
185 public void testUnregisterRpcServiceImpl() {
186 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
187 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
188 rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
189 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
192 //Stub for RpcService class.
193 public class TestRpcService implements RpcService {}