Return singleton empty collection instead of null in Read service code 60/2060/2
authorAlessandro Boch <aboch@cisco.com>
Mon, 21 Oct 2013 23:46:15 +0000 (16:46 -0700)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 24 Oct 2013 18:11:48 +0000 (18:11 +0000)
Change-Id: I763bdd6178047e3de43158176c29a8bd6f478b25
Signed-off-by: Alessandro Boch <aboch@cisco.com>
opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/FlowStatisticsConverter.java
opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/OFStatisticsManager.java
opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/PortStatisticsConverter.java
opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/ReadService.java
opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/ReadServiceFilter.java
opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/TableStatisticsConverter.java
opendaylight/sal/implementation/src/main/java/org/opendaylight/controller/sal/implementation/internal/ReadService.java
opendaylight/statisticsmanager/implementation/src/main/java/org/opendaylight/controller/statisticsmanager/internal/StatisticsManager.java

index 6873b528a2631f2d01480387f46be430e58b57c0..74af3db5ef9465e519f36cd5f35e36f196f3f336 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.controller.protocol_plugin.openflow.internal;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6StatsReply;
@@ -34,11 +35,10 @@ public class FlowStatisticsConverter {
     private List<FlowOnNode> flowOnNodeList;
 
     public FlowStatisticsConverter(List<OFStatistics> statsList) {
-        if (statsList == null) {// || statsList.isEmpty()) {
-            this.ofStatsList = new ArrayList<OFStatistics>(1); // dummy list
+        if (statsList == null) {
+            this.ofStatsList = Collections.emptyList();
         } else {
-            this.ofStatsList = statsList; // new
-                                          // ArrayList<OFStatistics>(statsList);
+            this.ofStatsList = statsList;
         }
         this.flowOnNodeList = null;
     }
index 48b46eac85b9fc467387fc6454e21bb156d74796..8a86d2f256fee58addddffaa9b998071b99c58ac 100644 (file)
@@ -86,7 +86,6 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
     private ConcurrentMap<Long, List<OFStatistics>> descStatistics;
     private ConcurrentMap<Long, List<OFStatistics>> portStatistics;
     private ConcurrentMap<Long, List<OFStatistics>> tableStatistics;
-    private List<OFStatistics> dummyList;
     private ConcurrentMap<Long, StatisticsTicks> statisticsTimerTicks;
     protected BlockingQueue<StatsRequest> pendingStatsRequests;
     protected BlockingQueue<Long> switchPortStatsUpdated;
@@ -191,7 +190,6 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
         descStatistics = new ConcurrentHashMap<Long, List<OFStatistics>>();
         portStatistics = new ConcurrentHashMap<Long, List<OFStatistics>>();
         tableStatistics = new ConcurrentHashMap<Long, List<OFStatistics>>();
-        dummyList = new ArrayList<OFStatistics>(1);
         pendingStatsRequests = new LinkedBlockingQueue<StatsRequest>(getStatsQueueSize());
         statisticsTimerTicks = new ConcurrentHashMap<Long, StatisticsTicks>(INITIAL_SIZE);
         switchPortStatsUpdated = new LinkedBlockingQueue<Long>(INITIAL_SIZE);
@@ -522,7 +520,7 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
         List<OFStatistics> values = this.fetchStatisticsFromSwitch(switchId, statType, null);
 
         // If got a valid response update local cache and notify listeners
-        if (values != null && !values.isEmpty()) {
+        if (!values.isEmpty()) {
             switch (statType) {
                 case FLOW:
                 case VENDOR:
@@ -589,7 +587,7 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
     @SuppressWarnings("unchecked")
     private List<OFStatistics> fetchStatisticsFromSwitch(Long switchId,
             OFStatisticsType statsType, Object target) {
-        List<OFStatistics> values = null;
+        List<OFStatistics> values = Collections.emptyList();
         String type = null;
         ISwitch sw = controller.getSwitch(switchId);
 
@@ -608,7 +606,7 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
                     // Malformed request
                     log.warn("Invalid target type for Flow stats request: {}",
                             target.getClass());
-                    return null;
+                    return Collections.emptyList();
                 } else {
                     // Specific flow request
                     match = (OFMatch) target;
@@ -649,7 +647,7 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
                     // Malformed request
                     log.warn("Invalid target type for Port stats request: {}",
                             target.getClass());
-                    return null;
+                    return Collections.emptyList();
                 } else {
                     // Specific port request
                     targetPort = (Short) target;
@@ -676,7 +674,7 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
                         // Malformed request
                         log.warn("Invalid table id for table stats request: {}",
                                 target.getClass());
-                        return null;
+                        return Collections.emptyList();
                     }
                     byte targetTable = (Byte) target;
                     OFTableStatistics specificReq = new OFTableStatistics();
@@ -719,7 +717,7 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
          * Check on emptiness as interference between add and get is still
          * possible on the inner list (the concurrentMap entry's value)
          */
-        return (list == null || list.isEmpty()) ? this.dummyList
+        return (list == null || list.isEmpty()) ? Collections.<OFStatistics>emptyList()
                 : (list.get(0) instanceof OFVendorStatistics) ? this
                         .v6StatsListToOFStatsList(list) : list;
     }
@@ -733,7 +731,7 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
          * possible on the inner list (the concurrentMap entry's value)
          */
         if (statsList == null || statsList.isEmpty()) {
-            return this.dummyList;
+            return Collections.emptyList();
         }
 
         if (statsList.get(0) instanceof OFVendorStatistics) {
@@ -766,22 +764,22 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
                 }
             }
         }
-        return this.dummyList;
+        return Collections.emptyList();
     }
 
     /*
      * Converts the v6 vendor statistics to the OFStatistics
      */
-    private List<OFStatistics> v6StatsListToOFStatsList(
-            List<OFStatistics> statistics) {
+    private List<OFStatistics> v6StatsListToOFStatsList(List<OFStatistics> statistics) {
+        if (statistics == null || statistics.isEmpty()) {
+            return Collections.emptyList();
+        }
         List<OFStatistics> v6statistics = new ArrayList<OFStatistics>();
-        if (statistics != null && !statistics.isEmpty()) {
-            for (OFStatistics stats : statistics) {
-                if (stats instanceof OFVendorStatistics) {
-                    List<OFStatistics> r = getV6ReplyStatistics((OFVendorStatistics) stats);
-                    if (r != null) {
-                        v6statistics.addAll(r);
-                    }
+        for (OFStatistics stats : statistics) {
+            if (stats instanceof OFVendorStatistics) {
+                List<OFStatistics> r = getV6ReplyStatistics((OFVendorStatistics) stats);
+                if (r != null) {
+                    v6statistics.addAll(r);
                 }
             }
         }
@@ -792,8 +790,10 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
             OFVendorStatistics stat) {
         int length = stat.getLength();
         List<OFStatistics> results = new ArrayList<OFStatistics>();
-        if (length < 12)
-            return null; // Nicira Hdr is 12 bytes. We need atleast that much
+        if (length < 12) {
+            // Nicira Hdr is 12 bytes. We need at least that much
+            return Collections.emptyList();
+        }
         ByteBuffer data = ByteBuffer.allocate(length);
         stat.writeTo(data);
         data.rewind();
@@ -805,7 +805,7 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
         int vendor = data.getInt(); // first 4 bytes is vendor id.
         if (vendor != V6StatsRequest.NICIRA_VENDOR_ID) {
             log.warn("Unexpected vendor id: 0x{}", Integer.toHexString(vendor));
-            return null;
+            return Collections.emptyList();
         } else {
             // go ahead by 8 bytes which is 8 bytes of 0
             data.getLong(); // should be all 0's
@@ -818,12 +818,14 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
         while (length > 0) {
             v6statsreply = new V6StatsReply();
             min_len = v6statsreply.getLength();
-            if (length < v6statsreply.getLength())
+            if (length < v6statsreply.getLength()) {
                 break;
+            }
             v6statsreply.setActionFactory(stat.getActionFactory());
             v6statsreply.readFrom(data);
-            if (v6statsreply.getLength() < min_len)
+            if (v6statsreply.getLength() < min_len) {
                 break;
+            }
             v6statsreply.setVendorId(vendor);
             log.trace("V6StatsReply: {}", v6statsreply);
             length -= v6statsreply.getLength();
@@ -845,17 +847,16 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
             }
         }
 
-        List<OFStatistics> list = this.fetchStatisticsFromSwitch(switchId, statType,
-                target);
+        List<OFStatistics> list = this.fetchStatisticsFromSwitch(switchId, statType, target);
 
-        return (list == null) ? null :
-            (statType == OFStatisticsType.VENDOR) ? v6StatsListToOFStatsList(list) : list;
+        return (statType == OFStatisticsType.VENDOR) ? v6StatsListToOFStatsList(list) : list;
     }
 
     @Override
     public List<OFStatistics> getOFDescStatistics(Long switchId) {
-        if (!descStatistics.containsKey(switchId))
-            return this.dummyList;
+        if (!descStatistics.containsKey(switchId)) {
+            return Collections.emptyList();
+        }
 
         return descStatistics.get(switchId);
     }
@@ -863,7 +864,7 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
     @Override
     public List<OFStatistics> getOFPortStatistics(Long switchId) {
         if (!portStatistics.containsKey(switchId)) {
-            return this.dummyList;
+            return Collections.emptyList();
         }
 
         return portStatistics.get(switchId);
@@ -872,7 +873,7 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
     @Override
     public List<OFStatistics> getOFPortStatistics(Long switchId, short portId) {
         if (!portStatistics.containsKey(switchId)) {
-            return this.dummyList;
+            return Collections.emptyList();
         }
         List<OFStatistics> list = new ArrayList<OFStatistics>(1);
         for (OFStatistics stats : portStatistics.get(switchId)) {
@@ -887,7 +888,7 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
     @Override
     public List<OFStatistics> getOFTableStatistics(Long switchId) {
         if (!tableStatistics.containsKey(switchId)) {
-            return this.dummyList;
+            return Collections.emptyList();
         }
 
         return tableStatistics.get(switchId);
@@ -896,7 +897,7 @@ public class OFStatisticsManager implements IOFStatisticsManager, IInventoryShim
     @Override
     public List<OFStatistics> getOFTableStatistics(Long switchId, Byte tableId) {
         if (!tableStatistics.containsKey(switchId)) {
-            return this.dummyList;
+            return Collections.emptyList();
         }
 
         List<OFStatistics> list = new ArrayList<OFStatistics>(1);
index b18eae971b9526fef38a927b798c0a1dccc8e9d9..0fe1c72345810e507c372e379f8e4c8fc3153cbb 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.controller.protocol_plugin.openflow.internal;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import org.opendaylight.controller.sal.core.Node;
@@ -36,7 +37,7 @@ public class PortStatisticsConverter {
     public PortStatisticsConverter(long switchId, List<OFStatistics> statsList) {
         this.switchId = switchId;
         if (statsList == null || statsList.isEmpty()) {
-            this.ofStatsList = new ArrayList<OFStatistics>(1); // dummy list
+            this.ofStatsList = Collections.emptyList();
         } else {
             this.ofStatsList = new ArrayList<OFStatistics>(statsList);
         }
index 215216e25f00a8fb3f9538d59902d9daa5e86357..f1f5944f379cdd96bf49e2293a52cb1e1d111277 100644 (file)
@@ -9,6 +9,7 @@
 
 package org.opendaylight.controller.protocol_plugin.openflow.internal;
 
+import java.util.Collections;
 import java.util.Dictionary;
 import java.util.List;
 import java.util.Set;
@@ -122,12 +123,12 @@ public class ReadService implements IPluginInReadService, IReadFilterInternalLis
     public List<FlowOnNode> readAllFlow(Node node, boolean cached) {
         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
             logger.error("Invalid node type");
-            return null;
+            return Collections.emptyList();
         }
 
         if (!connectionOutService.isLocal(node)) {
             logger.debug("This Controller is not the master for the node : " + node);
-            return null;
+            return Collections.emptyList();
         }
 
         return filter.readAllFlow(containerName, node, cached);
@@ -170,12 +171,12 @@ public class ReadService implements IPluginInReadService, IReadFilterInternalLis
             boolean cached) {
         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
             logger.error("Invalid node type");
-            return null;
+            return Collections.emptyList();
         }
 
         if (!connectionOutService.isLocal(node)) {
             logger.debug("This Controller is not the master for node : " + node);
-            return null;
+            return Collections.emptyList();
         }
 
         return filter.readAllNodeConnector(containerName, node, cached);
@@ -217,12 +218,12 @@ public class ReadService implements IPluginInReadService, IReadFilterInternalLis
     public List<NodeTableStatistics> readAllNodeTable(Node node, boolean cached) {
         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
             logger.error("Invalid node type");
-            return null;
+            return Collections.emptyList();
         }
 
         if (!connectionOutService.isLocal(node)) {
             logger.debug("This Controller is not the master for node : " + node);
-            return null;
+            return Collections.emptyList();
         }
 
         return filter.readAllNodeTable(containerName, node, cached);
index d7bf2e22586188f578c72e02d9c453551a380ccd..b21a105f157809a94d7e4ff3530318c1921e9f4d 100644 (file)
@@ -189,7 +189,7 @@ public class ReadServiceFilter implements IReadServiceFilter, IContainerListener
         List<FlowOnNode> flowOnNodeList = new FlowStatisticsConverter(ofList).getFlowOnNodeList(node);
         List<FlowOnNode> filteredList = filterFlowListPerContainer(container, node, flowOnNodeList);
 
-        return (filteredList == null || filteredList.isEmpty()) ? null : filteredList.get(0);
+        return (filteredList.isEmpty()) ? null : filteredList.get(0);
     }
 
     @Override
@@ -203,10 +203,8 @@ public class ReadServiceFilter implements IReadServiceFilter, IContainerListener
 
         // Convert and filter the statistics per container
         List<FlowOnNode> flowOnNodeList = new FlowStatisticsConverter(ofList).getFlowOnNodeList(node);
-        List<FlowOnNode> filteredList = filterFlowListPerContainer(container, node, flowOnNodeList);
-
-        return (filteredList == null) ? null : filteredList;
 
+        return filterFlowListPerContainer(container, node, flowOnNodeList);
     }
 
     @Override
@@ -233,10 +231,10 @@ public class ReadServiceFilter implements IReadServiceFilter, IContainerListener
      * @param list
      * @return
      */
-    public List<FlowOnNode> filterFlowListPerContainer(String container,
+    private List<FlowOnNode> filterFlowListPerContainer(String container,
             Node nodeId, List<FlowOnNode> list) {
         if (list == null) {
-            return null;
+            return Collections.emptyList();
         }
 
         // Create new filtered list of flows
@@ -260,9 +258,9 @@ public class ReadServiceFilter implements IReadServiceFilter, IContainerListener
      * @param list
      * @return
      */
-    public List<OFStatistics> filterPortListPerContainer(String container, long switchId, List<OFStatistics> list) {
+    private List<OFStatistics> filterPortListPerContainer(String container, long switchId, List<OFStatistics> list) {
         if (list == null) {
-            return null;
+            return Collections.emptyList();
         }
 
         // Create new filtered list of flows
@@ -281,10 +279,10 @@ public class ReadServiceFilter implements IReadServiceFilter, IContainerListener
     }
 
 
-    public List<OFStatistics> filterTableListPerContainer(
+    private List<OFStatistics> filterTableListPerContainer(
             String container, long switchId, List<OFStatistics> list) {
         if (list == null) {
-            return null;
+            return Collections.emptyList();
         }
 
         // Create new filtered list of node tables
index 993f8976fc6299fc3d0bece5d49da0d30432c64d..bcc2445808a677573892b299b16a3a2d6a5beed8 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.controller.protocol_plugin.openflow.internal;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import org.opendaylight.controller.sal.core.Node;
@@ -33,7 +34,7 @@ public class TableStatisticsConverter {
     public TableStatisticsConverter(long switchId, List<OFStatistics> statsList) {
         this.switchId = switchId;
         if (statsList == null || statsList.isEmpty()) {
-            this.ofStatsList = new ArrayList<OFStatistics>(1); // dummy list
+            this.ofStatsList = Collections.emptyList();
         } else {
             this.ofStatsList = new ArrayList<OFStatistics>(statsList);
         }
index bdd546055c22ce29228d587b0a7cc266a7a700e6..12de35f53677492581412017e9c763de96a05c0b 100644 (file)
@@ -12,6 +12,7 @@ package org.opendaylight.controller.sal.implementation.internal;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -201,7 +202,7 @@ public class ReadService implements IReadService, CommandProvider, IPluginOutRea
             }
         }
         logger.warn("Plugin {} unavailable", node.getType());
-        return null;
+        return Collections.emptyList();
     }
 
     @Override
@@ -213,7 +214,7 @@ public class ReadService implements IReadService, CommandProvider, IPluginOutRea
             }
         }
         logger.warn("Plugin {} unavailable", node.getType());
-        return null;
+        return Collections.emptyList();
     }
 
     @Override
@@ -276,7 +277,7 @@ public class ReadService implements IReadService, CommandProvider, IPluginOutRea
             }
         }
         logger.warn("Plugin {} unavailable", node.getType());
-        return null;
+        return Collections.emptyList();
     }
 
     @Override
@@ -288,7 +289,7 @@ public class ReadService implements IReadService, CommandProvider, IPluginOutRea
             }
         }
         logger.warn("Plugin {} unavailable", node.getType());
-        return null;
+        return Collections.emptyList();
     }
 
 
@@ -327,7 +328,7 @@ public class ReadService implements IReadService, CommandProvider, IPluginOutRea
             }
         }
         logger.warn("Plugin {} unavailable", node.getType());
-        return null;
+        return Collections.emptyList();
     }
 
     @Override
index 92ed44efbbde51e80046048f3d4afe9e1a4de56f..aa6e4ac383ee2be208ed8124c1c5248fa95f26dc 100644 (file)
@@ -101,7 +101,6 @@ public class StatisticsManager implements IStatisticsManager, IReadServiceListen
         triggers = new ConcurrentHashMap<Integer, Node>();
     }
 
-    @SuppressWarnings("deprecation")
     private void allocateCaches() {
         if (clusterContainerService == null) {
             nonClusterObjectCreate();
@@ -126,7 +125,7 @@ public class StatisticsManager implements IStatisticsManager, IReadServiceListen
             log.debug("Skipping statistics cache creation - already present");
         }
     }
-    @SuppressWarnings({ "unchecked", "deprecation" })
+    @SuppressWarnings({ "unchecked" })
     private void retrieveCaches() {
         ConcurrentMap<?, ?> map;
 
@@ -426,7 +425,7 @@ public class StatisticsManager implements IStatisticsManager, IReadServiceListen
     @Override
     public List<NodeConnectorStatistics> getNodeConnectorStatistics(Node node) {
         if (node == null){
-            return null;
+            return Collections.emptyList();
         }
 
         List<NodeConnectorStatistics> statList = new ArrayList<NodeConnectorStatistics>();
@@ -456,7 +455,7 @@ public class StatisticsManager implements IStatisticsManager, IReadServiceListen
     @Override
     public List<NodeTableStatistics> getNodeTableStatistics(Node node){
         if (node == null){
-            return null;
+            return Collections.emptyList();
         }
         List<NodeTableStatistics> statList = new ArrayList<NodeTableStatistics>();
         List<NodeTableStatistics> cachedList = tableStatistics.get(node);