OpenDaylight Controller functional modules.
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / internal / PortStatisticsConverter.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.protocol_plugin.openflow.internal;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.openflow.protocol.statistics.OFPortStatisticsReply;
16 import org.openflow.protocol.statistics.OFStatistics;
17
18 import org.opendaylight.controller.sal.core.Node;
19 import org.opendaylight.controller.sal.reader.NodeConnectorStatistics;
20 import org.opendaylight.controller.sal.utils.NodeCreator;
21
22 /**
23  * Converts an openflow list of port statistics in a SAL list of
24  * NodeConnectorStatistics objects
25  *
26  *
27  *
28  */
29 public class PortStatisticsConverter {
30     private long switchId;
31     private List<OFStatistics> ofStatsList;
32     private List<NodeConnectorStatistics> ncStatsList;
33
34     public PortStatisticsConverter(long switchId, List<OFStatistics> statsList) {
35         this.switchId = switchId;
36         if (statsList == null || statsList.isEmpty()) {
37             this.ofStatsList = new ArrayList<OFStatistics>(1); // dummy list
38         } else {
39             this.ofStatsList = new ArrayList<OFStatistics>(statsList);
40         }
41         this.ncStatsList = null;
42     }
43
44     public List<NodeConnectorStatistics> getNodeConnectorStatsList() {
45         if (this.ofStatsList != null && this.ncStatsList == null) {
46             this.ncStatsList = new ArrayList<NodeConnectorStatistics>();
47             OFPortStatisticsReply ofPortStat;
48             Node node = NodeCreator.createOFNode(switchId);
49             for (OFStatistics ofStat : this.ofStatsList) {
50                 ofPortStat = (OFPortStatisticsReply) ofStat;
51                 NodeConnectorStatistics NCStat = new NodeConnectorStatistics();
52                 NCStat.setNodeConnector(PortConverter.toNodeConnector(
53                         ofPortStat.getPortNumber(), node));
54                 NCStat.setReceivePacketCount(ofPortStat.getreceivePackets());
55                 NCStat.setTransmitPacketCount(ofPortStat.getTransmitPackets());
56                 NCStat.setReceiveByteCount(ofPortStat.getReceiveBytes());
57                 NCStat.setTransmitByteCount(ofPortStat.getTransmitBytes());
58                 NCStat.setReceiveDropCount(ofPortStat.getReceiveDropped());
59                 NCStat.setTransmitDropCount(ofPortStat.getTransmitDropped());
60                 NCStat.setReceiveErrorCount(ofPortStat.getreceiveErrors());
61                 NCStat.setTransmitErrorCount(ofPortStat.getTransmitErrors());
62                 NCStat.setReceiveFrameErrorCount(ofPortStat
63                         .getReceiveFrameErrors());
64                 NCStat.setReceiveOverRunErrorCount(ofPortStat
65                         .getReceiveOverrunErrors());
66                 NCStat
67                         .setReceiveCRCErrorCount(ofPortStat
68                                 .getReceiveCRCErrors());
69                 NCStat.setCollisionCount(ofPortStat.getCollisions());
70                 this.ncStatsList.add(NCStat);
71             }
72         }
73         return this.ncStatsList;
74     }
75
76 }