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