blob: 2d0ce7b7d8dca9c58bd09d65233ef9bffcc5155d [file] [log] [blame]
Dan Talayco6cc1fda2012-09-18 06:39:24 -07001"""
2Support an illegal message
3"""
4
Rich Laned7b0ffa2013-03-08 15:53:42 -08005import of10
Dan Talayco6cc1fda2012-09-18 06:39:24 -07006
7ILLEGAL_MESSAGE_TYPE=217
8
Rich Laneb73808c2013-03-11 15:22:23 -07009class illegal_message_type(of10.ofp_header):
Dan Talayco6cc1fda2012-09-18 06:39:24 -070010 """
Rich Laneb73808c2013-03-11 15:22:23 -070011 Wrapper class for illegal_message_type
Dan Talayco6cc1fda2012-09-18 06:39:24 -070012
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 Laneb73808c2013-03-11 15:22:23 -070017 @arg type: The message type (ILLEGAL_MESSAGE_TYPE=217)
Dan Talayco6cc1fda2012-09-18 06:39:24 -070018
Rich Laneb73808c2013-03-11 15:22:23 -070019 Data members inherited from ofp_header:
20 @arg version
21 @arg type
22 @arg length
23 @arg xid
Dan Talayco6cc1fda2012-09-18 06:39:24 -070024 @arg data: Binary string following message members
25
Dan Talayco6cc1fda2012-09-18 06:39:24 -070026 """
27
Rich Laneb73808c2013-03-11 15:22:23 -070028 def __init__(self, **kwargs):
29 of10.ofp_header.__init__(self)
30 self.version = of10.OFP_VERSION
31 self.type = ILLEGAL_MESSAGE_TYPE
Dan Talayco6cc1fda2012-09-18 06:39:24 -070032 self.data = ""
Rich Laneb73808c2013-03-11 15:22:23 -070033 for (k, v) in kwargs.items():
34 if hasattr(self, k):
35 setattr(self, k, v)
36 else:
37 raise NameError("field %s does not exist in %s" % (k, self.__class__))
38
Dan Talayco6cc1fda2012-09-18 06:39:24 -070039
40 def pack(self):
41 """
42 Pack object into string
43
44 @return The packed string which can go on the wire
45
46 """
Rich Laneb73808c2013-03-11 15:22:23 -070047 self.length = len(self)
48 packed = ""
Dan Talayco6cc1fda2012-09-18 06:39:24 -070049
Rich Laneb73808c2013-03-11 15:22:23 -070050 packed += of10.ofp_header.pack(self, assertstruct=False)
Dan Talayco6cc1fda2012-09-18 06:39:24 -070051 packed += self.data
52 return packed
53
54 def unpack(self, binary_string):
55 """
56 Unpack object from a binary string
57
58 @param binary_string The wire protocol byte string holding the object
59 represented as an array of bytes.
60 @return The remainder of binary_string that was not parsed.
61
62 """
Dan Talayco6cc1fda2012-09-18 06:39:24 -070063
Rich Laneb73808c2013-03-11 15:22:23 -070064 binary_string = of10.ofp_header.unpack(self, binary_string)
Dan Talayco6cc1fda2012-09-18 06:39:24 -070065 self.data = binary_string
66 binary_string = ''
67 return binary_string
68
69 def __len__(self):
70 """
71 Return the length of this object once packed into a string
72
73 @return An integer representing the number bytes in the packed
74 string.
75
76 """
Rich Laneb73808c2013-03-11 15:22:23 -070077 length = 0
Dan Talayco6cc1fda2012-09-18 06:39:24 -070078
Rich Laneb73808c2013-03-11 15:22:23 -070079 length += of10.ofp_header.__len__(self)
Dan Talayco6cc1fda2012-09-18 06:39:24 -070080 length += len(self.data)
81 return length
82
83 def show(self, prefix=''):
84 """
85 Generate a string (with multiple lines) describing the contents
86 of the object in a readable manner
87
88 @param prefix Pre-pended at the beginning of each line.
89
90 """
91
Rich Laneb73808c2013-03-11 15:22:23 -070092 outstr = prefix + 'illegal_message (217)\n'
Dan Talayco6cc1fda2012-09-18 06:39:24 -070093 prefix += ' '
94 outstr += prefix + 'ofp header\n'
Rich Laneb73808c2013-03-11 15:22:23 -070095 outstr += of10.ofp_header.show(self, prefix)
Dan Talayco6cc1fda2012-09-18 06:39:24 -070096 outstr += prefix + 'data is of length ' + str(len(self.data)) + '\n'
Rich Laneb73808c2013-03-11 15:22:23 -070097 ##@todo Fix this circular reference
98 # if len(self.data) > 0:
99 # obj = of_message_parse(self.data)
100 # if obj != None:
101 # outstr += obj.show(prefix)
102 # else:
103 # outstr += prefix + "Unable to parse data\n"
Dan Talayco6cc1fda2012-09-18 06:39:24 -0700104 return outstr
105
106 def __eq__(self, other):
107 """
108 Return True if self and other hold the same data
109
110 @param other Other object in comparison
111
112 """
113 if type(self) != type(other): return False
Dan Talayco6cc1fda2012-09-18 06:39:24 -0700114
Rich Laneb73808c2013-03-11 15:22:23 -0700115 if not of10.ofp_header.__eq__(self, other): return False
Dan Talayco6cc1fda2012-09-18 06:39:24 -0700116 if self.data != other.data: return False
117 return True
118
119 def __ne__(self, other):
120 """
121 Return True if self and other do not hold the same data
122
123 @param other Other object in comparison
124
125 """
126 return not self.__eq__(other)