Return singleton empty collection instead of null in Read service code
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / internal / DataPacketMuxDemux.java
index cc8bcfa6b9c7c25fe828a4c7d8346dc161df4405..a1fcd1ab23b7c5a8836d5ad1200533b5a08a03ec 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
@@ -10,6 +9,7 @@
 package org.opendaylight.controller.protocol_plugin.openflow.internal;
 
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -23,18 +23,10 @@ import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimExtern
 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
 import org.opendaylight.controller.protocol_plugin.openflow.core.IMessageListener;
 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
-import org.openflow.protocol.OFMessage;
-import org.openflow.protocol.OFPacketIn;
-import org.openflow.protocol.OFPacketOut;
-import org.openflow.protocol.OFPort;
-import org.openflow.protocol.OFType;
-import org.openflow.protocol.action.OFAction;
-import org.openflow.protocol.action.OFActionOutput;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
+import org.opendaylight.controller.sal.connection.IPluginOutConnectionService;
 import org.opendaylight.controller.sal.core.ConstructionException;
 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;
@@ -44,21 +36,33 @@ import org.opendaylight.controller.sal.packet.IPluginOutDataPacketService;
 import org.opendaylight.controller.sal.packet.PacketResult;
 import org.opendaylight.controller.sal.packet.RawPacket;
 import org.opendaylight.controller.sal.utils.GlobalConstants;
+import org.opendaylight.controller.sal.utils.HexEncode;
+import org.openflow.protocol.OFMessage;
+import org.openflow.protocol.OFPacketIn;
+import org.openflow.protocol.OFPacketOut;
+import org.openflow.protocol.OFPort;
+import org.openflow.protocol.OFType;
+import org.openflow.protocol.action.OFAction;
+import org.openflow.protocol.action.OFActionOutput;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class DataPacketMuxDemux implements IContainerListener,
-        IMessageListener, IDataPacketMux, IInventoryShimExternalListener {
+        IMessageListener, IDataPacketMux, IInventoryShimExternalListener, IContainerAware {
     protected static final Logger logger = LoggerFactory
             .getLogger(DataPacketMuxDemux.class);
     private IController controller = null;
     private ConcurrentMap<Long, ISwitch> swID2ISwitch = new ConcurrentHashMap<Long, ISwitch>();
     // Gives a map between a Container and all the DataPacket listeners on SAL
     private ConcurrentMap<String, IPluginOutDataPacketService> pluginOutDataPacketServices = new ConcurrentHashMap<String, IPluginOutDataPacketService>();
-    // Gives a map between a NodeConnector and the containers to which it belongs
+    // Gives a map between a NodeConnector and the containers to which it
+    // belongs
     private ConcurrentMap<NodeConnector, List<String>> nc2Container = new ConcurrentHashMap<NodeConnector, List<String>>();
     // Gives a map between a Container and the FlowSpecs on it
     private ConcurrentMap<String, List<ContainerFlow>> container2FlowSpecs = new ConcurrentHashMap<String, List<ContainerFlow>>();
     // Track local data packet listener
     private List<IDataPacketListen> iDataPacketListen = new CopyOnWriteArrayList<IDataPacketListen>();
+    private IPluginOutConnectionService connectionOutService;
 
     void setIDataPacketListen(IDataPacketListen s) {
         if (this.iDataPacketListen != null) {
@@ -127,6 +131,16 @@ public class DataPacketMuxDemux implements IContainerListener,
         }
     }
 
+    void setIPluginOutConnectionService(IPluginOutConnectionService s) {
+        connectionOutService = s;
+    }
+
+    void unsetIPluginOutConnectionService(IPluginOutConnectionService s) {
+        if (connectionOutService == s) {
+            connectionOutService = null;
+        }
+    }
+
     /**
      * Function called by the dependency manager when all the required
      * dependencies are satisfied
@@ -137,9 +151,9 @@ public class DataPacketMuxDemux implements IContainerListener,
     }
 
     /**
-     * Function called by the dependency manager when at least one
-     * dependency become unsatisfied or when the component is shutting
-     * down because for example bundle is being stopped.
+     * Function called by the dependency manager when at least one dependency
+     * become unsatisfied or when the component is shutting down because for
+     * example bundle is being stopped.
      *
      */
     void destroy() {
@@ -158,13 +172,26 @@ public class DataPacketMuxDemux implements IContainerListener,
         if (sw == null || msg == null
                 || this.pluginOutDataPacketServices == null) {
             // Something fishy, we cannot do anything
-               logger.debug("sw: {} and/or msg: {} and/or pluginOutDataPacketServices: {} is null!",
-                                       new Object[]{sw, msg, this.pluginOutDataPacketServices});
+            logger.debug(
+                    "sw: {} and/or msg: {} and/or pluginOutDataPacketServices: {} is null!",
+                    new Object[] { sw, msg, this.pluginOutDataPacketServices });
             return;
         }
+
+        Long ofSwitchID = Long.valueOf(sw.getId());
+        try {
+            Node n = new Node(Node.NodeIDType.OPENFLOW, ofSwitchID);
+            if (!connectionOutService.isLocal(n)) {
+                logger.debug("Connection service refused DataPacketMuxDemux receive {} {}", sw, msg);
+                return;
+            }
+        }
+        catch (Exception e) {
+            return;
+        }
+
         if (msg instanceof OFPacketIn) {
             OFPacketIn ofPacket = (OFPacketIn) msg;
-            Long ofSwitchID = Long.valueOf(sw.getId());
             Short ofPortID = Short.valueOf(ofPacket.getInPort());
 
             try {
@@ -193,11 +220,18 @@ public class DataPacketMuxDemux implements IContainerListener,
                         .get(GlobalConstants.DEFAULT.toString());
                 if (defaultOutService != null) {
                     defaultOutService.receiveDataPacket(dataPacket);
-                    logger.trace("Dispatched to apps a frame of size: {} on container: {}",
-                            ofPacket.getPacketData().length, GlobalConstants.DEFAULT.toString());
+                    if (logger.isTraceEnabled()) {
+                      logger.trace(
+                              "Dispatched to apps a frame of size: {} on " +
+                              "container: {}: {}", new Object[] {
+                                    ofPacket.getPacketData().length,
+                                    GlobalConstants.DEFAULT.toString(),
+                                    HexEncode.bytesToHexString(dataPacket
+                                            .getPacketData()) });
+                    }
                 }
                 // Now check the mapping between nodeConnector and
-                // Container and later on optinally filter based on
+                // Container and later on optimally filter based on
                 // flowSpec
                 List<String> containersRX = this.nc2Container.get(p);
                 if (containersRX != null) {
@@ -208,9 +242,15 @@ public class DataPacketMuxDemux implements IContainerListener,
                         if (s != null) {
                             // TODO add filtering on a per-flowSpec base
                             s.receiveDataPacket(dataPacket);
-                            logger.trace("Dispatched to apps a frame of size: {} on container: {}",
-                                    ofPacket.getPacketData().length, GlobalConstants.DEFAULT.toString());
-
+                            if (logger.isTraceEnabled()) {
+                              logger.trace(
+                                      "Dispatched to apps a frame of size: {}" +
+                                      " on container: {}: {}", new Object[] {
+                                            ofPacket.getPacketData().length,
+                                            container,
+                                            HexEncode.bytesToHexString(dataPacket
+                                                    .getPacketData()) });
+                            }
                         }
                     }
                 }
@@ -233,20 +273,26 @@ public class DataPacketMuxDemux implements IContainerListener,
     public void transmitDataPacket(RawPacket outPkt) {
         // Sanity check area
         if (outPkt == null) {
-               logger.debug("outPkt is null!");
+            logger.debug("outPkt is null!");
             return;
         }
 
         NodeConnector outPort = outPkt.getOutgoingNodeConnector();
         if (outPort == null) {
-               logger.debug("outPort is null! outPkt: {}", outPkt);
+            logger.debug("outPort is null! outPkt: {}", outPkt);
             return;
         }
 
+        if (!connectionOutService.isLocal(outPort.getNode())) {
+            logger.debug("data packets will not be sent to {} in a non-master controller", outPort.toString());
+            return;
+        }
+
+
         if (!outPort.getType().equals(
                 NodeConnector.NodeConnectorIDType.OPENFLOW)) {
             // The output Port is not of type OpenFlow
-               logger.debug("outPort is not OF Type! outPort: {}", outPort);
+            logger.debug("outPort is not OF Type! outPort: {}", outPort);
             return;
         }
 
@@ -257,7 +303,7 @@ public class DataPacketMuxDemux implements IContainerListener,
         if (sw == null) {
             // If we cannot get the controller descriptor we cannot even
             // send out the frame
-               logger.debug("swID: {} - sw is null!", swID);
+            logger.debug("swID: {} - sw is null!", swID);
             return;
         }
 
@@ -265,8 +311,9 @@ public class DataPacketMuxDemux implements IContainerListener,
         // build action
         OFActionOutput action = new OFActionOutput().setPort(port);
         // build packet out
-        OFPacketOut po = new OFPacketOut().setBufferId(
-                OFPacketOut.BUFFER_ID_NONE).setInPort(OFPort.OFPP_NONE)
+        OFPacketOut po = new OFPacketOut()
+                .setBufferId(OFPacketOut.BUFFER_ID_NONE)
+                .setInPort(OFPort.OFPP_NONE)
                 .setActions(Collections.singletonList((OFAction) action))
                 .setActionsLength((short) OFActionOutput.MINIMUM_LENGTH);
 
@@ -274,36 +321,37 @@ public class DataPacketMuxDemux implements IContainerListener,
                 + data.length);
         po.setPacketData(data);
 
-        sw.asyncSend(po);
+        // send PACKET_OUT at high priority
+        sw.asyncFastSend(po);
         logger.trace("Transmitted a frame of size: {}", data.length);
     }
 
     public void addNode(Node node, Set<Property> props) {
         if (node == null) {
-               logger.debug("node is null!");
+            logger.debug("node is null!");
             return;
-        } 
+        }
 
         long sid = (Long) node.getID();
         ISwitch sw = controller.getSwitches().get(sid);
         if (sw == null) {
-               logger.debug("sid: {} - sw is null!", sid);
-               return;
+            logger.debug("sid: {} - sw is null!", sid);
+            return;
         }
         this.swID2ISwitch.put(sw.getId(), sw);
     }
 
     public void removeNode(Node node) {
         if (node == null) {
-               logger.debug("node is null!");
+            logger.debug("node is null!");
             return;
         }
 
         long sid = (Long) node.getID();
         ISwitch sw = controller.getSwitches().get(sid);
         if (sw == null) {
-               logger.debug("sid: {} - sw is null!", sid);
-               return;
+            logger.debug("sid: {} - sw is null!", sid);
+            return;
         }
         this.swID2ISwitch.remove(sw.getId());
     }
@@ -328,14 +376,13 @@ public class DataPacketMuxDemux implements IContainerListener,
         }
         switch (t) {
         case ADDED:
-            if (!fSpecs.contains(previousFlow)) {
-                fSpecs.add(previousFlow);
+            if (!fSpecs.contains(currentFlow)) {
+                fSpecs.add(currentFlow);
             }
+            container2FlowSpecs.put(containerName, fSpecs);
             break;
         case REMOVED:
-            if (fSpecs.contains(previousFlow)) {
-                fSpecs.remove(previousFlow);
-            }
+            fSpecs.remove(currentFlow);
             break;
         case CHANGED:
             break;
@@ -405,4 +452,29 @@ public class DataPacketMuxDemux implements IContainerListener,
             UpdateType type, Set<Property> props) {
         // do nothing
     }
+
+    @Override
+    public void containerCreate(String containerName) {
+        // do nothing
+    }
+
+    @Override
+    public void containerDestroy(String containerName) {
+        Set<NodeConnector> removeNodeConnectorSet = new HashSet<NodeConnector>();
+        for (Map.Entry<NodeConnector, List<String>> entry : nc2Container.entrySet()) {
+            List<String> ncContainers = entry.getValue();
+            if (ncContainers.contains(containerName)) {
+                NodeConnector nodeConnector = entry.getKey();
+                removeNodeConnectorSet.add(nodeConnector);
+            }
+        }
+        for (NodeConnector nodeConnector : removeNodeConnectorSet) {
+            List<String> ncContainers = nc2Container.get(nodeConnector);
+            ncContainers.remove(containerName);
+            if (ncContainers.isEmpty()) {
+                nc2Container.remove(nodeConnector);
+            }
+        }
+        container2FlowSpecs.remove(containerName);
+    }
 }