blob: 83850f52c8e7b272e6f1477a38f09405f3ecaafa [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=''):
Dan Talayco46755fa2010-03-09 21:44:29 -080062 outstr = prefix + "--TYPE--_error_msg\\m"
63 outstr += self.header.show(prefix + ' ')
64 outstr += ofp_error_msg.show(self, prefix + ' ')
65 outstr += prefix + "data is of length " + str(len(self.data)) + '\\n'
Dan Talayco6d2470b2010-02-07 22:59:49 -080066 ##@todo Consider trying to parse the string
Dan Talayco46755fa2010-03-09 21:44:29 -080067 return outstr
Dan Talayco6d2470b2010-02-07 22:59:49 -080068
Dan Talayco54a90e92010-02-10 22:39:38 -080069 def __eq__(self, other):
70 if type(self) != type(other): return False
71 return (self.header == other.header and
72 ofp_error_msg.__eq__(self, other) and
73 self.data == other.data)
74
75 def __ne__(self, other): return not self.__eq__(other)
Dan Talaycof75360a2010-02-05 22:22:54 -080076"""
77
78error_types = [
79 'hello_failed',
80 'bad_request',
81 'bad_action',
82 'flow_mod_failed',
83 'port_mod_failed',
84 'queue_op_failed']
85
86for t in error_types:
87 error_name = "OFPET_" + t.upper()
88 to_print = re.sub('--TYPE--', t, template)
89 to_print = re.sub('--ERROR_NAME--', error_name, to_print)
90 print to_print