Major overhaul of oftest command interface

Added tests/oft as top level executable

Support command line options for many config params

Use logging module for output

Got rid of oft_config.py; consolidate configuration in
oft (top level script) and pass around as a dictionary

Add oft_assert.py (the one useful piece of oft_config that
remained).
diff --git a/tests/basic.py b/tests/basic.py
index f7db544..268d313 100644
--- a/tests/basic.py
+++ b/tests/basic.py
@@ -1,6 +1,10 @@
 """
 Basic test cases for the oftest OpenFlow test framework
 
+It is recommended that these definitions be kept in their own
+namespace as different groups of tests will likely define 
+similar identifiers.
+
 Current Assumptions:
 
   The oftest framework source is in ../src/python/oftest
@@ -8,6 +12,7 @@
   The switch is actively attempting to contact the controller at the address
 indicated oin oft_config
 
+
 """
 
 from scapy.all import *
@@ -16,43 +21,73 @@
 import time
 import signal
 import sys
+##@todo Use setup to place OFT modules in path
 sys.path.append("../src/python/oftest")
 from message import *
 from dataplane import *
 from controller import *
-from oft_config import *
+import logging
 
-#debug_level_default = DEBUG_VERBOSE
-dbg_lvl = debug_level_default
+basic_port_map = None
+basic_logger = None
+basic_config = None
+
+def test_set_init(config):
+    """
+    Set up function for basic test classes
+
+    @param config The configuration dictionary; see oft
+    @return TestSuite object for the class; may depend on config
+    """
+
+    global basic_port_map
+    global basic_logger
+    global basic_config
+
+    basic_logger = logging.getLogger("basic")
+    basic_logger.info("Initializing test set")
+    basic_port_map = config["port_map"]
+    basic_config = config
+    return suite()
+
+def suite():
+    suite = unittest.TestSuite()
+    suite.addTest(SimpleProtocolTestCase())
+    suite.addTest(SimpleDataPlaneTestCase())
+    suite.addTest(EchoTestCase())
+    suite.addTest(EchoWithDataTestCase())
+    suite.addTest(PacketInTestCase())
+    suite.addTest(PacketOutTestCase())
+    return suite
 
 class SimpleProtocolTestCase(unittest.TestCase):
     """
     Root class for setting up the controller
     """
 
-    def dbg(self, level, string):
-        debug_log(str(self), dbg_lvl, level, string)
-
     def sig_handler(self):
+        basic_logger.critical("Received interrupt signal; exiting")
         print "Received interrupt signal; exiting"
         self.controller.shutdown()
         sys.exit(1)
 
     def setUp(self):
         signal.signal(signal.SIGINT, self.sig_handler)
-        self.dbg(DEBUG_INFO, "Setup for " + str(self))
-        self.controller = Controller()
+        basic_logger.info("Setup for " + str(self))
+        self.controller = Controller(host=basic_config["controller_host"],
+                                     port=basic_config["controller_port"])
         self.controller.start()
         self.controller.connect(timeout=20)
-        self.dbg(DEBUG_INFO, "Connected " + str(self.controller.switch_addr))
+        basic_logger.info("Connected " + str(self.controller.switch_addr))
 
     def tearDown(self):
-        self.dbg(DEBUG_INFO, "Teardown for simple proto test")
+        basic_logger.info("Teardown for simple proto test")
         self.controller.shutdown()
         # self.controller.join()
 
     def runTest(self):
         # Just a simple sanity check as illustration
+        basic_logger.info("Running simple proto test")
         self.assertTrue(self.controller.switch_socket is not None,
                         str(self) + 'No connection to switch')
 
@@ -63,14 +98,14 @@
     def setUp(self):
         SimpleProtocolTestCase.setUp(self)
         self.dataplane = DataPlane()
-        for of_port, ifname in interface_ofport_map.items():
+        for of_port, ifname in basic_port_map.items():
             self.dataplane.port_add(ifname, of_port)
 
     def tearDown(self):
-        self.dbg(DEBUG_INFO, "Teardown for simple dataplane test")
+        basic_logger.info("Teardown for simple dataplane test")
         SimpleProtocolTestCase.tearDown(self)
-        self.dataplane.kill(join_threads=False)
-        self.dbg(DEBUG_INFO, "Teardown done")
+        self.dataplane.kill(join_threads=True)
+        basic_logger.info("Teardown done")
 
     def runTest(self):
         self.assertTrue(self.controller.switch_socket is not None,
@@ -116,8 +151,8 @@
         # Poll controller with expect message type packet in
         # For now, a random packet from scapy tutorial
 
-        for of_port in interface_ofport_map.keys():
-            self.dbg(DEBUG_INFO, "PKT IN test, port " + str(of_port))
+        for of_port in basic_port_map.keys():
+            basic_logger.info("PKT IN test, port " + str(of_port))
             pkt=Ether()/IP(dst="www.slashdot.org")/TCP()/\
                 ("GET /index.html HTTP/1.0. port" + str(of_port))
             self.dataplane.send(of_port, str(pkt))
@@ -126,8 +161,11 @@
 
             self.assertTrue(response is not None, 
                             'Packet in message not received')
-            # Data has CRC on it, so take off last 4 bytes
-            self.assertEqual(str(pkt), response.data[:-4], 
+            if str(pkt) != response.data:
+                basic_logger.debug("pkt: "+str(pkt)+"  resp: " +
+                                   str(response))
+
+            self.assertEqual(str(pkt), response.data,
                              'Response packet does not match send packet')
 
 class PacketOutTestCase(SimpleDataPlaneTestCase):
@@ -143,27 +181,30 @@
         # These will get put into function
         outpkt=Ether()/IP(dst="www.slashdot.org")/TCP()/\
             "GET /index.html HTTP/1.0 \n\n"
-        msg = packet_out()
-        msg.data = str(outpkt)
-        act = action_output()
-        dp_port = act.port = interface_ofport_map.keys()[0]
-        self.assertTrue(msg.actions.add(act), 'Could not add action to msg')
+        of_ports = basic_port_map.keys()
+        of_ports.sort()
+        for dp_port in of_ports:
+            msg = packet_out()
+            msg.data = str(outpkt)
+            act = action_output()
+            act.port = dp_port
+            self.assertTrue(msg.actions.add(act), 'Could not add action to msg')
 
-        self.dbg(DEBUG_INFO, "pkt out to port " + str(dp_port))
-        rv = self.controller.message_send(msg)
-        self.assertTrue(rv == 0, "Error sending out message")
+            basic_logger.info("PacketOut to: " + str(dp_port))
+            rv = self.controller.message_send(msg)
+            self.assertTrue(rv == 0, "Error sending out message")
 
-        time.sleep(1) # @todo Implement poll timeout for test cases
-        (of_port, pkt, pkt_time) = self.dataplane.poll()
+            (of_port, pkt, pkt_time) = self.dataplane.poll(timeout=1)
 
-        self.assertTrue(pkt is not None, 'Packet not received')
-        if of_port is not None:
-            self.assertEqual(of_port, dp_port, "Unexpected receive port")
-        self.assertEqual(str(outpkt), str(pkt),
-                         'Response packet does not match send packet')
-
+            self.assertTrue(pkt is not None, 'Packet not received')
+            basic_logger.info("PacketOut: got pkt from " + str(of_port))
+            if of_port is not None:
+                self.assertEqual(of_port, dp_port, "Unexpected receive port")
+            self.assertEqual(str(outpkt), str(pkt),
+                             'Response packet does not match send packet')
 
 if __name__ == "__main__":
     unittest.main()
+
 #    suite = unittest.TestLoader().loadTestsFromTestCase(PacketOutTestCase)
 #    unittest.TextTestRunner(verbosity=2).run(suite) 
diff --git a/tests/local.py b/tests/local.py
new file mode 100644
index 0000000..0a3bc04
--- /dev/null
+++ b/tests/local.py
@@ -0,0 +1,15 @@
+"""
+Platform configuration file
+platform == local
+
+Update this file to override defaults
+"""
+
+def platform_config_update(config):
+    """
+    Update configuration for the local platform
+
+    @param config The configuration dictionary to use/update
+
+    Update this routine if values other the defaults are required
+    """
diff --git a/tests/oft b/tests/oft
new file mode 100755
index 0000000..1faa8a1
--- /dev/null
+++ b/tests/oft
@@ -0,0 +1,251 @@
+#!/usr/bin/env python
+"""
+@package oft
+
+OpenFlow test framework top level script
+
+This script is the entry point for running OpenFlow tests
+using the OFT framework.
+
+The global configuration is passed around in a dictionary
+ygenerally called config.  The keys have the following
+significance.
+
+    platform          : String identifying the target platform
+    controller_host   : Host on which test controller is running (for sockets)
+    controller_port   : Port on which test controller listens for switch cxn
+    port_count        : (Optional) Number of ports in dataplane
+    base_of_port      : (Optional) Base OpenFlow port number in dataplane
+    base_if_index     : (Optional) Base OS network interface for dataplane
+    test_spec         : (TBD) Specification of test(s) to run
+    log_file          : Filename for test logging
+    debug             : String giving debug level (info, warning, error...)
+    dbg_level         : logging module value of debug level
+    port_map          : Map of dataplane OpenFlow port to OS interface names
+
+See config_defaults below for the default values.
+
+To add configuration to the system, first add an entry to config_default
+below.  If you want this to be a command line parameter, edit config_setup
+to add the option and default value to the parser.  Then edit config_get
+to make sure the option value gets copied into the configuration 
+structure (which then gets passed to everyone else).
+
+By convention, oft attempts to import the contents of a file by the 
+name of $platform.py into the local namespace.  
+
+IMPORTANT: That file should define a function platform_config_update which
+takes a configuration dictionary as an argument and updates it for the
+current run.  In particular, it should set up config["port_map"] with
+the proper map from OF port numbers to OF interface names.
+
+You can add your own platform, say gp104, by adding a file gp104.py
+that defines the function platform_config_update and then use the
+parameter --platform=gp104 on the command line.
+
+If platform_config_update does not set config["port_map"], an attempt
+is made to generate a default map via the function default_port_map_setup.
+This will use "local" and "remote" for platform names if available
+and generate a sequential map based on the values of base_of_port and
+base_if_index in the configuration structure.
+
+@todo Determine and implement conventions for test_spec.
+
+The current model for test sets is basic.py.  The current convention is
+that the test set should implement a function test_set_init which takes
+an oft configuration dictionary and returns a unittest.TestSuite object.
+Future test sets should do the same thing.
+
+"""
+
+import sys
+from optparse import OptionParser
+import logging
+import unittest
+
+# Import test files
+import basic
+
+##@var DEBUG_LEVELS
+# Map from strings to debugging levels
+DEBUG_LEVELS = {
+    'debug'              : logging.DEBUG,
+    'verbose'            : logging.DEBUG,
+    'info'               : logging.INFO,
+    'warning'            : logging.WARNING,
+    'warn'               : logging.WARNING,
+    'error'              : logging.ERROR,
+    'critical'           : logging.CRITICAL
+}
+
+_debug_default = "warning"
+_debug_level_default = DEBUG_LEVELS[_debug_default]
+
+##@var config_default
+# The default configuration dictionary for OFT
+config_default = {
+    "platform"           : "local",
+    "controller_host"    : "127.0.0.1",
+    "controller_port"    : 6633,
+    "port_count"         : 4,
+    "base_of_port"       : 1,
+    "base_if_index"      : 1,
+    "test_spec"          : "basic",
+    "log_file"           : "oft.log",
+    "debug"              : _debug_default,
+    "dbg_level"          : _debug_level_default,
+    "port_map"           : {}
+}
+
+# Map options to config structure
+def config_get(opts):
+    "Convert options class to OFT configuration dictionary"
+    cfg = config_default.copy()
+    cfg["platform"] = opts.platform
+    cfg["controller_host"] = opts.controller_host
+    cfg["controller_port"] = opts.controller_port
+    cfg["test_spec"] = opts.test_spec
+    cfg["log_file"] = opts.log_file
+    if opts.debug not in DEBUG_LEVELS.keys():
+        print "Warning:  Bad value specified for debug level; using default"
+        opts.debug = _debug_default
+    cfg["debug"] = opts.debug
+    cfg["dbg_level"] = DEBUG_LEVELS[cfg["debug"]]
+    cfg["base_of_port"] = opts.base_of_port
+    cfg["base_if_index"] = opts.base_if_index
+    cfg["port_count"] = opts.port_count
+    return cfg
+
+def config_setup(cfg_dflt):
+    """
+    Set up the configuration including parsing the arguments
+
+    @param cfg_dflt The default configuration dictionary
+    @return A pair (config, args) where config is an config
+    object and args is any additional arguments from the command line
+    """
+
+    parser = OptionParser(version="%prog 0.1")
+
+    # Set up default values
+    parser.set_defaults(platform=cfg_dflt["platform"])
+    parser.set_defaults(controller_host=cfg_dflt["controller_host"])
+    parser.set_defaults(controller_port=cfg_dflt["controller_port"])
+    parser.set_defaults(test_spec=cfg_dflt["test_spec"])
+    parser.set_defaults(log_file=cfg_dflt["log_file"])
+    parser.set_defaults(debug=cfg_dflt["debug"])
+    parser.set_defaults(base_of_port=cfg_dflt["base_of_port"])
+    parser.set_defaults(base_if_index=cfg_dflt["base_if_index"])
+    parser.set_defaults(port_count=cfg_dflt["port_count"])
+
+    plat_help = """Set the platform type.  Valid values include:
+        local:  User space virtual ethernet pair setup
+        remote:  Remote embedded Broadcom based switch
+        """
+    parser.add_option("-P", "--platform", help=plat_help)
+    parser.add_option("-H", "--host", dest="controller_host",
+                      help="The IP/name of the test controller host")
+    parser.add_option("-p", "--port", dest="controller_port",
+                      type="int", help="Port number of the test controller")
+    parser.add_option("--test-spec", "--test-list", 
+                      help="Indicate tests to run (TBD)")
+    parser.add_option("--log-file", 
+                      help="Name of log file, empty string to log to console")
+    parser.add_option("--debug",
+                      help="Debug lvl: debug, info, warning, error, critical")
+    parser.add_option("--port_count",
+                      help="Number of ports to use (optional)")
+    parser.add_option("--base_of_port",
+                      help="Base OpenFlow port number (optional)")
+    parser.add_option("--base_if_index",
+                      help="Base interface index number (optional)")
+    # Might need this if other parsers want command line
+    # parser.allow_interspersed_args = False
+    (options, args) = parser.parse_args()
+
+    config = config_get(options)
+
+    return (config, args)
+
+def logging_setup(config):
+    """
+    Set up logging based on config
+    """
+    _format = "%(asctime)s  %(name)-10s: %(levelname)-8s: %(message)s"
+    _datefmt = "%H:%M:%S"
+    if config["log_file"]:
+        logging.basicConfig(filename=config["log_file"],
+                            level=config["dbg_level"],
+                            format=_format, datefmt=_datefmt)
+    #@todo Handle "no log file"
+
+def default_port_map_setup(config):
+    """
+    Setup the OF port mapping based on config
+    @param config The OFT configuration structure
+    @return Port map dictionary
+    """
+    if (config["base_of_port"] is None) or not config["port_count"]:
+        return None
+    port_map = {}
+    if config["platform"] == "local":
+        # For local, use every other veth port
+        for idx in range(config["port_count"]):
+            port_map[config["base_of_port"] + idx] = "veth" + \
+                str(config["base_if_index"] + (2 * idx))
+    elif config["platform"] == "remote":
+        # For remote, use eth ports
+        for idx in range(config["port_count"]):
+            port_map[config["base_of_port"] + idx] = "eth" + \
+                str(config["base_if_index"] + idx)
+    else:
+        return None
+
+    logging.info("Built default port map")
+    return port_map
+
+#
+# Main script
+#
+
+# Get configuration, set up logging, import platform from file
+(config, args) = config_setup(config_default)
+logging_setup(config)
+logging.info("*** STARTING TEST RUN ***")
+
+of_os_port_map = None
+if config["platform"]:
+    _imp_string = "from " + config["platform"] + " import *"
+    logging.info("Importing: " + _imp_string)
+    try:
+        exec(_imp_string)
+    except:
+        logging.warn("Failed to import " + config["platform"] + " file")
+
+try:
+    platform_config_update(config)
+except:
+    logging.warn("Could not run platform host configuration")
+
+if not config["port_map"]:
+    # Try to set up default port mapping if not done by platform
+    config["port_map"] = default_port_map_setup(config)
+
+if not config["port_map"]:
+    logging.critical("Interface port map is not defined.  Exiting")
+    print("Interface port map is not defined.  Exiting")
+    sys.exit(1)
+
+logging.debug("Configuration: " + str(config))
+logging.info("OF port map: " + str(config["port_map"]))
+
+# Init the test sets
+#@todo Use test-spec from config to determine which tests to run
+basic_suite = basic.test_set_init(config)
+if config["dbg_level"] >= logging.WARNING: _verb = 1
+else: _verb = 2
+
+unittest.TextTestRunner(verbosity=_verb).run(basic_suite)
+
+logging.info("*** END OF TESTS ***")
+
diff --git a/tests/remote.py b/tests/remote.py
new file mode 100644
index 0000000..07779f9
--- /dev/null
+++ b/tests/remote.py
@@ -0,0 +1,22 @@
+"""
+Platform configuration file
+platform == remote
+"""
+
+remote_port_map = {
+    23 : "eth2",
+    24 : "eth3",
+    25 : "eth4",
+    26 : "eth5"
+    }
+
+def platform_config_update(config):
+    """
+    Update configuration for the remote platform
+
+    @param config The configuration dictionary to use/update
+    This routine defines the port map used for this configuration
+    """
+
+    global remote_port_map
+    config["port_map"] = remote_port_map.copy()