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.After;
20 import org.junit.Assert;
21 import org.junit.Test;
22 import org.mockito.ArgumentCaptor;
23 import org.mockito.ArgumentMatchers;
24 import org.mockito.Captor;
25 import org.mockito.Mockito;
26 import org.opendaylight.openflowplugin.api.OFConstants;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortStatsCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.stats._case.MultipartReplyPortStatsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.stats._case.multipart.reply.port.stats.PortStatsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetNodeConnectorStatisticsInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetNodeConnectorStatisticsOutput;
38 import org.opendaylight.yangtools.yang.binding.Notification;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
43 * Test for {@link OpendaylightPortStatisticsServiceImpl}.
45 public class OpendaylightPortStatisticsServiceImplTest extends AbstractSingleStatsServiceTest {
48 private ArgumentCaptor<MultipartRequestInput> requestInput;
50 private OpendaylightPortStatisticsServiceImpl portStatisticsService;
53 portStatisticsService = new OpendaylightPortStatisticsServiceImpl(rqContextStack, deviceContext,
54 new AtomicLong(), notificationPublishService);
56 Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
57 .commitEntry(eq(42L), requestInput.capture(), any(FutureCallback.class));
61 public void tearDown() throws Exception {
62 Mockito.verify(notificationPublishService).offerNotification(ArgumentMatchers.<Notification>any());
66 public void testGetAllNodeConnectorsStatistics() throws Exception {
67 GetAllNodeConnectorsStatisticsInputBuilder input = new GetAllNodeConnectorsStatisticsInputBuilder()
68 .setNode(createNodeRef("unitProt:123"));
70 rpcResult = buildPortStatisticsReply();
72 final Future<RpcResult<GetAllNodeConnectorsStatisticsOutput>> resultFuture
73 = portStatisticsService.getAllNodeConnectorsStatistics(input.build());
75 Assert.assertTrue(resultFuture.isDone());
76 final RpcResult<GetAllNodeConnectorsStatisticsOutput> rpcResult = resultFuture.get();
77 Assert.assertTrue(rpcResult.isSuccessful());
78 Assert.assertEquals(MultipartType.OFPMPPORTSTATS, requestInput.getValue().getType());
81 private static RpcResult<Object> buildPortStatisticsReply() {
82 return RpcResultBuilder.<Object>success(Collections.singletonList(
83 new MultipartReplyMessageBuilder()
84 .setVersion(OFConstants.OFP_VERSION_1_3)
85 .setMultipartReplyBody(new MultipartReplyPortStatsCaseBuilder()
86 .setMultipartReplyPortStats(new MultipartReplyPortStatsBuilder()
87 .setPortStats(Collections.singletonList(new PortStatsBuilder()
90 .setCollisions(BigInteger.valueOf(92L))
92 .setRxBytes(BigInteger.valueOf(94L))
93 .setRxCrcErr(BigInteger.valueOf(95L))
94 .setRxDropped(BigInteger.valueOf(96L))
95 .setRxFrameErr(BigInteger.valueOf(97L))
96 .setRxErrors(BigInteger.valueOf(98L))
97 .setRxOverErr(BigInteger.valueOf(99L))
98 .setRxPackets(BigInteger.valueOf(100L))
99 .setTxBytes(BigInteger.valueOf(94L))
100 .setTxDropped(BigInteger.valueOf(96L))
101 .setTxErrors(BigInteger.valueOf(98L))
102 .setTxPackets(BigInteger.valueOf(98L))
111 public void testGetNodeConnectorStatistics() throws Exception {
112 GetNodeConnectorStatisticsInputBuilder input = new GetNodeConnectorStatisticsInputBuilder()
113 .setNode(createNodeRef("unitProt:123"))
114 .setNodeConnectorId(new NodeConnectorId("unitProt:123:321"));
116 rpcResult = buildPortStatisticsReply();
118 final Future<RpcResult<GetNodeConnectorStatisticsOutput>> resultFuture
119 = portStatisticsService.getNodeConnectorStatistics(input.build());
121 Assert.assertTrue(resultFuture.isDone());
122 final RpcResult<GetNodeConnectorStatisticsOutput> rpcResult = resultFuture.get();
123 Assert.assertTrue(rpcResult.isSuccessful());
124 Assert.assertEquals(MultipartType.OFPMPPORTSTATS, requestInput.getValue().getType());