2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.statistics.services;
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;
41 * Test for {@link OpendaylightFlowStatisticsServiceImpl} - only not delegated method
43 public class OpendaylightFlowStatisticsServiceImpl2Test extends AbstractStatsServiceTest {
46 private ArgumentCaptor<MultipartRequestInput> requestInput;
48 private MessageTranslator<MultipartReply, AggregatedFlowStatistics> translator;
50 private AbstractRequestContext<List<MultipartReply>> rqContextMp;
52 private OpendaylightFlowStatisticsServiceImpl flowStatisticsService;
56 flowStatisticsService = OpendaylightFlowStatisticsServiceImpl.createWithOook(rqContextStack, deviceContext);
58 rqContextMp = new AbstractRequestContext<List<MultipartReply>>(42L) {
64 Mockito.when(rqContextStack.<List<MultipartReply>>createRequestContext()).thenReturn(rqContextMp);
65 Mockito.when(translatorLibrary.<MultipartReply, AggregatedFlowStatistics>lookupTranslator(Matchers.any(TranslatorKey.class)))
66 .thenReturn(translator);
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>() {
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());
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());
89 GetAggregateFlowStatisticsFromFlowTableForGivenMatchInputBuilder input =
90 new GetAggregateFlowStatisticsFromFlowTableForGivenMatchInputBuilder()
91 .setNode(createNodeRef("unitProt:123"))
93 .setTableId((short) 1);
95 final Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> resultFuture
96 = flowStatisticsService.getAggregateFlowStatisticsFromFlowTableForGivenMatch(input.build());
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());