blob: a9531dd5c4df5876a83ecf5a8e1e899a2d0aaf8e [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 \"""
Dan Talaycof75360a2010-02-05 22:22:54 -0800108 # Additional base data members declared here
Dan Talaycoac1cb812010-02-06 20:34:18 -0800109
Dan Talaycof75360a2010-02-05 22:22:54 -0800110 # Normally will define pack, unpack, __len__ functions
111
112class template_msg(ofp_template_msg):
113 \"""
114 Sample class wrapper for template_msg
115 This class should live in the of_message name space and provides the
116 high level API for an OpenFlow message object. These objects must
117 implement the functions indicated in this template.
118
119 \"""
120 def __init__(self):
121 \"""
122 Constructor
123 Must set the header type value appropriately for the message
124
125 \"""
Dan Talaycoac1cb812010-02-06 20:34:18 -0800126
127 ##@var header
128 # OpenFlow message header: length, version, xid, type
Dan Talaycof75360a2010-02-05 22:22:54 -0800129 ofp_template_msg.__init__(self)
130 # For a real message, will be set to an integer
Rich Laneb73808c2013-03-11 15:22:23 -0700131 self.type = "TEMPLATE_MSG_VALUE"
Dan Talaycof75360a2010-02-05 22:22:54 -0800132 def pack(self):
133 \"""
134 Pack object into string
135
136 @return The packed string which can go on the wire
137
138 \"""
139 pass
140 def unpack(self, binary_string):
141 \"""
142 Unpack object from a binary string
143
144 @param binary_string The wire protocol byte string holding the object
145 represented as an array of bytes.
146
147 @return Typically returns the remainder of binary_string that
148 was not parsed. May give a warning if that string is non-empty
149
150 \"""
151 pass
152 def __len__(self):
153 \"""
154 Return the length of this object once packed into a string
155
156 @return An integer representing the number bytes in the packed
157 string.
158
159 \"""
160 pass
161 def show(self, prefix=''):
162 \"""
Dan Talayco46755fa2010-03-09 21:44:29 -0800163 Generate a string (with multiple lines) describing the contents
164 of the object in a readable manner
Dan Talaycof75360a2010-02-05 22:22:54 -0800165
Dan Talayco46755fa2010-03-09 21:44:29 -0800166 @param prefix Pre-pended at the beginning of each line.
Dan Talaycof75360a2010-02-05 22:22:54 -0800167
168 \"""
169 pass
170 def __eq__(self, other):
171 \"""
172 Return True if self and other hold the same data
173
174 @param other Other object in comparison
175
176 \"""
177 pass
178 def __ne__(self, other):
179 \"""
180 Return True if self and other do not hold the same data
181
182 @param other Other object in comparison
183
184 \"""
185 pass
186"""
187
188# Dictionary mapping wrapped classes to the auto-generated structure
189# underlieing the class (body only, not header or var-length data)
190message_class_map = {
191 "hello" : "ofp_header",
192 "error" : "ofp_error_msg",
193 "echo_request" : "ofp_header",
194 "echo_reply" : "ofp_header",
195 "vendor" : "ofp_vendor_header",
196 "features_request" : "ofp_header",
197 "features_reply" : "ofp_switch_features",
198 "get_config_request" : "ofp_header",
199 "get_config_reply" : "ofp_switch_config",
200 "set_config" : "ofp_switch_config",
201 "packet_in" : "ofp_packet_in",
202 "flow_removed" : "ofp_flow_removed",
203 "port_status" : "ofp_port_status",
204 "packet_out" : "ofp_packet_out",
205 "flow_mod" : "ofp_flow_mod",
206 "port_mod" : "ofp_port_mod",
207 "stats_request" : "ofp_stats_request",
208 "stats_reply" : "ofp_stats_reply",
209 "barrier_request" : "ofp_header",
210 "barrier_reply" : "ofp_header",
211 "queue_get_config_request" : "ofp_queue_get_config_request",
212 "queue_get_config_reply" : "ofp_queue_get_config_reply"
213}
214
215# These messages have a string member at the end of the data
216string_members = [
217 "hello",
218 "error",
219 "echo_request",
220 "echo_reply",
221 "vendor",
222 "packet_in",
223 "packet_out"
224]
225
226# These messages have a list (with the given name) in the data,
227# after the core members; the type is given for validation
228list_members = {
229 "features_reply" : ('ports', None),
230 "packet_out" : ('actions', 'action_list'),
231 "flow_mod" : ('actions', 'action_list'),
232 "queue_get_config_reply" : ('queues', None)
233}
234
235_ind = " "
236
237def _p1(s): print _ind + s
238def _p2(s): print _ind * 2 + s
239def _p3(s): print _ind * 3 + s
240def _p4(s): print _ind * 4 + s
241
242# Okay, this gets kind of ugly:
243# There are three variables:
244# has_core_members: If parent class is not ofp_header, has inheritance
245# has_list: Whether class has trailing array or class
246# has_string: Whether class has trailing string
247
248def gen_message_wrapper(msg):
249 """
250 Generate a wrapper for the given message based on above info
251 @param msg String identifying the message name for the class
252 """
253
254 msg_name = "OFPT_" + msg.upper()
255 parent = message_class_map[msg]
256
257 has_list = False # Has trailing list
Rich Laneb73808c2013-03-11 15:22:23 -0700258 has_core_members = True
Dan Talaycof75360a2010-02-05 22:22:54 -0800259 has_string = False # Has trailing string
Dan Talaycof75360a2010-02-05 22:22:54 -0800260 if msg in list_members.keys():
261 (list_var, list_type) = list_members[msg]
262 has_list = True
263 if msg in string_members:
264 has_string = True
265
266 if has_core_members:
267 print "class " + msg + "(" + parent + "):"
268 else:
269 print "class " + msg + ":"
270 _p1('"""')
271 _p1("Wrapper class for " + msg)
272 print
Dan Talaycoac1cb812010-02-06 20:34:18 -0800273 _p1("OpenFlow message header: length, version, xid, type")
274 _p1("@arg length: The total length of the message")
275 _p1("@arg version: The OpenFlow version (" + str(OFP_VERSION) + ")")
276 _p1("@arg xid: The transaction ID")
277 _p1("@arg type: The message type (" + msg_name + "=" +
278 str(eval(msg_name)) + ")")
279 print
280 if has_core_members and parent in class_to_members_map.keys():
281 _p1("Data members inherited from " + parent + ":")
282 for var in class_to_members_map[parent]:
283 _p1("@arg " + var)
Dan Talaycof75360a2010-02-05 22:22:54 -0800284 if has_list:
285 if list_type == None:
Dan Talaycoac1cb812010-02-06 20:34:18 -0800286 _p1("@arg " + list_var + ": Variable length array of TBD")
Dan Talaycof75360a2010-02-05 22:22:54 -0800287 else:
Dan Talaycoac1cb812010-02-06 20:34:18 -0800288 _p1("@arg " + list_var + ": Object of type " + list_type);
Dan Talaycof75360a2010-02-05 22:22:54 -0800289 if has_string:
Dan Talaycoac1cb812010-02-06 20:34:18 -0800290 _p1("@arg data: Binary string following message members")
291 print
Dan Talaycof75360a2010-02-05 22:22:54 -0800292 _p1('"""')
293
294 print
Rich Lanec3c2ae12013-01-04 10:13:17 -0800295 _p1("def __init__(self, **kwargs):")
Dan Talaycof75360a2010-02-05 22:22:54 -0800296 if has_core_members:
297 _p2(parent + ".__init__(self)")
Rich Laneb73808c2013-03-11 15:22:23 -0700298 _p2("self.version = OFP_VERSION")
299 _p2("self.type = " + msg_name)
Dan Talaycof75360a2010-02-05 22:22:54 -0800300 if has_list:
Rich Lane0f0adc92013-01-04 15:13:02 -0800301 _p2('self.' + list_var + ' = []')
Dan Talaycof75360a2010-02-05 22:22:54 -0800302 if has_string:
303 _p2('self.data = ""')
Rich Lanec3c2ae12013-01-04 10:13:17 -0800304 _p2('for (k, v) in kwargs.items():')
305 _p3('if hasattr(self, k):')
306 _p4('setattr(self, k, v)')
307 _p3('else:')
308 _p4('raise NameError("field %s does not exist in %s" % (k, self.__class__))')
Dan Talaycof75360a2010-02-05 22:22:54 -0800309
Dan Talaycob9cb5482010-02-09 15:23:12 -0800310 print """
311
312 def pack(self):
313 \"""
314 Pack object into string
315
316 @return The packed string which can go on the wire
317
318 \"""
Rich Laneb73808c2013-03-11 15:22:23 -0700319 self.length = len(self)
320 packed = ""
Dan Talaycob9cb5482010-02-09 15:23:12 -0800321"""
322
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800323 # Have to special case the action length calculation for pkt out
324 if msg == 'packet_out':
Rich Lane62e96852013-03-11 12:04:45 -0700325 _p2('self.actions_len = 0')
326 _p2('for obj in self.actions:')
327 _p3('self.actions_len += len(obj)')
Dan Talaycof75360a2010-02-05 22:22:54 -0800328 if has_core_members:
Dan Talayco6d2470b2010-02-07 22:59:49 -0800329 _p2("packed += " + parent + ".pack(self)")
Dan Talaycof75360a2010-02-05 22:22:54 -0800330 if has_list:
331 if list_type == None:
332 _p2('for obj in self.' + list_var + ':')
333 _p3('packed += obj.pack()')
334 else:
Rich Lane62e96852013-03-11 12:04:45 -0700335 _p2('packed += ' + list_type + '(self.' + list_var + ').pack()')
Dan Talaycof75360a2010-02-05 22:22:54 -0800336 if has_string:
337 _p2('packed += self.data')
Dan Talayco6d2470b2010-02-07 22:59:49 -0800338 _p2("return packed")
Dan Talaycof75360a2010-02-05 22:22:54 -0800339
Dan Talaycob9cb5482010-02-09 15:23:12 -0800340 print """
341 def unpack(self, binary_string):
342 \"""
343 Unpack object from a binary string
344
345 @param binary_string The wire protocol byte string holding the object
346 represented as an array of bytes.
Dan Talayco411489d2010-02-12 23:03:46 -0800347 @return The remainder of binary_string that was not parsed.
Dan Talaycob9cb5482010-02-09 15:23:12 -0800348
349 \"""
Dan Talaycob9cb5482010-02-09 15:23:12 -0800350"""
Dan Talaycof75360a2010-02-05 22:22:54 -0800351 if has_core_members:
352 _p2("binary_string = " + parent + ".unpack(self, binary_string)")
353 if has_list:
Dan Talaycoff606492010-05-13 14:22:37 -0700354 if msg == "features_reply": # Special case port parsing
355 # For now, cheat and assume the rest of the message is port list
356 _p2("while len(binary_string) >= OFP_PHY_PORT_BYTES:")
357 _p3("new_port = ofp_phy_port()")
358 _p3("binary_string = new_port.unpack(binary_string)")
359 _p3("self.ports.append(new_port)")
360 elif list_type == None:
Dan Talaycof75360a2010-02-05 22:22:54 -0800361 _p2("for obj in self." + list_var + ":")
362 _p3("binary_string = obj.unpack(binary_string)")
363 elif msg == "packet_out": # Special case this
Rich Lane62e96852013-03-11 12:04:45 -0700364 _p2("obj = action_list()")
365 _p2('binary_string = obj.unpack(' +
Dan Talayco6d2470b2010-02-07 22:59:49 -0800366 'binary_string, bytes=self.actions_len)')
Rich Lane62e96852013-03-11 12:04:45 -0700367 _p2("self.actions = list(obj)")
Dan Talaycof75360a2010-02-05 22:22:54 -0800368 elif msg == "flow_mod": # Special case this
Rich Laneb73808c2013-03-11 15:22:23 -0700369 _p2("ai_len = self.length - (OFP_FLOW_MOD_BYTES + " +
Dan Talayco6d2470b2010-02-07 22:59:49 -0800370 "OFP_HEADER_BYTES)")
Rich Lane62e96852013-03-11 12:04:45 -0700371 _p2("obj = action_list()")
372 _p2("binary_string = obj.unpack(binary_string, " +
Dan Talayco6d2470b2010-02-07 22:59:49 -0800373 "bytes=ai_len)")
Rich Lane62e96852013-03-11 12:04:45 -0700374 _p2("self.actions = list(obj)")
Dan Talaycof75360a2010-02-05 22:22:54 -0800375 else:
Rich Lane62e96852013-03-11 12:04:45 -0700376 _p2("obj = " + list_type + "()")
377 _p2("binary_string = obj.unpack(binary_string)")
378 _p2("self." + list_var + " = list(obj)")
Dan Talaycof75360a2010-02-05 22:22:54 -0800379 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 \"""
Rich Laneb73808c2013-03-11 15:22:23 -0700395 length = 0
Dan Talaycob9cb5482010-02-09 15:23:12 -0800396"""
Dan Talayco6d2470b2010-02-07 22:59:49 -0800397 if has_core_members:
398 _p2("length += " + parent + ".__len__(self)")
399 if has_list:
Rich Lane62e96852013-03-11 12:04:45 -0700400 _p2("for obj in self." + list_var + ":")
401 _p3("length += len(obj)")
Dan Talayco6d2470b2010-02-07 22:59:49 -0800402 if has_string:
403 _p2("length += len(self.data)")
404 _p2("return length")
Dan Talaycof75360a2010-02-05 22:22:54 -0800405
Dan Talaycob9cb5482010-02-09 15:23:12 -0800406 print """
Dan Talaycob9cb5482010-02-09 15:23:12 -0800407 def show(self, prefix=''):
408 \"""
Dan Talayco46755fa2010-03-09 21:44:29 -0800409 Generate a string (with multiple lines) describing the contents
410 of the object in a readable manner
Dan Talaycob9cb5482010-02-09 15:23:12 -0800411
Dan Talayco46755fa2010-03-09 21:44:29 -0800412 @param prefix Pre-pended at the beginning of each line.
Dan Talaycob9cb5482010-02-09 15:23:12 -0800413
414 \"""
415"""
Dan Talayco46755fa2010-03-09 21:44:29 -0800416 _p2("outstr = prefix + '" + msg + " (" + msg_name + ")\\n'")
Dan Talaycof75360a2010-02-05 22:22:54 -0800417 _p2("prefix += ' '")
Dan Talayco46755fa2010-03-09 21:44:29 -0800418 _p2("outstr += prefix + 'ofp header\\n'")
Dan Talaycof75360a2010-02-05 22:22:54 -0800419 if has_core_members:
Dan Talayco46755fa2010-03-09 21:44:29 -0800420 _p2("outstr += " + parent + ".show(self, prefix)")
Dan Talaycof75360a2010-02-05 22:22:54 -0800421 if has_list:
422 if list_type == None:
Dan Talayco46755fa2010-03-09 21:44:29 -0800423 _p2('outstr += prefix + "Array ' + list_var + '\\n"')
Dan Talaycof75360a2010-02-05 22:22:54 -0800424 _p2('for obj in self.' + list_var +':')
Dan Talayco46755fa2010-03-09 21:44:29 -0800425 _p3("outstr += obj.show(prefix + ' ')")
Dan Talaycof75360a2010-02-05 22:22:54 -0800426 else:
Dan Talayco46755fa2010-03-09 21:44:29 -0800427 _p2('outstr += prefix + "List ' + list_var + '\\n"')
Rich Lanee6ea3fe2013-03-08 17:54:38 -0800428 _p2('for obj in self.' + list_var + ':')
429 _p3('outstr += obj.show(prefix + " ")')
Dan Talaycof75360a2010-02-05 22:22:54 -0800430 if has_string:
Dan Talayco46755fa2010-03-09 21:44:29 -0800431 _p2("outstr += prefix + 'data is of length ' + str(len(self.data)) + '\\n'")
Dan Talayco6d2470b2010-02-07 22:59:49 -0800432 _p2("##@todo Fix this circular reference")
433 _p2("# if len(self.data) > 0:")
434 _p3("# obj = of_message_parse(self.data)")
435 _p3("# if obj != None:")
Dan Talayco46755fa2010-03-09 21:44:29 -0800436 _p4("# outstr += obj.show(prefix)")
Dan Talayco6d2470b2010-02-07 22:59:49 -0800437 _p3("# else:")
Dan Talayco46755fa2010-03-09 21:44:29 -0800438 _p4('# outstr += prefix + "Unable to parse data\\n"')
439 _p2('return outstr')
Dan Talaycof75360a2010-02-05 22:22:54 -0800440
Dan Talaycob9cb5482010-02-09 15:23:12 -0800441 print """
442 def __eq__(self, other):
443 \"""
444 Return True if self and other hold the same data
445
446 @param other Other object in comparison
447
448 \"""
449 if type(self) != type(other): return False
Dan Talaycob9cb5482010-02-09 15:23:12 -0800450"""
Dan Talaycof75360a2010-02-05 22:22:54 -0800451 if has_core_members:
Dan Talayco6d2470b2010-02-07 22:59:49 -0800452 _p2("if not " + parent + ".__eq__(self, other): return False")
Dan Talaycof75360a2010-02-05 22:22:54 -0800453 if has_string:
454 _p2("if self.data != other.data: return False")
455 if has_list:
456 _p2("if self." + list_var + " != other." + list_var + ": return False")
457 _p2("return True")
458
Dan Talaycob9cb5482010-02-09 15:23:12 -0800459 print """
460 def __ne__(self, other):
461 \"""
462 Return True if self and other do not hold the same data
Dan Talaycof75360a2010-02-05 22:22:54 -0800463
Dan Talaycob9cb5482010-02-09 15:23:12 -0800464 @param other Other object in comparison
465
466 \"""
467 return not self.__eq__(other)
468 """
Dan Talaycof75360a2010-02-05 22:22:54 -0800469
470
471################################################################
472#
473# Stats request subclasses
474# description_request, flow, aggregate, table, port, vendor
475#
476################################################################
477
478# table and desc stats requests are special with empty body
479extra_ofp_stats_req_defs = """
480# Stats request bodies for desc and table stats are not defined in the
481# OpenFlow header; We define them here. They are empty classes, really
482
483class ofp_desc_stats_request:
484 \"""
485 Forced definition of ofp_desc_stats_request (empty class)
486 \"""
487 def __init__(self):
488 pass
489 def pack(self, assertstruct=True):
490 return ""
491 def unpack(self, binary_string):
492 return binary_string
493 def __len__(self):
494 return 0
495 def show(self, prefix=''):
Dan Talayco46755fa2010-03-09 21:44:29 -0800496 return prefix + "ofp_desc_stats_request (empty)\\n"
Dan Talaycof75360a2010-02-05 22:22:54 -0800497 def __eq__(self, other):
498 return type(self) == type(other)
499 def __ne__(self, other):
500 return type(self) != type(other)
501
502OFP_DESC_STATS_REQUEST_BYTES = 0
503
504class ofp_table_stats_request:
505 \"""
506 Forced definition of ofp_table_stats_request (empty class)
507 \"""
508 def __init__(self):
509 pass
510 def pack(self, assertstruct=True):
511 return ""
512 def unpack(self, binary_string):
513 return binary_string
514 def __len__(self):
515 return 0
516 def show(self, prefix=''):
Dan Talayco46755fa2010-03-09 21:44:29 -0800517 return prefix + "ofp_table_stats_request (empty)\\n"
Dan Talaycof75360a2010-02-05 22:22:54 -0800518 def __eq__(self, other):
519 return type(self) == type(other)
520 def __ne__(self, other):
521 return type(self) != type(other)
522
523OFP_TABLE_STATS_REQUEST_BYTES = 0
524
525"""
526
527stats_request_template = """
528class --TYPE--_stats_request(ofp_stats_request, ofp_--TYPE--_stats_request):
529 \"""
530 Wrapper class for --TYPE-- stats request message
531 \"""
Rich Lanec3c2ae12013-01-04 10:13:17 -0800532 def __init__(self, **kwargs):
Dan Talaycof75360a2010-02-05 22:22:54 -0800533 ofp_stats_request.__init__(self)
534 ofp_--TYPE--_stats_request.__init__(self)
Rich Laneb73808c2013-03-11 15:22:23 -0700535 self.version = OFP_VERSION
536 self.type = OFPT_STATS_REQUEST
Rich Lane7c7342a2013-03-11 14:16:58 -0700537 self.stats_type = --STATS_NAME--
Rich Lanec3c2ae12013-01-04 10:13:17 -0800538 for (k, v) in kwargs.items():
539 if hasattr(self, k):
540 setattr(self, k, v)
541 else:
542 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Dan Talaycof75360a2010-02-05 22:22:54 -0800543
544 def pack(self, assertstruct=True):
Rich Laneb73808c2013-03-11 15:22:23 -0700545 self.length = len(self)
546 packed = ""
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800547 packed += ofp_stats_request.pack(self)
Dan Talaycof75360a2010-02-05 22:22:54 -0800548 packed += ofp_--TYPE--_stats_request.pack(self)
Dan Talayco6d2470b2010-02-07 22:59:49 -0800549 return packed
Dan Talaycof75360a2010-02-05 22:22:54 -0800550
551 def unpack(self, binary_string):
552 binary_string = ofp_stats_request.unpack(self, binary_string)
553 binary_string = ofp_--TYPE--_stats_request.unpack(self, binary_string)
554 if len(binary_string) != 0:
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800555 print "ERROR unpacking --TYPE--: extra data"
Dan Talaycof75360a2010-02-05 22:22:54 -0800556 return binary_string
557
558 def __len__(self):
Rich Laneb73808c2013-03-11 15:22:23 -0700559 return OFP_STATS_REQUEST_BYTES + \\
Dan Talaycof75360a2010-02-05 22:22:54 -0800560 OFP_--TYPE_UPPER--_STATS_REQUEST_BYTES
561
562 def show(self, prefix=''):
Dan Talayco46755fa2010-03-09 21:44:29 -0800563 outstr = prefix + "--TYPE--_stats_request\\n"
564 outstr += prefix + "ofp header:\\n"
Dan Talayco46755fa2010-03-09 21:44:29 -0800565 outstr += ofp_stats_request.show(self)
566 outstr += ofp_--TYPE--_stats_request.show(self)
567 return outstr
Dan Talaycof75360a2010-02-05 22:22:54 -0800568
569 def __eq__(self, other):
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800570 if type(self) != type(other): return False
Rich Laneb73808c2013-03-11 15:22:23 -0700571 return (ofp_stats_request.__eq__(self, other) and
Dan Talaycof75360a2010-02-05 22:22:54 -0800572 ofp_--TYPE--_stats_request.__eq__(self, other))
573
574 def __ne__(self, other): return not self.__eq__(other)
575"""
576
577################################################################
578#
579# Stats replies always have an array at the end.
580# For aggregate and desc, these arrays are always of length 1
581# This array is always called stats
582#
583################################################################
584
585
586# Template for objects stats reply messages
587stats_reply_template = """
588class --TYPE--_stats_reply(ofp_stats_reply):
589 \"""
590 Wrapper class for --TYPE-- stats reply
591 \"""
592 def __init__(self):
Dan Talaycof75360a2010-02-05 22:22:54 -0800593 ofp_stats_reply.__init__(self)
Rich Laneb73808c2013-03-11 15:22:23 -0700594 self.version = OFP_VERSION
595 self.type = OFPT_STATS_REPLY
596 self.stats_type = --STATS_NAME--
Dan Talaycof75360a2010-02-05 22:22:54 -0800597 # stats: Array of type --TYPE--_stats_entry
Rich Lane5fd6faf2013-03-11 13:30:20 -0700598 self.entries = []
Dan Talaycof75360a2010-02-05 22:22:54 -0800599
600 def pack(self, assertstruct=True):
Rich Laneb73808c2013-03-11 15:22:23 -0700601 self.length = len(self)
602 packed = ""
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800603 packed += ofp_stats_reply.pack(self)
Rich Lane5fd6faf2013-03-11 13:30:20 -0700604 for obj in self.entries:
Dan Talaycof75360a2010-02-05 22:22:54 -0800605 packed += obj.pack()
Dan Talayco6d2470b2010-02-07 22:59:49 -0800606 return packed
Dan Talaycof75360a2010-02-05 22:22:54 -0800607
608 def unpack(self, binary_string):
609 binary_string = ofp_stats_reply.unpack(self, binary_string)
610 dummy = --TYPE--_stats_entry()
611 while len(binary_string) >= len(dummy):
612 obj = --TYPE--_stats_entry()
613 binary_string = obj.unpack(binary_string)
Rich Lane5fd6faf2013-03-11 13:30:20 -0700614 self.entries.append(obj)
Dan Talaycof75360a2010-02-05 22:22:54 -0800615 if len(binary_string) != 0:
616 print "ERROR unpacking --TYPE-- stats string: extra bytes"
617 return binary_string
618
619 def __len__(self):
Rich Laneb73808c2013-03-11 15:22:23 -0700620 length = OFP_STATS_REPLY_BYTES
Rich Lane5fd6faf2013-03-11 13:30:20 -0700621 for obj in self.entries:
Dan Talaycof75360a2010-02-05 22:22:54 -0800622 length += len(obj)
623 return length
624
625 def show(self, prefix=''):
Dan Talayco46755fa2010-03-09 21:44:29 -0800626 outstr = prefix + "--TYPE--_stats_reply\\n"
627 outstr += prefix + "ofp header:\\n"
Dan Talayco46755fa2010-03-09 21:44:29 -0800628 outstr += ofp_stats_reply.show(self)
Rich Lane5fd6faf2013-03-11 13:30:20 -0700629 outstr += prefix + "Stats array of length " + str(len(self.entries)) + '\\n'
630 for obj in self.entries:
Dan Talayco46755fa2010-03-09 21:44:29 -0800631 outstr += obj.show()
632 return outstr
Dan Talaycof75360a2010-02-05 22:22:54 -0800633
634 def __eq__(self, other):
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800635 if type(self) != type(other): return False
Rich Laneb73808c2013-03-11 15:22:23 -0700636 return (ofp_stats_reply.__eq__(self, other) and
Rich Lane5fd6faf2013-03-11 13:30:20 -0700637 self.entries == other.entries)
Dan Talaycof75360a2010-02-05 22:22:54 -0800638
639 def __ne__(self, other): return not self.__eq__(other)
640"""
641
642#
643# To address variations in stats reply bodies, the following
644# "_entry" classes are defined for each element in the reply
645#
646
647extra_stats_entry_defs = """
648# Stats entries define the content of one element in a stats
649# reply for the indicated type; define _entry for consistency
650
651aggregate_stats_entry = ofp_aggregate_stats_reply
652desc_stats_entry = ofp_desc_stats
653port_stats_entry = ofp_port_stats
654queue_stats_entry = ofp_queue_stats
655table_stats_entry = ofp_table_stats
656"""
657
658# Special case flow_stats to handle actions_list
659
660flow_stats_entry_def = """
661#
662# Flow stats entry contains an action list of variable length, so
663# it is done by hand
664#
665
666class flow_stats_entry(ofp_flow_stats):
667 \"""
668 Special case flow stats entry to handle action list object
669 \"""
670 def __init__(self):
671 ofp_flow_stats.__init__(self)
Rich Lane62e96852013-03-11 12:04:45 -0700672 self.actions = []
Dan Talaycof75360a2010-02-05 22:22:54 -0800673
674 def pack(self, assertstruct=True):
Dan Talayco6d2470b2010-02-07 22:59:49 -0800675 self.length = len(self)
Dan Talaycof75360a2010-02-05 22:22:54 -0800676 packed = ofp_flow_stats.pack(self, assertstruct)
Rich Lane62e96852013-03-11 12:04:45 -0700677 packed += action_list(self.actions).pack()
Dan Talayco6d2470b2010-02-07 22:59:49 -0800678 if len(packed) != self.length:
679 print("ERROR: flow_stats_entry pack length not equal",
680 self.length, len(packed))
Dan Talaycof75360a2010-02-05 22:22:54 -0800681 return packed
682
683 def unpack(self, binary_string):
684 binary_string = ofp_flow_stats.unpack(self, binary_string)
685 ai_len = self.length - OFP_FLOW_STATS_BYTES
Dan Talayco6d2470b2010-02-07 22:59:49 -0800686 if ai_len < 0:
687 print("ERROR: flow_stats_entry unpack length too small",
688 self.length)
Rich Lane62e96852013-03-11 12:04:45 -0700689 obj = action_list()
690 binary_string = obj.unpack(binary_string, bytes=ai_len)
691 self.actions = list(obj)
Dan Talaycof75360a2010-02-05 22:22:54 -0800692 return binary_string
693
694 def __len__(self):
695 return OFP_FLOW_STATS_BYTES + len(self.actions)
696
697 def show(self, prefix=''):
Dan Talayco46755fa2010-03-09 21:44:29 -0800698 outstr = prefix + "flow_stats_entry\\n"
699 outstr += ofp_flow_stats.show(self, prefix + ' ')
Rich Lanee6ea3fe2013-03-08 17:54:38 -0800700 outstr += prefix + "List actions\\n"
701 for obj in self.actions:
702 outstr += obj.show(prefix + ' ')
Dan Talayco46755fa2010-03-09 21:44:29 -0800703 return outstr
Dan Talaycof75360a2010-02-05 22:22:54 -0800704
705 def __eq__(self, other):
Dan Talayco36f2f1f2010-02-10 22:40:26 -0800706 if type(self) != type(other): return False
Dan Talaycof75360a2010-02-05 22:22:54 -0800707 return (ofp_flow_stats.__eq__(self, other) and
708 self.actions == other.actions)
709
710 def __ne__(self, other): return not self.__eq__(other)
711"""
712
713stats_types = [
714 'aggregate',
715 'desc',
716 'flow',
717 'port',
718 'queue',
719 'table']
720
721if __name__ == '__main__':
722
723 print message_top_matter
724
725 print """
726################################################################
727#
728# OpenFlow Message Definitions
729#
730################################################################
731"""
732
733 msg_types = message_class_map.keys()
734 msg_types.sort()
735
736 for t in msg_types:
737 gen_message_wrapper(t)
738 print
739
740 print """
741################################################################
742#
743# Stats request and reply subclass definitions
744#
745################################################################
746"""
747
748 print extra_ofp_stats_req_defs
749 print extra_stats_entry_defs
750 print flow_stats_entry_def
751
752 # Generate stats request and reply subclasses
753 for t in stats_types:
754 stats_name = "OFPST_" + t.upper()
755 to_print = re.sub('--TYPE--', t, stats_request_template)
756 to_print = re.sub('--TYPE_UPPER--', t.upper(), to_print)
757 to_print = re.sub('--STATS_NAME--', stats_name, to_print)
758 print to_print
759 to_print = re.sub('--TYPE--', t, stats_reply_template)
760 to_print = re.sub('--STATS_NAME--', stats_name, to_print)
761 print to_print
762
Dan Talayco411489d2010-02-12 23:03:46 -0800763 # Lastly, generate a tuple containing all the message classes
764 print """
765message_type_list = (
766 aggregate_stats_reply,
767 aggregate_stats_request,
768 bad_action_error_msg,
769 bad_request_error_msg,
770 barrier_reply,
771 barrier_request,
772 desc_stats_reply,
773 desc_stats_request,
774 echo_reply,
775 echo_request,
776 features_reply,
777 features_request,
778 flow_mod,
779 flow_mod_failed_error_msg,
780 flow_removed,
781 flow_stats_reply,
782 flow_stats_request,
783 get_config_reply,
784 get_config_request,
785 hello,
786 hello_failed_error_msg,
787 packet_in,
788 packet_out,
789 port_mod,
790 port_mod_failed_error_msg,
791 port_stats_reply,
792 port_stats_request,
793 port_status,
794 queue_get_config_reply,
795 queue_get_config_request,
796 queue_op_failed_error_msg,
797 queue_stats_reply,
798 queue_stats_request,
799 set_config,
800 table_stats_reply,
801 table_stats_request,
802 vendor
803 )
804"""
Dan Talaycof75360a2010-02-05 22:22:54 -0800805
806#
807# OFP match variants
808# ICMP 0x801 (?) ==> icmp_type/code replace tp_src/dst
809#
810
811