Fix statistics race condition on big flows
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / MultipartRequestOnTheFlyCallbackTest.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 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.spy;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import com.google.common.base.Optional;
20 import com.google.common.util.concurrent.CheckedFuture;
21 import com.google.common.util.concurrent.Futures;
22 import java.math.BigInteger;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.concurrent.ExecutionException;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Matchers;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.runners.MockitoJUnitRunner;
33 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
34 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
35 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
36 import org.opendaylight.openflowplugin.api.OFConstants;
37 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
38 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
39 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
40 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
41 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
42 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
43 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
44 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
45 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
46 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
47 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
48 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
49 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCaseBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlowBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStatsBuilder;
72 import org.opendaylight.yangtools.yang.binding.DataObject;
73 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
74 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
75 import org.opendaylight.yangtools.yang.common.RpcError;
76 import org.opendaylight.yangtools.yang.common.RpcResult;
77 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
78
79 @RunWith(MockitoJUnitRunner.class)
80 public class MultipartRequestOnTheFlyCallbackTest {
81
82     private static final String DUMMY_NODE_ID = "dummyNodeId";
83     private static final String DUMMY_EVENT_NAME = "dummy event name 1";
84     private static final String DUMMY_DEVICE_ID = "dummy device id 1";
85     private static final Long DUMMY_XID = 55L;
86     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_PATH = KeyedInstanceIdentifier
87             .create(Nodes.class)
88             .child(Node.class, new NodeKey(new NodeId("uf-node:123")));
89
90     @Mock
91     private DeviceContext mockedDeviceContext;
92     @Mock
93     private RequestContext<List<MultipartReply>> mockedRequestContext;
94     @Mock
95     private ConnectionContext mockedPrimaryConnection;
96     @Mock
97     private NodeId mockedNodeId;
98     @Mock
99     private FeaturesReply mockedFeaturesReply;
100     @Mock
101     private DeviceState mockedDeviceState;
102     @Mock
103     private DeviceInfo mockedDeviceInfo;
104     @Mock
105     private GetFeaturesOutput mocketGetFeaturesOutput;
106     @Mock
107     private DeviceFlowRegistry mockedFlowRegistry;
108     @Mock
109     private FlowDescriptor mockedFlowDescriptor;
110     @Mock
111     private ReadOnlyTransaction mockedReadOnlyTx;
112
113     private AbstractRequestContext<List<MultipartReply>> dummyRequestContext;
114     private final EventIdentifier dummyEventIdentifier = new EventIdentifier(DUMMY_EVENT_NAME, DUMMY_DEVICE_ID);
115     private MultipartRequestOnTheFlyCallback multipartRequestOnTheFlyCallback;
116     private final short tableId = 0;
117
118     @Before
119     public void initialization() throws Exception{
120         when(mockedDeviceContext.getMessageSpy()).thenReturn(new MessageIntelligenceAgencyImpl());
121         when(mockedNodeId.toString()).thenReturn(DUMMY_NODE_ID);
122         when(mockedPrimaryConnection.getNodeId()).thenReturn(mockedNodeId);
123         when(mockedPrimaryConnection.getFeatures()).thenReturn(mockedFeaturesReply);
124         when(mockedFeaturesReply.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
125         when(mockedFeaturesReply.getDatapathId()).thenReturn(BigInteger.valueOf(123L));
126
127         when(mocketGetFeaturesOutput.getTables()).thenReturn(tableId);
128         when(mocketGetFeaturesOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
129         when(mocketGetFeaturesOutput.getDatapathId()).thenReturn(BigInteger.valueOf(123L));
130
131         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimaryConnection);
132         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_PATH);
133         when(mockedDeviceInfo.getNodeId()).thenReturn(mockedNodeId);
134         when(mockedDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
135         when(mockedDeviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
136
137         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
138         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
139         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(mockedFlowRegistry);
140         when(mockedFlowRegistry.retrieveDescriptor(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
141
142         final InstanceIdentifier<FlowCapableNode> nodePath = mockedDeviceInfo.getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
143         final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
144         flowNodeBuilder.setTable(Collections.<Table> emptyList());
145         final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
146
147         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture = Futures.immediateCheckedFuture(flowNodeOpt);
148         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFutureSpy = spy(flowNodeFuture);
149         when(mockedReadOnlyTx.read(LogicalDatastoreType.OPERATIONAL, nodePath)).thenReturn(flowNodeFutureSpy);
150         when(flowNodeFutureSpy.get()).thenReturn(flowNodeOpt);
151         when(mockedDeviceContext.getReadTransaction()).thenReturn(mockedReadOnlyTx);
152
153         dummyRequestContext = new AbstractRequestContext<List<MultipartReply>>(DUMMY_XID) {
154
155             @Override
156             public void close() {
157                 //NOOP
158             }
159         };
160
161         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
162         multipartRequestOnTheFlyCallback = new MultipartRequestOnTheFlyCallback(dummyRequestContext, String.class,
163                 mockedDeviceContext.getMessageSpy(),dummyEventIdentifier, mockedDeviceInfo,
164                 mockedDeviceContext.getDeviceFlowRegistry(), mockedDeviceContext, convertorManager);
165     }
166
167
168     @Test
169     public void testOnSuccessWithNull() throws Exception {
170         multipartRequestOnTheFlyCallback.onSuccess(null);
171         final RpcResult<List<MultipartReply>> expectedRpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
172         final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
173         assertEquals(expectedRpcResult.getErrors(), actualResult.getErrors());
174         assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
175         assertEquals(expectedRpcResult.isSuccessful(), actualResult.isSuccessful());
176     }
177
178     @Test
179     public void testOnSuccessWithNotMultiNoMultipart() throws Exception {
180         final HelloMessage mockedHelloMessage = mock(HelloMessage.class);
181         multipartRequestOnTheFlyCallback.onSuccess(mockedHelloMessage);
182
183         final RpcResult<List<MultipartReply>> expectedRpcResult =
184                 RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
185                         String.format("Unexpected response type received %s.", mockedHelloMessage.getClass())).build();
186         final RpcResult<List<MultipartReply>> actualResult = dummyRequestContext.getFuture().get();
187         assertNotNull(actualResult.getErrors());
188         assertEquals(1, actualResult.getErrors().size());
189
190         final RpcError actualError = actualResult.getErrors().iterator().next();
191         assertEquals(actualError.getMessage(), String.format("Unexpected response type received %s.", mockedHelloMessage.getClass()));
192         assertEquals(actualError.getErrorType(),RpcError.ErrorType.APPLICATION);
193         assertEquals(expectedRpcResult.getResult(), actualResult.getResult());
194         assertEquals(expectedRpcResult.isSuccessful(), actualResult.isSuccessful());
195
196         Mockito.verify(mockedDeviceContext, Mockito.never()).writeToTransaction(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
197                 Matchers.<InstanceIdentifier>any(), Matchers.<DataObject>any());
198         Mockito.verify(mockedDeviceContext).submitTransaction();
199     }
200
201     /**
202      * not the last reply
203      *
204      * @throws ExecutionException
205      * @throws InterruptedException
206      */
207     @Test
208     public void testOnSuccessWithValidMultipart1() throws Exception {
209         final MatchBuilder matchBuilder = new MatchBuilder()
210                 .setMatchEntry(Collections.<MatchEntry>emptyList());
211         final FlowStatsBuilder flowStatsBuilder = new FlowStatsBuilder()
212                 .setTableId(tableId)
213                 .setPriority(2)
214                 .setCookie(BigInteger.ZERO)
215                 .setByteCount(BigInteger.TEN)
216                 .setPacketCount(BigInteger.ONE)
217                 .setDurationSec(11L)
218                 .setDurationNsec(12L)
219                 .setMatch(matchBuilder.build())
220                 .setFlags(new FlowModFlags(true, false, false, false, false));
221         final MultipartReplyFlowBuilder multipartReplyFlowBuilder = new MultipartReplyFlowBuilder()
222                 .setFlowStats(Collections.singletonList(flowStatsBuilder.build()));
223         final MultipartReplyFlowCaseBuilder multipartReplyFlowCaseBuilder = new MultipartReplyFlowCaseBuilder()
224                 .setMultipartReplyFlow(multipartReplyFlowBuilder.build());
225         final MultipartReplyMessageBuilder mpReplyMessage = new MultipartReplyMessageBuilder()
226                 .setType(MultipartType.OFPMPFLOW)
227                 .setFlags(new MultipartRequestFlags(true))
228                 .setMultipartReplyBody(multipartReplyFlowCaseBuilder.build())
229                 .setXid(21L);
230
231         final InstanceIdentifier<FlowCapableNode> nodePath = mockedDeviceInfo.getNodeInstanceIdentifier()
232                 .augmentation(FlowCapableNode.class);
233         final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
234         final TableBuilder tableDataBld = new TableBuilder();
235         tableDataBld.setId(tableId);
236         flowNodeBuilder.setTable(Collections.singletonList(tableDataBld.build()));
237         final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
238         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture = Futures
239                 .immediateCheckedFuture(flowNodeOpt);
240         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFutureSpy = spy(flowNodeFuture);
241
242         when(mockedReadOnlyTx.read(LogicalDatastoreType.OPERATIONAL, nodePath)).thenReturn(flowNodeFutureSpy);
243         when(flowNodeFutureSpy.get()).thenReturn(flowNodeOpt);
244         when(mockedDeviceContext.getReadTransaction()).thenReturn(mockedReadOnlyTx);
245
246         multipartRequestOnTheFlyCallback.onSuccess(mpReplyMessage.build());
247         final InstanceIdentifier<Table> tableIdent = nodePath.child(Table.class, new TableKey(tableId));
248
249         verify(mockedReadOnlyTx, times(1)).read(LogicalDatastoreType.OPERATIONAL, nodePath);
250         verify(mockedReadOnlyTx, times(1)).close();
251         verify(mockedDeviceContext, times(1)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
252                 eq(tableIdent), Matchers.<Table> any());
253         /*
254          * One call for Table one call for Flow
255          * we are not able to create Flow InstanceIdentifier because we are missing FlowId
256          */
257         verify(mockedDeviceContext, times(2)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
258                 Matchers.<InstanceIdentifier> any(), Matchers.<DataObject> any());
259     }
260
261     /**
262      * the last reply
263      *
264      * @throws ExecutionException
265      * @throws InterruptedException
266      */
267     @Test
268     public void testOnSuccessWithValidMultipart2() throws Exception {
269         final MatchBuilder matchBuilder = new MatchBuilder()
270                 .setMatchEntry(Collections.<MatchEntry>emptyList());
271         final FlowStatsBuilder flowStatsBuilder = new FlowStatsBuilder()
272                 .setTableId(tableId)
273                 .setPriority(2)
274                 .setCookie(BigInteger.ZERO)
275                 .setByteCount(BigInteger.TEN)
276                 .setPacketCount(BigInteger.ONE)
277                 .setDurationSec(11L)
278                 .setDurationNsec(12L)
279                 .setMatch(matchBuilder.build())
280                 .setFlags(new FlowModFlags(true, false, false, false, false));
281         final MultipartReplyFlowBuilder multipartReplyFlowBuilder = new MultipartReplyFlowBuilder()
282                 .setFlowStats(Collections.singletonList(flowStatsBuilder.build()));
283         final MultipartReplyFlowCaseBuilder multipartReplyFlowCaseBuilder = new MultipartReplyFlowCaseBuilder()
284                 .setMultipartReplyFlow(multipartReplyFlowBuilder.build());
285         final MultipartReplyMessageBuilder mpReplyMessage = new MultipartReplyMessageBuilder()
286                 .setType(MultipartType.OFPMPFLOW)
287                 .setFlags(new MultipartRequestFlags(false))
288                 .setMultipartReplyBody(multipartReplyFlowCaseBuilder.build())
289                 .setXid(21L);
290
291         final InstanceIdentifier<FlowCapableNode> nodePath = mockedDeviceInfo.getNodeInstanceIdentifier()
292                 .augmentation(FlowCapableNode.class);
293         final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
294         final TableBuilder tableDataBld = new TableBuilder();
295         tableDataBld.setId(tableId);
296         flowNodeBuilder.setTable(Collections.singletonList(tableDataBld.build()));
297         final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
298         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture = Futures
299                 .immediateCheckedFuture(flowNodeOpt);
300         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFutureSpy = spy(flowNodeFuture);
301
302         when(mockedReadOnlyTx.read(LogicalDatastoreType.OPERATIONAL, nodePath)).thenReturn(flowNodeFutureSpy);
303         when(flowNodeFutureSpy.get()).thenReturn(flowNodeOpt);
304         when(mockedDeviceContext.getReadTransaction()).thenReturn(mockedReadOnlyTx);
305
306         multipartRequestOnTheFlyCallback.onSuccess(mpReplyMessage.build());
307         final InstanceIdentifier<Table> tableIdent = nodePath.child(Table.class, new TableKey(tableId));
308
309         verify(mockedReadOnlyTx, times(1)).read(LogicalDatastoreType.OPERATIONAL, nodePath);
310         verify(mockedReadOnlyTx, times(1)).close();
311         verify(mockedDeviceContext, times(1)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
312                 eq(tableIdent), Matchers.<Table> any());
313         /*
314          * One call for Table one call for Flow
315          * we are not able to create Flow InstanceIdentifier because we are missing FlowId
316          */
317         verify(mockedDeviceContext, times(2)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
318                 Matchers.<InstanceIdentifier> any(), Matchers.<DataObject> any());
319     }
320 }