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.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
14 import static org.junit.Assert.assertEquals;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
26 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.XidSequencer;
28 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.binding.RpcService;
38 import java.util.concurrent.Semaphore;
40 @RunWith(MockitoJUnitRunner.class)
41 public class RpcContextImplTest {
43 private static final int MAX_REQUESTS = 5;
44 private RpcContextImpl rpcContext;
48 private BindingAwareBroker.ProviderContext rpcProviderRegistry;
50 private DeviceState deviceState;
52 private XidSequencer xidSequencer;
54 private MessageSpy messageSpy;
56 private DeviceContext deviceContext;
58 private BindingAwareBroker.RoutedRpcRegistration routedRpcReg;
60 private NotificationPublishService notificationPublishService;
62 private TestRpcService serviceInstance;
64 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
68 final NodeId nodeId = new NodeId("openflow:1");
69 nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
71 when(deviceContext.getDeviceState()).thenReturn(deviceState);
72 when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
73 when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
75 rpcContext = new RpcContextImpl(rpcProviderRegistry,deviceContext, messageSpy, MAX_REQUESTS,nodeInstanceIdentifier);
77 when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance)).thenReturn(routedRpcReg);
82 public void testStoreOrFail() throws Exception {
83 try (final RpcContext rpcContext = new RpcContextImpl(rpcProviderRegistry, xidSequencer,
84 messageSpy, 100, nodeInstanceIdentifier)) {
85 final RequestContext<?> requestContext = rpcContext.createRequestContext();
86 assertNotNull(requestContext);
91 public void testStoreOrFailThatFails() throws Exception {
92 try (final RpcContext rpcContext = new RpcContextImpl(rpcProviderRegistry, xidSequencer,
93 messageSpy, 0, nodeInstanceIdentifier)) {
94 final RequestContext<?> requestContext = rpcContext.createRequestContext();
95 assertNull(requestContext);
100 public void testStoreAndCloseOrFail() throws Exception {
101 try (final RpcContext rpcContext = new RpcContextImpl(rpcProviderRegistry, deviceContext, messageSpy,
102 100, nodeInstanceIdentifier)) {
103 final RequestContext<?> requestContext = rpcContext.createRequestContext();
104 assertNotNull(requestContext);
105 requestContext.close();
106 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
110 public void testRegisterRpcServiceImplementation() {
111 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
112 verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
113 verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
114 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
119 public void testLookupRpcService() {
120 when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
121 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
122 TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
123 assertEquals(serviceInstance,temp);
127 public void testClose() {
128 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
130 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
134 * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned
135 * @throws InterruptedException
138 public void testCreateRequestContext1() throws InterruptedException {
139 when(deviceContext.reserveXidForDeviceMessage()).thenReturn(null);
140 assertEquals(rpcContext.createRequestContext(),null);
144 * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned
145 * @throws InterruptedException
149 public void testCreateRequestContext2() throws InterruptedException {
150 RequestContext temp = rpcContext.createRequestContext();
152 verify(messageSpy).spyMessage(RpcContextImpl.class,MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
156 public void testUnregisterRpcServiceImpl() {
157 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
158 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
159 rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
160 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
163 //Stub for RpcService class
164 public class TestRpcService implements RpcService {}