Merge "BUG-5573: Altering the HandshakePool to have a LinkedBlockingQueue instead...
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / OpendaylightQueueStatisticsServiceImplTest.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.flow.types.queue.rev130925.QueueId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyQueueCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.MultipartReplyQueueBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.multipart.reply.queue.QueueStatsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromGivenPortInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromGivenPortOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetQueueStatisticsFromGivenPortInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetQueueStatisticsFromGivenPortOutput;
38 import org.opendaylight.yangtools.yang.binding.Notification;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
41
42 /**
43  * Test for {@link OpendaylightQueueStatisticsServiceImpl}
44  */
45 public class OpendaylightQueueStatisticsServiceImplTest extends AbstractSingleStatsServiceTest {
46
47     @Captor
48     private ArgumentCaptor<MultipartRequestInput> requestInput;
49
50     private OpendaylightQueueStatisticsServiceImpl queueStatisticsService;
51
52     public void setUp() {
53         queueStatisticsService = new OpendaylightQueueStatisticsServiceImpl(rqContextStack, deviceContext,
54                 new AtomicLong(), notificationPublishService);
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 testGetAllQueuesStatisticsFromAllPorts() throws Exception {
64         Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
65                 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
66
67         GetAllQueuesStatisticsFromAllPortsInputBuilder input = new GetAllQueuesStatisticsFromAllPortsInputBuilder()
68                 .setNode(createNodeRef("unitProt:123"));
69
70         rpcResult = buildQueueStatsReply();
71
72         final Future<RpcResult<GetAllQueuesStatisticsFromAllPortsOutput>> resultFuture
73                 = queueStatisticsService.getAllQueuesStatisticsFromAllPorts(input.build());
74
75         Assert.assertTrue(resultFuture.isDone());
76         final RpcResult<GetAllQueuesStatisticsFromAllPortsOutput> rpcResult = resultFuture.get();
77         Assert.assertTrue(rpcResult.isSuccessful());
78         Assert.assertEquals(MultipartType.OFPMPQUEUE, requestInput.getValue().getType());
79     }
80
81     protected RpcResult<Object> buildQueueStatsReply() {
82         return RpcResultBuilder.<Object>success(Collections.singletonList(
83                 new MultipartReplyMessageBuilder()
84                         .setVersion(OFConstants.OFP_VERSION_1_3)
85                         .setMultipartReplyBody(new MultipartReplyQueueCaseBuilder()
86                                 .setMultipartReplyQueue(new MultipartReplyQueueBuilder()
87                                         .setQueueStats(Collections.singletonList(new QueueStatsBuilder()
88                                                 .setDurationSec(41L)
89                                                 .setDurationNsec(42L)
90                                                 .setTxBytes(BigInteger.valueOf(43L))
91                                                 .setTxErrors(BigInteger.valueOf(44L))
92                                                 .setTxPackets(BigInteger.valueOf(45L))
93                                                 .setPortNo(46L)
94                                                 .setQueueId(47L)
95                                                 .build()))
96                                         .build())
97                                 .build())
98                         .build()
99         )).build();
100     }
101
102     @Test
103     public void testGetAllQueuesStatisticsFromGivenPort() throws Exception {
104         Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
105                 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
106
107         GetAllQueuesStatisticsFromGivenPortInputBuilder input = new GetAllQueuesStatisticsFromGivenPortInputBuilder()
108                 .setNode(createNodeRef("unitProt:123"))
109                 .setNodeConnectorId(new NodeConnectorId("unitProt:123:321"));
110
111         rpcResult = buildQueueStatsReply();
112
113         final Future<RpcResult<GetAllQueuesStatisticsFromGivenPortOutput>> resultFuture
114                 = queueStatisticsService.getAllQueuesStatisticsFromGivenPort(input.build());
115
116         Assert.assertTrue(resultFuture.isDone());
117         final RpcResult<GetAllQueuesStatisticsFromGivenPortOutput> rpcResult = resultFuture.get();
118         Assert.assertTrue(rpcResult.isSuccessful());
119         Assert.assertEquals(MultipartType.OFPMPQUEUE, requestInput.getValue().getType());
120     }
121
122     @Test
123     public void testGetQueueStatisticsFromGivenPort() throws Exception {
124         Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
125                 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
126
127         GetQueueStatisticsFromGivenPortInputBuilder input = new GetQueueStatisticsFromGivenPortInputBuilder()
128                 .setNode(createNodeRef("unitProt:123"))
129                 .setNodeConnectorId(new NodeConnectorId("unitProt:123:321"))
130                 .setQueueId(new QueueId(21L));
131
132         rpcResult = buildQueueStatsReply();
133
134         final Future<RpcResult<GetQueueStatisticsFromGivenPortOutput>> resultFuture
135                 = queueStatisticsService.getQueueStatisticsFromGivenPort(input.build());
136
137         Assert.assertTrue(resultFuture.isDone());
138         final RpcResult<GetQueueStatisticsFromGivenPortOutput> rpcResult = resultFuture.get();
139         Assert.assertTrue(rpcResult.isSuccessful());
140         Assert.assertEquals(MultipartType.OFPMPQUEUE, requestInput.getValue().getType());
141     }
142 }