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.ArgumentMatchers.anySet;
14 import static org.mockito.ArgumentMatchers.eq;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
18 import com.google.common.collect.ImmutableSet;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
26 import org.opendaylight.mdsal.binding.api.RpcProviderService;
27 import org.opendaylight.openflowplugin.api.openflow.FlowGroupCacheManager;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
31 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
32 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
33 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
34 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
35 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yangtools.concepts.ObjectRegistration;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.binding.RpcService;
45 @RunWith(MockitoJUnitRunner.class)
46 public class RpcContextImplTest {
48 private static final int MAX_REQUESTS = 5;
49 private RpcContextImpl rpcContext;
52 private FlowGroupCacheManager flowGroupCacheManager;
54 private RpcProviderService rpcProviderRegistry;
56 private DeviceState deviceState;
58 private MessageSpy messageSpy;
60 private DeviceContext deviceContext;
62 private ObjectRegistration<TestRpcService> routedRpcReg;
65 private NotificationPublishService notificationPublishService;
67 private TestRpcService serviceInstance;
69 private DeviceInfo deviceInfo;
71 private ExtensionConverterProvider extensionConverterProvider;
73 private ConvertorExecutor convertorExecutor;
75 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
79 final NodeId nodeId = new NodeId("openflow:1");
80 nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
82 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
83 when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
84 when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
85 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
87 rpcContext = new RpcContextImpl(
91 extensionConverterProvider,
93 notificationPublishService, true, flowGroupCacheManager);
95 when(rpcProviderRegistry.registerRpcImplementation(eq(TestRpcService.class), eq(serviceInstance), anySet()))
96 .thenReturn(routedRpcReg);
100 public void testStoreOrFail() {
101 try (RpcContext rpcContext = new RpcContextImpl(
105 extensionConverterProvider,
107 notificationPublishService, true, flowGroupCacheManager)) {
108 final RequestContext<?> requestContext = rpcContext.createRequestContext();
109 assertNotNull(requestContext);
114 public void testStoreOrFailThatFails() {
115 try (RpcContext rpcContext = new RpcContextImpl(
119 extensionConverterProvider,
121 notificationPublishService, true, flowGroupCacheManager)) {
122 final RequestContext<?> requestContext = rpcContext.createRequestContext();
123 assertNull(requestContext);
128 public void testStoreAndCloseOrFail() {
129 try (RpcContext rpcContext = new RpcContextImpl(
133 extensionConverterProvider,
135 notificationPublishService, true, flowGroupCacheManager)) {
136 final RequestContext<?> requestContext = rpcContext.createRequestContext();
137 assertNotNull(requestContext);
138 requestContext.close();
139 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
143 public void testRegisterRpcServiceImplementation() {
144 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
145 verify(rpcProviderRegistry, Mockito.times(1)).registerRpcImplementation(TestRpcService.class, serviceInstance,
146 ImmutableSet.of(nodeInstanceIdentifier));
147 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
151 public void testLookupRpcService() {
152 when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
153 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
154 TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
155 assertEquals(serviceInstance,temp);
159 public void testClose() {
160 when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
161 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
163 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
167 * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned.
170 public void testCreateRequestContext1() {
171 when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(null);
172 assertEquals(rpcContext.createRequestContext(),null);
176 * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned.
180 public void testCreateRequestContext2() {
181 RequestContext temp = rpcContext.createRequestContext();
183 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
187 public void testUnregisterRpcServiceImpl() {
188 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
189 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
190 rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
191 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
194 //Stub for RpcService class.
195 public class TestRpcService implements RpcService {