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.Ignore;
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);
85 when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
86 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
88 rpcContext = new RpcContextImpl(
92 extensionConverterProvider,
94 notificationPublishService, true);
96 when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance)).thenReturn(routedRpcReg);
101 public void testStoreOrFail() throws Exception {
102 try (final RpcContext rpcContext = new RpcContextImpl(
106 extensionConverterProvider,
108 notificationPublishService, true)){
109 final RequestContext<?> requestContext = rpcContext.createRequestContext();
110 assertNotNull(requestContext);
115 public void testStoreOrFailThatFails() throws Exception {
116 try (final RpcContext rpcContext = new RpcContextImpl(
120 extensionConverterProvider,
122 notificationPublishService, true)){
123 final RequestContext<?> requestContext = rpcContext.createRequestContext();
124 assertNull(requestContext);
129 public void testStoreAndCloseOrFail() throws Exception {
130 try (final RpcContext rpcContext = new RpcContextImpl(
134 extensionConverterProvider,
136 notificationPublishService, true)){
137 final RequestContext<?> requestContext = rpcContext.createRequestContext();
138 assertNotNull(requestContext);
139 requestContext.close();
140 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
144 public void testRegisterRpcServiceImplementation() {
145 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
146 verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
147 verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
148 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
153 public void testLookupRpcService() {
154 when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
155 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
156 TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
157 assertEquals(serviceInstance,temp);
160 //TODO: RPCServices should be removed on event from singleton service
163 public void testClose() {
164 serviceClass = TestRpcService.class;
165 when(routedRpcReg.getServiceType()).thenReturn(serviceClass);
166 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
168 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
172 * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned
173 * @throws InterruptedException
176 public void testCreateRequestContext1() throws InterruptedException {
177 when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(null);
178 assertEquals(rpcContext.createRequestContext(),null);
182 * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned
183 * @throws InterruptedException
187 public void testCreateRequestContext2() throws InterruptedException {
188 RequestContext temp = rpcContext.createRequestContext();
190 verify(messageSpy).spyMessage(RpcContextImpl.class,MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
194 public void testUnregisterRpcServiceImpl() {
195 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
196 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
197 rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
198 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
201 //Stub for RpcService class
202 public class TestRpcService implements RpcService {}