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.doNothing;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.runners.MockitoJUnitRunner;
23 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
28 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.XidSequencer;
30 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
31 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
32 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
33 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
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.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.binding.RpcService;
43 @RunWith(MockitoJUnitRunner.class)
44 public class RpcContextImplTest {
46 private static final int MAX_REQUESTS = 5;
47 private RpcContextImpl rpcContext;
51 private BindingAwareBroker.ProviderContext rpcProviderRegistry;
53 private DeviceState deviceState;
55 private XidSequencer xidSequencer;
57 private MessageSpy messageSpy;
59 private DeviceContext deviceContext;
61 private BindingAwareBroker.RoutedRpcRegistration<TestRpcService> routedRpcReg;
63 private Class<TestRpcService> serviceClass;
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(deviceContext.getDeviceState()).thenReturn(deviceState);
83 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
84 when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
86 rpcContext = new RpcContextImpl(
91 nodeInstanceIdentifier,
93 extensionConverterProvider,
95 notificationPublishService);
97 when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance)).thenReturn(routedRpcReg);
102 public void testStoreOrFail() throws Exception {
103 try (final RpcContext rpcContext = new RpcContextImpl(
108 nodeInstanceIdentifier,
110 extensionConverterProvider,
112 notificationPublishService)){
113 final RequestContext<?> requestContext = rpcContext.createRequestContext();
114 assertNotNull(requestContext);
119 public void testStoreOrFailThatFails() throws Exception {
120 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(
142 nodeInstanceIdentifier,
144 extensionConverterProvider,
146 notificationPublishService)){
147 final RequestContext<?> requestContext = rpcContext.createRequestContext();
148 assertNotNull(requestContext);
149 requestContext.close();
150 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
154 public void testRegisterRpcServiceImplementation() {
155 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
156 verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
157 verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
158 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
163 public void testLookupRpcService() {
164 when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
165 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
166 TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
167 assertEquals(serviceInstance,temp);
171 public void testClose() {
172 serviceClass = TestRpcService.class;
173 when(routedRpcReg.getServiceType()).thenReturn(serviceClass);
174 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
176 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
180 * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned
181 * @throws InterruptedException
184 public void testCreateRequestContext1() throws InterruptedException {
185 when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(null);
186 assertEquals(rpcContext.createRequestContext(),null);
190 * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned
191 * @throws InterruptedException
195 public void testCreateRequestContext2() throws InterruptedException {
196 RequestContext temp = rpcContext.createRequestContext();
198 verify(messageSpy).spyMessage(RpcContextImpl.class,MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
202 public void testUnregisterRpcServiceImpl() {
203 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
204 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
205 rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
206 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
209 //Stub for RpcService class
210 public class TestRpcService implements RpcService {}