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(
88 nodeInstanceIdentifier,
90 extensionConverterProvider,
92 notificationPublishService);
94 when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance)).thenReturn(routedRpcReg);
99 public void testStoreOrFail() throws Exception {
100 try (final RpcContext rpcContext = new RpcContextImpl(
105 nodeInstanceIdentifier,
107 extensionConverterProvider,
109 notificationPublishService)){
110 final RequestContext<?> requestContext = rpcContext.createRequestContext();
111 assertNotNull(requestContext);
116 public void testStoreOrFailThatFails() throws Exception {
117 try (final RpcContext rpcContext = new RpcContextImpl(
122 nodeInstanceIdentifier,
124 extensionConverterProvider,
126 notificationPublishService)){
127 final RequestContext<?> requestContext = rpcContext.createRequestContext();
128 assertNull(requestContext);
133 public void testStoreAndCloseOrFail() throws Exception {
134 try (final RpcContext rpcContext = new RpcContextImpl(
139 nodeInstanceIdentifier,
141 extensionConverterProvider,
143 notificationPublishService)){
144 final RequestContext<?> requestContext = rpcContext.createRequestContext();
145 assertNotNull(requestContext);
146 requestContext.close();
147 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
151 public void testRegisterRpcServiceImplementation() {
152 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
153 verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
154 verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
155 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
160 public void testLookupRpcService() {
161 when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
162 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
163 TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
164 assertEquals(serviceInstance,temp);
168 public void testClose() {
169 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
171 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
175 * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned
176 * @throws InterruptedException
179 public void testCreateRequestContext1() throws InterruptedException {
180 when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(null);
181 assertEquals(rpcContext.createRequestContext(),null);
185 * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned
186 * @throws InterruptedException
190 public void testCreateRequestContext2() throws InterruptedException {
191 RequestContext temp = rpcContext.createRequestContext();
193 verify(messageSpy).spyMessage(RpcContextImpl.class,MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
197 public void testUnregisterRpcServiceImpl() {
198 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
199 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
200 rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
201 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
204 //Stub for RpcService class
205 public class TestRpcService implements RpcService {}