7aa587f32111e399afe51199aa38652545dbaee9
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / OpendaylightPortStatisticsServiceImplTest.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.math.BigInteger;
13 import java.util.Collections;
14 import java.util.concurrent.Future;
15 import java.util.concurrent.atomic.AtomicLong;
16 import org.junit.After;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.mockito.ArgumentCaptor;
20 import org.mockito.Captor;
21 import org.mockito.Matchers;
22 import org.mockito.Mockito;
23 import org.opendaylight.openflowplugin.api.OFConstants;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortStatsCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.stats._case.MultipartReplyPortStatsBuilder;
30 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;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetNodeConnectorStatisticsInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetNodeConnectorStatisticsOutput;
35 import org.opendaylight.yangtools.yang.binding.Notification;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38
39 /**
40  * Test for {@link OpendaylightPortStatisticsServiceImpl}
41  */
42 public class OpendaylightPortStatisticsServiceImplTest extends AbstractSingleStatsServiceTest {
43
44     @Captor
45     private ArgumentCaptor<MultipartRequestInput> requestInput;
46
47     private OpendaylightPortStatisticsServiceImpl portStatisticsService;
48
49     public void setUp() {
50         portStatisticsService = new OpendaylightPortStatisticsServiceImpl(rqContextStack, deviceContext,
51                 new AtomicLong(), notificationPublishService);
52
53         Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
54                 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
55     }
56
57     @After
58     public void tearDown() throws Exception {
59         Mockito.verify(notificationPublishService).offerNotification(Matchers.<Notification>any());
60     }
61
62     @Test
63     public void testGetAllNodeConnectorsStatistics() throws Exception {
64         GetAllNodeConnectorsStatisticsInputBuilder input = new GetAllNodeConnectorsStatisticsInputBuilder()
65                 .setNode(createNodeRef("unitProt:123"));
66
67         rpcResult = buildPortStatisticsReply();
68
69         final Future<RpcResult<GetAllNodeConnectorsStatisticsOutput>> resultFuture
70                 = portStatisticsService.getAllNodeConnectorsStatistics(input.build());
71
72         Assert.assertTrue(resultFuture.isDone());
73         final RpcResult<GetAllNodeConnectorsStatisticsOutput> rpcResult = resultFuture.get();
74         Assert.assertTrue(rpcResult.isSuccessful());
75         Assert.assertEquals(MultipartType.OFPMPPORTSTATS, requestInput.getValue().getType());
76     }
77
78     private static RpcResult<Object> buildPortStatisticsReply() {
79         return RpcResultBuilder.<Object>success(Collections.singletonList(
80                 new MultipartReplyMessageBuilder()
81                         .setVersion(OFConstants.OFP_VERSION_1_3)
82                         .setMultipartReplyBody(new MultipartReplyPortStatsCaseBuilder()
83                                 .setMultipartReplyPortStats(new MultipartReplyPortStatsBuilder()
84                                         .setPortStats(Collections.singletonList(new PortStatsBuilder()
85                                                 .setDurationSec(90L)
86                                                 .setDurationNsec(91L)
87                                                 .setCollisions(BigInteger.valueOf(92L))
88                                                 .setPortNo(93L)
89                                                 .setRxBytes(BigInteger.valueOf(94L))
90                                                 .setRxCrcErr(BigInteger.valueOf(95L))
91                                                 .setRxDropped(BigInteger.valueOf(96L))
92                                                 .setRxFrameErr(BigInteger.valueOf(97L))
93                                                 .setRxErrors(BigInteger.valueOf(98L))
94                                                 .setRxOverErr(BigInteger.valueOf(99L))
95                                                 .setRxPackets(BigInteger.valueOf(100L))
96                                                 .setTxBytes(BigInteger.valueOf(94L))
97                                                 .setTxDropped(BigInteger.valueOf(96L))
98                                                 .setTxErrors(BigInteger.valueOf(98L))
99                                                 .setTxPackets(BigInteger.valueOf(98L))
100                                                 .build()))
101                                         .build())
102                                 .build())
103                         .build()
104         )).build();
105     }
106
107     @Test
108     public void testGetNodeConnectorStatistics() throws Exception {
109         GetNodeConnectorStatisticsInputBuilder input = new GetNodeConnectorStatisticsInputBuilder()
110                 .setNode(createNodeRef("unitProt:123"))
111                 .setNodeConnectorId(new NodeConnectorId("unitProt:123:321"));
112
113         rpcResult = buildPortStatisticsReply();
114
115         final Future<RpcResult<GetNodeConnectorStatisticsOutput>> resultFuture
116                 = portStatisticsService.getNodeConnectorStatistics(input.build());
117
118         Assert.assertTrue(resultFuture.isDone());
119         final RpcResult<GetNodeConnectorStatisticsOutput> rpcResult = resultFuture.get();
120         Assert.assertTrue(rpcResult.isSuccessful());
121         Assert.assertEquals(MultipartType.OFPMPPORTSTATS, requestInput.getValue().getType());
122     }
123 }