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;
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.openflowplugin.api.openflow.device.DeviceContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
25 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.XidSequencer;
27 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
28 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.RpcService;
38 @RunWith(MockitoJUnitRunner.class)
39 public class RpcContextImplTest {
41 private static final int MAX_REQUESTS = 5;
42 private RpcContextImpl rpcContext;
46 private BindingAwareBroker.ProviderContext rpcProviderRegistry;
48 private DeviceState deviceState;
50 private XidSequencer xidSequencer;
52 private MessageSpy messageSpy;
54 private DeviceContext deviceContext;
56 private BindingAwareBroker.RoutedRpcRegistration routedRpcReg;
58 private NotificationPublishService notificationPublishService;
60 private TestRpcService serviceInstance;
62 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
66 final NodeId nodeId = new NodeId("openflow:1");
67 nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
69 when(deviceContext.getDeviceState()).thenReturn(deviceState);
70 when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
71 when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
73 rpcContext = new RpcContextImpl(rpcProviderRegistry,deviceContext, messageSpy, MAX_REQUESTS,nodeInstanceIdentifier);
75 when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance)).thenReturn(routedRpcReg);
80 public void testStoreOrFail() throws Exception {
81 try (final RpcContext rpcContext = new RpcContextImpl(rpcProviderRegistry, xidSequencer,
82 messageSpy, 100, nodeInstanceIdentifier)) {
83 final RequestContext<?> requestContext = rpcContext.createRequestContext();
84 assertNotNull(requestContext);
89 public void testStoreOrFailThatFails() throws Exception {
90 try (final RpcContext rpcContext = new RpcContextImpl(rpcProviderRegistry, xidSequencer,
91 messageSpy, 0, nodeInstanceIdentifier)) {
92 final RequestContext<?> requestContext = rpcContext.createRequestContext();
93 assertNull(requestContext);
98 public void testStoreAndCloseOrFail() throws Exception {
99 try (final RpcContext rpcContext = new RpcContextImpl(rpcProviderRegistry, deviceContext, messageSpy,
100 100, nodeInstanceIdentifier)) {
101 final RequestContext<?> requestContext = rpcContext.createRequestContext();
102 assertNotNull(requestContext);
103 requestContext.close();
104 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
108 public void testRegisterRpcServiceImplementation() {
109 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
110 verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
111 verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
112 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
117 public void testLookupRpcService() {
118 when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
119 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
120 TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
121 assertEquals(serviceInstance,temp);
125 public void testClose() {
126 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
128 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
132 * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned
133 * @throws InterruptedException
136 public void testCreateRequestContext1() throws InterruptedException {
137 when(deviceContext.reserveXidForDeviceMessage()).thenReturn(null);
138 assertEquals(rpcContext.createRequestContext(),null);
142 * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned
143 * @throws InterruptedException
147 public void testCreateRequestContext2() throws InterruptedException {
148 RequestContext temp = rpcContext.createRequestContext();
150 verify(messageSpy).spyMessage(RpcContextImpl.class,MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
154 public void testUnregisterRpcServiceImpl() {
155 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
156 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
157 rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
158 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
161 //Stub for RpcService class
162 public class TestRpcService implements RpcService {}