2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.statistics.services.direct;
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;
24 import java.math.BigInteger;
25 import java.util.Arrays;
26 import java.util.List;
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;
36 public class QueueDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
37 static final Long QUEUE_NO = 1L;
38 private QueueDirectStatisticsService service;
41 public void setUp() throws Exception {
42 service = new QueueDirectStatisticsService(requestContextStack, deviceContext);
46 public void testBuildRequestBody() throws Exception {
47 final GetQueueStatisticsInput input = mock(GetQueueStatisticsInput.class);
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));
53 final MultipartRequestQueueCase body = (MultipartRequestQueueCase) service.buildRequestBody(input);
54 final MultipartRequestQueue queue = body.getMultipartRequestQueue();
56 assertEquals(PORT_NO, queue.getPortNo());
57 assertEquals(QUEUE_NO, queue.getQueueId());
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);
69 when(queue.getQueueStats()).thenReturn(queueStats);
70 when(queueCase.getMultipartReplyQueue()).thenReturn(queue);
71 when(reply.getMultipartReplyBody()).thenReturn(queueCase);
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);
79 final GetQueueStatisticsOutput output = service.buildReply(input, true);
80 assertTrue(output.getQueueIdAndStatisticsMap().size() > 0);
82 final QueueIdAndStatisticsMap map = output.getQueueIdAndStatisticsMap().get(0);
83 assertEquals(map.getQueueId().getValue(), QUEUE_NO);
84 assertEquals(map.getNodeConnectorId(), nodeConnectorId);
88 public void testStoreStatistics() throws Exception {
89 final QueueIdAndStatisticsMap map = mock(QueueIdAndStatisticsMap.class);
90 when(map.getQueueId()).thenReturn(new QueueId(QUEUE_NO));
92 final List<QueueIdAndStatisticsMap> maps = Arrays.asList(map);
93 final GetQueueStatisticsOutput output = mock(GetQueueStatisticsOutput.class);
94 when(output.getQueueIdAndStatisticsMap()).thenReturn(maps);
96 service.storeStatistics(output);
97 verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());