Fix NPE in FlowProgrammerService in openflow plugin
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / internal / ReadServiceFilter.java
index 1ab89b34cb7c875b65e1dee5be409ebbbc5e1378..b21a105f157809a94d7e4ff3530318c1921e9f4d 100644 (file)
@@ -26,8 +26,8 @@ import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
 import org.opendaylight.controller.sal.action.Action;
 import org.opendaylight.controller.sal.action.ActionType;
 import org.opendaylight.controller.sal.action.Output;
-import org.opendaylight.controller.sal.connection.IPluginOutConnectionService;
 import org.opendaylight.controller.sal.core.ContainerFlow;
+import org.opendaylight.controller.sal.core.IContainerAware;
 import org.opendaylight.controller.sal.core.IContainerListener;
 import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.core.NodeConnector;
@@ -56,7 +56,7 @@ import org.slf4j.LoggerFactory;
  * Read Service shim layer which is in charge of filtering the flow statistics
  * based on container. It is a Global instance.
  */
-public class ReadServiceFilter implements IReadServiceFilter, IContainerListener, IOFStatisticsListener {
+public class ReadServiceFilter implements IReadServiceFilter, IContainerListener, IOFStatisticsListener, IContainerAware {
     private static final Logger logger = LoggerFactory
             .getLogger(ReadServiceFilter.class);
     private IController controller = null;
@@ -65,7 +65,8 @@ public class ReadServiceFilter implements IReadServiceFilter, IContainerListener
     private ConcurrentMap<String, Set<Node>> containerToNode;
     private ConcurrentMap<String, Set<NodeTable>> containerToNt;
     private ConcurrentMap<String, Set<ContainerFlow>> containerFlows;
-    private ConcurrentMap<String, IReadFilterInternalListener> readFilterInternalListeners;
+    private ConcurrentMap<String, IReadFilterInternalListener> readFilterInternalListeners =
+        new ConcurrentHashMap<String, IReadFilterInternalListener>();
 
     public void setController(IController core) {
         this.controller = core;
@@ -120,7 +121,6 @@ public class ReadServiceFilter implements IReadServiceFilter, IContainerListener
         containerToNt = new ConcurrentHashMap<String, Set<NodeTable>>();
         containerToNode = new ConcurrentHashMap<String, Set<Node>>();
         containerFlows = new ConcurrentHashMap<String, Set<ContainerFlow>>();
-        readFilterInternalListeners = new ConcurrentHashMap<String, IReadFilterInternalListener>();
     }
 
     /**
@@ -130,6 +130,7 @@ public class ReadServiceFilter implements IReadServiceFilter, IContainerListener
      *
      */
     void destroy() {
+        readFilterInternalListeners.clear();
     }
 
     /**
@@ -158,17 +159,6 @@ public class ReadServiceFilter implements IReadServiceFilter, IContainerListener
         this.statsMgr = null;
     }
 
-    IPluginOutConnectionService connectionPluginOutService;
-    void setIPluginOutConnectionService(IPluginOutConnectionService s) {
-        connectionPluginOutService = s;
-    }
-
-    void unsetIPluginOutConnectionService(IPluginOutConnectionService s) {
-        if (connectionPluginOutService == s) {
-            connectionPluginOutService = null;
-        }
-    }
-
     @Override
     public FlowOnNode readFlow(String container, Node node, Flow flow, boolean cached) {
 
@@ -199,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
@@ -213,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
@@ -243,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
@@ -270,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
@@ -291,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
@@ -643,4 +631,17 @@ public class ReadServiceFilter implements IReadServiceFilter, IContainerListener
             l.getValue().nodeTableStatisticsUpdated(node, tableStatsList);
         }
     }
+
+    @Override
+    public void containerCreate(String containerName) {
+        // do nothing
+    }
+
+    @Override
+    public void containerDestroy(String containerName) {
+        containerToNc.remove(containerName);
+        containerToNode.remove(containerName);
+        containerToNt.remove(containerName);
+        containerFlows.remove(containerName);
+    }
 }