More OFP production code migration
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / direct / multilayer / QueueDirectStatisticsServiceTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.direct.multilayer;
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.lenient;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Map;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
25 import org.opendaylight.openflowplugin.impl.statistics.services.direct.AbstractDirectStatisticsServiceTest;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetQueueStatisticsInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetQueueStatisticsOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.queue.rev130925.QueueId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyQueueCase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.MultipartReplyQueue;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.multipart.reply.queue.QueueStats;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestQueueCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.queue._case.MultipartRequestQueue;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMapKey;
40 import org.opendaylight.yangtools.yang.common.Uint32;
41 import org.opendaylight.yangtools.yang.common.Uint64;
42
43 public class QueueDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
44     static final Uint32 QUEUE_NO = Uint32.ONE;
45     private QueueDirectStatisticsService service;
46
47     @Override
48     public void setUp() {
49         service = new QueueDirectStatisticsService(requestContextStack,
50                                                    deviceContext,
51                                                    convertorManager,
52                                                    multipartWriterProvider);
53     }
54
55     @Override
56     public void testBuildRequestBody() {
57         final GetQueueStatisticsInput input = mock(GetQueueStatisticsInput.class);
58
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));
62
63         final MultipartRequestQueueCase body = (MultipartRequestQueueCase) ((MultipartRequestInput) service
64             .buildRequest(new Xid(Uint32.valueOf(42)), input))
65             .getMultipartRequestBody();
66
67         final MultipartRequestQueue queue = body.getMultipartRequestQueue();
68
69         assertEquals(PORT_NO, queue.getPortNo());
70         assertEquals(QUEUE_NO, queue.getQueueId());
71     }
72
73     @Override
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 = Collections.singletonList(queueStat);
80         final List<MultipartReply> input = Collections.singletonList(reply);
81
82         when(queue.getQueueStats()).thenReturn(queueStats);
83         when(queue.nonnullQueueStats()).thenCallRealMethod();
84         when(queueCase.getMultipartReplyQueue()).thenReturn(queue);
85         when(reply.getMultipartReplyBody()).thenReturn(queueCase);
86
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);
94
95         final GetQueueStatisticsOutput output = service.buildReply(input, true);
96         assertTrue(output.nonnullQueueIdAndStatisticsMap().size() > 0);
97
98         final QueueIdAndStatisticsMap map = output.nonnullQueueIdAndStatisticsMap().values().iterator().next();
99         assertEquals(map.getQueueId().getValue(), QUEUE_NO);
100         assertEquals(map.getNodeConnectorId(), nodeConnectorId);
101     }
102
103     @Test
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)));
109
110         final Map<QueueIdAndStatisticsMapKey, QueueIdAndStatisticsMap> maps
111                 = Collections.singletonMap(map.key(), map);
112         final GetQueueStatisticsOutput output = mock(GetQueueStatisticsOutput.class);
113         when(output.nonnullQueueIdAndStatisticsMap()).thenReturn(maps);
114
115         multipartWriterProvider.lookup(MultipartType.OFPMPQUEUE).get().write(output, true);
116         verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
117     }
118
119     @Override
120     public void testStoreStatistics() {
121         final QueueIdAndStatisticsMap map = mock(QueueIdAndStatisticsMap.class);
122         when(map.getQueueId()).thenReturn(new QueueId(QUEUE_NO));
123         when(map.getNodeConnectorId()).thenReturn(new NodeConnectorId("openflow:1:1"));
124         when(map.key()).thenReturn(new QueueIdAndStatisticsMapKey(new NodeConnectorId("openflow:1:1"),
125             new QueueId(QUEUE_NO)));
126
127         final Map<QueueIdAndStatisticsMapKey, QueueIdAndStatisticsMap> maps
128                 = Collections.singletonMap(map.key(), map);
129         final GetQueueStatisticsOutput output = mock(GetQueueStatisticsOutput.class);
130         when(output.nonnullQueueIdAndStatisticsMap()).thenReturn(maps);
131
132         multipartWriterProvider.lookup(MultipartType.OFPMPQUEUE).get().write(output, true);
133         verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
134     }
135 }