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.device.DeviceContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
30 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
31 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
32 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
33 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
34 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
39 import org.opendaylight.yangtools.concepts.ObjectRegistration;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.binding.RpcService;
44 @RunWith(MockitoJUnitRunner.class)
45 public class RpcContextImplTest {
47 private static final int MAX_REQUESTS = 5;
48 private RpcContextImpl rpcContext;
52 private RpcProviderService rpcProviderRegistry;
54 private DeviceState deviceState;
56 private MessageSpy messageSpy;
58 private DeviceContext deviceContext;
60 private ObjectRegistration<TestRpcService> routedRpcReg;
63 private NotificationPublishService notificationPublishService;
65 private TestRpcService serviceInstance;
67 private DeviceInfo deviceInfo;
69 private ExtensionConverterProvider extensionConverterProvider;
71 private ConvertorExecutor convertorExecutor;
73 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
77 final NodeId nodeId = new NodeId("openflow:1");
78 nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
80 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
81 when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
82 when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
83 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
85 rpcContext = new RpcContextImpl(
89 extensionConverterProvider,
91 notificationPublishService, true);
93 when(rpcProviderRegistry.registerRpcImplementation(eq(TestRpcService.class), eq(serviceInstance), anySet()))
94 .thenReturn(routedRpcReg);
98 public void testStoreOrFail() {
99 try (RpcContext rpcContext = new RpcContextImpl(
103 extensionConverterProvider,
105 notificationPublishService, true)) {
106 final RequestContext<?> requestContext = rpcContext.createRequestContext();
107 assertNotNull(requestContext);
112 public void testStoreOrFailThatFails() {
113 try (RpcContext rpcContext = new RpcContextImpl(
117 extensionConverterProvider,
119 notificationPublishService, true)) {
120 final RequestContext<?> requestContext = rpcContext.createRequestContext();
121 assertNull(requestContext);
126 public void testStoreAndCloseOrFail() {
127 try (RpcContext rpcContext = new RpcContextImpl(
131 extensionConverterProvider,
133 notificationPublishService, true)) {
134 final RequestContext<?> requestContext = rpcContext.createRequestContext();
135 assertNotNull(requestContext);
136 requestContext.close();
137 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
141 public void testRegisterRpcServiceImplementation() {
142 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
143 verify(rpcProviderRegistry, Mockito.times(1)).registerRpcImplementation(TestRpcService.class, serviceInstance,
144 ImmutableSet.of(nodeInstanceIdentifier));
145 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
149 public void testLookupRpcService() {
150 when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
151 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
152 TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
153 assertEquals(serviceInstance,temp);
157 public void testClose() {
158 when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
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() {
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() {
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 {}