Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # This python script generates error subclasses |
| 4 | # |
| 5 | |
| 6 | import re |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 7 | import sys |
Rich Lane | 6242d9f | 2013-01-06 17:35:39 -0800 | [diff] [blame] | 8 | sys.path.append("../../src/python/of10") |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 9 | from cstruct import * |
| 10 | from class_maps import class_to_members_map |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 11 | |
| 12 | print """ |
| 13 | # Python OpenFlow error wrapper classes |
| 14 | |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 15 | from cstruct import * |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 16 | |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 17 | """ |
| 18 | |
| 19 | ################################################################ |
| 20 | # |
| 21 | # Error message subclasses |
| 22 | # |
| 23 | ################################################################ |
| 24 | |
| 25 | # Template for error subclasses |
| 26 | |
| 27 | template = """ |
| 28 | class --TYPE--_error_msg(ofp_error_msg): |
| 29 | \""" |
| 30 | Wrapper class for --TYPE-- error message class |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 31 | |
| 32 | Data members inherited from ofp_error_msg: |
| 33 | @arg type |
| 34 | @arg code |
| 35 | @arg data: Binary string following message members |
| 36 | |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 37 | \""" |
| 38 | def __init__(self): |
| 39 | ofp_error_msg.__init__(self) |
| 40 | self.header = ofp_header() |
| 41 | self.header.type = OFPT_ERROR |
| 42 | self.type = --ERROR_NAME-- |
| 43 | self.data = "" |
| 44 | |
| 45 | def pack(self, assertstruct=True): |
| 46 | self.header.length = self.__len__() |
Dan Talayco | 54a90e9 | 2010-02-10 22:39:38 -0800 | [diff] [blame] | 47 | packed = self.header.pack() |
| 48 | packed += ofp_error_msg.pack(self) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 49 | packed += self.data |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 50 | return packed |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 51 | |
| 52 | def unpack(self, binary_string): |
Dan Talayco | 54a90e9 | 2010-02-10 22:39:38 -0800 | [diff] [blame] | 53 | binary_string = self.header.unpack(binary_string) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 54 | binary_string = ofp_error_msg.unpack(self, binary_string) |
| 55 | self.data = binary_string |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 56 | return "" |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 57 | |
| 58 | def __len__(self): |
| 59 | return OFP_HEADER_BYTES + OFP_ERROR_MSG_BYTES + len(self.data) |
| 60 | |
| 61 | def show(self, prefix=''): |
Dan Talayco | 46755fa | 2010-03-09 21:44:29 -0800 | [diff] [blame] | 62 | outstr = prefix + "--TYPE--_error_msg\\m" |
| 63 | outstr += self.header.show(prefix + ' ') |
| 64 | outstr += ofp_error_msg.show(self, prefix + ' ') |
| 65 | outstr += prefix + "data is of length " + str(len(self.data)) + '\\n' |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 66 | ##@todo Consider trying to parse the string |
Dan Talayco | 46755fa | 2010-03-09 21:44:29 -0800 | [diff] [blame] | 67 | return outstr |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 68 | |
Dan Talayco | 54a90e9 | 2010-02-10 22:39:38 -0800 | [diff] [blame] | 69 | def __eq__(self, other): |
| 70 | if type(self) != type(other): return False |
| 71 | return (self.header == other.header and |
| 72 | ofp_error_msg.__eq__(self, other) and |
| 73 | self.data == other.data) |
| 74 | |
| 75 | def __ne__(self, other): return not self.__eq__(other) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 76 | """ |
| 77 | |
| 78 | error_types = [ |
| 79 | 'hello_failed', |
| 80 | 'bad_request', |
| 81 | 'bad_action', |
| 82 | 'flow_mod_failed', |
| 83 | 'port_mod_failed', |
| 84 | 'queue_op_failed'] |
| 85 | |
| 86 | for t in error_types: |
| 87 | error_name = "OFPET_" + t.upper() |
| 88 | to_print = re.sub('--TYPE--', t, template) |
| 89 | to_print = re.sub('--ERROR_NAME--', error_name, to_print) |
| 90 | print to_print |