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