Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 1 | #!/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 | """ |
| 18 | Generate wrapper classes for OpenFlow messages |
| 19 | |
| 20 | (C) Copyright Stanford University |
| 21 | Date February 2010 |
| 22 | Created by dtalayco |
| 23 | |
| 24 | Attempting to follow http://www.python.org/dev/peps/pep-0008/ |
| 25 | The main exception is that our class names do not use CamelCase |
| 26 | so as to more closely match the original C code names. |
| 27 | |
| 28 | This file is meant to generate a file of_wrapper.py which imports |
| 29 | the base classes generated form automatic processing of openflow.h |
| 30 | and produces wrapper classes for each OpenFlow message type. |
| 31 | |
| 32 | This file will normally be included in of_message.py which provides |
| 33 | additional hand-generated work. |
| 34 | |
| 35 | There are two types of structures/classes here: base components and |
| 36 | message classes. |
| 37 | |
| 38 | Base components are the base data classes which are fixed |
| 39 | length structures including: |
| 40 | ofp_header |
| 41 | Each ofp_action structure |
| 42 | ofp_phy_port |
| 43 | The array elements of all the stats reply messages |
| 44 | The base components are to be imported from a file of_header.py. |
| 45 | |
| 46 | Message classes define a complete message on the wire. These are |
| 47 | comprised of possibly variable length lists of possibly variably |
| 48 | typed objects from the base component list above. |
| 49 | |
| 50 | Each OpenFlow message has a header and zero or more fixed length |
| 51 | members (the "core members" of the class) followed by zero or more |
| 52 | variable length lists. |
| 53 | |
| 54 | The wrapper classes should live in their own name space, probably |
| 55 | of_message. Automatically generated base component and skeletons for |
| 56 | the message classes are assumed generated and the wrapper classes |
| 57 | will inherit from those. |
| 58 | |
| 59 | Every message class must implement pack and unpack functions to |
| 60 | convert between the class and a string representing what goes on the |
| 61 | wire. |
| 62 | |
| 63 | For unpacking, the low level (base-component) classes must implement |
| 64 | their own unpack functions. A single top level unpack function |
| 65 | will do the parsing and call the lower layer unpack functions as |
| 66 | appropriate. |
| 67 | |
| 68 | Every base and message class should implement a show function to |
| 69 | (recursively) display the contents of the object. |
| 70 | |
| 71 | Certain OpenFlow message types are further subclassed. These include |
| 72 | stats_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 | |
| 80 | import re |
| 81 | import string |
| 82 | import sys |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 83 | sys.path.append("../../src/python/oftest") |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 84 | from cstruct import * |
| 85 | from class_maps import class_to_members_map |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 86 | |
| 87 | message_top_matter = """ |
| 88 | # Python OpenFlow message wrapper classes |
| 89 | |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 90 | from cstruct import * |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 91 | from action_list import action_list |
Dan Talayco | 411489d | 2010-02-12 23:03:46 -0800 | [diff] [blame] | 92 | from error import * |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 93 | |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 94 | # Define templates for documentation |
| 95 | class 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 Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 110 | |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 111 | # Normally will define pack, unpack, __len__ functions |
| 112 | |
| 113 | class 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 Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 127 | |
| 128 | ##@var header |
| 129 | # OpenFlow message header: length, version, xid, type |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 130 | ofp_template_msg.__init__(self) |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 131 | self.header = ofp_header() |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 132 | # 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 | \""" |
| 165 | Display the contents of the object in a readable manner |
| 166 | |
| 167 | @param prefix Printed at the beginning of each line. |
| 168 | |
| 169 | \""" |
| 170 | pass |
| 171 | def __eq__(self, other): |
| 172 | \""" |
| 173 | Return True if self and other hold the same data |
| 174 | |
| 175 | @param other Other object in comparison |
| 176 | |
| 177 | \""" |
| 178 | pass |
| 179 | def __ne__(self, other): |
| 180 | \""" |
| 181 | Return True if self and other do not hold the same data |
| 182 | |
| 183 | @param other Other object in comparison |
| 184 | |
| 185 | \""" |
| 186 | pass |
| 187 | """ |
| 188 | |
| 189 | # Dictionary mapping wrapped classes to the auto-generated structure |
| 190 | # underlieing the class (body only, not header or var-length data) |
| 191 | message_class_map = { |
| 192 | "hello" : "ofp_header", |
| 193 | "error" : "ofp_error_msg", |
| 194 | "echo_request" : "ofp_header", |
| 195 | "echo_reply" : "ofp_header", |
| 196 | "vendor" : "ofp_vendor_header", |
| 197 | "features_request" : "ofp_header", |
| 198 | "features_reply" : "ofp_switch_features", |
| 199 | "get_config_request" : "ofp_header", |
| 200 | "get_config_reply" : "ofp_switch_config", |
| 201 | "set_config" : "ofp_switch_config", |
| 202 | "packet_in" : "ofp_packet_in", |
| 203 | "flow_removed" : "ofp_flow_removed", |
| 204 | "port_status" : "ofp_port_status", |
| 205 | "packet_out" : "ofp_packet_out", |
| 206 | "flow_mod" : "ofp_flow_mod", |
| 207 | "port_mod" : "ofp_port_mod", |
| 208 | "stats_request" : "ofp_stats_request", |
| 209 | "stats_reply" : "ofp_stats_reply", |
| 210 | "barrier_request" : "ofp_header", |
| 211 | "barrier_reply" : "ofp_header", |
| 212 | "queue_get_config_request" : "ofp_queue_get_config_request", |
| 213 | "queue_get_config_reply" : "ofp_queue_get_config_reply" |
| 214 | } |
| 215 | |
| 216 | # These messages have a string member at the end of the data |
| 217 | string_members = [ |
| 218 | "hello", |
| 219 | "error", |
| 220 | "echo_request", |
| 221 | "echo_reply", |
| 222 | "vendor", |
| 223 | "packet_in", |
| 224 | "packet_out" |
| 225 | ] |
| 226 | |
| 227 | # These messages have a list (with the given name) in the data, |
| 228 | # after the core members; the type is given for validation |
| 229 | list_members = { |
| 230 | "features_reply" : ('ports', None), |
| 231 | "packet_out" : ('actions', 'action_list'), |
| 232 | "flow_mod" : ('actions', 'action_list'), |
| 233 | "queue_get_config_reply" : ('queues', None) |
| 234 | } |
| 235 | |
| 236 | _ind = " " |
| 237 | |
| 238 | def _p1(s): print _ind + s |
| 239 | def _p2(s): print _ind * 2 + s |
| 240 | def _p3(s): print _ind * 3 + s |
| 241 | def _p4(s): print _ind * 4 + s |
| 242 | |
| 243 | # Okay, this gets kind of ugly: |
| 244 | # There are three variables: |
| 245 | # has_core_members: If parent class is not ofp_header, has inheritance |
| 246 | # has_list: Whether class has trailing array or class |
| 247 | # has_string: Whether class has trailing string |
| 248 | |
| 249 | def gen_message_wrapper(msg): |
| 250 | """ |
| 251 | Generate a wrapper for the given message based on above info |
| 252 | @param msg String identifying the message name for the class |
| 253 | """ |
| 254 | |
| 255 | msg_name = "OFPT_" + msg.upper() |
| 256 | parent = message_class_map[msg] |
| 257 | |
| 258 | has_list = False # Has trailing list |
| 259 | has_core_members = False |
| 260 | has_string = False # Has trailing string |
| 261 | if parent != 'ofp_header': |
| 262 | has_core_members = True |
| 263 | if msg in list_members.keys(): |
| 264 | (list_var, list_type) = list_members[msg] |
| 265 | has_list = True |
| 266 | if msg in string_members: |
| 267 | has_string = True |
| 268 | |
| 269 | if has_core_members: |
| 270 | print "class " + msg + "(" + parent + "):" |
| 271 | else: |
| 272 | print "class " + msg + ":" |
| 273 | _p1('"""') |
| 274 | _p1("Wrapper class for " + msg) |
| 275 | print |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 276 | _p1("OpenFlow message header: length, version, xid, type") |
| 277 | _p1("@arg length: The total length of the message") |
| 278 | _p1("@arg version: The OpenFlow version (" + str(OFP_VERSION) + ")") |
| 279 | _p1("@arg xid: The transaction ID") |
| 280 | _p1("@arg type: The message type (" + msg_name + "=" + |
| 281 | str(eval(msg_name)) + ")") |
| 282 | print |
| 283 | if has_core_members and parent in class_to_members_map.keys(): |
| 284 | _p1("Data members inherited from " + parent + ":") |
| 285 | for var in class_to_members_map[parent]: |
| 286 | _p1("@arg " + var) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 287 | if has_list: |
| 288 | if list_type == None: |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 289 | _p1("@arg " + list_var + ": Variable length array of TBD") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 290 | else: |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 291 | _p1("@arg " + list_var + ": Object of type " + list_type); |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 292 | if has_string: |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 293 | _p1("@arg data: Binary string following message members") |
| 294 | print |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 295 | _p1('"""') |
| 296 | |
| 297 | print |
| 298 | _p1("def __init__(self):") |
| 299 | if has_core_members: |
| 300 | _p2(parent + ".__init__(self)") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 301 | _p2("self.header = ofp_header()") |
| 302 | _p2("self.header.type = " + msg_name) |
| 303 | if has_list: |
| 304 | if list_type == None: |
| 305 | _p2('self.' + list_var + ' = []') |
| 306 | else: |
| 307 | _p2('self.' + list_var + ' = ' + list_type + '()') |
| 308 | if has_string: |
| 309 | _p2('self.data = ""') |
| 310 | |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 311 | print """ |
| 312 | |
| 313 | def pack(self): |
| 314 | \""" |
| 315 | Pack object into string |
| 316 | |
| 317 | @return The packed string which can go on the wire |
| 318 | |
| 319 | \""" |
| 320 | self.header.length = len(self) |
| 321 | packed = self.header.pack() |
| 322 | """ |
| 323 | |
Dan Talayco | 36f2f1f | 2010-02-10 22:40:26 -0800 | [diff] [blame] | 324 | # Have to special case the action length calculation for pkt out |
| 325 | if msg == 'packet_out': |
| 326 | _p2('self.actions_len = len(self.actions)') |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 327 | if has_core_members: |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 328 | _p2("packed += " + parent + ".pack(self)") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 329 | if has_list: |
| 330 | if list_type == None: |
| 331 | _p2('for obj in self.' + list_var + ':') |
| 332 | _p3('packed += obj.pack()') |
| 333 | else: |
| 334 | _p2('packed += self.' + list_var + '.pack()') |
| 335 | if has_string: |
| 336 | _p2('packed += self.data') |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 337 | _p2("return packed") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 338 | |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 339 | print """ |
| 340 | def unpack(self, binary_string): |
| 341 | \""" |
| 342 | Unpack object from a binary string |
| 343 | |
| 344 | @param binary_string The wire protocol byte string holding the object |
| 345 | represented as an array of bytes. |
Dan Talayco | 411489d | 2010-02-12 23:03:46 -0800 | [diff] [blame] | 346 | @return The remainder of binary_string that was not parsed. |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 347 | |
| 348 | \""" |
| 349 | binary_string = self.header.unpack(binary_string) |
| 350 | """ |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 351 | if has_core_members: |
| 352 | _p2("binary_string = " + parent + ".unpack(self, binary_string)") |
| 353 | if has_list: |
| 354 | if list_type == None: |
| 355 | _p2("for obj in self." + list_var + ":") |
| 356 | _p3("binary_string = obj.unpack(binary_string)") |
| 357 | elif msg == "packet_out": # Special case this |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 358 | _p2('binary_string = self.actions.unpack(' + |
| 359 | 'binary_string, bytes=self.actions_len)') |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 360 | elif msg == "flow_mod": # Special case this |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 361 | _p2("ai_len = self.header.length - (OFP_FLOW_MOD_BYTES + " + |
| 362 | "OFP_HEADER_BYTES)") |
| 363 | _p2("binary_string = self.actions.unpack(binary_string, " + |
| 364 | "bytes=ai_len)") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 365 | else: |
| 366 | _p2("binary_string = self." + list_var + ".unpack(binary_string)") |
| 367 | if has_string: |
| 368 | _p2("self.data = binary_string") |
| 369 | _p2("binary_string = ''") |
| 370 | else: |
| 371 | _p2("# Fixme: If no self.data, add check for data remaining") |
| 372 | _p2("return binary_string") |
| 373 | |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 374 | print """ |
| 375 | def __len__(self): |
| 376 | \""" |
| 377 | Return the length of this object once packed into a string |
| 378 | |
| 379 | @return An integer representing the number bytes in the packed |
| 380 | string. |
| 381 | |
| 382 | \""" |
| 383 | length = OFP_HEADER_BYTES |
| 384 | """ |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 385 | if has_core_members: |
| 386 | _p2("length += " + parent + ".__len__(self)") |
| 387 | if has_list: |
| 388 | if list_type == None: |
| 389 | _p2("for obj in self." + list_var + ":") |
| 390 | _p3("length += len(obj)") |
| 391 | else: |
| 392 | _p2("length += len(self." + list_var + ")") |
| 393 | if has_string: |
| 394 | _p2("length += len(self.data)") |
| 395 | _p2("return length") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 396 | |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 397 | print """ |
| 398 | ##@todo Convert this to __str__ |
| 399 | def show(self, prefix=''): |
| 400 | \""" |
| 401 | Display the contents of the object in a readable manner |
| 402 | |
| 403 | @param prefix Printed at the beginning of each line. |
| 404 | |
| 405 | \""" |
| 406 | """ |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 407 | _p2("print prefix + '" + msg + " (" + msg_name + ")'") |
| 408 | _p2("prefix += ' '") |
Dan Talayco | 4557d78 | 2010-02-18 15:18:01 -0800 | [diff] [blame^] | 409 | _p2("print prefix + 'ofp header'") |
| 410 | _p2("self.header.show(prefix + ' ')") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 411 | if has_core_members: |
| 412 | _p2(parent + ".show(self, prefix)") |
| 413 | if has_list: |
| 414 | if list_type == None: |
| 415 | _p2('print prefix + "Array ' + list_var + '"') |
| 416 | _p2('for obj in self.' + list_var +':') |
| 417 | _p3("obj.show(prefix + ' ')") |
| 418 | else: |
| 419 | _p2('print prefix + "List ' + list_var + '"') |
| 420 | _p2('self.' + list_var + ".show(prefix + ' ')") |
| 421 | if has_string: |
| 422 | _p2("print prefix + 'data is of length ' + str(len(self.data))") |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 423 | _p2("##@todo Fix this circular reference") |
| 424 | _p2("# if len(self.data) > 0:") |
| 425 | _p3("# obj = of_message_parse(self.data)") |
| 426 | _p3("# if obj != None:") |
| 427 | _p4("# obj.show(prefix)") |
| 428 | _p3("# else:") |
| 429 | _p4('# print prefix + "Unable to parse data"') |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 430 | |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 431 | print """ |
| 432 | def __eq__(self, other): |
| 433 | \""" |
| 434 | Return True if self and other hold the same data |
| 435 | |
| 436 | @param other Other object in comparison |
| 437 | |
| 438 | \""" |
| 439 | if type(self) != type(other): return False |
| 440 | if not self.header.__eq__(other.header): return False |
| 441 | """ |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 442 | if has_core_members: |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 443 | _p2("if not " + parent + ".__eq__(self, other): return False") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 444 | if has_string: |
| 445 | _p2("if self.data != other.data: return False") |
| 446 | if has_list: |
| 447 | _p2("if self." + list_var + " != other." + list_var + ": return False") |
| 448 | _p2("return True") |
| 449 | |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 450 | print """ |
| 451 | def __ne__(self, other): |
| 452 | \""" |
| 453 | Return True if self and other do not hold the same data |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 454 | |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 455 | @param other Other object in comparison |
| 456 | |
| 457 | \""" |
| 458 | return not self.__eq__(other) |
| 459 | """ |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 460 | |
| 461 | |
| 462 | ################################################################ |
| 463 | # |
| 464 | # Stats request subclasses |
| 465 | # description_request, flow, aggregate, table, port, vendor |
| 466 | # |
| 467 | ################################################################ |
| 468 | |
| 469 | # table and desc stats requests are special with empty body |
| 470 | extra_ofp_stats_req_defs = """ |
| 471 | # Stats request bodies for desc and table stats are not defined in the |
| 472 | # OpenFlow header; We define them here. They are empty classes, really |
| 473 | |
| 474 | class ofp_desc_stats_request: |
| 475 | \""" |
| 476 | Forced definition of ofp_desc_stats_request (empty class) |
| 477 | \""" |
| 478 | def __init__(self): |
| 479 | pass |
| 480 | def pack(self, assertstruct=True): |
| 481 | return "" |
| 482 | def unpack(self, binary_string): |
| 483 | return binary_string |
| 484 | def __len__(self): |
| 485 | return 0 |
| 486 | def show(self, prefix=''): |
Dan Talayco | 36f2f1f | 2010-02-10 22:40:26 -0800 | [diff] [blame] | 487 | print prefix + "ofp_desc_stats_request (empty)" |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 488 | def __eq__(self, other): |
| 489 | return type(self) == type(other) |
| 490 | def __ne__(self, other): |
| 491 | return type(self) != type(other) |
| 492 | |
| 493 | OFP_DESC_STATS_REQUEST_BYTES = 0 |
| 494 | |
| 495 | class ofp_table_stats_request: |
| 496 | \""" |
| 497 | Forced definition of ofp_table_stats_request (empty class) |
| 498 | \""" |
| 499 | def __init__(self): |
| 500 | pass |
| 501 | def pack(self, assertstruct=True): |
| 502 | return "" |
| 503 | def unpack(self, binary_string): |
| 504 | return binary_string |
| 505 | def __len__(self): |
| 506 | return 0 |
| 507 | def show(self, prefix=''): |
Dan Talayco | 36f2f1f | 2010-02-10 22:40:26 -0800 | [diff] [blame] | 508 | print prefix + "ofp_table_stats_request (empty)" |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 509 | def __eq__(self, other): |
| 510 | return type(self) == type(other) |
| 511 | def __ne__(self, other): |
| 512 | return type(self) != type(other) |
| 513 | |
| 514 | OFP_TABLE_STATS_REQUEST_BYTES = 0 |
| 515 | |
| 516 | """ |
| 517 | |
| 518 | stats_request_template = """ |
| 519 | class --TYPE--_stats_request(ofp_stats_request, ofp_--TYPE--_stats_request): |
| 520 | \""" |
| 521 | Wrapper class for --TYPE-- stats request message |
| 522 | \""" |
| 523 | def __init__(self): |
| 524 | self.header = ofp_header() |
| 525 | ofp_stats_request.__init__(self) |
| 526 | ofp_--TYPE--_stats_request.__init__(self) |
| 527 | self.header.type = OFPT_STATS_REQUEST |
| 528 | self.type = --STATS_NAME-- |
| 529 | |
| 530 | def pack(self, assertstruct=True): |
Dan Talayco | 36f2f1f | 2010-02-10 22:40:26 -0800 | [diff] [blame] | 531 | packed = self.header.pack() |
| 532 | packed += ofp_stats_request.pack(self) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 533 | packed += ofp_--TYPE--_stats_request.pack(self) |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 534 | return packed |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 535 | |
| 536 | def unpack(self, binary_string): |
Dan Talayco | 36f2f1f | 2010-02-10 22:40:26 -0800 | [diff] [blame] | 537 | binary_string = self.header.unpack(binary_string) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 538 | binary_string = ofp_stats_request.unpack(self, binary_string) |
| 539 | binary_string = ofp_--TYPE--_stats_request.unpack(self, binary_string) |
| 540 | if len(binary_string) != 0: |
Dan Talayco | 36f2f1f | 2010-02-10 22:40:26 -0800 | [diff] [blame] | 541 | print "ERROR unpacking --TYPE--: extra data" |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 542 | return binary_string |
| 543 | |
| 544 | def __len__(self): |
| 545 | return len(self.header) + OFP_STATS_REQUEST_BYTES + \\ |
| 546 | OFP_--TYPE_UPPER--_STATS_REQUEST_BYTES |
| 547 | |
| 548 | def show(self, prefix=''): |
| 549 | print prefix + "--TYPE--_stats_request" |
Dan Talayco | 36f2f1f | 2010-02-10 22:40:26 -0800 | [diff] [blame] | 550 | print prefix + "ofp header:" |
| 551 | self.header.show(prefix + ' ') |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 552 | ofp_stats_request.show(self) |
| 553 | ofp_--TYPE--_stats_request.show(self) |
| 554 | |
| 555 | def __eq__(self, other): |
Dan Talayco | 36f2f1f | 2010-02-10 22:40:26 -0800 | [diff] [blame] | 556 | if type(self) != type(other): return False |
| 557 | return (self.header == other.header and |
| 558 | ofp_stats_request.__eq__(self, other) and |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 559 | ofp_--TYPE--_stats_request.__eq__(self, other)) |
| 560 | |
| 561 | def __ne__(self, other): return not self.__eq__(other) |
| 562 | """ |
| 563 | |
| 564 | ################################################################ |
| 565 | # |
| 566 | # Stats replies always have an array at the end. |
| 567 | # For aggregate and desc, these arrays are always of length 1 |
| 568 | # This array is always called stats |
| 569 | # |
| 570 | ################################################################ |
| 571 | |
| 572 | |
| 573 | # Template for objects stats reply messages |
| 574 | stats_reply_template = """ |
| 575 | class --TYPE--_stats_reply(ofp_stats_reply): |
| 576 | \""" |
| 577 | Wrapper class for --TYPE-- stats reply |
| 578 | \""" |
| 579 | def __init__(self): |
| 580 | self.header = ofp_header() |
| 581 | ofp_stats_reply.__init__(self) |
| 582 | self.header.type = OFPT_STATS_REPLY |
| 583 | self.type = --STATS_NAME-- |
| 584 | # stats: Array of type --TYPE--_stats_entry |
| 585 | self.stats = [] |
| 586 | |
| 587 | def pack(self, assertstruct=True): |
Dan Talayco | 36f2f1f | 2010-02-10 22:40:26 -0800 | [diff] [blame] | 588 | packed = self.header.pack() |
| 589 | packed += ofp_stats_reply.pack(self) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 590 | for obj in self.stats: |
| 591 | packed += obj.pack() |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 592 | return packed |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 593 | |
| 594 | def unpack(self, binary_string): |
Dan Talayco | 36f2f1f | 2010-02-10 22:40:26 -0800 | [diff] [blame] | 595 | binary_string = self.header.unpack(binary_string) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 596 | binary_string = ofp_stats_reply.unpack(self, binary_string) |
| 597 | dummy = --TYPE--_stats_entry() |
| 598 | while len(binary_string) >= len(dummy): |
| 599 | obj = --TYPE--_stats_entry() |
| 600 | binary_string = obj.unpack(binary_string) |
| 601 | self.stats.append(obj) |
| 602 | if len(binary_string) != 0: |
| 603 | print "ERROR unpacking --TYPE-- stats string: extra bytes" |
| 604 | return binary_string |
| 605 | |
| 606 | def __len__(self): |
| 607 | length = len(self.header) + OFP_STATS_REPLY_BYTES |
| 608 | for obj in self.stats: |
| 609 | length += len(obj) |
| 610 | return length |
| 611 | |
| 612 | def show(self, prefix=''): |
| 613 | print prefix + "--TYPE--_stats_reply" |
Dan Talayco | 36f2f1f | 2010-02-10 22:40:26 -0800 | [diff] [blame] | 614 | print prefix + "ofp header:" |
| 615 | self.header.show(prefix + ' ') |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 616 | ofp_stats_reply.show(self) |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 617 | print prefix + "Stats array of length " + str(len(self.stats)) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 618 | for obj in self.stats: |
| 619 | obj.show() |
| 620 | |
| 621 | def __eq__(self, other): |
Dan Talayco | 36f2f1f | 2010-02-10 22:40:26 -0800 | [diff] [blame] | 622 | if type(self) != type(other): return False |
| 623 | return (self.header == other.header and |
| 624 | ofp_stats_reply.__eq__(self, other) and |
| 625 | self.stats == other.stats) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 626 | |
| 627 | def __ne__(self, other): return not self.__eq__(other) |
| 628 | """ |
| 629 | |
| 630 | # |
| 631 | # To address variations in stats reply bodies, the following |
| 632 | # "_entry" classes are defined for each element in the reply |
| 633 | # |
| 634 | |
| 635 | extra_stats_entry_defs = """ |
| 636 | # Stats entries define the content of one element in a stats |
| 637 | # reply for the indicated type; define _entry for consistency |
| 638 | |
| 639 | aggregate_stats_entry = ofp_aggregate_stats_reply |
| 640 | desc_stats_entry = ofp_desc_stats |
| 641 | port_stats_entry = ofp_port_stats |
| 642 | queue_stats_entry = ofp_queue_stats |
| 643 | table_stats_entry = ofp_table_stats |
| 644 | """ |
| 645 | |
| 646 | # Special case flow_stats to handle actions_list |
| 647 | |
| 648 | flow_stats_entry_def = """ |
| 649 | # |
| 650 | # Flow stats entry contains an action list of variable length, so |
| 651 | # it is done by hand |
| 652 | # |
| 653 | |
| 654 | class flow_stats_entry(ofp_flow_stats): |
| 655 | \""" |
| 656 | Special case flow stats entry to handle action list object |
| 657 | \""" |
| 658 | def __init__(self): |
| 659 | ofp_flow_stats.__init__(self) |
| 660 | self.actions = action_list() |
| 661 | |
| 662 | def pack(self, assertstruct=True): |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 663 | self.length = len(self) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 664 | packed = ofp_flow_stats.pack(self, assertstruct) |
| 665 | packed += self.actions.pack() |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 666 | if len(packed) != self.length: |
| 667 | print("ERROR: flow_stats_entry pack length not equal", |
| 668 | self.length, len(packed)) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 669 | return packed |
| 670 | |
| 671 | def unpack(self, binary_string): |
| 672 | binary_string = ofp_flow_stats.unpack(self, binary_string) |
| 673 | ai_len = self.length - OFP_FLOW_STATS_BYTES |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 674 | if ai_len < 0: |
| 675 | print("ERROR: flow_stats_entry unpack length too small", |
| 676 | self.length) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 677 | binary_string = self.actions.unpack(binary_string, bytes=ai_len) |
| 678 | return binary_string |
| 679 | |
| 680 | def __len__(self): |
| 681 | return OFP_FLOW_STATS_BYTES + len(self.actions) |
| 682 | |
| 683 | def show(self, prefix=''): |
| 684 | print prefix + "flow_stats_entry" |
| 685 | ofp_flow_stats.show(self, prefix + ' ') |
| 686 | self.actions.show(prefix + ' ') |
| 687 | |
| 688 | def __eq__(self, other): |
Dan Talayco | 36f2f1f | 2010-02-10 22:40:26 -0800 | [diff] [blame] | 689 | if type(self) != type(other): return False |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 690 | return (ofp_flow_stats.__eq__(self, other) and |
| 691 | self.actions == other.actions) |
| 692 | |
| 693 | def __ne__(self, other): return not self.__eq__(other) |
| 694 | """ |
| 695 | |
| 696 | stats_types = [ |
| 697 | 'aggregate', |
| 698 | 'desc', |
| 699 | 'flow', |
| 700 | 'port', |
| 701 | 'queue', |
| 702 | 'table'] |
| 703 | |
| 704 | if __name__ == '__main__': |
| 705 | |
| 706 | print message_top_matter |
| 707 | |
| 708 | print """ |
| 709 | ################################################################ |
| 710 | # |
| 711 | # OpenFlow Message Definitions |
| 712 | # |
| 713 | ################################################################ |
| 714 | """ |
| 715 | |
| 716 | msg_types = message_class_map.keys() |
| 717 | msg_types.sort() |
| 718 | |
| 719 | for t in msg_types: |
| 720 | gen_message_wrapper(t) |
| 721 | print |
| 722 | |
| 723 | print """ |
| 724 | ################################################################ |
| 725 | # |
| 726 | # Stats request and reply subclass definitions |
| 727 | # |
| 728 | ################################################################ |
| 729 | """ |
| 730 | |
| 731 | print extra_ofp_stats_req_defs |
| 732 | print extra_stats_entry_defs |
| 733 | print flow_stats_entry_def |
| 734 | |
| 735 | # Generate stats request and reply subclasses |
| 736 | for t in stats_types: |
| 737 | stats_name = "OFPST_" + t.upper() |
| 738 | to_print = re.sub('--TYPE--', t, stats_request_template) |
| 739 | to_print = re.sub('--TYPE_UPPER--', t.upper(), to_print) |
| 740 | to_print = re.sub('--STATS_NAME--', stats_name, to_print) |
| 741 | print to_print |
| 742 | to_print = re.sub('--TYPE--', t, stats_reply_template) |
| 743 | to_print = re.sub('--STATS_NAME--', stats_name, to_print) |
| 744 | print to_print |
| 745 | |
Dan Talayco | 411489d | 2010-02-12 23:03:46 -0800 | [diff] [blame] | 746 | # Lastly, generate a tuple containing all the message classes |
| 747 | print """ |
| 748 | message_type_list = ( |
| 749 | aggregate_stats_reply, |
| 750 | aggregate_stats_request, |
| 751 | bad_action_error_msg, |
| 752 | bad_request_error_msg, |
| 753 | barrier_reply, |
| 754 | barrier_request, |
| 755 | desc_stats_reply, |
| 756 | desc_stats_request, |
| 757 | echo_reply, |
| 758 | echo_request, |
| 759 | features_reply, |
| 760 | features_request, |
| 761 | flow_mod, |
| 762 | flow_mod_failed_error_msg, |
| 763 | flow_removed, |
| 764 | flow_stats_reply, |
| 765 | flow_stats_request, |
| 766 | get_config_reply, |
| 767 | get_config_request, |
| 768 | hello, |
| 769 | hello_failed_error_msg, |
| 770 | packet_in, |
| 771 | packet_out, |
| 772 | port_mod, |
| 773 | port_mod_failed_error_msg, |
| 774 | port_stats_reply, |
| 775 | port_stats_request, |
| 776 | port_status, |
| 777 | queue_get_config_reply, |
| 778 | queue_get_config_request, |
| 779 | queue_op_failed_error_msg, |
| 780 | queue_stats_reply, |
| 781 | queue_stats_request, |
| 782 | set_config, |
| 783 | table_stats_reply, |
| 784 | table_stats_request, |
| 785 | vendor |
| 786 | ) |
| 787 | """ |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 788 | |
| 789 | # |
| 790 | # OFP match variants |
| 791 | # ICMP 0x801 (?) ==> icmp_type/code replace tp_src/dst |
| 792 | # |
| 793 | |
| 794 | |