Add port stats test
diff --git a/tests/port_stats.py b/tests/port_stats.py
new file mode 100644
index 0000000..a22c267
--- /dev/null
+++ b/tests/port_stats.py
@@ -0,0 +1,185 @@
+"""
+Flow stats test case.
+Similar to Flow stats test case in the perl test harness.
+
+"""
+
+import logging
+
+import unittest
+import random
+
+import oftest.controller as controller
+import oftest.cstruct as ofp
+import oftest.message as message
+import oftest.dataplane as dataplane
+import oftest.action as action
+import oftest.parse as parse
+import basic
+
+from testutils import *
+from time import sleep
+
+#@var fs_port_map Local copy of the configuration map from OF port
+# numbers to OS interfaces
+fs_port_map = None
+#@var fs_logger Local logger object
+fs_logger = None
+#@var fs_config Local copy of global configuration data
+fs_config = None
+
+# TODO: ovs has problems with VLAN id?
+WILDCARD_VALUES = [ofp.OFPFW_IN_PORT,
+                   # (ofp.OFPFW_DL_VLAN | ofp.OFPFW_DL_VLAN_PCP),
+                   ofp.OFPFW_DL_SRC,
+                   ofp.OFPFW_DL_DST,
+                   (ofp.OFPFW_DL_TYPE | ofp.OFPFW_NW_SRC_ALL |
+                    ofp.OFPFW_NW_DST_ALL | ofp.OFPFW_NW_TOS | ofp.OFPFW_NW_PROTO |
+                    ofp.OFPFW_TP_SRC | ofp.OFPFW_TP_DST),
+                   (ofp.OFPFW_NW_PROTO | ofp.OFPFW_TP_SRC | ofp.OFPFW_TP_DST),
+                   ofp.OFPFW_TP_SRC,
+                   ofp.OFPFW_TP_DST,
+                   ofp.OFPFW_NW_SRC_MASK,
+                   ofp.OFPFW_NW_DST_MASK,
+                   ofp.OFPFW_DL_VLAN_PCP,
+                   ofp.OFPFW_NW_TOS]
+
+def test_set_init(config):
+    """
+    Set up function for packet action test classes
+
+    @param config The configuration dictionary; see oft
+    """
+
+    basic.test_set_init(config)
+
+    global fs_port_map
+    global fs_logger
+    global fs_config
+
+    fs_logger = logging.getLogger("flow_stats")
+    fs_logger.info("Initializing test set")
+    fs_port_map = config["port_map"]
+    fs_config = config
+
+def sendPacket(obj, pkt, ingress_port, egress_port, test_timeout):
+
+    fs_logger.info("Sending packet to dp port " + str(ingress_port) +
+                   ", expecting output on " + str(egress_port))
+    obj.dataplane.send(ingress_port, str(pkt))
+
+    exp_pkt_arg = None
+    exp_port = None
+    if fs_config["relax"]:
+        exp_pkt_arg = pkt
+        exp_port = egress_port
+
+    (rcv_port, rcv_pkt, pkt_time) = obj.dataplane.poll(port_number=exp_port,
+                                                       exp_pkt=exp_pkt_arg)
+    obj.assertTrue(rcv_pkt is not None,
+                   "Packet not received on port " + str(egress_port))
+    fs_logger.debug("Packet len " + str(len(rcv_pkt)) + " in on " + 
+                    str(rcv_port))
+    obj.assertEqual(rcv_port, egress_port,
+                    "Packet received on port " + str(rcv_port) +
+                    ", expected port " + str(egress_port))
+    obj.assertEqual(str(pkt), str(rcv_pkt),
+                    'Response packet does not match send packet')
+
+class SingleFlowStats(basic.SimpleDataPlane):
+    """
+    Verify flow stats are properly retrieved.
+
+    Generate a packet
+    Generate and install a matching flow
+    Send the packet
+    Send a flow stats request to match the flow and retrieve stats
+    Verify that the packet counter has incremented
+    """
+
+    def verifyStats(self, port, test_timeout, packet_sent, packet_recv):
+        stat_req = message.port_stats_request()
+        stat_req.port_no = port
+
+        all_packets_received = 0
+        all_packets_sent = 0
+        for i in range(0,test_timeout):
+            fs_logger.info("Sending stats request")
+            response, pkt = self.controller.transact(stat_req,
+                                                     timeout=test_timeout)
+            self.assertTrue(response is not None, 
+                            "No response to stats request")
+            self.assertTrue(len(response.stats) == 1,
+                            "Did not receive port stats reply")
+            for obj in response.stats:
+                fs_logger.info("Sent " + str(obj.tx_packets) + " packets")
+                if obj.tx_packets == packet_sent:
+                    all_packets_sent = 1
+                fs_logger.info("Received " + str(obj.rx_packets) + " packets")
+                if obj.rx_packets == packet_recv:
+                    all_packets_received = 1
+
+            if all_packets_received and all_packets_sent:
+                break
+            sleep(1)
+
+        self.assertTrue(all_packets_sent,
+                        "Packet sent does not match number sent")
+        self.assertTrue(all_packets_received,
+                        "Packet received does not match number sent")
+
+    def runTest(self):
+        global fs_port_map
+
+        # TODO: set from command-line parameter
+        test_timeout = 60
+
+        of_ports = fs_port_map.keys()
+        of_ports.sort()
+        self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
+
+        rc = delete_all_flows(self.controller, fs_logger)
+        self.assertEqual(rc, 0, "Failed to delete all flows")
+
+        # build packet
+        pkt = simple_tcp_packet()
+        match = parse.packet_to_flow_match(pkt)
+        match.wildcards &= ~ofp.OFPFW_IN_PORT
+        self.assertTrue(match is not None, 
+                        "Could not generate flow match from pkt")
+        act = action.action_output()
+
+        # build flow
+        ingress_port = of_ports[0];
+        egress_port = of_ports[1];
+        fs_logger.info("Ingress " + str(ingress_port) + 
+                       " to egress " + str(egress_port))
+        match.in_port = ingress_port
+        flow_mod_msg = message.flow_mod()
+        flow_mod_msg.match = match
+        flow_mod_msg.cookie = random.randint(0,9007199254740992)
+        flow_mod_msg.buffer_id = 0xffffffff
+        flow_mod_msg.idle_timeout = 0
+        flow_mod_msg.hard_timeout = 0
+        act.port = egress_port
+        self.assertTrue(flow_mod_msg.actions.add(act), "Could not add action")
+       
+        # send flow
+        fs_logger.info("Inserting flow")
+        rv = self.controller.message_send(flow_mod_msg)
+        self.assertTrue(rv != -1, "Error installing flow mod")
+        self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
+
+        # no packets sent, so zero packet count
+        self.verifyStats(ingress_port, test_timeout, 0, 0)
+        self.verifyStats(egress_port, test_timeout, 0, 0)
+
+        # send packet N times
+        num_sends = random.randint(10,20)
+        fs_logger.info("Sending " + str(num_sends) + " test packets")
+        for i in range(0,num_sends):
+            sendPacket(self, pkt, ingress_port, egress_port,
+                       test_timeout)
+
+        self.verifyStats(ingress_port, test_timeout, 0, num_sends)
+        self.verifyStats(egress_port, test_timeout, num_sends, 0)