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 | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 83 | sys.path.append("../../src/python/oftest/ofmsg") |
| 84 | from ofp import * |
| 85 | from ofp_aux 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 | |
| 90 | from ofp import * |
| 91 | from action_list import action_list |
| 92 | |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 93 | # Define templates for documentation |
| 94 | class ofp_template_msg: |
| 95 | \""" |
| 96 | Sample base class for template_msg; normally auto generated |
| 97 | This class should live in the of_header name space and provides the |
| 98 | base class for this type of message. It will be wrapped for the |
| 99 | high level API. |
| 100 | |
| 101 | \""" |
| 102 | def __init__(self): |
| 103 | \""" |
| 104 | Constructor for base class |
| 105 | |
| 106 | \""" |
| 107 | self.header = ofp_header() |
| 108 | # Additional base data members declared here |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 109 | |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 110 | # Normally will define pack, unpack, __len__ functions |
| 111 | |
| 112 | class 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 Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 126 | |
| 127 | ##@var header |
| 128 | # OpenFlow message header: length, version, xid, type |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 129 | ofp_template_msg.__init__(self) |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 130 | self.header = ofp_header() |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 131 | # For a real message, will be set to an integer |
| 132 | self.header.type = "TEMPLATE_MSG_VALUE" |
| 133 | def pack(self): |
| 134 | \""" |
| 135 | Pack object into string |
| 136 | |
| 137 | @return The packed string which can go on the wire |
| 138 | |
| 139 | \""" |
| 140 | pass |
| 141 | def unpack(self, binary_string): |
| 142 | \""" |
| 143 | Unpack object from a binary string |
| 144 | |
| 145 | @param binary_string The wire protocol byte string holding the object |
| 146 | represented as an array of bytes. |
| 147 | |
| 148 | @return Typically returns the remainder of binary_string that |
| 149 | was not parsed. May give a warning if that string is non-empty |
| 150 | |
| 151 | \""" |
| 152 | pass |
| 153 | def __len__(self): |
| 154 | \""" |
| 155 | Return the length of this object once packed into a string |
| 156 | |
| 157 | @return An integer representing the number bytes in the packed |
| 158 | string. |
| 159 | |
| 160 | \""" |
| 161 | pass |
| 162 | def show(self, prefix=''): |
| 163 | \""" |
| 164 | Display the contents of the object in a readable manner |
| 165 | |
| 166 | @param prefix Printed at the beginning of each line. |
| 167 | |
| 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) |
| 190 | message_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 |
| 216 | string_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 |
| 228 | list_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 | |
| 237 | def _p1(s): print _ind + s |
| 238 | def _p2(s): print _ind * 2 + s |
| 239 | def _p3(s): print _ind * 3 + s |
| 240 | def _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 | |
| 248 | def 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 |
| 258 | has_core_members = False |
| 259 | has_string = False # Has trailing string |
| 260 | if parent != 'ofp_header': |
| 261 | has_core_members = True |
| 262 | if msg in list_members.keys(): |
| 263 | (list_var, list_type) = list_members[msg] |
| 264 | has_list = True |
| 265 | if msg in string_members: |
| 266 | has_string = True |
| 267 | |
| 268 | if has_core_members: |
| 269 | print "class " + msg + "(" + parent + "):" |
| 270 | else: |
| 271 | print "class " + msg + ":" |
| 272 | _p1('"""') |
| 273 | _p1("Wrapper class for " + msg) |
| 274 | print |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 275 | _p1("OpenFlow message header: length, version, xid, type") |
| 276 | _p1("@arg length: The total length of the message") |
| 277 | _p1("@arg version: The OpenFlow version (" + str(OFP_VERSION) + ")") |
| 278 | _p1("@arg xid: The transaction ID") |
| 279 | _p1("@arg type: The message type (" + msg_name + "=" + |
| 280 | str(eval(msg_name)) + ")") |
| 281 | print |
| 282 | if has_core_members and parent in class_to_members_map.keys(): |
| 283 | _p1("Data members inherited from " + parent + ":") |
| 284 | for var in class_to_members_map[parent]: |
| 285 | _p1("@arg " + var) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 286 | if has_list: |
| 287 | if list_type == None: |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 288 | _p1("@arg " + list_var + ": Variable length array of TBD") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 289 | else: |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 290 | _p1("@arg " + list_var + ": Object of type " + list_type); |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 291 | if has_string: |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 292 | _p1("@arg data: Binary string following message members") |
| 293 | print |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 294 | _p1('"""') |
| 295 | |
| 296 | print |
| 297 | _p1("def __init__(self):") |
| 298 | if has_core_members: |
| 299 | _p2(parent + ".__init__(self)") |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 300 | _p2("##@var header") |
| 301 | _p2("# OpenFlow message header: length, version, xid, type") |
| 302 | _p2("# @arg length: The total length of the message") |
| 303 | _p2("# @arg version: The OpenFlow version (" + str(OFP_VERSION) + ")") |
| 304 | _p2("# @arg xid: The transaction ID") |
| 305 | _p2("# @arg type: The message type (" + msg_name + "=" + |
| 306 | str(eval(msg_name)) + ")") |
| 307 | print |
| 308 | if has_list: |
| 309 | _p2("##@var " + list_var) |
| 310 | if list_type == None: |
| 311 | _p2("# Array of objects of type TBD") |
| 312 | else: |
| 313 | _p2("# Object of type " + list_type) |
| 314 | print |
| 315 | if has_string: |
| 316 | _p2("##@var data") |
| 317 | _p2("# Binary string following message members") |
| 318 | print |
| 319 | |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 320 | _p2("self.header = ofp_header()") |
| 321 | _p2("self.header.type = " + msg_name) |
| 322 | if has_list: |
| 323 | if list_type == None: |
| 324 | _p2('self.' + list_var + ' = []') |
| 325 | else: |
| 326 | _p2('self.' + list_var + ' = ' + list_type + '()') |
| 327 | if has_string: |
| 328 | _p2('self.data = ""') |
| 329 | |
| 330 | print |
| 331 | _p1("def pack(self):") |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 332 | _p2("self.header.length = len(self)") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 333 | _p2("packed = self.header.pack()") |
| 334 | if has_core_members: |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 335 | _p2("packed += " + parent + ".pack(self)") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 336 | if has_list: |
| 337 | if list_type == None: |
| 338 | _p2('for obj in self.' + list_var + ':') |
| 339 | _p3('packed += obj.pack()') |
| 340 | else: |
| 341 | _p2('packed += self.' + list_var + '.pack()') |
| 342 | if has_string: |
| 343 | _p2('packed += self.data') |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 344 | _p2("return packed") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 345 | |
| 346 | print |
| 347 | _p1("def unpack(self, binary_string):") |
| 348 | _p2("binary_string = self.header.unpack(binary_string)") |
| 349 | if has_core_members: |
| 350 | _p2("binary_string = " + parent + ".unpack(self, binary_string)") |
| 351 | if has_list: |
| 352 | if list_type == None: |
| 353 | _p2("for obj in self." + list_var + ":") |
| 354 | _p3("binary_string = obj.unpack(binary_string)") |
| 355 | elif msg == "packet_out": # Special case this |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 356 | _p2('binary_string = self.actions.unpack(' + |
| 357 | 'binary_string, bytes=self.actions_len)') |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 358 | elif msg == "flow_mod": # Special case this |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 359 | _p2("ai_len = self.header.length - (OFP_FLOW_MOD_BYTES + " + |
| 360 | "OFP_HEADER_BYTES)") |
| 361 | _p2("binary_string = self.actions.unpack(binary_string, " + |
| 362 | "bytes=ai_len)") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 363 | else: |
| 364 | _p2("binary_string = self." + list_var + ".unpack(binary_string)") |
| 365 | if has_string: |
| 366 | _p2("self.data = binary_string") |
| 367 | _p2("binary_string = ''") |
| 368 | else: |
| 369 | _p2("# Fixme: If no self.data, add check for data remaining") |
| 370 | _p2("return binary_string") |
| 371 | |
| 372 | print |
| 373 | _p1("def __len__(self):") |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 374 | _p2("length = OFP_HEADER_BYTES") |
| 375 | if has_core_members: |
| 376 | _p2("length += " + parent + ".__len__(self)") |
| 377 | if has_list: |
| 378 | if list_type == None: |
| 379 | _p2("for obj in self." + list_var + ":") |
| 380 | _p3("length += len(obj)") |
| 381 | else: |
| 382 | _p2("length += len(self." + list_var + ")") |
| 383 | if has_string: |
| 384 | _p2("length += len(self.data)") |
| 385 | _p2("return length") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 386 | |
| 387 | print |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 388 | _p1("##@todo Convert this to __str__") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 389 | _p1("def show(self, prefix=''):") |
| 390 | _p2("print prefix + '" + msg + " (" + msg_name + ")'") |
| 391 | _p2("prefix += ' '") |
| 392 | _p2("self.header.show(prefix)") |
| 393 | if has_core_members: |
| 394 | _p2(parent + ".show(self, prefix)") |
| 395 | if has_list: |
| 396 | if list_type == None: |
| 397 | _p2('print prefix + "Array ' + list_var + '"') |
| 398 | _p2('for obj in self.' + list_var +':') |
| 399 | _p3("obj.show(prefix + ' ')") |
| 400 | else: |
| 401 | _p2('print prefix + "List ' + list_var + '"') |
| 402 | _p2('self.' + list_var + ".show(prefix + ' ')") |
| 403 | if has_string: |
| 404 | _p2("print prefix + 'data is of length ' + str(len(self.data))") |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 405 | _p2("##@todo Fix this circular reference") |
| 406 | _p2("# if len(self.data) > 0:") |
| 407 | _p3("# obj = of_message_parse(self.data)") |
| 408 | _p3("# if obj != None:") |
| 409 | _p4("# obj.show(prefix)") |
| 410 | _p3("# else:") |
| 411 | _p4('# print prefix + "Unable to parse data"') |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 412 | |
| 413 | print |
| 414 | _p1("def __eq__(self, other):") |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 415 | _p2("if type(self) != type(other): return False") |
| 416 | _p2("if not self.header.__eq__(other.header): return False") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 417 | if has_core_members: |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 418 | _p2("if not " + parent + ".__eq__(self, other): return False") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 419 | if has_string: |
| 420 | _p2("if self.data != other.data: return False") |
| 421 | if has_list: |
| 422 | _p2("if self." + list_var + " != other." + list_var + ": return False") |
| 423 | _p2("return True") |
| 424 | |
| 425 | print |
| 426 | _p1("def __ne__(self, other): return not self.__eq__(other)") |
| 427 | |
| 428 | |
| 429 | |
| 430 | ################################################################ |
| 431 | # |
| 432 | # Stats request subclasses |
| 433 | # description_request, flow, aggregate, table, port, vendor |
| 434 | # |
| 435 | ################################################################ |
| 436 | |
| 437 | # table and desc stats requests are special with empty body |
| 438 | extra_ofp_stats_req_defs = """ |
| 439 | # Stats request bodies for desc and table stats are not defined in the |
| 440 | # OpenFlow header; We define them here. They are empty classes, really |
| 441 | |
| 442 | class ofp_desc_stats_request: |
| 443 | \""" |
| 444 | Forced definition of ofp_desc_stats_request (empty class) |
| 445 | \""" |
| 446 | def __init__(self): |
| 447 | pass |
| 448 | def pack(self, assertstruct=True): |
| 449 | return "" |
| 450 | def unpack(self, binary_string): |
| 451 | return binary_string |
| 452 | def __len__(self): |
| 453 | return 0 |
| 454 | def show(self, prefix=''): |
| 455 | pass |
| 456 | def __eq__(self, other): |
| 457 | return type(self) == type(other) |
| 458 | def __ne__(self, other): |
| 459 | return type(self) != type(other) |
| 460 | |
| 461 | OFP_DESC_STATS_REQUEST_BYTES = 0 |
| 462 | |
| 463 | class ofp_table_stats_request: |
| 464 | \""" |
| 465 | Forced definition of ofp_table_stats_request (empty class) |
| 466 | \""" |
| 467 | def __init__(self): |
| 468 | pass |
| 469 | def pack(self, assertstruct=True): |
| 470 | return "" |
| 471 | def unpack(self, binary_string): |
| 472 | return binary_string |
| 473 | def __len__(self): |
| 474 | return 0 |
| 475 | def show(self, prefix=''): |
| 476 | pass |
| 477 | def __eq__(self, other): |
| 478 | return type(self) == type(other) |
| 479 | def __ne__(self, other): |
| 480 | return type(self) != type(other) |
| 481 | |
| 482 | OFP_TABLE_STATS_REQUEST_BYTES = 0 |
| 483 | |
| 484 | """ |
| 485 | |
| 486 | stats_request_template = """ |
| 487 | class --TYPE--_stats_request(ofp_stats_request, ofp_--TYPE--_stats_request): |
| 488 | \""" |
| 489 | Wrapper class for --TYPE-- stats request message |
| 490 | \""" |
| 491 | def __init__(self): |
| 492 | self.header = ofp_header() |
| 493 | ofp_stats_request.__init__(self) |
| 494 | ofp_--TYPE--_stats_request.__init__(self) |
| 495 | self.header.type = OFPT_STATS_REQUEST |
| 496 | self.type = --STATS_NAME-- |
| 497 | |
| 498 | def pack(self, assertstruct=True): |
| 499 | packed = ofp_stats_request.pack(self) |
| 500 | packed += ofp_--TYPE--_stats_request.pack(self) |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 501 | return packed |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 502 | |
| 503 | def unpack(self, binary_string): |
| 504 | binary_string = ofp_stats_request.unpack(self, binary_string) |
| 505 | binary_string = ofp_--TYPE--_stats_request.unpack(self, binary_string) |
| 506 | if len(binary_string) != 0: |
| 507 | print "Error unpacking --TYPE--: extra data" |
| 508 | return binary_string |
| 509 | |
| 510 | def __len__(self): |
| 511 | return len(self.header) + OFP_STATS_REQUEST_BYTES + \\ |
| 512 | OFP_--TYPE_UPPER--_STATS_REQUEST_BYTES |
| 513 | |
| 514 | def show(self, prefix=''): |
| 515 | print prefix + "--TYPE--_stats_request" |
| 516 | ofp_stats_request.show(self) |
| 517 | ofp_--TYPE--_stats_request.show(self) |
| 518 | |
| 519 | def __eq__(self, other): |
| 520 | return (ofp_stats_request.__eq__(self, other) and |
| 521 | ofp_--TYPE--_stats_request.__eq__(self, other)) |
| 522 | |
| 523 | def __ne__(self, other): return not self.__eq__(other) |
| 524 | """ |
| 525 | |
| 526 | ################################################################ |
| 527 | # |
| 528 | # Stats replies always have an array at the end. |
| 529 | # For aggregate and desc, these arrays are always of length 1 |
| 530 | # This array is always called stats |
| 531 | # |
| 532 | ################################################################ |
| 533 | |
| 534 | |
| 535 | # Template for objects stats reply messages |
| 536 | stats_reply_template = """ |
| 537 | class --TYPE--_stats_reply(ofp_stats_reply): |
| 538 | \""" |
| 539 | Wrapper class for --TYPE-- stats reply |
| 540 | \""" |
| 541 | def __init__(self): |
| 542 | self.header = ofp_header() |
| 543 | ofp_stats_reply.__init__(self) |
| 544 | self.header.type = OFPT_STATS_REPLY |
| 545 | self.type = --STATS_NAME-- |
| 546 | # stats: Array of type --TYPE--_stats_entry |
| 547 | self.stats = [] |
| 548 | |
| 549 | def pack(self, assertstruct=True): |
| 550 | packed = ofp_stats_reply.pack(self) |
| 551 | for obj in self.stats: |
| 552 | packed += obj.pack() |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 553 | return packed |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 554 | |
| 555 | def unpack(self, binary_string): |
| 556 | binary_string = ofp_stats_reply.unpack(self, binary_string) |
| 557 | dummy = --TYPE--_stats_entry() |
| 558 | while len(binary_string) >= len(dummy): |
| 559 | obj = --TYPE--_stats_entry() |
| 560 | binary_string = obj.unpack(binary_string) |
| 561 | self.stats.append(obj) |
| 562 | if len(binary_string) != 0: |
| 563 | print "ERROR unpacking --TYPE-- stats string: extra bytes" |
| 564 | return binary_string |
| 565 | |
| 566 | def __len__(self): |
| 567 | length = len(self.header) + OFP_STATS_REPLY_BYTES |
| 568 | for obj in self.stats: |
| 569 | length += len(obj) |
| 570 | return length |
| 571 | |
| 572 | def show(self, prefix=''): |
| 573 | print prefix + "--TYPE--_stats_reply" |
| 574 | ofp_stats_reply.show(self) |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 575 | print prefix + "Stats array of length " + str(len(self.stats)) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 576 | for obj in self.stats: |
| 577 | obj.show() |
| 578 | |
| 579 | def __eq__(self, other): |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 580 | if not ofp_stats_reply.__eq__(self, other): return False |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 581 | return self.stats == other.stats |
| 582 | |
| 583 | def __ne__(self, other): return not self.__eq__(other) |
| 584 | """ |
| 585 | |
| 586 | # |
| 587 | # To address variations in stats reply bodies, the following |
| 588 | # "_entry" classes are defined for each element in the reply |
| 589 | # |
| 590 | |
| 591 | extra_stats_entry_defs = """ |
| 592 | # Stats entries define the content of one element in a stats |
| 593 | # reply for the indicated type; define _entry for consistency |
| 594 | |
| 595 | aggregate_stats_entry = ofp_aggregate_stats_reply |
| 596 | desc_stats_entry = ofp_desc_stats |
| 597 | port_stats_entry = ofp_port_stats |
| 598 | queue_stats_entry = ofp_queue_stats |
| 599 | table_stats_entry = ofp_table_stats |
| 600 | """ |
| 601 | |
| 602 | # Special case flow_stats to handle actions_list |
| 603 | |
| 604 | flow_stats_entry_def = """ |
| 605 | # |
| 606 | # Flow stats entry contains an action list of variable length, so |
| 607 | # it is done by hand |
| 608 | # |
| 609 | |
| 610 | class flow_stats_entry(ofp_flow_stats): |
| 611 | \""" |
| 612 | Special case flow stats entry to handle action list object |
| 613 | \""" |
| 614 | def __init__(self): |
| 615 | ofp_flow_stats.__init__(self) |
| 616 | self.actions = action_list() |
| 617 | |
| 618 | def pack(self, assertstruct=True): |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 619 | self.length = len(self) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 620 | packed = ofp_flow_stats.pack(self, assertstruct) |
| 621 | packed += self.actions.pack() |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 622 | if len(packed) != self.length: |
| 623 | print("ERROR: flow_stats_entry pack length not equal", |
| 624 | self.length, len(packed)) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 625 | return packed |
| 626 | |
| 627 | def unpack(self, binary_string): |
| 628 | binary_string = ofp_flow_stats.unpack(self, binary_string) |
| 629 | ai_len = self.length - OFP_FLOW_STATS_BYTES |
Dan Talayco | 6d2470b | 2010-02-07 22:59:49 -0800 | [diff] [blame] | 630 | if ai_len < 0: |
| 631 | print("ERROR: flow_stats_entry unpack length too small", |
| 632 | self.length) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 633 | binary_string = self.actions.unpack(binary_string, bytes=ai_len) |
| 634 | return binary_string |
| 635 | |
| 636 | def __len__(self): |
| 637 | return OFP_FLOW_STATS_BYTES + len(self.actions) |
| 638 | |
| 639 | def show(self, prefix=''): |
| 640 | print prefix + "flow_stats_entry" |
| 641 | ofp_flow_stats.show(self, prefix + ' ') |
| 642 | self.actions.show(prefix + ' ') |
| 643 | |
| 644 | def __eq__(self, other): |
| 645 | return (ofp_flow_stats.__eq__(self, other) and |
| 646 | self.actions == other.actions) |
| 647 | |
| 648 | def __ne__(self, other): return not self.__eq__(other) |
| 649 | """ |
| 650 | |
| 651 | stats_types = [ |
| 652 | 'aggregate', |
| 653 | 'desc', |
| 654 | 'flow', |
| 655 | 'port', |
| 656 | 'queue', |
| 657 | 'table'] |
| 658 | |
| 659 | if __name__ == '__main__': |
| 660 | |
| 661 | print message_top_matter |
| 662 | |
| 663 | print """ |
| 664 | ################################################################ |
| 665 | # |
| 666 | # OpenFlow Message Definitions |
| 667 | # |
| 668 | ################################################################ |
| 669 | """ |
| 670 | |
| 671 | msg_types = message_class_map.keys() |
| 672 | msg_types.sort() |
| 673 | |
| 674 | for t in msg_types: |
| 675 | gen_message_wrapper(t) |
| 676 | print |
| 677 | |
| 678 | print """ |
| 679 | ################################################################ |
| 680 | # |
| 681 | # Stats request and reply subclass definitions |
| 682 | # |
| 683 | ################################################################ |
| 684 | """ |
| 685 | |
| 686 | print extra_ofp_stats_req_defs |
| 687 | print extra_stats_entry_defs |
| 688 | print flow_stats_entry_def |
| 689 | |
| 690 | # Generate stats request and reply subclasses |
| 691 | for t in stats_types: |
| 692 | stats_name = "OFPST_" + t.upper() |
| 693 | to_print = re.sub('--TYPE--', t, stats_request_template) |
| 694 | to_print = re.sub('--TYPE_UPPER--', t.upper(), to_print) |
| 695 | to_print = re.sub('--STATS_NAME--', stats_name, to_print) |
| 696 | print to_print |
| 697 | to_print = re.sub('--TYPE--', t, stats_reply_template) |
| 698 | to_print = re.sub('--STATS_NAME--', stats_name, to_print) |
| 699 | print to_print |
| 700 | |
| 701 | |
| 702 | # |
| 703 | # OFP match variants |
| 704 | # ICMP 0x801 (?) ==> icmp_type/code replace tp_src/dst |
| 705 | # |
| 706 | |
| 707 | |