Fix statistics race condition on big flows
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / SalFlowServiceImplTest.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.services;
9
10
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
13
14 import com.google.common.util.concurrent.Futures;
15 import java.math.BigInteger;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
18 import junit.framework.TestCase;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Matchers;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
27 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
28 import org.opendaylight.openflowplugin.api.OFConstants;
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.device.RequestContext;
34 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
35 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
36 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
37 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
38 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
39 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
40 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
41 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
42 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
68 import org.opendaylight.yangtools.yang.binding.DataObject;
69 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
70 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
71 import org.opendaylight.yangtools.yang.common.RpcResult;
72 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
73
74 @RunWith(MockitoJUnitRunner.class)
75 public class SalFlowServiceImplTest extends TestCase {
76
77     private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
78     private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
79     private static final String DUMMY_NODE_ID = "dummyNodeID";
80     private static final String DUMMY_FLOW_ID = "dummyFlowID";
81     private static final Short DUMMY_TABLE_ID = (short) 0;
82
83     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
84             = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
85
86     private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
87             = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
88
89     @Mock
90     private RequestContextStack mockedRequestContextStack;
91     @Mock
92     private DeviceContext mockedDeviceContext;
93     @Mock
94     private ConnectionContext mockedPrimConnectionContext;
95     @Mock
96     private FeaturesReply mockedFeatures;
97     @Mock
98     private ConnectionAdapter mockedConnectionAdapter;
99     @Mock
100     private MessageSpy mockedMessagSpy;
101     @Mock
102     private RequestContext<Object> requestContext;
103     @Mock
104     private OutboundQueue outboundQueue;
105     @Mock
106     private Match match;
107     private SalFlowServiceImpl salFlowService;
108
109     @Mock
110     private DeviceState mockedDeviceState;
111     @Mock
112     private DeviceInfo mockedDeviceInfo;
113     @Mock
114     private DeviceFlowRegistry deviceFlowRegistry;
115     @Mock
116     private GetFeaturesOutput mockedFeaturesOutput;
117
118     @Before
119     public void initialization() {
120         when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
121         when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
122         when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
123         when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
124
125         when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
126         when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
127         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
128
129         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
130
131         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
132         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
133         when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
134
135         when(requestContext.getXid()).thenReturn(new Xid(84L));
136         when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
137
138         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_II);
139         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
140         when(mockedDeviceInfo.getVersion()).thenReturn(DUMMY_VERSION);
141         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
142         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
143
144         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
145         salFlowService = new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
146     }
147
148     @Test
149     public void testAddFlow() throws Exception {
150         addFlow(null);
151     }
152
153     @Test
154     public void testAddFlowFailCallback() throws Exception {
155         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
156                 .setMatch(match)
157                 .setTableId((short)1)
158                 .build();
159
160         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
161                 .when(requestContext).getFuture();
162
163         mockingFlowRegistryLookup();
164         final Future<RpcResult<AddFlowOutput>> rpcResultFuture = salFlowService.addFlow(mockedAddFlowInput);
165
166         assertNotNull(rpcResultFuture);
167         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
168         assertNotNull(addFlowOutputRpcResult);
169         assertFalse(addFlowOutputRpcResult.isSuccessful());
170     }
171
172     @Test
173     public void testRemoveFlowFailCallback() throws Exception {
174         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
175                 .setMatch(match)
176                 .build();
177
178         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
179                 .when(requestContext).getFuture();
180
181         final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = salFlowService.removeFlow(mockedRemoveFlowInput);
182
183         assertNotNull(rpcResultFuture);
184         final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
185         assertNotNull(removeFlowOutputRpcResult);
186         assertFalse(removeFlowOutputRpcResult.isSuccessful());
187     }
188
189     @Test
190     public void testAddFlowWithItemLifecycle() throws Exception {
191         addFlow(mock(ItemLifecycleListener.class));
192     }
193
194     private void addFlow(final ItemLifecycleListener itemLifecycleListener) throws ExecutionException, InterruptedException {
195         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
196                 .setMatch(match)
197                 .setTableId((short)1)
198                 .build();
199
200         salFlowService.setItemLifecycleListener(itemLifecycleListener);
201
202         mockingFlowRegistryLookup();
203         verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
204         if (itemLifecycleListener != null) {
205             Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
206         }
207     }
208
209     @Test
210     public void testRemoveFlow() throws Exception {
211         removeFlow(null);
212     }
213
214     @Test
215     public void testRemoveFlowWithItemLifecycle() throws Exception {
216         removeFlow(mock(ItemLifecycleListener.class));
217     }
218
219     private void removeFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
220         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
221                 .setMatch(match)
222                 .setTableId((short)1)
223                 .build();
224
225         if (itemLifecycleListener != null) {
226             salFlowService.setItemLifecycleListener(itemLifecycleListener);
227             mockingFlowRegistryLookup();
228
229         }
230
231         verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
232         if (itemLifecycleListener != null) {
233             Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
234         }
235
236     }
237
238     @Test
239     public void testUpdateFlow() throws Exception {
240         updateFlow(null);
241     }
242
243     @Test
244     public void testUpdateFlowWithItemLifecycle() throws Exception {
245         updateFlow(mock(ItemLifecycleListener.class));
246     }
247
248     private void updateFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
249         UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
250
251         UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
252                 .setMatch(match)
253                 .setTableId((short)1)
254                 .build();
255
256         when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
257
258         FlowRef mockedFlowRef = mock(FlowRef.class);
259         Mockito.doReturn(TABLE_II.child(Flow.class, new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
260         when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
261
262         OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
263                 .setMatch(match)
264                 .setTableId((short)1)
265                 .build();
266
267         when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
268
269         if (itemLifecycleListener != null) {
270             salFlowService.setItemLifecycleListener(itemLifecycleListener);
271             mockingFlowRegistryLookup();
272         }
273
274         verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
275
276         if (itemLifecycleListener != null) {
277             Mockito.verify(itemLifecycleListener).onUpdated(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
278         }
279
280     }
281
282     private void mockingFlowRegistryLookup() {
283         FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
284         FlowId flowId = new FlowId(DUMMY_FLOW_ID);
285         when(mockedFlowDescriptor.getFlowId()).thenReturn(flowId);
286         when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
287
288         when(deviceFlowRegistry.retrieveDescriptor(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
289     }
290
291     private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
292         assertNotNull(rpcResultFuture);
293         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
294         assertNotNull(addFlowOutputRpcResult);
295         assertTrue(addFlowOutputRpcResult.isSuccessful());
296     }
297 }