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