Merge "Bug 6050 - TcpSrcCodec is not maskable"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / OpendaylightFlowStatisticsServiceImpl2Test.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
9 package org.opendaylight.openflowplugin.impl.statistics.services;
10
11 import com.google.common.util.concurrent.FutureCallback;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.concurrent.Future;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.mockito.ArgumentCaptor;
18 import org.mockito.Captor;
19 import org.mockito.Matchers;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.invocation.InvocationOnMock;
23 import org.mockito.stubbing.Answer;
24 import org.opendaylight.openflowplugin.api.OFConstants;
25 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
26 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
27 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
28 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatistics;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatisticsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
39
40 /**
41  * Test for {@link OpendaylightFlowStatisticsServiceImpl} - only not delegated method
42  */
43 public class OpendaylightFlowStatisticsServiceImpl2Test extends AbstractStatsServiceTest {
44
45     @Captor
46     private ArgumentCaptor<MultipartRequestInput> requestInput;
47     @Mock
48     private MessageTranslator<MultipartReply, AggregatedFlowStatistics> translator;
49
50     private AbstractRequestContext<List<MultipartReply>> rqContextMp;
51
52     private OpendaylightFlowStatisticsServiceImpl flowStatisticsService;
53
54
55     public void setUp() {
56         flowStatisticsService = OpendaylightFlowStatisticsServiceImpl.createWithOook(rqContextStack, deviceContext);
57
58         rqContextMp = new AbstractRequestContext<List<MultipartReply>>(42L) {
59             @Override
60             public void close() {
61                 //NOOP
62             }
63         };
64         Mockito.when(rqContextStack.<List<MultipartReply>>createRequestContext()).thenReturn(rqContextMp);
65         Mockito.when(translatorLibrary.<MultipartReply, AggregatedFlowStatistics>lookupTranslator(Matchers.any(TranslatorKey.class)))
66                 .thenReturn(translator);
67     }
68
69     @Test
70     public void testGetAggregateFlowStatisticsFromFlowTableForGivenMatch() throws Exception {
71         Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
72                 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
73         Mockito.doAnswer(new Answer<Void>() {
74                              @Override
75                              public Void answer(InvocationOnMock invocation) throws Throwable {
76                                  final MultipartReplyMessageBuilder messageBuilder = new MultipartReplyMessageBuilder()
77                                          .setVersion(OFConstants.OFP_VERSION_1_3);
78                                  rqContextMp.setResult(RpcResultBuilder.success(
79                                          Collections.<MultipartReply>singletonList(messageBuilder.build())).build());
80                                  return null;
81                              }
82                          }
83         ).when(multiMsgCollector).endCollecting(Matchers.any(EventIdentifier.class));
84         Mockito.when(translator.translate(
85                         Matchers.any(MultipartReply.class), Matchers.same(deviceInfo), Matchers.isNull())
86         ).thenReturn(new AggregatedFlowStatisticsBuilder().build());
87
88
89         GetAggregateFlowStatisticsFromFlowTableForGivenMatchInputBuilder input =
90                 new GetAggregateFlowStatisticsFromFlowTableForGivenMatchInputBuilder()
91                         .setNode(createNodeRef("unitProt:123"))
92                         .setPriority(5)
93                         .setTableId((short) 1);
94
95         final Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> resultFuture
96                 = flowStatisticsService.getAggregateFlowStatisticsFromFlowTableForGivenMatch(input.build());
97
98         Assert.assertTrue(resultFuture.isDone());
99         final RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput> rpcResult = resultFuture.get();
100         Assert.assertTrue(rpcResult.isSuccessful());
101         Assert.assertEquals(1, rpcResult.getResult().getAggregatedFlowStatistics().size());
102         Assert.assertEquals(MultipartType.OFPMPAGGREGATE, requestInput.getValue().getType());
103     }
104
105
106 }