Fixing a recent breakage in Flow programmer introduced by https://git.opendaylight...
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / internal / ReadService.java
index 20d13b7a3dc9325244ff05f5f586cd1b8e31e1d9..f1f5944f379cdd96bf49e2293a52cb1e1d111277 100644 (file)
@@ -9,14 +9,16 @@
 
 package org.opendaylight.controller.protocol_plugin.openflow.internal;
 
+import java.util.Collections;
 import java.util.Dictionary;
 import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArraySet;
 
 import org.apache.felix.dm.Component;
-import org.opendaylight.controller.protocol_plugin.openflow.IPluginReadServiceFilter;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
+import org.opendaylight.controller.protocol_plugin.openflow.IReadFilterInternalListener;
+import org.opendaylight.controller.protocol_plugin.openflow.IReadServiceFilter;
+import org.opendaylight.controller.sal.connection.IPluginOutConnectionService;
 import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.core.Node.NodeIDType;
 import org.opendaylight.controller.sal.core.NodeConnector;
@@ -24,21 +26,23 @@ import org.opendaylight.controller.sal.core.NodeTable;
 import org.opendaylight.controller.sal.flowprogrammer.Flow;
 import org.opendaylight.controller.sal.reader.FlowOnNode;
 import org.opendaylight.controller.sal.reader.IPluginInReadService;
+import org.opendaylight.controller.sal.reader.IPluginOutReadService;
 import org.opendaylight.controller.sal.reader.NodeConnectorStatistics;
 import org.opendaylight.controller.sal.reader.NodeDescription;
 import org.opendaylight.controller.sal.reader.NodeTableStatistics;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Container Instance of IPluginInReadService implementation class
- *
- *
- *
  */
-public class ReadService implements IPluginInReadService {
+public class ReadService implements IPluginInReadService, IReadFilterInternalListener {
     private static final Logger logger = LoggerFactory
             .getLogger(ReadService.class);
-    private IPluginReadServiceFilter filter;
+    private IReadServiceFilter filter;
+    private Set<IPluginOutReadService> pluginOutReadServices = new CopyOnWriteArraySet<IPluginOutReadService>();
     private String containerName;
+    private IPluginOutConnectionService connectionOutService;
 
     /**
      * Function called by the dependency manager when all the required
@@ -48,8 +52,7 @@ public class ReadService implements IPluginInReadService {
     @SuppressWarnings("unchecked")
     void init(Component c) {
         Dictionary<Object, Object> props = c.getServiceProperties();
-        containerName = (props != null) ? (String) props.get("containerName")
-                : null;
+        containerName = (props != null) ? (String) props.get("containerName") : null;
     }
 
     /**
@@ -59,6 +62,7 @@ public class ReadService implements IPluginInReadService {
      *
      */
     void destroy() {
+        pluginOutReadServices.clear();
     }
 
     /**
@@ -79,14 +83,28 @@ public class ReadService implements IPluginInReadService {
     void stop() {
     }
 
-    public void setService(IPluginReadServiceFilter filter) {
+    public void setService(IReadServiceFilter filter) {
         this.filter = filter;
     }
 
-    public void unsetService(IPluginReadServiceFilter filter) {
+    public void unsetService(IReadServiceFilter filter) {
         this.filter = null;
     }
 
+    public void setPluginOutReadServices(IPluginOutReadService service) {
+        logger.trace("Got a service set request {}", service);
+        if (this.pluginOutReadServices != null) {
+            this.pluginOutReadServices.add(service);
+        }
+    }
+
+    public void unsetPluginOutReadServices(
+            IPluginOutReadService service) {
+        logger.trace("Got a service UNset request");
+        if (this.pluginOutReadServices != null) {
+            this.pluginOutReadServices.remove(service);
+        }
+    }
     @Override
     public FlowOnNode readFlow(Node node, Flow flow, boolean cached) {
         if (!node.getType().equals(NodeIDType.OPENFLOW)) {
@@ -94,6 +112,10 @@ public class ReadService implements IPluginInReadService {
             return null;
         }
 
+        if (!connectionOutService.isLocal(node)) {
+            logger.debug("This Controller is not the master for the node : " + node);
+            return null;
+        }
         return filter.readFlow(containerName, node, flow, cached);
     }
 
@@ -101,7 +123,12 @@ public class ReadService implements IPluginInReadService {
     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 Collections.emptyList();
         }
 
         return filter.readAllFlow(containerName, node, cached);
@@ -114,6 +141,11 @@ public class ReadService implements IPluginInReadService {
             return null;
         }
 
+        if (!connectionOutService.isLocal(node)) {
+            logger.debug("This Controller is not the master for the node : " + node);
+            return null;
+        }
+
         return filter.readDescription(node, cached);
     }
 
@@ -125,6 +157,12 @@ public class ReadService implements IPluginInReadService {
             logger.error("Invalid node type");
             return null;
         }
+
+        if (!connectionOutService.isLocal(connector.getNode())) {
+            logger.debug("This Controller is not the master for connector : "+connector);
+            return null;
+        }
+
         return filter.readNodeConnector(containerName, connector, cached);
     }
 
@@ -133,7 +171,12 @@ public class ReadService implements IPluginInReadService {
             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 Collections.emptyList();
         }
 
         return filter.readAllNodeConnector(containerName, node, cached);
@@ -146,6 +189,12 @@ public class ReadService implements IPluginInReadService {
             logger.error("Invalid node type");
             return 0;
         }
+
+        if (!connectionOutService.isLocal(connector.getNode())) {
+            logger.debug("This Controller is not the master for connector : "+connector);
+            return 0;
+        }
+
         return filter.getTransmitRate(containerName, connector);
     }
 
@@ -156,6 +205,12 @@ public class ReadService implements IPluginInReadService {
             logger.error("Invalid node type");
             return null;
         }
+
+        if (!connectionOutService.isLocal(table.getNode())) {
+            logger.debug("This Controller is not the master for connector : "+table);
+            return null;
+        }
+
         return filter.readNodeTable(containerName, table, cached);
     }
 
@@ -163,9 +218,68 @@ public class ReadService implements IPluginInReadService {
     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 Collections.emptyList();
         }
 
         return filter.readAllNodeTable(containerName, node, cached);
     }
+
+    @Override
+    public void nodeFlowStatisticsUpdated(Node node, List<FlowOnNode> flowStatsList) {
+        if (!connectionOutService.isLocal(node)) {
+            logger.debug("This Controller is not the master for node : " + node);
+            return;
+        }
+        for (IPluginOutReadService service : pluginOutReadServices) {
+            service.nodeFlowStatisticsUpdated(node, flowStatsList);
+        }
+    }
+
+    @Override
+    public void nodeConnectorStatisticsUpdated(Node node, List<NodeConnectorStatistics> ncStatsList) {
+        if (!connectionOutService.isLocal(node)) {
+            logger.debug("This Controller is not the master for node : " + node);
+            return;
+        }
+        for (IPluginOutReadService service : pluginOutReadServices) {
+            service.nodeConnectorStatisticsUpdated(node, ncStatsList);
+        }
+    }
+
+    @Override
+    public void nodeTableStatisticsUpdated(Node node, List<NodeTableStatistics> tableStatsList) {
+        if (!connectionOutService.isLocal(node)) {
+            logger.debug("This Controller is not the master for node : " + node);
+            return;
+        }
+        for (IPluginOutReadService service : pluginOutReadServices) {
+            service.nodeTableStatisticsUpdated(node, tableStatsList);
+        }
+    }
+
+    @Override
+    public void nodeDescriptionStatisticsUpdated(Node node, NodeDescription nodeDescription) {
+        if (!connectionOutService.isLocal(node)) {
+            logger.debug("This Controller is not the master for node : " + node);
+            return;
+        }
+        for (IPluginOutReadService service : pluginOutReadServices) {
+            service.descriptionStatisticsUpdated(node, nodeDescription);
+        }
+    }
+
+    void setIPluginOutConnectionService(IPluginOutConnectionService s) {
+        connectionOutService = s;
+    }
+
+    void unsetIPluginOutConnectionService(IPluginOutConnectionService s) {
+        if (connectionOutService == s) {
+            connectionOutService = null;
+        }
+    }
 }