Add test for BSN mirror action and config extensions
diff --git a/tests/bsn_mirror.py b/tests/bsn_mirror.py
new file mode 100644
index 0000000..c0ecc07
--- /dev/null
+++ b/tests/bsn_mirror.py
@@ -0,0 +1,168 @@
+"""
+"""
+import struct
+
+import logging
+
+from oftest import config
+import oftest.controller as controller
+import oftest.cstruct as ofp
+import oftest.message as message
+import oftest.action as action
+import oftest.action_list as action_list
+import oftest.base_tests as base_tests
+
+from oftest.testutils import *
+
+class bsn_action_mirror(action.action_vendor):
+    def __init__(self):
+        self.type = ofp.OFPAT_VENDOR
+        self.len = 24
+        self.vendor = 0x005c16c7
+        self.subtype = 1
+        self.dest_port = 0
+        self.vlan_tag = 0
+        self.copy_stage = 0
+
+    def __assert(self):
+        return (True, None)
+
+    def pack(self, assertstruct=True):
+        return struct.pack("!HHLLLLBBBB", self.type, self.len, self.vendor,
+                           self.subtype, self.dest_port, self.vlan_tag,
+                           self.copy_stage, 0, 0, 0)
+
+    def unpack(self, binaryString):
+        if len(binaryString) < self.len:
+            raise Exception("too short")
+        x = struct.unpack("!HHLLLLBBBB", binaryString[:self.len])
+        if x[0] != self.type:
+            raise Exception("wrong type")
+        if x[1] != self.len:
+            raise Exception("wrong length")
+        if x[2] != self.vendor:
+            raise Exception("wrong vendor")
+        if x[3] != self.subtype:
+            raise Exception("wrong subtype")
+        self.dest_port = x[4]
+        self.vlan_tag = x[5]
+        self.copy_stage = x[6]
+        return binaryString[self.len:]
+
+    def __len__(self):
+        return self.len
+
+    def __eq__(self, other):
+        if type(self) != type(other): return False
+        if self.type != other.type: return False
+        if self.len != other.len: return False
+        if self.vendor != other.vendor: return False
+        if self.subtype != other.subtype: return False
+        if self.dest_port != other.dest_port: return False
+        if self.vlan_tag != other.vlan_tag: return False
+        if self.copy_stage != other.copy_stage: return False
+        return True
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
+
+    def show(self, prefix=""):
+        outstr = prefix + "action_vendor\n"
+        for f in ["type", "len", "vendor", "subtype", "dest_port", "vlan_tag",
+                  "copy_stage"]:
+            outstr += prefix + ("%s: %s\n" % (f, getattr(self, f)))
+        return outstr
+
+action_list.action_object_map[ofp.OFPAT_VENDOR] = bsn_action_mirror
+
+class BSNMirrorAction(base_tests.SimpleDataPlane):
+    """
+    Exercise BSN vendor extension for copying packets to a mirror destination
+    port
+    """
+
+    priority = -1
+
+    def bsn_set_mirroring(self, enabled):
+        """
+        Use the BSN_SET_MIRRORING vendor command to enable/disable
+        mirror action support
+        """
+        m = message.vendor()
+        m.vendor = 0x005c16c7
+        m.data = struct.pack("!LBBBB", 3, enabled, 0, 0, 0)
+        rc = self.controller.message_send(m)
+        self.assertNotEqual(rc, -1, "Error sending set mirroring command")
+
+    def bsn_get_mirroring(self):
+        """
+        Use the BSN_GET_MIRRORING_REQUEST vendor command to get the
+        enabled/disabled state of mirror action support
+        """
+        m = message.vendor()
+        m.vendor = 0x005c16c7
+        m.data = struct.pack("!LBBBB", 4, 0, 0, 0, 0)
+        rc = self.controller.message_send(m)
+        self.assertNotEqual(rc, -1, "Error sending get mirroring command")
+        m, r = self.controller.poll(ofp.OFPT_VENDOR, 2)
+        self.assertEqual(m.vendor, 0x005c16c7, "Wrong vendor ID")
+        x = struct.unpack("!LBBBB", m.data)
+        self.assertEqual(x[0], 5, "Wrong subtype")
+        return x[1]
+
+    def runTest(self):
+        mirror_ports = test_param_get("mirror_ports")
+        ports = [p for p in config["port_map"].keys() if p not in mirror_ports]
+        pkt = simple_tcp_packet()
+        match = packet_to_flow_match(self, pkt)
+        match.in_port = ports[0]
+        match.wildcards &= ~ofp.OFPFW_IN_PORT
+
+        logging.info("Checking that mirror ports are not reported")
+        self.assertEqual(bool(self.bsn_get_mirroring()), False)
+        m, r = self.controller.transact(message.features_request(), 2)
+        p = dict([(pt.port_no, pt) for pt in m.ports])
+        self.assertFalse(mirror_ports[0] in p or mirror_ports[1] in p,
+                         "Mirror port in features reply")
+
+        logging.info("Enabling mirror port reporting")
+        self.bsn_set_mirroring(True)
+
+        logging.info("Checking that mirror ports are reported")
+        self.assertEqual(bool(self.bsn_get_mirroring()), True)
+        m, r = self.controller.transact(message.features_request(), 2)
+        p = dict([(pt.port_no, pt) for pt in m.ports])
+        self.assertTrue(mirror_ports[0] in p and mirror_ports[1] in p,
+                        "Mirror port not in features reply")
+        self.assertTrue(p[mirror_ports[0]].config & (1 << 31),
+                        "Mirror port config flag not set in features reply")
+        self.assertTrue(p[mirror_ports[1]].config & (1 << 31),
+                        "Mirror port config flag not set in features reply")
+
+        act1 = bsn_action_mirror()
+        act1.dest_port = mirror_ports[0]
+        act1.copy_stage = 0
+        act2 = bsn_action_mirror()
+        act2.dest_port = mirror_ports[1]
+        act2.copy_stage = 0
+        act3 = action.action_output()
+        act3.port = ports[1]
+        flow_mod = message.flow_mod()
+        flow_mod.match = match
+        self.assertTrue(flow_mod.actions.add(act1), "Could not add mirror action")
+        self.assertTrue(flow_mod.actions.add(act2), "Could not add mirror action")
+        self.assertTrue(flow_mod.actions.add(act3), "Could not add output action")
+        self.assertEqual(delete_all_flows(self.controller), 0,
+                         "Failed to delete all flows")
+        self.assertNotEqual(self.controller.message_send(flow_mod), -1,
+                            "Error installing flow mod")
+        self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
+        
+        logging.info("Sending packet to port %s" % ports[0])
+        self.dataplane.send(ports[0], str(pkt))
+        logging.info("Checking that packet was received from output port %s, "
+                     "mirror ports %s and %s" % (
+              ports[1], mirror_ports[0], mirror_ports[1]))
+        receive_pkt_check(self.dataplane, pkt,
+                          [ports[1], mirror_ports[0], mirror_ports[1]], [],
+                          self)