4e32c092db2545c564ceaaee2eb779570bef86fc
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / direct / singlelayer / 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
9 package org.opendaylight.openflowplugin.impl.statistics.services.direct.singlelayer;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.eq;
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.math.BigInteger;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
23 import org.opendaylight.controller.md.sal.common.api.data.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.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetQueueStatisticsInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetQueueStatisticsOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.queue.rev130925.QueueId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReply;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReplyBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartRequest;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.multipart.reply.multipart.reply.body.MultipartReplyQueueStatsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.multipart.request.multipart.request.body.MultipartRequestQueueStats;
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.QueueIdAndStatisticsMapBuilder;
39
40 public class QueueDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
41     static final Long QUEUE_NO = 1L;
42     private QueueDirectStatisticsService service;
43
44     @Override
45     public void setUp() throws Exception {
46         service = new QueueDirectStatisticsService(requestContextStack, deviceContext, convertorManager, multipartWriterProvider);
47     }
48
49     @Override
50     public void testBuildRequestBody() throws Exception {
51         final GetQueueStatisticsInput input = mock(GetQueueStatisticsInput.class);
52
53         when(input.getNode()).thenReturn(createNodeRef(NODE_ID));
54         when(input.getQueueId()).thenReturn(new QueueId(QUEUE_NO));
55         when(input.getNodeConnectorId()).thenReturn(nodeConnectorId);
56
57         final MultipartRequestQueueStats body = (MultipartRequestQueueStats) ((MultipartRequest) service
58             .buildRequest(new Xid(42L), input))
59             .getMultipartRequestBody();
60
61         assertEquals(nodeConnectorId, body.getNodeConnectorId());
62         assertEquals(QUEUE_NO, body.getQueueId().getValue());
63     }
64
65     @Override
66     public void testBuildReply() throws Exception {
67         final QueueIdAndStatisticsMap queueStats = new QueueIdAndStatisticsMapBuilder()
68                 .setQueueId(new QueueId(QUEUE_NO))
69                 .setNodeConnectorId(new NodeConnectorId(PORT_NO.toString()))
70                 .setTransmittedBytes(new Counter64(BigInteger.ONE))
71                 .setTransmissionErrors(new Counter64(BigInteger.ONE))
72                 .setTransmittedBytes(new Counter64(BigInteger.ONE))
73                 .build();
74
75         final MultipartReply reply = new MultipartReplyBuilder()
76                 .setMultipartReplyBody(new MultipartReplyQueueStatsBuilder()
77                         .setQueueIdAndStatisticsMap(Collections.singletonList(queueStats))
78                         .build())
79                 .build();
80
81         final List<MultipartReply> input = Collections.singletonList(reply);
82         final GetQueueStatisticsOutput output = service.buildReply(input, true);
83         assertTrue(output.getQueueIdAndStatisticsMap().size() > 0);
84
85         final QueueIdAndStatisticsMap map = output.getQueueIdAndStatisticsMap().get(0);
86         assertEquals(map.getQueueId().getValue(), QUEUE_NO);
87         assertEquals(map.getNodeConnectorId().getValue(), PORT_NO.toString());
88     }
89
90     @Override
91     public void testStoreStatistics() throws Exception {
92         final QueueIdAndStatisticsMap map = mock(QueueIdAndStatisticsMap.class);
93         when(map.getQueueId()).thenReturn(new QueueId(QUEUE_NO));
94
95         final List<QueueIdAndStatisticsMap> maps = Arrays.asList(map);
96         final GetQueueStatisticsOutput output = mock(GetQueueStatisticsOutput.class);
97         when(output.getQueueIdAndStatisticsMap()).thenReturn(maps);
98
99         multipartWriterProvider.lookup(MultipartType.OFPMPQUEUE).get().write(output, true);
100         verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
101     }
102 }