Decompose RPC implementation classes
[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 package org.opendaylight.openflowplugin.impl.statistics.services;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.eq;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.verify;
16
17 import com.google.common.util.concurrent.FutureCallback;
18 import java.util.List;
19 import java.util.concurrent.atomic.AtomicLong;
20 import org.junit.After;
21 import org.junit.Test;
22 import org.mockito.ArgumentCaptor;
23 import org.mockito.Captor;
24 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.queue.rev130925.QueueId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyQueueCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.MultipartReplyQueueBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.multipart.reply.queue.QueueStatsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsInputBuilder;
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.GetQueueStatisticsFromGivenPortInputBuilder;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 import org.opendaylight.yangtools.yang.common.Uint32;
39 import org.opendaylight.yangtools.yang.common.Uint64;
40
41 @Deprecated
42 public class OpendaylightQueueStatisticsServiceImplTest extends AbstractSingleStatsServiceTest {
43     @Captor
44     private ArgumentCaptor<MultipartRequestInput> requestInput;
45
46     private GetAllQueuesStatisticsFromAllPortsImpl getAllQueuesStatisticsFromAllPorts;
47     private GetAllQueuesStatisticsFromGivenPortImpl getAllQueuesStatisticsFromGivenPort;
48     private GetQueueStatisticsFromGivenPortImpl getQueueStatisticsFromGivenPort;
49
50     @Override
51     public void setUp() {
52         final var xid = new AtomicLong();
53         getAllQueuesStatisticsFromAllPorts = new GetAllQueuesStatisticsFromAllPortsImpl(rqContextStack, deviceContext,
54                 xid, notificationPublishService);
55         getAllQueuesStatisticsFromGivenPort = new GetAllQueuesStatisticsFromGivenPortImpl(rqContextStack, deviceContext,
56             xid, notificationPublishService);
57         getQueueStatisticsFromGivenPort = new GetQueueStatisticsFromGivenPortImpl(rqContextStack, deviceContext,
58             xid, notificationPublishService);
59     }
60
61     @After
62     public void tearDown() {
63         verify(notificationPublishService).offerNotification(any());
64     }
65
66     @Test
67     public void testGetAllQueuesStatisticsFromAllPorts() throws Exception {
68         doAnswer(answerVoidToCallback).when(outboundQueueProvider)
69                 .commitEntry(eq(Uint32.valueOf(42)), requestInput.capture(), any(FutureCallback.class));
70
71         GetAllQueuesStatisticsFromAllPortsInputBuilder input = new GetAllQueuesStatisticsFromAllPortsInputBuilder()
72                 .setNode(createNodeRef("unitProt:123"));
73
74         rpcResult = buildQueueStatsReply();
75
76         final var resultFuture = getAllQueuesStatisticsFromAllPorts.invoke(input.build());
77
78         assertTrue(resultFuture.isDone());
79         final var rpcResult = resultFuture.get();
80         assertTrue(rpcResult.isSuccessful());
81         assertEquals(MultipartType.OFPMPQUEUE, requestInput.getValue().getType());
82     }
83
84     protected RpcResult<Object> buildQueueStatsReply() {
85         return RpcResultBuilder.<Object>success(List.of(
86                 new MultipartReplyMessageBuilder()
87                         .setVersion(EncodeConstants.OF_VERSION_1_3)
88                         .setMultipartReplyBody(new MultipartReplyQueueCaseBuilder()
89                                 .setMultipartReplyQueue(new MultipartReplyQueueBuilder()
90                                         .setQueueStats(List.of(new QueueStatsBuilder()
91                                                 .setDurationSec(Uint32.valueOf(41))
92                                                 .setDurationNsec(Uint32.valueOf(42))
93                                                 .setTxBytes(Uint64.valueOf(43))
94                                                 .setTxErrors(Uint64.valueOf(44))
95                                                 .setTxPackets(Uint64.valueOf(45))
96                                                 .setPortNo(Uint32.valueOf(46))
97                                                 .setQueueId(Uint32.valueOf(47))
98                                                 .build()))
99                                         .build())
100                                 .build())
101                         .build()
102         )).build();
103     }
104
105     @Test
106     public void testGetAllQueuesStatisticsFromGivenPort() throws Exception {
107         doAnswer(answerVoidToCallback).when(outboundQueueProvider)
108                 .commitEntry(eq(Uint32.valueOf(42)), 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 var resultFuture = getAllQueuesStatisticsFromGivenPort.invoke(input.build());
117
118         assertTrue(resultFuture.isDone());
119         final var rpcResult = resultFuture.get();
120         assertTrue(rpcResult.isSuccessful());
121         assertEquals(MultipartType.OFPMPQUEUE, requestInput.getValue().getType());
122     }
123
124     @Test
125     public void testGetQueueStatisticsFromGivenPort() throws Exception {
126         doAnswer(answerVoidToCallback).when(outboundQueueProvider)
127                 .commitEntry(eq(Uint32.valueOf(42)), requestInput.capture(), any(FutureCallback.class));
128
129         GetQueueStatisticsFromGivenPortInputBuilder input = new GetQueueStatisticsFromGivenPortInputBuilder()
130                 .setNode(createNodeRef("unitProt:123"))
131                 .setNodeConnectorId(new NodeConnectorId("unitProt:123:321"))
132                 .setQueueId(new QueueId(Uint32.valueOf(21)));
133
134         rpcResult = buildQueueStatsReply();
135
136         final var resultFuture = getQueueStatisticsFromGivenPort.invoke(input.build());
137
138         assertTrue(resultFuture.isDone());
139         final var rpcResult = resultFuture.get();
140         assertTrue(rpcResult.isSuccessful());
141         assertEquals(MultipartType.OFPMPQUEUE, requestInput.getValue().getType());
142     }
143 }