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.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.eq;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
18 import com.google.common.collect.ClassToInstanceMap;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Captor;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
28 import org.opendaylight.mdsal.binding.api.RpcProviderService;
29 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
33 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
34 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
35 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yangtools.concepts.ObjectRegistration;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.binding.Rpc;
44 import org.opendaylight.yangtools.yang.binding.RpcService;
45 import org.opendaylight.yangtools.yang.common.Uint32;
46 import org.opendaylight.yangtools.yang.common.Uint8;
48 @RunWith(MockitoJUnitRunner.class)
49 public class RpcContextImplTest {
51 private RpcProviderService rpcProviderRegistry;
53 private DeviceState deviceState;
55 private MessageSpy messageSpy;
57 private DeviceContext deviceContext;
59 private ConnectionContext connectionContext;
61 private ObjectRegistration<RpcService> registration;
63 private NotificationPublishService notificationPublishService;
65 private DeviceInfo deviceInfo;
67 private ExtensionConverterProvider extensionConverterProvider;
69 private ConvertorExecutor convertorExecutor;
71 private ArgumentCaptor<ClassToInstanceMap<Rpc<?, ?>>> captor;
73 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
74 private RpcContextImpl rpcContext;
78 final NodeId nodeId = new NodeId("openflow:1");
79 nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
81 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
82 when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
83 when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
84 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
85 when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(Uint32.TWO);
86 when(deviceInfo.getVersion()).thenReturn(Uint8.ONE);
87 when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
89 rpcContext = new RpcContextImpl(
93 extensionConverterProvider,
95 notificationPublishService, true);
99 public void testStoreOrFail() {
100 try (var rpcContext = new RpcContextImpl(
104 extensionConverterProvider,
106 notificationPublishService, true)) {
107 assertNotNull(rpcContext.createRequestContext());
112 public void testStoreOrFailThatFails() {
113 try (var rpcContext = new RpcContextImpl(
117 extensionConverterProvider,
119 notificationPublishService, true)) {
120 assertNull(rpcContext.createRequestContext());
125 public void testStoreAndCloseOrFail() {
126 try (var rpcContext = new RpcContextImpl(
130 extensionConverterProvider,
132 notificationPublishService, true)) {
133 try (var requestContext = rpcContext.createRequestContext()) {
134 assertNotNull(requestContext);
136 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
141 * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned.
144 public void testCreateRequestContext1() {
145 when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(null);
146 assertNull(rpcContext.createRequestContext());
150 * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned.
153 public void testCreateRequestContext2() {
154 try (var temp = rpcContext.createRequestContext()) {
157 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
161 public void testInstantiateServiceInstance() {
162 when(rpcProviderRegistry.registerRpcImplementations(any(),
163 eq(Set.of(nodeInstanceIdentifier)))).thenReturn(registration);
165 rpcContext.instantiateServiceInstance();
167 verify(rpcProviderRegistry).registerRpcImplementations(captor.capture(), eq(Set.of(nodeInstanceIdentifier)));
169 final var map = captor.getValue();
170 assertEquals(46, map.size());