blob: 7ee2d357a8220f0c593b563bca89a3d7bed27f7a [file] [log] [blame]
Dan Talaycof75360a2010-02-05 22:22:54 -08001#!/usr/bin/python
2#
3# This python script generates error subclasses
4#
5
6import re
Dan Talaycoac1cb812010-02-06 20:34:18 -08007import sys
Dan Talaycod7e2dbe2010-02-13 21:51:15 -08008sys.path.append("../../src/python/oftest")
Dan Talaycob9cb5482010-02-09 15:23:12 -08009from cstruct import *
10from class_maps import class_to_members_map
Dan Talaycof75360a2010-02-05 22:22:54 -080011
12print """
13# Python OpenFlow error wrapper classes
14
Dan Talaycob9cb5482010-02-09 15:23:12 -080015from cstruct import *
Dan Talaycof75360a2010-02-05 22:22:54 -080016
Dan Talaycof75360a2010-02-05 22:22:54 -080017"""
18
19################################################################
20#
21# Error message subclasses
22#
23################################################################
24
25# Template for error subclasses
26
27template = """
28class --TYPE--_error_msg(ofp_error_msg):
29 \"""
30 Wrapper class for --TYPE-- error message class
Dan Talaycoac1cb812010-02-06 20:34:18 -080031
32 Data members inherited from ofp_error_msg:
33 @arg type
34 @arg code
35 @arg data: Binary string following message members
36
Dan Talaycof75360a2010-02-05 22:22:54 -080037 \"""
38 def __init__(self):
39 ofp_error_msg.__init__(self)
40 self.header = ofp_header()
41 self.header.type = OFPT_ERROR
42 self.type = --ERROR_NAME--
43 self.data = ""
44
45 def pack(self, assertstruct=True):
46 self.header.length = self.__len__()
Dan Talayco54a90e92010-02-10 22:39:38 -080047 packed = self.header.pack()
48 packed += ofp_error_msg.pack(self)
Dan Talaycof75360a2010-02-05 22:22:54 -080049 packed += self.data
Dan Talayco6d2470b2010-02-07 22:59:49 -080050 return packed
Dan Talaycof75360a2010-02-05 22:22:54 -080051
52 def unpack(self, binary_string):
Dan Talayco54a90e92010-02-10 22:39:38 -080053 binary_string = self.header.unpack(binary_string)
Dan Talaycof75360a2010-02-05 22:22:54 -080054 binary_string = ofp_error_msg.unpack(self, binary_string)
55 self.data = binary_string
Dan Talayco6d2470b2010-02-07 22:59:49 -080056 return ""
Dan Talaycof75360a2010-02-05 22:22:54 -080057
58 def __len__(self):
59 return OFP_HEADER_BYTES + OFP_ERROR_MSG_BYTES + len(self.data)
60
61 def show(self, prefix=''):
62 print prefix + "--TYPE--_error_msg"
Dan Talayco54a90e92010-02-10 22:39:38 -080063 self.header.show(prefix + ' ')
64 ofp_error_msg.show(self, prefix + ' ')
Dan Talayco6d2470b2010-02-07 22:59:49 -080065 print prefix + "data is of length " + str(len(self.data))
66 ##@todo Consider trying to parse the string
67
Dan Talayco54a90e92010-02-10 22:39:38 -080068 def __eq__(self, other):
69 if type(self) != type(other): return False
70 return (self.header == other.header and
71 ofp_error_msg.__eq__(self, other) and
72 self.data == other.data)
73
74 def __ne__(self, other): return not self.__eq__(other)
Dan Talaycof75360a2010-02-05 22:22:54 -080075"""
76
77error_types = [
78 'hello_failed',
79 'bad_request',
80 'bad_action',
81 'flow_mod_failed',
82 'port_mod_failed',
83 'queue_op_failed']
84
85for t in error_types:
86 error_name = "OFPET_" + t.upper()
87 to_print = re.sub('--TYPE--', t, template)
88 to_print = re.sub('--ERROR_NAME--', error_name, to_print)
89 print to_print