blob: 6761935cff7f6e9f1b3a26c6ff09b5e5e9c6b3cc [file] [log] [blame]
Dan Talaycof75360a2010-02-05 22:22:54 -08001#!/usr/bin/python
2#
3# This python script generates wrapper functions for OpenFlow messages
4#
5# See the doc string below for more info
6#
7
8# To do:
9# Default type values for messages
10# Generate all message objects
11# Action list objects?
12# Autogen lengths when possible
13# Dictionaries for enum strings
14# Resolve sub struct initializers (see ofp_flow_mod)
15
16
17"""
18Generate wrapper classes for OpenFlow messages
19
20(C) Copyright Stanford University
21Date February 2010
22Created by dtalayco
23
24Attempting to follow http://www.python.org/dev/peps/pep-0008/
25The main exception is that our class names do not use CamelCase
26so as to more closely match the original C code names.
27
28This file is meant to generate a file of_wrapper.py which imports
29the base classes generated form automatic processing of openflow.h
30and produces wrapper classes for each OpenFlow message type.
31
32This file will normally be included in of_message.py which provides
33additional hand-generated work.
34
35There are two types of structures/classes here: base components and
36message classes.
37
38Base components are the base data classes which are fixed
39length structures including:
40 ofp_header
41 Each ofp_action structure
42 ofp_phy_port
43 The array elements of all the stats reply messages
44The base components are to be imported from a file of_header.py.
45
46Message classes define a complete message on the wire. These are
47comprised of possibly variable length lists of possibly variably
48typed objects from the base component list above.
49
50Each OpenFlow message has a header and zero or more fixed length
51members (the "core members" of the class) followed by zero or more
52variable length lists.
53
54The wrapper classes should live in their own name space, probably
55of_message. Automatically generated base component and skeletons for
56the message classes are assumed generated and the wrapper classes
57will inherit from those.
58
59Every message class must implement pack and unpack functions to
60convert between the class and a string representing what goes on the
61wire.
62
63For unpacking, the low level (base-component) classes must implement
64their own unpack functions. A single top level unpack function
65will do the parsing and call the lower layer unpack functions as
66appropriate.
67
68Every base and message class should implement a show function to
69(recursively) display the contents of the object.
70
71Certain OpenFlow message types are further subclassed. These include
72stats_request, stats_reply and error.
73
74"""
75
76# Don't generate header object in messages
77# Map each message to a body that doesn't include the header
78# The body has does not include variable length info at the end
79
80import re
81import string
82import sys
Rich Lane6242d9f2013-01-06 17:35:39 -080083sys.path.append("../../src/python/of10")
Dan Talaycob9cb5482010-02-09 15:23:12 -080084from cstruct import *
85from class_maps import class_to_members_map
Dan Talaycof75360a2010-02-05 22:22:54 -080086
87message_top_matter = """
88# Python OpenFlow message wrapper classes
89
Dan Talaycob9cb5482010-02-09 15:23:12 -080090from cstruct import *
Dan Talaycof75360a2010-02-05 22:22:54 -080091from action_list import action_list
Dan Talayco411489d2010-02-12 23:03:46 -080092from error import *
Dan Talaycof75360a2010-02-05 22:22:54 -080093
Dan Talaycof75360a2010-02-05 22:22:54 -080094# Define templates for documentation
95class ofp_template_msg:
96 \"""
97 Sample base class for template_msg; normally auto generated
98 This class should live in the of_header name space and provides the
99 base class for this type of message. It will be wrapped for the
100 high level API.
101
102 \"""
103 def __init__(self):
104 \"""
105 Constructor for base class
106
107 \"""
108 self.header = ofp_header()
109 # Additional base data members declared here
Dan Talaycoac1cb812010-02-06 20:34:18 -0800110
Dan Talaycof75360a2010-02-05 22:22:54 -0800111 # Normally will define pack, unpack, __len__ functions
112
113class template_msg(ofp_template_msg):
114 \"""
115 Sample class wrapper for template_msg
116 This class should live in the of_message name space and provides the
117 high level API for an OpenFlow message object. These objects must
118 implement the functions indicated in this template.
119
120 \"""
121 def __init__(self):
122 \"""
123 Constructor
124 Must set the header type value appropriately for the message
125
126 \"""
Dan Talaycoac1cb812010-02-06 20:34:18 -0800127
128 ##@var header
129 # OpenFlow message header: length, version, xid, type
Dan Talaycof75360a2010-02-05 22:22:54 -0800130 ofp_template_msg.__init__(self)
Dan Talaycoac1cb812010-02-06 20:34:18 -0800131 self.header = ofp_header()
Dan Talaycof75360a2010-02-05 22:22:54 -0800132 # For a real message, will be set to an integer
133 self.header.type = "TEMPLATE_MSG_VALUE"
134 def pack(self):
135 \"""
136 Pack object into string
137
138 @return The packed string which can go on the wire
139
140 \"""
141 pass
142 def unpack(self, binary_string):
143 \"""
144 Unpack object from a binary string
145
146 @param binary_string The wire protocol byte string holding the object
147 represented as an array of bytes.
148
149 @return Typically returns the remainder of binary_string that
150 was not parsed. May give a warning if that string is non-empty
151
152 \"""
153 pass
154 def __len__(self):
155 \"""
156 Return the length of this object once packed into a string
157
158 @return An integer representing the number bytes in the packed
159 string.
160
161 \"""
162 pass
163 def show(self, prefix=''):
164 \"""
Dan Talayco46755fa2010-03-09 21:44:29 -0800165 Generate a string (with multiple lines) describing the contents
166 of the object in a readable manner
Dan Talaycof75360a2010-02-05 22:22:54 -0800167
Dan Talayco46755fa2010-03-09 21:44:29 -0800168 @param prefix Pre-pended at the beginning of each line.
Dan Talaycof75360a2010-02-05 22:22:54 -0800169
170 \"""
171 pass
172 def __eq__(self, other):
173 \"""
174 Return True if self and other hold the same data
175
176 @param other Other object in comparison
177
178 \"""
179 pass
180 def __ne__(self, other):
181 \"""
182 Return True if self and other do not hold the same data
183
184 @param other Other object in comparison
185
186 \"""
187 pass
188"""
189
190# Dictionary mapping wrapped classes to the auto-generated structure
191# underlieing the class (body only, not header or var-length data)
192message_class_map = {
193 "hello" : "ofp_header",
194 "error" : "ofp_error_msg",
195 "echo_request" : "ofp_header",
196 "echo_reply" : "ofp_header",
197 "vendor" : "ofp_vendor_header",
198 "features_request" : "ofp_header",
199 "features_reply" : "ofp_switch_features",
200 "get_config_request" : "ofp_header",
201 "get_config_reply" : "ofp_switch_config",
202 "set_config" : "ofp_switch_config",
203 "packet_in" : "ofp_packet_in",
204 "flow_removed" : "ofp_flow_removed",
205 "port_status" : "ofp_port_status",
206 "packet_out" : "ofp_packet_out",
207 "flow_mod" : "ofp_flow_mod",
208 "port_mod" : "ofp_port_mod",
209 "stats_request" : "ofp_stats_request",
210 "stats_reply" : "ofp_stats_reply",
211 "barrier_request" : "ofp_header",
212 "barrier_reply" : "ofp_header",
213 "queue_get_config_request" : "ofp_queue_get_config_request",
214 "queue_get_config_reply" : "ofp_queue_get_config_reply"
215}
216
217# These messages have a string member at the end of the data
218string_members = [
219 "hello",
220 "error",
221 "echo_request",
222 "echo_reply",
223 "vendor",
224 "packet_in",
225 "packet_out"
226]
227
228# These messages have a list (with the given name) in the data,
229# after the core members; the type is given for validation
230list_members = {
231 "features_reply" : ('ports', None),
232 "packet_out" : ('actions', 'action_list'),
233 "flow_mod" : ('actions', 'action_list'),
234 "queue_get_config_reply" : ('queues', None)
235}
236
237_ind = " "
238
239def _p1(s): print _ind + s
240def _p2(s): print _ind * 2 + s
241def _p3(s): print _ind * 3 + s
242def _p4(s): print _ind * 4 + s
243
244# Okay, this gets kind of ugly:
245# There are three variables:
246# has_core_members: If parent class is not ofp_header, has inheritance
247# has_list: Whether class has trailing array or class
248# has_string: Whether class has trailing string
249
250def gen_message_wrapper(msg):
251 """
252 Generate a wrapper for the given message based on above info
253 @param msg String identifying the message name for the class
254 """
255
256 msg_name = "OFPT_" + msg.upper()
257 parent = message_class_map[msg]
258
259 has_list = False # Has trailing list
260 has_core_members = False
261 has_string = False # Has trailing string
262 if parent != 'ofp_header':
263 has_core_members = True
264 if msg in list_members.keys():
265 (list_var, list_type) = list_members[msg]
266 has_list = True
267 if msg in string_members:
268 has_string = True
269
270 if has_core_members:
271 print "class " + msg + "(" + parent + "):"
272 else:
273 print "class " + msg + ":"
274 _p1('"""')
275 _p1("Wrapper class for " + msg)
276 print
Dan Talaycoac1cb812010-02-06 20:34:18 -0800277 _p1("OpenFlow message header: length, version, xid, type")
278 _p1("@arg length: The total length of the message")
279 _p1("@arg version: The OpenFlow version (" + str(OFP_VERSION) + ")")
280 _p1("@arg xid: The transaction ID")
281 _p1("@arg type: The message type (" + msg_name + "=" +
282 str(eval(msg_name)) + ")")
283 print
284 if has_core_members and parent in class_to_members_map.keys():
285 _p1("Data members inherited from " + parent + ":")
286 for var in class_to_members_map[parent]:
287 _p1("@arg " + var)
Dan Talaycof75360a2010-02-05 22:22:54 -0800288 if has_list:
289 if list_type == None:
Dan Talaycoac1cb812010-02-06 20:34:18 -0800290 _p1("@arg " + list_var + ": Variable length array of TBD")
Dan Talaycof75360a2010-02-05 22:22:54 -0800291 else:
Dan Talaycoac1cb812010-02-06 20:34:18 -0800292 _p1("@arg " + list_var + ": Object of type " + list_type);
Dan Talaycof75360a2010-02-05 22:22:54 -0800293 if has_string:
Dan Talaycoac1cb812010-02-06 20:34:18 -0800294 _p1("@arg data: Binary string following message members")
295 print
Dan Talaycof75360a2010-02-05 22:22:54 -0800296 _p1('"""')
297
298 print
Rich Lanec3c2ae12013-01-04 10:13:17 -0800299 _p1("def __init__(self, **kwargs):")
Dan Talaycof75360a2010-02-05 22:22:54 -0800300 if has_core_members:
301 _p2(parent + ".__init__(self)")
Dan Talaycof75360a2010-02-05 22:22:54 -0800302 _p2("self.header = ofp_header()")
303 _p2("self.header.type = " + msg_name)
304 if has_list:
Rich Lane0f0adc92013-01-04 15:13:02 -0800305 _p2('self.' + list_var + ' = []')
Dan Talaycof75360a2010-02-05 22:22:54 -0800306 if has_string:
307 _p2('self.data = ""')
Rich Lanec3c2ae12013-01-04 10:13:17 -0800308 _p2('for (k, v) in kwargs.items():')
309 _p3('if hasattr(self, k):')
310 _p4('setattr(self, k, v)')
311 _p3('else:')
312 _p4('raise NameError("field %s does not exist in %s" % (k, self.__class__))')
Rich Lane0f0adc92013-01-04 15:13:02 -0800313 if has_list and list_type != None:
314 _p2('# Coerce keyword arg into list type')
315 _p2('self.%(list_var)s = %(list_type)s(self.%(list_var)s)' % dict(list_type=list_type, list_var=list_var))
Dan Talaycof75360a2010-02-05 22:22:54 -0800316
Dan Talaycob9cb5482010-02-09 15:23:12 -0800317 print """
318
319 def pack(self):
320 \"""
321 Pack object into string
322
323 @return The packed string which can go on the wire
324
325 \"""
326 self.header.length = len(self)
327 packed = self.header.pack()
328"""
329
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800330 # Have to special case the action length calculation for pkt out
331 if msg == 'packet_out':
332 _p2('self.actions_len = len(self.actions)')
Dan Talaycof75360a2010-02-05 22:22:54 -0800333 if has_core_members:
Dan Talayco6d2470b2010-02-07 22:59:49 -0800334 _p2("packed += " + parent + ".pack(self)")
Dan Talaycof75360a2010-02-05 22:22:54 -0800335 if has_list:
336 if list_type == None:
337 _p2('for obj in self.' + list_var + ':')
338 _p3('packed += obj.pack()')
339 else:
340 _p2('packed += self.' + list_var + '.pack()')
341 if has_string:
342 _p2('packed += self.data')
Dan Talayco6d2470b2010-02-07 22:59:49 -0800343 _p2("return packed")
Dan Talaycof75360a2010-02-05 22:22:54 -0800344
Dan Talaycob9cb5482010-02-09 15:23:12 -0800345 print """
346 def unpack(self, binary_string):
347 \"""
348 Unpack object from a binary string
349
350 @param binary_string The wire protocol byte string holding the object
351 represented as an array of bytes.
Dan Talayco411489d2010-02-12 23:03:46 -0800352 @return The remainder of binary_string that was not parsed.
Dan Talaycob9cb5482010-02-09 15:23:12 -0800353
354 \"""
355 binary_string = self.header.unpack(binary_string)
356"""
Dan Talaycof75360a2010-02-05 22:22:54 -0800357 if has_core_members:
358 _p2("binary_string = " + parent + ".unpack(self, binary_string)")
359 if has_list:
Dan Talaycoff606492010-05-13 14:22:37 -0700360 if msg == "features_reply": # Special case port parsing
361 # For now, cheat and assume the rest of the message is port list
362 _p2("while len(binary_string) >= OFP_PHY_PORT_BYTES:")
363 _p3("new_port = ofp_phy_port()")
364 _p3("binary_string = new_port.unpack(binary_string)")
365 _p3("self.ports.append(new_port)")
366 elif list_type == None:
Dan Talaycof75360a2010-02-05 22:22:54 -0800367 _p2("for obj in self." + list_var + ":")
368 _p3("binary_string = obj.unpack(binary_string)")
369 elif msg == "packet_out": # Special case this
Dan Talayco6d2470b2010-02-07 22:59:49 -0800370 _p2('binary_string = self.actions.unpack(' +
371 'binary_string, bytes=self.actions_len)')
Dan Talaycof75360a2010-02-05 22:22:54 -0800372 elif msg == "flow_mod": # Special case this
Dan Talayco6d2470b2010-02-07 22:59:49 -0800373 _p2("ai_len = self.header.length - (OFP_FLOW_MOD_BYTES + " +
374 "OFP_HEADER_BYTES)")
375 _p2("binary_string = self.actions.unpack(binary_string, " +
376 "bytes=ai_len)")
Dan Talaycof75360a2010-02-05 22:22:54 -0800377 else:
378 _p2("binary_string = self." + list_var + ".unpack(binary_string)")
379 if has_string:
380 _p2("self.data = binary_string")
381 _p2("binary_string = ''")
382 else:
383 _p2("# Fixme: If no self.data, add check for data remaining")
384 _p2("return binary_string")
385
Dan Talaycob9cb5482010-02-09 15:23:12 -0800386 print """
387 def __len__(self):
388 \"""
389 Return the length of this object once packed into a string
390
391 @return An integer representing the number bytes in the packed
392 string.
393
394 \"""
395 length = OFP_HEADER_BYTES
396"""
Dan Talayco6d2470b2010-02-07 22:59:49 -0800397 if has_core_members:
398 _p2("length += " + parent + ".__len__(self)")
399 if has_list:
400 if list_type == None:
401 _p2("for obj in self." + list_var + ":")
402 _p3("length += len(obj)")
403 else:
404 _p2("length += len(self." + list_var + ")")
405 if has_string:
406 _p2("length += len(self.data)")
407 _p2("return length")
Dan Talaycof75360a2010-02-05 22:22:54 -0800408
Dan Talaycob9cb5482010-02-09 15:23:12 -0800409 print """
Dan Talaycob9cb5482010-02-09 15:23:12 -0800410 def show(self, prefix=''):
411 \"""
Dan Talayco46755fa2010-03-09 21:44:29 -0800412 Generate a string (with multiple lines) describing the contents
413 of the object in a readable manner
Dan Talaycob9cb5482010-02-09 15:23:12 -0800414
Dan Talayco46755fa2010-03-09 21:44:29 -0800415 @param prefix Pre-pended at the beginning of each line.
Dan Talaycob9cb5482010-02-09 15:23:12 -0800416
417 \"""
418"""
Dan Talayco46755fa2010-03-09 21:44:29 -0800419 _p2("outstr = prefix + '" + msg + " (" + msg_name + ")\\n'")
Dan Talaycof75360a2010-02-05 22:22:54 -0800420 _p2("prefix += ' '")
Dan Talayco46755fa2010-03-09 21:44:29 -0800421 _p2("outstr += prefix + 'ofp header\\n'")
422 _p2("outstr += self.header.show(prefix + ' ')")
Dan Talaycof75360a2010-02-05 22:22:54 -0800423 if has_core_members:
Dan Talayco46755fa2010-03-09 21:44:29 -0800424 _p2("outstr += " + parent + ".show(self, prefix)")
Dan Talaycof75360a2010-02-05 22:22:54 -0800425 if has_list:
426 if list_type == None:
Dan Talayco46755fa2010-03-09 21:44:29 -0800427 _p2('outstr += prefix + "Array ' + list_var + '\\n"')
Dan Talaycof75360a2010-02-05 22:22:54 -0800428 _p2('for obj in self.' + list_var +':')
Dan Talayco46755fa2010-03-09 21:44:29 -0800429 _p3("outstr += obj.show(prefix + ' ')")
Dan Talaycof75360a2010-02-05 22:22:54 -0800430 else:
Dan Talayco46755fa2010-03-09 21:44:29 -0800431 _p2('outstr += prefix + "List ' + list_var + '\\n"')
Rich Lanee6ea3fe2013-03-08 17:54:38 -0800432 _p2('for obj in self.' + list_var + ':')
433 _p3('outstr += obj.show(prefix + " ")')
Dan Talaycof75360a2010-02-05 22:22:54 -0800434 if has_string:
Dan Talayco46755fa2010-03-09 21:44:29 -0800435 _p2("outstr += prefix + 'data is of length ' + str(len(self.data)) + '\\n'")
Dan Talayco6d2470b2010-02-07 22:59:49 -0800436 _p2("##@todo Fix this circular reference")
437 _p2("# if len(self.data) > 0:")
438 _p3("# obj = of_message_parse(self.data)")
439 _p3("# if obj != None:")
Dan Talayco46755fa2010-03-09 21:44:29 -0800440 _p4("# outstr += obj.show(prefix)")
Dan Talayco6d2470b2010-02-07 22:59:49 -0800441 _p3("# else:")
Dan Talayco46755fa2010-03-09 21:44:29 -0800442 _p4('# outstr += prefix + "Unable to parse data\\n"')
443 _p2('return outstr')
Dan Talaycof75360a2010-02-05 22:22:54 -0800444
Dan Talaycob9cb5482010-02-09 15:23:12 -0800445 print """
446 def __eq__(self, other):
447 \"""
448 Return True if self and other hold the same data
449
450 @param other Other object in comparison
451
452 \"""
453 if type(self) != type(other): return False
454 if not self.header.__eq__(other.header): return False
455"""
Dan Talaycof75360a2010-02-05 22:22:54 -0800456 if has_core_members:
Dan Talayco6d2470b2010-02-07 22:59:49 -0800457 _p2("if not " + parent + ".__eq__(self, other): return False")
Dan Talaycof75360a2010-02-05 22:22:54 -0800458 if has_string:
459 _p2("if self.data != other.data: return False")
460 if has_list:
461 _p2("if self." + list_var + " != other." + list_var + ": return False")
462 _p2("return True")
463
Dan Talaycob9cb5482010-02-09 15:23:12 -0800464 print """
465 def __ne__(self, other):
466 \"""
467 Return True if self and other do not hold the same data
Dan Talaycof75360a2010-02-05 22:22:54 -0800468
Dan Talaycob9cb5482010-02-09 15:23:12 -0800469 @param other Other object in comparison
470
471 \"""
472 return not self.__eq__(other)
473 """
Dan Talaycof75360a2010-02-05 22:22:54 -0800474
475
476################################################################
477#
478# Stats request subclasses
479# description_request, flow, aggregate, table, port, vendor
480#
481################################################################
482
483# table and desc stats requests are special with empty body
484extra_ofp_stats_req_defs = """
485# Stats request bodies for desc and table stats are not defined in the
486# OpenFlow header; We define them here. They are empty classes, really
487
488class ofp_desc_stats_request:
489 \"""
490 Forced definition of ofp_desc_stats_request (empty class)
491 \"""
492 def __init__(self):
493 pass
494 def pack(self, assertstruct=True):
495 return ""
496 def unpack(self, binary_string):
497 return binary_string
498 def __len__(self):
499 return 0
500 def show(self, prefix=''):
Dan Talayco46755fa2010-03-09 21:44:29 -0800501 return prefix + "ofp_desc_stats_request (empty)\\n"
Dan Talaycof75360a2010-02-05 22:22:54 -0800502 def __eq__(self, other):
503 return type(self) == type(other)
504 def __ne__(self, other):
505 return type(self) != type(other)
506
507OFP_DESC_STATS_REQUEST_BYTES = 0
508
509class ofp_table_stats_request:
510 \"""
511 Forced definition of ofp_table_stats_request (empty class)
512 \"""
513 def __init__(self):
514 pass
515 def pack(self, assertstruct=True):
516 return ""
517 def unpack(self, binary_string):
518 return binary_string
519 def __len__(self):
520 return 0
521 def show(self, prefix=''):
Dan Talayco46755fa2010-03-09 21:44:29 -0800522 return prefix + "ofp_table_stats_request (empty)\\n"
Dan Talaycof75360a2010-02-05 22:22:54 -0800523 def __eq__(self, other):
524 return type(self) == type(other)
525 def __ne__(self, other):
526 return type(self) != type(other)
527
528OFP_TABLE_STATS_REQUEST_BYTES = 0
529
530"""
531
532stats_request_template = """
533class --TYPE--_stats_request(ofp_stats_request, ofp_--TYPE--_stats_request):
534 \"""
535 Wrapper class for --TYPE-- stats request message
536 \"""
Rich Lanec3c2ae12013-01-04 10:13:17 -0800537 def __init__(self, **kwargs):
Dan Talaycof75360a2010-02-05 22:22:54 -0800538 self.header = ofp_header()
539 ofp_stats_request.__init__(self)
540 ofp_--TYPE--_stats_request.__init__(self)
541 self.header.type = OFPT_STATS_REQUEST
542 self.type = --STATS_NAME--
Rich Lanec3c2ae12013-01-04 10:13:17 -0800543 for (k, v) in kwargs.items():
544 if hasattr(self, k):
545 setattr(self, k, v)
546 else:
547 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Dan Talaycof75360a2010-02-05 22:22:54 -0800548
549 def pack(self, assertstruct=True):
Dan Talayco2f820be2010-03-07 11:36:29 -0800550 self.header.length = len(self)
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800551 packed = self.header.pack()
552 packed += ofp_stats_request.pack(self)
Dan Talaycof75360a2010-02-05 22:22:54 -0800553 packed += ofp_--TYPE--_stats_request.pack(self)
Dan Talayco6d2470b2010-02-07 22:59:49 -0800554 return packed
Dan Talaycof75360a2010-02-05 22:22:54 -0800555
556 def unpack(self, binary_string):
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800557 binary_string = self.header.unpack(binary_string)
Dan Talaycof75360a2010-02-05 22:22:54 -0800558 binary_string = ofp_stats_request.unpack(self, binary_string)
559 binary_string = ofp_--TYPE--_stats_request.unpack(self, binary_string)
560 if len(binary_string) != 0:
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800561 print "ERROR unpacking --TYPE--: extra data"
Dan Talaycof75360a2010-02-05 22:22:54 -0800562 return binary_string
563
564 def __len__(self):
565 return len(self.header) + OFP_STATS_REQUEST_BYTES + \\
566 OFP_--TYPE_UPPER--_STATS_REQUEST_BYTES
567
568 def show(self, prefix=''):
Dan Talayco46755fa2010-03-09 21:44:29 -0800569 outstr = prefix + "--TYPE--_stats_request\\n"
570 outstr += prefix + "ofp header:\\n"
571 outstr += self.header.show(prefix + ' ')
572 outstr += ofp_stats_request.show(self)
573 outstr += ofp_--TYPE--_stats_request.show(self)
574 return outstr
Dan Talaycof75360a2010-02-05 22:22:54 -0800575
576 def __eq__(self, other):
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800577 if type(self) != type(other): return False
578 return (self.header == other.header and
579 ofp_stats_request.__eq__(self, other) and
Dan Talaycof75360a2010-02-05 22:22:54 -0800580 ofp_--TYPE--_stats_request.__eq__(self, other))
581
582 def __ne__(self, other): return not self.__eq__(other)
583"""
584
585################################################################
586#
587# Stats replies always have an array at the end.
588# For aggregate and desc, these arrays are always of length 1
589# This array is always called stats
590#
591################################################################
592
593
594# Template for objects stats reply messages
595stats_reply_template = """
596class --TYPE--_stats_reply(ofp_stats_reply):
597 \"""
598 Wrapper class for --TYPE-- stats reply
599 \"""
600 def __init__(self):
601 self.header = ofp_header()
602 ofp_stats_reply.__init__(self)
603 self.header.type = OFPT_STATS_REPLY
604 self.type = --STATS_NAME--
605 # stats: Array of type --TYPE--_stats_entry
606 self.stats = []
607
608 def pack(self, assertstruct=True):
Dan Talayco2f820be2010-03-07 11:36:29 -0800609 self.header.length = len(self)
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800610 packed = self.header.pack()
611 packed += ofp_stats_reply.pack(self)
Dan Talaycof75360a2010-02-05 22:22:54 -0800612 for obj in self.stats:
613 packed += obj.pack()
Dan Talayco6d2470b2010-02-07 22:59:49 -0800614 return packed
Dan Talaycof75360a2010-02-05 22:22:54 -0800615
616 def unpack(self, binary_string):
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800617 binary_string = self.header.unpack(binary_string)
Dan Talaycof75360a2010-02-05 22:22:54 -0800618 binary_string = ofp_stats_reply.unpack(self, binary_string)
619 dummy = --TYPE--_stats_entry()
620 while len(binary_string) >= len(dummy):
621 obj = --TYPE--_stats_entry()
622 binary_string = obj.unpack(binary_string)
623 self.stats.append(obj)
624 if len(binary_string) != 0:
625 print "ERROR unpacking --TYPE-- stats string: extra bytes"
626 return binary_string
627
628 def __len__(self):
629 length = len(self.header) + OFP_STATS_REPLY_BYTES
630 for obj in self.stats:
631 length += len(obj)
632 return length
633
634 def show(self, prefix=''):
Dan Talayco46755fa2010-03-09 21:44:29 -0800635 outstr = prefix + "--TYPE--_stats_reply\\n"
636 outstr += prefix + "ofp header:\\n"
637 outstr += self.header.show(prefix + ' ')
638 outstr += ofp_stats_reply.show(self)
639 outstr += prefix + "Stats array of length " + str(len(self.stats)) + '\\n'
Dan Talaycof75360a2010-02-05 22:22:54 -0800640 for obj in self.stats:
Dan Talayco46755fa2010-03-09 21:44:29 -0800641 outstr += obj.show()
642 return outstr
Dan Talaycof75360a2010-02-05 22:22:54 -0800643
644 def __eq__(self, other):
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800645 if type(self) != type(other): return False
646 return (self.header == other.header and
647 ofp_stats_reply.__eq__(self, other) and
648 self.stats == other.stats)
Dan Talaycof75360a2010-02-05 22:22:54 -0800649
650 def __ne__(self, other): return not self.__eq__(other)
651"""
652
653#
654# To address variations in stats reply bodies, the following
655# "_entry" classes are defined for each element in the reply
656#
657
658extra_stats_entry_defs = """
659# Stats entries define the content of one element in a stats
660# reply for the indicated type; define _entry for consistency
661
662aggregate_stats_entry = ofp_aggregate_stats_reply
663desc_stats_entry = ofp_desc_stats
664port_stats_entry = ofp_port_stats
665queue_stats_entry = ofp_queue_stats
666table_stats_entry = ofp_table_stats
667"""
668
669# Special case flow_stats to handle actions_list
670
671flow_stats_entry_def = """
672#
673# Flow stats entry contains an action list of variable length, so
674# it is done by hand
675#
676
677class flow_stats_entry(ofp_flow_stats):
678 \"""
679 Special case flow stats entry to handle action list object
680 \"""
681 def __init__(self):
682 ofp_flow_stats.__init__(self)
683 self.actions = action_list()
684
685 def pack(self, assertstruct=True):
Dan Talayco6d2470b2010-02-07 22:59:49 -0800686 self.length = len(self)
Dan Talaycof75360a2010-02-05 22:22:54 -0800687 packed = ofp_flow_stats.pack(self, assertstruct)
688 packed += self.actions.pack()
Dan Talayco6d2470b2010-02-07 22:59:49 -0800689 if len(packed) != self.length:
690 print("ERROR: flow_stats_entry pack length not equal",
691 self.length, len(packed))
Dan Talaycof75360a2010-02-05 22:22:54 -0800692 return packed
693
694 def unpack(self, binary_string):
695 binary_string = ofp_flow_stats.unpack(self, binary_string)
696 ai_len = self.length - OFP_FLOW_STATS_BYTES
Dan Talayco6d2470b2010-02-07 22:59:49 -0800697 if ai_len < 0:
698 print("ERROR: flow_stats_entry unpack length too small",
699 self.length)
Dan Talaycof75360a2010-02-05 22:22:54 -0800700 binary_string = self.actions.unpack(binary_string, bytes=ai_len)
701 return binary_string
702
703 def __len__(self):
704 return OFP_FLOW_STATS_BYTES + len(self.actions)
705
706 def show(self, prefix=''):
Dan Talayco46755fa2010-03-09 21:44:29 -0800707 outstr = prefix + "flow_stats_entry\\n"
708 outstr += ofp_flow_stats.show(self, prefix + ' ')
Rich Lanee6ea3fe2013-03-08 17:54:38 -0800709 outstr += prefix + "List actions\\n"
710 for obj in self.actions:
711 outstr += obj.show(prefix + ' ')
Dan Talayco46755fa2010-03-09 21:44:29 -0800712 return outstr
Dan Talaycof75360a2010-02-05 22:22:54 -0800713
714 def __eq__(self, other):
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800715 if type(self) != type(other): return False
Dan Talaycof75360a2010-02-05 22:22:54 -0800716 return (ofp_flow_stats.__eq__(self, other) and
717 self.actions == other.actions)
718
719 def __ne__(self, other): return not self.__eq__(other)
720"""
721
722stats_types = [
723 'aggregate',
724 'desc',
725 'flow',
726 'port',
727 'queue',
728 'table']
729
730if __name__ == '__main__':
731
732 print message_top_matter
733
734 print """
735################################################################
736#
737# OpenFlow Message Definitions
738#
739################################################################
740"""
741
742 msg_types = message_class_map.keys()
743 msg_types.sort()
744
745 for t in msg_types:
746 gen_message_wrapper(t)
747 print
748
749 print """
750################################################################
751#
752# Stats request and reply subclass definitions
753#
754################################################################
755"""
756
757 print extra_ofp_stats_req_defs
758 print extra_stats_entry_defs
759 print flow_stats_entry_def
760
761 # Generate stats request and reply subclasses
762 for t in stats_types:
763 stats_name = "OFPST_" + t.upper()
764 to_print = re.sub('--TYPE--', t, stats_request_template)
765 to_print = re.sub('--TYPE_UPPER--', t.upper(), to_print)
766 to_print = re.sub('--STATS_NAME--', stats_name, to_print)
767 print to_print
768 to_print = re.sub('--TYPE--', t, stats_reply_template)
769 to_print = re.sub('--STATS_NAME--', stats_name, to_print)
770 print to_print
771
Dan Talayco411489d2010-02-12 23:03:46 -0800772 # Lastly, generate a tuple containing all the message classes
773 print """
774message_type_list = (
775 aggregate_stats_reply,
776 aggregate_stats_request,
777 bad_action_error_msg,
778 bad_request_error_msg,
779 barrier_reply,
780 barrier_request,
781 desc_stats_reply,
782 desc_stats_request,
783 echo_reply,
784 echo_request,
785 features_reply,
786 features_request,
787 flow_mod,
788 flow_mod_failed_error_msg,
789 flow_removed,
790 flow_stats_reply,
791 flow_stats_request,
792 get_config_reply,
793 get_config_request,
794 hello,
795 hello_failed_error_msg,
796 packet_in,
797 packet_out,
798 port_mod,
799 port_mod_failed_error_msg,
800 port_stats_reply,
801 port_stats_request,
802 port_status,
803 queue_get_config_reply,
804 queue_get_config_request,
805 queue_op_failed_error_msg,
806 queue_stats_reply,
807 queue_stats_request,
808 set_config,
809 table_stats_reply,
810 table_stats_request,
811 vendor
812 )
813"""
Dan Talaycof75360a2010-02-05 22:22:54 -0800814
815#
816# OFP match variants
817# ICMP 0x801 (?) ==> icmp_type/code replace tp_src/dst
818#
819
820