Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 1 | """ |
| 2 | Support an illegal message |
| 3 | """ |
| 4 | |
| 5 | from cstruct import * |
| 6 | |
| 7 | ILLEGAL_MESSAGE_TYPE=217 |
| 8 | |
| 9 | class illegal_message_type: |
| 10 | """ |
| 11 | Wrapper class for illegal message |
| 12 | |
| 13 | OpenFlow message header: length, version, xid, type |
| 14 | @arg length: The total length of the message |
| 15 | @arg version: The OpenFlow version (1) |
| 16 | @arg xid: The transaction ID |
| 17 | @arg type: The message type (OFPT_ECHO_REQUEST=2) |
| 18 | |
| 19 | @arg data: Binary string following message members |
| 20 | |
| 21 | The message type is set to "illegal" and the pack assert |
| 22 | check for the OF header is disabled |
| 23 | """ |
| 24 | |
| 25 | def __init__(self): |
| 26 | self.header = ofp_header() |
| 27 | self.header.type = ILLEGAL_MESSAGE_TYPE |
| 28 | self.data = "" |
| 29 | |
| 30 | def pack(self): |
| 31 | """ |
| 32 | Pack object into string |
| 33 | |
| 34 | @return The packed string which can go on the wire |
| 35 | |
| 36 | """ |
| 37 | self.header.length = len(self) |
| 38 | packed = self.header.pack(assertstruct=False) |
| 39 | |
| 40 | packed += self.data |
| 41 | return packed |
| 42 | |
| 43 | def unpack(self, binary_string): |
| 44 | """ |
| 45 | Unpack object from a binary string |
| 46 | |
| 47 | @param binary_string The wire protocol byte string holding the object |
| 48 | represented as an array of bytes. |
| 49 | @return The remainder of binary_string that was not parsed. |
| 50 | |
| 51 | """ |
| 52 | binary_string = self.header.unpack(binary_string) |
| 53 | |
| 54 | self.data = binary_string |
| 55 | binary_string = '' |
| 56 | return binary_string |
| 57 | |
| 58 | def __len__(self): |
| 59 | """ |
| 60 | Return the length of this object once packed into a string |
| 61 | |
| 62 | @return An integer representing the number bytes in the packed |
| 63 | string. |
| 64 | |
| 65 | """ |
| 66 | length = OFP_HEADER_BYTES |
| 67 | |
| 68 | length += len(self.data) |
| 69 | return length |
| 70 | |
| 71 | def show(self, prefix=''): |
| 72 | """ |
| 73 | Generate a string (with multiple lines) describing the contents |
| 74 | of the object in a readable manner |
| 75 | |
| 76 | @param prefix Pre-pended at the beginning of each line. |
| 77 | |
| 78 | """ |
| 79 | |
| 80 | outstr = prefix + 'illegal_message (' + \ |
| 81 | str(ILLEGAL_MESSAGE_TYPE) + ')\n' |
| 82 | prefix += ' ' |
| 83 | outstr += prefix + 'ofp header\n' |
| 84 | outstr += self.header.show(prefix + ' ') |
| 85 | outstr += prefix + 'data is of length ' + str(len(self.data)) + '\n' |
| 86 | return outstr |
| 87 | |
| 88 | def __eq__(self, other): |
| 89 | """ |
| 90 | Return True if self and other hold the same data |
| 91 | |
| 92 | @param other Other object in comparison |
| 93 | |
| 94 | """ |
| 95 | if type(self) != type(other): return False |
| 96 | if not self.header.__eq__(other.header): return False |
| 97 | |
| 98 | if self.data != other.data: return False |
| 99 | return True |
| 100 | |
| 101 | def __ne__(self, other): |
| 102 | """ |
| 103 | Return True if self and other do not hold the same data |
| 104 | |
| 105 | @param other Other object in comparison |
| 106 | |
| 107 | """ |
| 108 | return not self.__eq__(other) |