add ofproto 1.3 coverage, check key-error and attribute-error.

Conflicts: setup.cfg
Backport from https://github.com/faucetsdn/ryu/commit/98d4913
Story: #2009283
Task: #43617

Change-Id: I583942287b91606642c6df4f448ebe749d391f3c
This commit is contained in:
Josh Bailey 2021-06-06 21:52:17 +00:00 committed by elajkat
parent 2e8cf5bed2
commit f1735c323e
5 changed files with 64 additions and 49 deletions

View File

@ -298,7 +298,8 @@ class Datapath(ofproto_protocol.ProtocolDesc):
self.state = state
ev = ofp_event.EventOFPStateChange(self)
ev.state = state
self.ofp_brick.send_event_to_observers(ev, state)
if self.ofp_brick is not None:
self.ofp_brick.send_event_to_observers(ev, state)
# Low level socket handling layer
@_deactivate
@ -343,16 +344,17 @@ class Datapath(ofproto_protocol.ProtocolDesc):
# LOG.debug('queue msg %s cls %s', msg, msg.__class__)
if msg:
ev = ofp_event.ofp_msg_to_ev(msg)
self.ofp_brick.send_event_to_observers(ev, self.state)
if self.ofp_brick is not None:
self.ofp_brick.send_event_to_observers(ev, self.state)
def dispatchers(x):
return x.callers[ev.__class__].dispatchers
def dispatchers(x):
return x.callers[ev.__class__].dispatchers
handlers = [handler for handler in
self.ofp_brick.get_handlers(ev) if
self.state in dispatchers(handler)]
for handler in handlers:
handler(ev)
handlers = [handler for handler in
self.ofp_brick.get_handlers(ev) if
self.state in dispatchers(handler)]
for handler in handlers:
handler(ev)
buf = buf[msg_len:]
buf_len = len(buf)

View File

@ -149,6 +149,7 @@ def register_service(service):
there are applications consuming OFP events.
"""
frame = inspect.currentframe()
m_name = frame.f_back.f_globals['__name__']
m = sys.modules[m_name]
m._SERVICE_NAME = service
if frame is not None:
m_name = frame.f_back.f_globals['__name__']
m = sys.modules[m_name]
m._SERVICE_NAME = service

View File

@ -50,7 +50,7 @@ class MacToPortTable(object):
return self.mac_to_port[dpid].get(mac)
def mac_list(self, dpid, port):
return [mac for (mac, port_) in self.mac_to_port.get(dpid).items()
return [mac for (mac, port_) in self.mac_to_port.get(dpid, {}).items()
if port_ == port]
def mac_del(self, dpid, mac):

View File

@ -47,7 +47,7 @@ def register_switch_address(addr, interval=None):
def _retry_loop():
# Delays registration if ofp_handler is not started yet
while True:
if ofp_handler.controller is not None:
if ofp_handler is not None and ofp_handler.controller is not None:
for a, i in _TMP_ADDRESSES.items():
ofp_handler.controller.spawn_client_loop(a, i)
hub.sleep(1)
@ -69,6 +69,6 @@ def unregister_switch_address(addr):
"""
ofp_handler = app_manager.lookup_service_brick(ofp_event.NAME)
# Do nothing if ofp_handler is not started yet
if ofp_handler.controller is None:
if ofp_handler is None or ofp_handler.controller is None:
return
ofp_handler.controller.stop_client_loop(addr)

View File

@ -1755,7 +1755,7 @@ class OFPMatchField(StringifyMixin):
(value, mask) = struct.unpack_from(pack_str, buf, offset + 4)
else:
(value,) = struct.unpack_from(cls.pack_str, buf, offset + 4)
return cls(header, value, mask)
return cls(header, value, mask) # pytype: disable=wrong-arg-count
def serialize(self, buf, offset):
if ofproto.oxm_tlv_header_extract_hasmask(self.header):
@ -2774,8 +2774,9 @@ class OFPFlowMod(MsgBase):
try:
while offset < msg_len:
i = OFPInstruction.parser(buf, offset)
instructions.append(i)
offset += i.len
if i is not None:
instructions.append(i)
offset += i.len
except exception.OFPTruncatedMessage as e:
instructions.append(e.ofpmsg)
msg.instructions = instructions
@ -2806,7 +2807,9 @@ class OFPInstruction(StringifyMixin):
def parser(cls, buf, offset):
(type_, len_) = struct.unpack_from('!HH', buf, offset)
cls_ = cls._INSTRUCTION_TYPES.get(type_)
return cls_.parser(buf, offset)
if cls_ is not None:
return cls_.parser(buf, offset)
return None
@OFPInstruction.register_instruction_type([ofproto.OFPIT_GOTO_TABLE])
@ -3552,7 +3555,7 @@ class OFPActionExperimenter(OFPAction):
data = buf[(offset + ofproto.OFP_ACTION_EXPERIMENTER_HEADER_SIZE
): offset + len_]
if experimenter == ofproto_common.NX_EXPERIMENTER_ID:
obj = NXAction.parse(data) # noqa
obj = NXAction.parse(data) # pytype: disable=name-error # noqa
else:
obj = OFPActionExperimenterUnknown(experimenter, data)
obj.len = len_
@ -3933,22 +3936,23 @@ class OFPMultipartReply(MsgBase):
ofproto.OFP_MULTIPART_REPLY_PACK_STR, six.binary_type(buf),
ofproto.OFP_HEADER_SIZE)
stats_type_cls = cls._STATS_MSG_TYPES.get(type_)
msg = super(OFPMultipartReply, stats_type_cls).parser(
msg = super(OFPMultipartReply, stats_type_cls).parser( # pytype: disable=attribute-error
datapath, version, msg_type, msg_len, xid, buf)
msg.type = type_
msg.flags = flags
offset = ofproto.OFP_MULTIPART_REPLY_SIZE
body = []
while offset < msg_len:
b = stats_type_cls.cls_stats_body_cls.parser(msg.buf, offset)
body.append(b)
offset += b.length if hasattr(b, 'length') else b.len
if stats_type_cls is not None:
offset = ofproto.OFP_MULTIPART_REPLY_SIZE
body = []
while offset < msg_len:
b = stats_type_cls.cls_stats_body_cls.parser(msg.buf, offset)
body.append(b)
offset += b.length if hasattr(b, 'length') else b.len
if stats_type_cls.cls_body_single_struct:
msg.body = body[0]
else:
msg.body = body
if stats_type_cls.cls_body_single_struct:
msg.body = body[0]
else:
msg.body = body
return msg
@ -4578,12 +4582,13 @@ class OFPGroupStats(StringifyMixin):
group_stats = cls(*group)
group_stats.bucket_stats = []
total_len = group_stats.length + offset
offset += ofproto.OFP_GROUP_STATS_SIZE
while total_len > offset:
b = OFPBucketCounter.parser(buf, offset)
group_stats.bucket_stats.append(b)
offset += ofproto.OFP_BUCKET_COUNTER_SIZE
if group_stats.length is not None:
total_len = group_stats.length + offset
offset += ofproto.OFP_GROUP_STATS_SIZE
while total_len > offset:
b = OFPBucketCounter.parser(buf, offset)
group_stats.bucket_stats.append(b)
offset += ofproto.OFP_BUCKET_COUNTER_SIZE
return group_stats
@ -5771,7 +5776,7 @@ class ONFFlowMonitorRequest(StringifyMixin):
match_len = match.length
match_hdr_len = ofproto.OFP_MATCH_SIZE - 4 # exclude pad[4]
# strip ofp_match header and trailing padding
bin_match = bytes(bin_match)[match_hdr_len:match_len]
bin_match = bytearray(bin_match)[match_hdr_len:match_len]
self.match_len = len(bin_match)
buf = bytearray()
@ -5937,14 +5942,16 @@ class OFPQueueProp(OFPQueuePropHeader):
ofproto.OFP_QUEUE_PROP_HEADER_PACK_STR,
buf, offset)
cls_ = cls._QUEUE_PROP_PROPERTIES.get(property_)
p = cls_.parser(buf, offset + ofproto.OFP_QUEUE_PROP_HEADER_SIZE)
p.property = property_
p.len = len_
if property_ == ofproto.OFPQT_EXPERIMENTER:
rest = buf[offset + ofproto.OFP_QUEUE_PROP_EXPERIMENTER_SIZE:
offset + len_]
p.parse_experimenter_data(rest)
return p
if cls_ is not None:
p = cls_.parser(buf, offset + ofproto.OFP_QUEUE_PROP_HEADER_SIZE)
p.property = property_
p.len = len_
if property_ == ofproto.OFPQT_EXPERIMENTER:
rest = buf[offset + ofproto.OFP_QUEUE_PROP_EXPERIMENTER_SIZE:
offset + len_]
p.parse_experimenter_data(rest)
return p
return None
@OFPQueueProp.register_property(ofproto.OFPQT_MIN_RATE,
@ -6018,9 +6025,10 @@ class OFPPacketQueue(StringifyMixin):
properties = []
while length < len_:
queue_prop = OFPQueueProp.parser(buf, offset)
properties.append(queue_prop)
offset += queue_prop.len
length += queue_prop.len
if queue_prop is not None:
properties.append(queue_prop)
offset += queue_prop.len
length += queue_prop.len
o = cls(queue_id, port, properties)
o.len = len_
return o
@ -6343,6 +6351,10 @@ class OFPSetAsync(MsgBase):
self.flow_removed_mask[0], self.flow_removed_mask[1])
class OFPBundleProp(OFPPropBase):
_TYPES = {}
@_register_exp_type(ofproto_common.ONF_EXPERIMENTER_ID,
ofproto.ONF_ET_BUNDLE_CONTROL)
class ONFBundleCtrlMsg(OFPExperimenter):