Bug 5692 - Direct statistics RPC
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / direct / QueueDirectStatisticsServiceTest.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.direct;
10
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetQueueStatisticsInput;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetQueueStatisticsOutput;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.queue.rev130925.QueueId;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyQueueCase;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.MultipartReplyQueue;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.multipart.reply.queue.QueueStats;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestQueueCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.queue._case.MultipartRequestQueue;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
23
24 import java.math.BigInteger;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.eq;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35
36 public class QueueDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
37     static final Long QUEUE_NO = 1L;
38     private QueueDirectStatisticsService service;
39
40     @Override
41     public void setUp() throws Exception {
42         service = new QueueDirectStatisticsService(requestContextStack, deviceContext);
43     }
44
45     @Override
46     public void testBuildRequestBody() throws Exception {
47         final GetQueueStatisticsInput input = mock(GetQueueStatisticsInput.class);
48
49         when(input.getNode()).thenReturn(createNodeRef(NODE_ID));
50         when(input.getQueueId()).thenReturn(new QueueId(QUEUE_NO));
51         when(input.getNodeConnectorId()).thenReturn(new NodeConnectorId(NODE_ID + ":" + PORT_NO));
52
53         final MultipartRequestQueueCase body = (MultipartRequestQueueCase) service.buildRequestBody(input);
54         final MultipartRequestQueue queue = body.getMultipartRequestQueue();
55
56         assertEquals(PORT_NO, queue.getPortNo());
57         assertEquals(QUEUE_NO, queue.getQueueId());
58     }
59
60     @Override
61     public void testBuildReply() throws Exception {
62         final MultipartReply reply = mock(MultipartReply.class);
63         final MultipartReplyQueueCase queueCase = mock(MultipartReplyQueueCase.class);
64         final MultipartReplyQueue queue = mock(MultipartReplyQueue.class);
65         final QueueStats queueStat = mock(QueueStats.class);
66         final List<QueueStats> queueStats = Arrays.asList(queueStat);
67         final List<MultipartReply> input = Arrays.asList(reply);
68
69         when(queue.getQueueStats()).thenReturn(queueStats);
70         when(queueCase.getMultipartReplyQueue()).thenReturn(queue);
71         when(reply.getMultipartReplyBody()).thenReturn(queueCase);
72
73         when(queueStat.getPortNo()).thenReturn(PORT_NO);
74         when(queueStat.getQueueId()).thenReturn(QUEUE_NO);
75         when(queueStat.getTxBytes()).thenReturn(BigInteger.ONE);
76         when(queueStat.getTxErrors()).thenReturn(BigInteger.ONE);
77         when(queueStat.getTxPackets()).thenReturn(BigInteger.ONE);
78
79         final GetQueueStatisticsOutput output = service.buildReply(input, true);
80         assertTrue(output.getQueueIdAndStatisticsMap().size() > 0);
81
82         final QueueIdAndStatisticsMap map = output.getQueueIdAndStatisticsMap().get(0);
83         assertEquals(map.getQueueId().getValue(), QUEUE_NO);
84         assertEquals(map.getNodeConnectorId(), nodeConnectorId);
85     }
86
87     @Override
88     public void testStoreStatistics() throws Exception {
89         final QueueIdAndStatisticsMap map = mock(QueueIdAndStatisticsMap.class);
90         when(map.getQueueId()).thenReturn(new QueueId(QUEUE_NO));
91
92         final List<QueueIdAndStatisticsMap> maps = Arrays.asList(map);
93         final GetQueueStatisticsOutput output = mock(GetQueueStatisticsOutput.class);
94         when(output.getQueueIdAndStatisticsMap()).thenReturn(maps);
95
96         service.storeStatistics(output);
97         verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
98     }
99 }