Skip to content

Commit

Permalink
Merge pull request #144 from anarkiwi/pytype3
Browse files Browse the repository at this point in the history
add ofproto 1.3 coverage, check key-error and attribute-error.
  • Loading branch information
anarkiwi committed Jun 10, 2021
2 parents 34f3349 + 98d4913 commit 08efd69
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 51 deletions.
20 changes: 11 additions & 9 deletions ryu/controller/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ def set_state(self, state):
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
Expand Down Expand Up @@ -362,16 +363,17 @@ def _recv_loop(self):
# 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)
Expand Down
7 changes: 4 additions & 3 deletions ryu/controller/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion ryu/controller/mac_to_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def port_get(self, dpid, mac):
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):
Expand Down
4 changes: 2 additions & 2 deletions ryu/controller/ofp_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
80 changes: 46 additions & 34 deletions ryu/ofproto/ofproto_v1_3_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,7 @@ def field_parser(cls, header, buf, offset):
(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):
Expand Down Expand Up @@ -2773,8 +2773,9 @@ def parser(cls, datapath, version, msg_type, msg_len, xid, buf):
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
Expand Down Expand Up @@ -2805,7 +2806,9 @@ def _register_instruction_type(cls):
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])
Expand Down Expand Up @@ -3551,7 +3554,7 @@ def parser(cls, buf, offset):
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_
Expand Down Expand Up @@ -3932,22 +3935,23 @@ def parser(cls, datapath, version, msg_type, msg_len, xid, buf):
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


Expand Down Expand Up @@ -4577,12 +4581,13 @@ def parser(cls, buf, offset):
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

Expand Down Expand Up @@ -5770,7 +5775,7 @@ def serialize(self):
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()
Expand Down Expand Up @@ -5936,14 +5941,16 @@ def parser(cls, buf, offset):
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,
Expand Down Expand Up @@ -6017,9 +6024,10 @@ def parser(cls, buf, offset):
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
Expand Down Expand Up @@ -6342,6 +6350,10 @@ def _serialize_body(self):
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):
Expand Down
3 changes: 1 addition & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ console_scripts =
[pytype]
inputs =
ryu/controller/
ryu/ofproto/ofproto_v1_3*
disable =
attribute-error
import-error
key-error
module-attr
keep-going =
1

0 comments on commit 08efd69

Please sign in to comment.