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.device.XidSequencer;
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 BindingAwareBroker.ProviderContext rpcProviderRegistry;
52 private DeviceState deviceState;
54 private XidSequencer xidSequencer;
56 private MessageSpy messageSpy;
58 private DeviceContext deviceContext;
60 private BindingAwareBroker.RoutedRpcRegistration<TestRpcService> routedRpcReg;
62 private NotificationPublishService notificationPublishService;
64 private TestRpcService serviceInstance;
66 private DeviceInfo deviceInfo;
68 private ExtensionConverterProvider extensionConverterProvider;
70 private ConvertorExecutor convertorExecutor;
72 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
76 final NodeId nodeId = new NodeId("openflow:1");
77 nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
79 when(deviceContext.getDeviceState()).thenReturn(deviceState);
80 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
81 when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
83 rpcContext = new RpcContextImpl(
89 nodeInstanceIdentifier,
91 extensionConverterProvider,
93 notificationPublishService);
95 when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance)).thenReturn(routedRpcReg);
100 public void testStoreOrFail() throws Exception {
101 try (final RpcContext rpcContext = new RpcContextImpl(
107 nodeInstanceIdentifier,
109 extensionConverterProvider,
111 notificationPublishService)){
112 final RequestContext<?> requestContext = rpcContext.createRequestContext();
113 assertNotNull(requestContext);
118 public void testStoreOrFailThatFails() throws Exception {
119 try (final RpcContext rpcContext = new RpcContextImpl(
125 nodeInstanceIdentifier,
127 extensionConverterProvider,
129 notificationPublishService)){
130 final RequestContext<?> requestContext = rpcContext.createRequestContext();
131 assertNull(requestContext);
136 public void testStoreAndCloseOrFail() throws Exception {
137 try (final RpcContext rpcContext = new RpcContextImpl(
143 nodeInstanceIdentifier,
145 extensionConverterProvider,
147 notificationPublishService)){
148 final RequestContext<?> requestContext = rpcContext.createRequestContext();
149 assertNotNull(requestContext);
150 requestContext.close();
151 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
155 public void testRegisterRpcServiceImplementation() {
156 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
157 verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
158 verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
159 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
164 public void testLookupRpcService() {
165 when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
166 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
167 TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
168 assertEquals(serviceInstance,temp);
172 public void testClose() {
173 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
175 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
179 * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned
180 * @throws InterruptedException
183 public void testCreateRequestContext1() throws InterruptedException {
184 when(deviceContext.reserveXidForDeviceMessage()).thenReturn(null);
185 assertEquals(rpcContext.createRequestContext(),null);
189 * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned
190 * @throws InterruptedException
194 public void testCreateRequestContext2() throws InterruptedException {
195 RequestContext temp = rpcContext.createRequestContext();
197 verify(messageSpy).spyMessage(RpcContextImpl.class,MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
201 public void testUnregisterRpcServiceImpl() {
202 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
203 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
204 rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
205 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
208 //Stub for RpcService class
209 public class TestRpcService implements RpcService {}