minimize use of "from module import *"
This statement was causing strange bugs because of namespace pollution. The
order of imports mattered because later "from module import *" statements would
overwrite modules imported earlier. Transitive imports also made it difficult
to figure out where an identifier was being imported.
This statement is still allowed in test code since nothing else should be
importing it.
diff --git a/src/python/oftest/controller.py b/src/python/oftest/controller.py
index 47049a0..31da722 100644
--- a/src/python/oftest/controller.py
+++ b/src/python/oftest/controller.py
@@ -29,16 +29,16 @@
import os
import socket
import time
+import struct
+import select
+import logging
from threading import Thread
from threading import Lock
from threading import Condition
-from of10.message import *
-from of10.parse import *
-from ofutils import *
-# For some reason, it seems select to be last (or later).
-# Otherwise get an attribute error when calling select.select
-import select
-import logging
+import of10.message
+import of10.parse
+import of10.cstruct
+import ofutils
FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.'
@@ -106,7 +106,7 @@
self.message_cv = Condition()
# Used to wake up the event loop from another thread
- self.waker = EventDescriptor()
+ self.waker = ofutils.EventDescriptor()
# Counters
self.socket_errors = 0
@@ -206,7 +206,7 @@
offset = 0
while offset < len(pkt):
# Parse the header to get type
- hdr = of_header_parse(pkt[offset:])
+ hdr = of10.parse.of_header_parse(pkt[offset:])
if not hdr or hdr.length == 0:
self.logger.error("Could not parse header")
self.logger.error("pkt len %d." % len(pkt))
@@ -225,17 +225,17 @@
if self.filter_packet(rawmsg, hdr):
continue
- self.logger.debug("Msg in: buf len %d. hdr.type %s. hdr.len %d" %
- (len(pkt), ofp_type_map[hdr.type], hdr.length))
- if hdr.version != OFP_VERSION:
+ self.logger.debug("Msg in: buf len %d. hdr.type %s. hdr.len %d hdr.version %d" %
+ (len(pkt), of10.cstruct.ofp_type_map[hdr.type], hdr.length, hdr.version))
+ if hdr.version != of10.cstruct.OFP_VERSION:
self.logger.error("Version %d does not match OFTest version %d"
- % (hdr.version, OFP_VERSION))
+ % (hdr.version, of10.cstruct.OFP_VERSION))
print "Version %d does not match OFTest version %d" % \
- (hdr.version, OFP_VERSION)
+ (hdr.version, of10.cstruct.OFP_VERSION)
self.disconnect()
return
- msg = of_message_parse(rawmsg)
+ msg = of10.parse.of_message_parse(rawmsg)
if not msg:
self.parse_errors += 1
self.logger.warn("Could not parse message")
@@ -253,7 +253,7 @@
# Check if keep alive is set; if so, respond to echo requests
if self.keep_alive:
- if hdr.type == OFPT_ECHO_REQUEST:
+ if hdr.type == of10.cstruct.OFPT_ECHO_REQUEST:
self.logger.debug("Responding to echo request")
rep = echo_reply()
rep.header.xid = hdr.xid
@@ -270,7 +270,7 @@
handled = self.handlers["all"](self, msg, rawmsg)
if not handled: # Not handled, enqueue
- self.logger.debug("Enqueuing pkt type " + ofp_type_map[hdr.type])
+ self.logger.debug("Enqueuing pkt type " + of10.cstruct.ofp_type_map[hdr.type])
with self.packets_cv:
if len(self.packets) >= self.max_pkts:
self.packets.pop(0)
@@ -314,7 +314,7 @@
self.switch_socket.setsockopt(socket.IPPROTO_TCP,
socket.TCP_NODELAY, True)
if self.initial_hello:
- self.message_send(hello())
+ self.message_send(of10.message.hello())
self.connect_cv.notify() # Notify anyone waiting
elif s and s == self.switch_socket:
for idx in range(3): # debug: try a couple of times
@@ -443,8 +443,8 @@
self.active = False
else:
with self.connect_cv:
- timed_wait(self.connect_cv, lambda: self.switch_socket,
- timeout=timeout)
+ ofutils.timed_wait(self.connect_cv, lambda: self.switch_socket,
+ timeout=timeout)
return self.switch_socket is not None
@@ -468,9 +468,9 @@
"""
with self.connect_cv:
- timed_wait(self.connect_cv,
- lambda: True if not self.switch_socket else None,
- timeout=timeout)
+ ofutils.timed_wait(self.connect_cv,
+ lambda: True if not self.switch_socket else None,
+ timeout=timeout)
return self.switch_socket is None
def kill(self):
@@ -551,7 +551,7 @@
"""
if exp_msg is not None:
- self.logger.debug("Poll for %s" % ofp_type_map[exp_msg])
+ self.logger.debug("Poll for %s" % of10.cstruct.ofp_type_map[exp_msg])
else:
self.logger.debug("Poll for any OF message")
@@ -563,10 +563,10 @@
(msg, pkt) = self.packets.pop(0)
return (msg, pkt)
else:
- self.logger.debug("Looking for %s" % ofp_type_map[exp_msg])
+ self.logger.debug("Looking for %s" % of10.cstruct.ofp_type_map[exp_msg])
for i in range(len(self.packets)):
msg = self.packets[i][0]
- self.logger.debug("Checking packets[%d] (%s)" % (i, ofp_type_map[msg.header.type]))
+ self.logger.debug("Checking packets[%d] (%s)" % (i, of10.cstruct.ofp_type_map[msg.header.type]))
if msg.header.type == exp_msg:
(msg, pkt) = self.packets.pop(i)
return (msg, pkt)
@@ -575,7 +575,7 @@
return None
with self.packets_cv:
- ret = timed_wait(self.packets_cv, grab, timeout=timeout)
+ ret = ofutils.timed_wait(self.packets_cv, grab, timeout=timeout)
if ret != None:
(msg, pkt) = ret
@@ -601,7 +601,7 @@
"""
if not zero_xid and msg.header.xid == 0:
- msg.header.xid = gen_xid()
+ msg.header.xid = ofutils.gen_xid()
self.logger.debug("Running transaction %d" % msg.header.xid)
@@ -615,7 +615,7 @@
self.message_send(msg.pack())
self.logger.debug("Waiting for transaction %d" % msg.header.xid)
- timed_wait(self.xid_cv, lambda: self.xid_response, timeout=timeout)
+ ofutils.timed_wait(self.xid_cv, lambda: self.xid_response, timeout=timeout)
if self.xid_response:
(resp, pkt) = self.xid_response
@@ -645,16 +645,17 @@
#@todo If not string, try to pack
if type(msg) != type(""):
if msg.header.xid == 0 and not zero_xid:
- msg.header.xid = gen_xid()
+ msg.header.xid = ofutils.gen_xid()
outpkt = msg.pack()
else:
outpkt = msg
msg_version, msg_type, msg_len, msg_xid = struct.unpack_from("!BBHL", outpkt)
- self.logger.debug("Msg out: buf len %d. hdr.type %s. hdr.len %d",
+ self.logger.debug("Msg out: buf len %d. hdr.type %s. hdr.len %d hdr.version %d",
len(outpkt),
- ofp_type_map.get(msg_type, "unknown (%d)" % msg_type),
- msg_len)
+ of10.cstruct.ofp_type_map.get(msg_type, "unknown (%d)" % msg_type),
+ msg_len,
+ msg_version)
if self.switch_socket.sendall(outpkt) is not None:
raise AssertionError("failed to send message to switch")