Merge "SONAR TD - Remove unused fields, fix naming"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / compatibility / NodeConnectorStatisticsToNotificationTransformer.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.compatibility;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
15 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
16 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.duration.DurationBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.node.connector.statistics.BytesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.node.connector.statistics.PacketsBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortStatsCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.stats._case.MultipartReplyPortStats;
25 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;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdateBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMapBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMapKey;
31
32 /**
33  * pulled out port stats to notification transformation
34  */
35 public class NodeConnectorStatisticsToNotificationTransformer {
36
37     private NodeConnectorStatisticsToNotificationTransformer() {
38         // Hide implicit constructor
39     }
40
41     /**
42      * @param mpReplyList   raw multipart response from device
43      * @param deviceInfo    device basic info
44      * @param ofVersion     device version
45      * @param emulatedTxId
46      * @return notification containing flow stats
47      */
48     public static NodeConnectorStatisticsUpdate transformToNotification(final List<MultipartReply> mpReplyList,
49                                                                         final DeviceInfo deviceInfo,
50                                                                         final OpenflowVersion ofVersion,
51                                                                         final TransactionId emulatedTxId) {
52
53         NodeConnectorStatisticsUpdateBuilder notification = new NodeConnectorStatisticsUpdateBuilder();
54         notification.setId(deviceInfo.getNodeId());
55         notification.setMoreReplies(Boolean.FALSE);
56         notification.setTransactionId(emulatedTxId);
57
58         notification.setNodeConnectorStatisticsAndPortNumberMap(new ArrayList<NodeConnectorStatisticsAndPortNumberMap>());
59         for (MultipartReply mpReply : mpReplyList) {
60             MultipartReplyPortStatsCase caseBody = (MultipartReplyPortStatsCase) mpReply.getMultipartReplyBody();
61
62             MultipartReplyPortStats replyBody = caseBody.getMultipartReplyPortStats();
63             for (PortStats portStats : replyBody.getPortStats()) {
64                 NodeConnectorStatisticsAndPortNumberMapBuilder statsBuilder =
65                         processSingleNodeConnectorStats(deviceInfo, ofVersion, portStats);
66                 notification.getNodeConnectorStatisticsAndPortNumberMap().add(statsBuilder.build());
67             }
68         }
69         return notification.build();
70     }
71
72     @VisibleForTesting
73     static NodeConnectorStatisticsAndPortNumberMapBuilder processSingleNodeConnectorStats(DeviceInfo deviceInfo, OpenflowVersion ofVersion, PortStats portStats) {
74         NodeConnectorStatisticsAndPortNumberMapBuilder statsBuilder =
75                 new NodeConnectorStatisticsAndPortNumberMapBuilder();
76         statsBuilder.setNodeConnectorId(
77                 InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(
78                         deviceInfo.getDatapathId(),
79                         portStats.getPortNo(), ofVersion));
80
81         BytesBuilder bytesBuilder = new BytesBuilder();
82         bytesBuilder.setReceived(portStats.getRxBytes());
83         bytesBuilder.setTransmitted(portStats.getTxBytes());
84         statsBuilder.setBytes(bytesBuilder.build());
85
86         PacketsBuilder packetsBuilder = new PacketsBuilder();
87         packetsBuilder.setReceived(portStats.getRxPackets());
88         packetsBuilder.setTransmitted(portStats.getTxPackets());
89         statsBuilder.setPackets(packetsBuilder.build());
90
91         DurationBuilder durationBuilder = new DurationBuilder();
92         if (portStats.getDurationSec() != null) {
93             durationBuilder.setSecond(new Counter32(portStats.getDurationSec()));
94         }
95         if (portStats.getDurationNsec() != null) {
96             durationBuilder.setNanosecond(new Counter32(portStats.getDurationNsec()));
97         }
98         statsBuilder.setDuration(durationBuilder.build());
99         statsBuilder.setCollisionCount(portStats.getCollisions());
100         statsBuilder.setKey(new NodeConnectorStatisticsAndPortNumberMapKey(statsBuilder.getNodeConnectorId()));
101         statsBuilder.setReceiveCrcError(portStats.getRxCrcErr());
102         statsBuilder.setReceiveDrops(portStats.getRxDropped());
103         statsBuilder.setReceiveErrors(portStats.getRxErrors());
104         statsBuilder.setReceiveFrameError(portStats.getRxFrameErr());
105         statsBuilder.setReceiveOverRunError(portStats.getRxOverErr());
106         statsBuilder.setTransmitDrops(portStats.getTxDropped());
107         statsBuilder.setTransmitErrors(portStats.getTxErrors());
108         return statsBuilder;
109     }
110 }