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) |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 40 | self.version = OFP_VERSION |
| 41 | self.type = OFPT_ERROR |
Rich Lane | 8fbfd66 | 2013-03-11 15:30:44 -0700 | [diff] [blame^] | 42 | self.xid = None |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 43 | self.err_type = --ERROR_NAME-- |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 44 | self.data = "" |
| 45 | |
| 46 | def pack(self, assertstruct=True): |
| 47 | self.header.length = self.__len__() |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 48 | packed = "" |
Dan Talayco | 54a90e9 | 2010-02-10 22:39:38 -0800 | [diff] [blame] | 49 | packed += ofp_error_msg.pack(self) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 50 | packed += self.data |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 51 | return packed |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 52 | |
| 53 | def unpack(self, binary_string): |
| 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): |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 59 | return OFP_ERROR_MSG_BYTES + len(self.data) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 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" |
Dan Talayco | 46755fa | 2010-03-09 21:44:29 -0800 | [diff] [blame] | 63 | outstr += ofp_error_msg.show(self, prefix + ' ') |
| 64 | outstr += prefix + "data is of length " + str(len(self.data)) + '\\n' |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 65 | ##@todo Consider trying to parse the string |
Dan Talayco | 46755fa | 2010-03-09 21:44:29 -0800 | [diff] [blame] | 66 | return outstr |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 67 | |
Dan Talayco | 54a90e9 | 2010-02-10 22:39:38 -0800 | [diff] [blame] | 68 | def __eq__(self, other): |
| 69 | if type(self) != type(other): return False |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 70 | return (ofp_error_msg.__eq__(self, other) and |
Dan Talayco | 54a90e9 | 2010-02-10 22:39:38 -0800 | [diff] [blame] | 71 | self.data == other.data) |
| 72 | |
| 73 | def __ne__(self, other): return not self.__eq__(other) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 74 | """ |
| 75 | |
| 76 | error_types = [ |
| 77 | 'hello_failed', |
| 78 | 'bad_request', |
| 79 | 'bad_action', |
| 80 | 'flow_mod_failed', |
| 81 | 'port_mod_failed', |
| 82 | 'queue_op_failed'] |
| 83 | |
| 84 | for t in error_types: |
| 85 | error_name = "OFPET_" + t.upper() |
| 86 | to_print = re.sub('--TYPE--', t, template) |
| 87 | to_print = re.sub('--ERROR_NAME--', error_name, to_print) |
| 88 | print to_print |