Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 1 | """ |
| 2 | Support an illegal message |
| 3 | """ |
| 4 | |
Rich Lane | d7b0ffa | 2013-03-08 15:53:42 -0800 | [diff] [blame] | 5 | import of10 |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 6 | |
| 7 | ILLEGAL_MESSAGE_TYPE=217 |
| 8 | |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 9 | class illegal_message_type(of10.ofp_header): |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 10 | """ |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 11 | Wrapper class for illegal_message_type |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 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 |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 17 | @arg type: The message type (ILLEGAL_MESSAGE_TYPE=217) |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 18 | |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 19 | Data members inherited from ofp_header: |
| 20 | @arg version |
| 21 | @arg type |
| 22 | @arg length |
| 23 | @arg xid |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 24 | @arg data: Binary string following message members |
| 25 | |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 26 | """ |
| 27 | |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 28 | def __init__(self, **kwargs): |
| 29 | of10.ofp_header.__init__(self) |
| 30 | self.version = of10.OFP_VERSION |
| 31 | self.type = ILLEGAL_MESSAGE_TYPE |
Rich Lane | 8fbfd66 | 2013-03-11 15:30:44 -0700 | [diff] [blame] | 32 | self.xid = None |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 33 | self.data = "" |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 34 | for (k, v) in kwargs.items(): |
| 35 | if hasattr(self, k): |
| 36 | setattr(self, k, v) |
| 37 | else: |
| 38 | raise NameError("field %s does not exist in %s" % (k, self.__class__)) |
| 39 | |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 40 | |
| 41 | def pack(self): |
| 42 | """ |
| 43 | Pack object into string |
| 44 | |
| 45 | @return The packed string which can go on the wire |
| 46 | |
| 47 | """ |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 48 | self.length = len(self) |
| 49 | packed = "" |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 50 | |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 51 | packed += of10.ofp_header.pack(self, assertstruct=False) |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 52 | packed += self.data |
| 53 | return packed |
| 54 | |
| 55 | def unpack(self, binary_string): |
| 56 | """ |
| 57 | Unpack object from a binary string |
| 58 | |
| 59 | @param binary_string The wire protocol byte string holding the object |
| 60 | represented as an array of bytes. |
| 61 | @return The remainder of binary_string that was not parsed. |
| 62 | |
| 63 | """ |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 64 | |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 65 | binary_string = of10.ofp_header.unpack(self, binary_string) |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 66 | self.data = binary_string |
| 67 | binary_string = '' |
| 68 | return binary_string |
| 69 | |
| 70 | def __len__(self): |
| 71 | """ |
| 72 | Return the length of this object once packed into a string |
| 73 | |
| 74 | @return An integer representing the number bytes in the packed |
| 75 | string. |
| 76 | |
| 77 | """ |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 78 | length = 0 |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 79 | |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 80 | length += of10.ofp_header.__len__(self) |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 81 | length += len(self.data) |
| 82 | return length |
| 83 | |
| 84 | def show(self, prefix=''): |
| 85 | """ |
| 86 | Generate a string (with multiple lines) describing the contents |
| 87 | of the object in a readable manner |
| 88 | |
| 89 | @param prefix Pre-pended at the beginning of each line. |
| 90 | |
| 91 | """ |
| 92 | |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 93 | outstr = prefix + 'illegal_message (217)\n' |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 94 | prefix += ' ' |
| 95 | outstr += prefix + 'ofp header\n' |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 96 | outstr += of10.ofp_header.show(self, prefix) |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 97 | outstr += prefix + 'data is of length ' + str(len(self.data)) + '\n' |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 98 | ##@todo Fix this circular reference |
| 99 | # if len(self.data) > 0: |
| 100 | # obj = of_message_parse(self.data) |
| 101 | # if obj != None: |
| 102 | # outstr += obj.show(prefix) |
| 103 | # else: |
| 104 | # outstr += prefix + "Unable to parse data\n" |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 105 | return outstr |
| 106 | |
| 107 | def __eq__(self, other): |
| 108 | """ |
| 109 | Return True if self and other hold the same data |
| 110 | |
| 111 | @param other Other object in comparison |
| 112 | |
| 113 | """ |
| 114 | if type(self) != type(other): return False |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 115 | |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 116 | if not of10.ofp_header.__eq__(self, other): return False |
Dan Talayco | 6cc1fda | 2012-09-18 06:39:24 -0700 | [diff] [blame] | 117 | if self.data != other.data: return False |
| 118 | return True |
| 119 | |
| 120 | def __ne__(self, other): |
| 121 | """ |
| 122 | Return True if self and other do not hold the same data |
| 123 | |
| 124 | @param other Other object in comparison |
| 125 | |
| 126 | """ |
| 127 | return not self.__eq__(other) |