Merge "GetFlowNodeCache cli"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / rpc / RpcContextImplTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.rpc;
9
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.anySet;
14 import static org.mockito.ArgumentMatchers.eq;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.collect.ImmutableSet;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
26 import org.opendaylight.mdsal.binding.api.RpcProviderService;
27 import org.opendaylight.openflowplugin.api.openflow.FlowGroupCacheManager;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
31 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
32 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
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.RpcService;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class RpcContextImplTest {
47
48     private static final int MAX_REQUESTS = 5;
49     private RpcContextImpl rpcContext;
50
51     @Mock
52     private FlowGroupCacheManager flowGroupCacheManager;
53     @Mock
54     private RpcProviderService rpcProviderRegistry;
55     @Mock
56     private DeviceState deviceState;
57     @Mock
58     private MessageSpy messageSpy;
59     @Mock
60     private DeviceContext deviceContext;
61     @Mock
62     private ObjectRegistration<TestRpcService> routedRpcReg;
63
64     @Mock
65     private NotificationPublishService notificationPublishService;
66     @Mock
67     private TestRpcService serviceInstance;
68     @Mock
69     private DeviceInfo deviceInfo;
70     @Mock
71     private ExtensionConverterProvider extensionConverterProvider;
72     @Mock
73     private ConvertorExecutor convertorExecutor;
74
75     private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
76
77     @Before
78     public void setup() {
79         final NodeId nodeId = new NodeId("openflow:1");
80         nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
81
82         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
83         when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
84         when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
85         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
86
87         rpcContext = new RpcContextImpl(
88                 rpcProviderRegistry,
89                 MAX_REQUESTS,
90                 deviceContext,
91                 extensionConverterProvider,
92                 convertorExecutor,
93                 notificationPublishService, true, flowGroupCacheManager);
94
95         when(rpcProviderRegistry.registerRpcImplementation(eq(TestRpcService.class), eq(serviceInstance), anySet()))
96                 .thenReturn(routedRpcReg);
97     }
98
99     @Test
100     public void testStoreOrFail() {
101         try (RpcContext rpcContext = new RpcContextImpl(
102                 rpcProviderRegistry,
103                 100,
104                 deviceContext,
105                 extensionConverterProvider,
106                 convertorExecutor,
107                 notificationPublishService, true, flowGroupCacheManager)) {
108             final RequestContext<?> requestContext = rpcContext.createRequestContext();
109             assertNotNull(requestContext);
110         }
111     }
112
113     @Test
114     public void testStoreOrFailThatFails() {
115         try (RpcContext rpcContext = new RpcContextImpl(
116                 rpcProviderRegistry,
117                 0,
118                 deviceContext,
119                 extensionConverterProvider,
120                 convertorExecutor,
121                 notificationPublishService, true, flowGroupCacheManager)) {
122             final RequestContext<?> requestContext = rpcContext.createRequestContext();
123             assertNull(requestContext);
124         }
125     }
126
127     @Test
128     public void testStoreAndCloseOrFail() {
129         try (RpcContext rpcContext = new RpcContextImpl(
130                 rpcProviderRegistry,
131                 100,
132                 deviceContext,
133                 extensionConverterProvider,
134                 convertorExecutor,
135                 notificationPublishService, true, flowGroupCacheManager)) {
136             final RequestContext<?> requestContext = rpcContext.createRequestContext();
137             assertNotNull(requestContext);
138             requestContext.close();
139             verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
140         }
141     }
142
143     public void testRegisterRpcServiceImplementation() {
144         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
145         verify(rpcProviderRegistry, Mockito.times(1)).registerRpcImplementation(TestRpcService.class, serviceInstance,
146             ImmutableSet.of(nodeInstanceIdentifier));
147         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
148     }
149
150     @Test
151     public void testLookupRpcService() {
152         when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
153         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
154         TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
155         assertEquals(serviceInstance,temp);
156     }
157
158     @Test
159     public void testClose() {
160         when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
161         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
162         rpcContext.close();
163         assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
164     }
165
166     /**
167      * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned.
168      */
169     @Test
170     public void testCreateRequestContext1() {
171         when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(null);
172         assertEquals(rpcContext.createRequestContext(),null);
173     }
174
175     /**
176      * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned.
177      */
178
179     @Test
180     public void testCreateRequestContext2() {
181         RequestContext temp = rpcContext.createRequestContext();
182         temp.close();
183         verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
184     }
185
186     @Test
187     public void testUnregisterRpcServiceImpl() {
188         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
189         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
190         rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
191         assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
192     }
193
194     //Stub for RpcService class.
195     public class TestRpcService implements RpcService {
196
197     }
198 }