993f8976fc6299fc3d0bece5d49da0d30432c64d
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / internal / TableStatisticsConverter.java
1 /*
2  * Copyright (c) 2013 Big Switch Networks, Inc.  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 package org.opendaylight.controller.protocol_plugin.openflow.internal;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import org.opendaylight.controller.sal.core.Node;
14 import org.opendaylight.controller.sal.reader.NodeTableStatistics;
15 import org.opendaylight.controller.sal.utils.NodeCreator;
16 import org.openflow.protocol.statistics.OFStatistics;
17 import org.openflow.protocol.statistics.OFTableStatistics;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Converts an openflow list of table statistics in a SAL list of
23  * NodeTableStatistics objects
24  */
25 public class TableStatisticsConverter {
26     private static final Logger log = LoggerFactory
27             .getLogger(TableStatisticsConverter.class);
28
29     private final long switchId;
30     private List<OFStatistics> ofStatsList;
31     private List<NodeTableStatistics> ntStatsList;
32
33     public TableStatisticsConverter(long switchId, List<OFStatistics> statsList) {
34         this.switchId = switchId;
35         if (statsList == null || statsList.isEmpty()) {
36             this.ofStatsList = new ArrayList<OFStatistics>(1); // dummy list
37         } else {
38             this.ofStatsList = new ArrayList<OFStatistics>(statsList);
39         }
40         this.ntStatsList = null;
41     }
42
43     public List<NodeTableStatistics> getNodeTableStatsList() {
44         if (this.ofStatsList != null && this.ntStatsList == null) {
45             this.ntStatsList = new ArrayList<NodeTableStatistics>();
46             OFTableStatistics ofTableStat;
47             Node node = NodeCreator.createOFNode(switchId);
48             for (OFStatistics ofStat : this.ofStatsList) {
49                 ofTableStat = (OFTableStatistics) ofStat;
50                 NodeTableStatistics ntStat = new NodeTableStatistics();
51                 ntStat.setNodeTable(TableConverter.toNodeTable(
52                         ofTableStat.getTableId(), node));
53                 ntStat.setActiveCount(ofTableStat.getActiveCount());
54                 ntStat.setLookupCount(ofTableStat.getLookupCount());
55                 ntStat.setMatchedCount(ofTableStat.getMatchedCount());
56                 this.ntStatsList.add(ntStat);
57             }
58         }
59         log.trace("OFStatistics: {} NodeTableStatistics: {}", ofStatsList,
60                 ntStatsList);
61         return this.ntStatsList;
62     }
63 }