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 static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.eq;
14 import com.google.common.util.concurrent.FutureCallback;
15 import java.math.BigInteger;
16 import java.util.Collections;
17 import java.util.concurrent.Future;
18 import java.util.concurrent.atomic.AtomicLong;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.mockito.ArgumentCaptor;
22 import org.mockito.ArgumentMatchers;
23 import org.mockito.Captor;
24 import org.mockito.Mockito;
25 import org.opendaylight.openflowplugin.api.OFConstants;
26 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.MultipartReplyTableBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.multipart.reply.table.TableStatsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableCase;
37 import org.opendaylight.yangtools.yang.binding.Notification;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
42 * Test for {@link OpendaylightFlowTableStatisticsServiceImpl}.
44 public class OpendaylightFlowTableStatisticsServiceImplTest extends AbstractSingleStatsServiceTest {
46 private static final Short TABLE_ID = (short) 123;
48 private ArgumentCaptor<MultipartRequestInput> requestInput;
50 private OpendaylightFlowTableStatisticsServiceImpl flowTableStatisticsService;
53 flowTableStatisticsService = new OpendaylightFlowTableStatisticsServiceImpl(rqContextStack, deviceContext,
54 new AtomicLong(), notificationPublishService);
58 public void testGetFlowTablesStatistics() throws Exception {
59 Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
60 .commitEntry(eq(42L), requestInput.capture(), any(FutureCallback.class));
62 GetFlowTablesStatisticsInputBuilder input = new GetFlowTablesStatisticsInputBuilder()
63 .setNode(createNodeRef("unitProt:123"));
65 rpcResult = RpcResultBuilder.<Object>success(Collections.singletonList(
66 new MultipartReplyMessageBuilder()
67 .setVersion(OFConstants.OFP_VERSION_1_3)
68 .setMultipartReplyBody(new MultipartReplyTableCaseBuilder()
69 .setMultipartReplyTable(new MultipartReplyTableBuilder()
70 .setTableStats(Collections.singletonList(new TableStatsBuilder()
72 .setLookupCount(BigInteger.valueOf(32L))
73 .setMatchedCount(BigInteger.valueOf(33L))
75 .setName("test-table")
76 .setNwDstMask((short) 35)
77 .setNwSrcMask((short) 36)
85 final Future<RpcResult<GetFlowTablesStatisticsOutput>> resultFuture
86 = flowTableStatisticsService.getFlowTablesStatistics(input.build());
88 Assert.assertTrue(resultFuture.isDone());
89 final RpcResult<GetFlowTablesStatisticsOutput> rpcResult = resultFuture.get();
90 Assert.assertTrue(rpcResult.isSuccessful());
91 Assert.assertEquals(MultipartType.OFPMPTABLE, requestInput.getValue().getType());
93 Mockito.verify(notificationPublishService).offerNotification(ArgumentMatchers.<Notification>any());
97 public void testBuildRequest() throws Exception {
98 Xid xid = new Xid(42L);
99 GetFlowTablesStatisticsInputBuilder input = new GetFlowTablesStatisticsInputBuilder()
100 .setNode(createNodeRef("unitProt:123"));
101 final OfHeader request = flowTableStatisticsService.buildRequest(xid, input.build());
102 Assert.assertTrue(request instanceof MultipartRequestInput);
103 final MultipartRequestInput mpRequest = (MultipartRequestInput) request;
104 Assert.assertEquals(MultipartType.OFPMPTABLE, mpRequest.getType());
105 Assert.assertTrue(mpRequest.getMultipartRequestBody() instanceof MultipartRequestTableCase);
106 final MultipartRequestTableCase mpRequestBody =
107 (MultipartRequestTableCase) (mpRequest.getMultipartRequestBody());
108 Assert.assertTrue(mpRequestBody.getMultipartRequestTable().isEmpty());