2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
8 package org.opendaylight.openflowplugin.impl.statistics.services.direct.multilayer;
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.lenient;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
19 import java.util.List;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
24 import org.opendaylight.openflowplugin.impl.statistics.services.direct.AbstractDirectStatisticsServiceTest;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetQueueStatisticsInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetQueueStatisticsOutput;
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.MultipartReply;
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.MultipartReplyQueueCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.MultipartReplyQueue;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.multipart.reply.queue.QueueStats;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestQueueCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.queue._case.MultipartRequestQueue;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMapKey;
39 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
40 import org.opendaylight.yangtools.yang.common.Uint32;
41 import org.opendaylight.yangtools.yang.common.Uint64;
43 public class QueueDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
44 static final Uint32 QUEUE_NO = Uint32.ONE;
45 private QueueDirectStatisticsService service;
49 service = new QueueDirectStatisticsService(requestContextStack,
52 multipartWriterProvider);
56 public void testBuildRequestBody() {
57 final GetQueueStatisticsInput input = mock(GetQueueStatisticsInput.class);
59 lenient().when(input.getNode()).thenReturn(createNodeRef(NODE_ID));
60 when(input.getQueueId()).thenReturn(new QueueId(QUEUE_NO));
61 when(input.getNodeConnectorId()).thenReturn(new NodeConnectorId(NODE_ID + ":" + PORT_NO));
63 final MultipartRequestQueueCase body = (MultipartRequestQueueCase) ((MultipartRequestInput) service
64 .buildRequest(new Xid(Uint32.valueOf(42)), input))
65 .getMultipartRequestBody();
67 final MultipartRequestQueue queue = body.getMultipartRequestQueue();
69 assertEquals(PORT_NO, queue.getPortNo());
70 assertEquals(QUEUE_NO, queue.getQueueId());
74 public void testBuildReply() {
75 final MultipartReply reply = mock(MultipartReply.class);
76 final MultipartReplyQueueCase queueCase = mock(MultipartReplyQueueCase.class);
77 final MultipartReplyQueue queue = mock(MultipartReplyQueue.class);
78 final QueueStats queueStat = mock(QueueStats.class);
79 final List<QueueStats> queueStats = List.of(queueStat);
80 final List<MultipartReply> input = List.of(reply);
82 when(queue.getQueueStats()).thenReturn(queueStats);
83 when(queue.nonnullQueueStats()).thenCallRealMethod();
84 when(queueCase.getMultipartReplyQueue()).thenReturn(queue);
85 when(reply.getMultipartReplyBody()).thenReturn(queueCase);
87 when(queueStat.getPortNo()).thenReturn(PORT_NO);
88 when(queueStat.getQueueId()).thenReturn(QUEUE_NO);
89 when(queueStat.getTxBytes()).thenReturn(Uint64.ONE);
90 when(queueStat.getTxErrors()).thenReturn(Uint64.ONE);
91 when(queueStat.getTxPackets()).thenReturn(Uint64.ONE);
92 when(queueStat.getDurationSec()).thenReturn(Uint32.ZERO);
93 when(queueStat.getDurationNsec()).thenReturn(Uint32.ZERO);
95 final GetQueueStatisticsOutput output = service.buildReply(input, true);
96 assertTrue(output.nonnullQueueIdAndStatisticsMap().size() > 0);
98 final QueueIdAndStatisticsMap map = output.nonnullQueueIdAndStatisticsMap().values().iterator().next();
99 assertEquals(map.getQueueId().getValue(), QUEUE_NO);
100 assertEquals(map.getNodeConnectorId(), nodeConnectorId);
104 public void testStoreStatisticsBarePortNo() {
105 final QueueIdAndStatisticsMap map = mock(QueueIdAndStatisticsMap.class);
106 when(map.getQueueId()).thenReturn(new QueueId(QUEUE_NO));
107 when(map.getNodeConnectorId()).thenReturn(new NodeConnectorId("1"));
108 when(map.key()).thenReturn(new QueueIdAndStatisticsMapKey(new NodeConnectorId("1"), new QueueId(QUEUE_NO)));
110 final Map<QueueIdAndStatisticsMapKey, QueueIdAndStatisticsMap> maps = BindingMap.of(map);
111 final GetQueueStatisticsOutput output = mock(GetQueueStatisticsOutput.class);
112 when(output.nonnullQueueIdAndStatisticsMap()).thenReturn(maps);
114 multipartWriterProvider.lookup(MultipartType.OFPMPQUEUE).orElseThrow().write(output, true);
115 verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
119 public void testStoreStatistics() {
120 final QueueIdAndStatisticsMap map = mock(QueueIdAndStatisticsMap.class);
121 when(map.getQueueId()).thenReturn(new QueueId(QUEUE_NO));
122 when(map.getNodeConnectorId()).thenReturn(new NodeConnectorId("openflow:1:1"));
123 when(map.key()).thenReturn(new QueueIdAndStatisticsMapKey(new NodeConnectorId("openflow:1:1"),
124 new QueueId(QUEUE_NO)));
126 final Map<QueueIdAndStatisticsMapKey, QueueIdAndStatisticsMap> maps = BindingMap.of(map);
127 final GetQueueStatisticsOutput output = mock(GetQueueStatisticsOutput.class);
128 when(output.nonnullQueueIdAndStatisticsMap()).thenReturn(maps);
130 multipartWriterProvider.lookup(MultipartType.OFPMPQUEUE).orElseThrow().write(output, true);
131 verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());