Improve cleanup after device disconnected event
[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.Mockito.verify;
14 import static org.mockito.Mockito.when;
15
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;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class RpcContextImplTest {
44
45     private static final int MAX_REQUESTS = 5;
46     private RpcContextImpl rpcContext;
47
48
49     @Mock
50     private BindingAwareBroker.ProviderContext rpcProviderRegistry;
51     @Mock
52     private DeviceState deviceState;
53     @Mock
54     private XidSequencer xidSequencer;
55     @Mock
56     private MessageSpy messageSpy;
57     @Mock
58     private DeviceContext deviceContext;
59     @Mock
60     private BindingAwareBroker.RoutedRpcRegistration<TestRpcService> routedRpcReg;
61
62     private Class<TestRpcService> serviceClass;
63     @Mock
64     private NotificationPublishService notificationPublishService;
65     @Mock
66     private TestRpcService serviceInstance;
67     @Mock
68     private DeviceInfo deviceInfo;
69     @Mock
70     private ExtensionConverterProvider extensionConverterProvider;
71     @Mock
72     private ConvertorExecutor convertorExecutor;
73
74     private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
75
76     @Before
77     public void setup() {
78         final NodeId nodeId = new NodeId("openflow:1");
79         nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
80
81         when(deviceContext.getDeviceState()).thenReturn(deviceState);
82         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
83         when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
84
85         rpcContext = new RpcContextImpl(
86                 deviceInfo,
87                 rpcProviderRegistry,
88                 messageSpy,
89                 MAX_REQUESTS,
90                 nodeInstanceIdentifier,
91                 deviceContext,
92                 extensionConverterProvider,
93                 convertorExecutor,
94                 notificationPublishService);
95
96         when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance)).thenReturn(routedRpcReg);
97
98     }
99
100     @Test
101     public void testStoreOrFail() throws Exception {
102         try (final RpcContext rpcContext = new RpcContextImpl(
103                 deviceInfo,
104                 rpcProviderRegistry,
105                 messageSpy,
106                 100,
107                 nodeInstanceIdentifier,
108                 deviceContext,
109                 extensionConverterProvider,
110                 convertorExecutor,
111                 notificationPublishService)){
112             final RequestContext<?> requestContext = rpcContext.createRequestContext();
113             assertNotNull(requestContext);
114         }
115     }
116
117     @Test
118     public void testStoreOrFailThatFails() throws Exception {
119         try (final RpcContext rpcContext = new RpcContextImpl(
120                 deviceInfo,
121                 rpcProviderRegistry,
122                 messageSpy,
123                 0,
124                 nodeInstanceIdentifier,
125                 deviceContext,
126                 extensionConverterProvider,
127                 convertorExecutor,
128                 notificationPublishService)){
129             final RequestContext<?> requestContext = rpcContext.createRequestContext();
130             assertNull(requestContext);
131         }
132     }
133
134     @Test
135     public void testStoreAndCloseOrFail() throws Exception {
136         try (final RpcContext rpcContext = new RpcContextImpl(
137                 deviceInfo,
138                 rpcProviderRegistry,
139                 messageSpy,
140                 100,
141                 nodeInstanceIdentifier,
142                 deviceContext,
143                 extensionConverterProvider,
144                 convertorExecutor,
145                 notificationPublishService)){
146             final RequestContext<?> requestContext = rpcContext.createRequestContext();
147             assertNotNull(requestContext);
148             requestContext.close();
149             verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
150         }
151     }
152
153     public void testRegisterRpcServiceImplementation() {
154         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
155         verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
156         verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
157         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
158     }
159
160
161     @Test
162     public void testLookupRpcService() {
163         when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
164         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
165         TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
166         assertEquals(serviceInstance,temp);
167     }
168
169     @Test
170     public void testClose() {
171         serviceClass = TestRpcService.class;
172         when(routedRpcReg.getServiceType()).thenReturn(serviceClass);
173         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
174         rpcContext.close();
175         assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
176     }
177
178     /**
179      * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned
180      * @throws InterruptedException
181      */
182     @Test
183     public void testCreateRequestContext1() throws InterruptedException {
184         when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(null);
185         assertEquals(rpcContext.createRequestContext(),null);
186     }
187
188     /**
189      * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned
190      * @throws InterruptedException
191      */
192
193     @Test
194     public void testCreateRequestContext2() throws InterruptedException {
195         RequestContext temp = rpcContext.createRequestContext();
196         temp.close();
197         verify(messageSpy).spyMessage(RpcContextImpl.class,MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
198     }
199
200     @Test
201     public void testUnregisterRpcServiceImpl() {
202         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
203         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
204         rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
205         assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
206     }
207
208     //Stub for RpcService class
209     public class TestRpcService implements RpcService {}
210 }