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
9 package org.opendaylight.openflowplugin.impl.statistics.services.direct.multilayer;
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;
19 import java.math.BigInteger;
20 import java.util.Arrays;
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.opendaylight.direct.statistics.rev160511.GetNodeConnectorStatisticsInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetNodeConnectorStatisticsOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortStatsCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.stats._case.MultipartReplyPortStats;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.stats._case.multipart.reply.port.stats.PortStats;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestPortStatsCase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.port.stats._case.MultipartRequestPortStats;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
37 public class NodeConnectorDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
38 private PortDirectStatisticsService service;
41 public void setUp() throws Exception {
42 service = new PortDirectStatisticsService(requestContextStack,
45 multipartWriterProvider);
49 public void testBuildRequestBody() throws Exception {
50 final GetNodeConnectorStatisticsInput input = mock(GetNodeConnectorStatisticsInput.class);
52 when(input.getNode()).thenReturn(createNodeRef(NODE_ID));
53 when(input.getNodeConnectorId()).thenReturn(nodeConnectorId);
55 final MultipartRequestPortStatsCase body = (MultipartRequestPortStatsCase) ((MultipartRequestInput)service
56 .buildRequest(new Xid(42L), input))
57 .getMultipartRequestBody();
59 final MultipartRequestPortStats nodeConnector = body.getMultipartRequestPortStats();
61 assertEquals(PORT_NO, nodeConnector.getPortNo());
65 public void testBuildReply() throws Exception {
66 final MultipartReply reply = mock(MultipartReply.class);
67 final MultipartReplyPortStatsCase nodeConnectorCase = mock(MultipartReplyPortStatsCase.class);
68 final MultipartReplyPortStats nodeConnector = mock(MultipartReplyPortStats.class);
69 final PortStats nodeConnectorStat = mock(PortStats.class);
70 final List<PortStats> nodeConnectorStats = Arrays.asList(nodeConnectorStat);
71 final List<MultipartReply> input = Arrays.asList(reply);
73 when(nodeConnector.getPortStats()).thenReturn(nodeConnectorStats);
74 when(nodeConnectorCase.getMultipartReplyPortStats()).thenReturn(nodeConnector);
75 when(reply.getMultipartReplyBody()).thenReturn(nodeConnectorCase);
77 when(nodeConnectorStat.getPortNo()).thenReturn(PORT_NO);
78 when(nodeConnectorStat.getTxBytes()).thenReturn(BigInteger.ONE);
79 when(nodeConnectorStat.getCollisions()).thenReturn(BigInteger.ONE);
80 when(nodeConnectorStat.getRxBytes()).thenReturn(BigInteger.ONE);
81 when(nodeConnectorStat.getRxCrcErr()).thenReturn(BigInteger.ONE);
82 when(nodeConnectorStat.getRxDropped()).thenReturn(BigInteger.ONE);
83 when(nodeConnectorStat.getRxErrors()).thenReturn(BigInteger.ONE);
84 when(nodeConnectorStat.getRxFrameErr()).thenReturn(BigInteger.ONE);
85 when(nodeConnectorStat.getRxOverErr()).thenReturn(BigInteger.ONE);
86 when(nodeConnectorStat.getRxPackets()).thenReturn(BigInteger.ONE);
87 when(nodeConnectorStat.getTxDropped()).thenReturn(BigInteger.ONE);
88 when(nodeConnectorStat.getTxErrors()).thenReturn(BigInteger.ONE);
90 final GetNodeConnectorStatisticsOutput output = service.buildReply(input, true);
91 assertTrue(output.getNodeConnectorStatisticsAndPortNumberMap().size() > 0);
93 final NodeConnectorStatisticsAndPortNumberMap stats =
94 output.getNodeConnectorStatisticsAndPortNumberMap().get(0);
96 assertEquals(stats.getNodeConnectorId(), nodeConnectorId);
100 public void testStoreStatistics() throws Exception {
101 final NodeConnectorStatisticsAndPortNumberMap stat = mock(NodeConnectorStatisticsAndPortNumberMap.class);
102 when(stat.getNodeConnectorId()).thenReturn(nodeConnectorId);
104 final List<NodeConnectorStatisticsAndPortNumberMap> stats = Arrays.asList(stat);
105 final GetNodeConnectorStatisticsOutput output = mock(GetNodeConnectorStatisticsOutput.class);
106 when(output.getNodeConnectorStatisticsAndPortNumberMap()).thenReturn(stats);
108 multipartWriterProvider.lookup(MultipartType.OFPMPPORTSTATS).get().write(output, true);
109 verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());