Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # This python script generates action subclasses |
| 4 | # |
| 5 | |
| 6 | import re |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 7 | import sys |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 8 | sys.path.append("../../src/python/oftest") |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 9 | from cstruct import * |
| 10 | from class_maps import class_to_members_map |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 11 | |
| 12 | print """ |
| 13 | # Python OpenFlow action wrapper classes |
| 14 | |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 15 | from cstruct import * |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 16 | |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 17 | """ |
| 18 | |
| 19 | ################################################################ |
| 20 | # |
| 21 | # Action subclasses |
| 22 | # |
| 23 | ################################################################ |
| 24 | |
| 25 | action_structs = [ |
| 26 | 'output', |
| 27 | 'vlan_vid', |
| 28 | 'vlan_pcp', |
| 29 | 'dl_addr', |
| 30 | 'nw_addr', |
| 31 | 'tp_port', |
| 32 | 'nw_tos', |
| 33 | 'vendor_header'] |
| 34 | |
| 35 | action_types = [ |
| 36 | 'output', |
| 37 | 'set_vlan_vid', |
| 38 | 'set_vlan_pcp', |
| 39 | 'strip_vlan', |
| 40 | 'set_dl_src', |
| 41 | 'set_dl_dst', |
| 42 | 'set_nw_src', |
| 43 | 'set_nw_dst', |
| 44 | 'set_nw_tos', |
| 45 | 'set_tp_src', |
| 46 | 'set_tp_dst', |
| 47 | 'enqueue', |
| 48 | 'vendor' |
| 49 | ] |
| 50 | action_types.sort() |
| 51 | |
| 52 | action_class_map = { |
| 53 | 'output' : 'ofp_action_output', |
| 54 | 'set_vlan_vid' : 'ofp_action_vlan_vid', |
| 55 | 'set_vlan_pcp' : 'ofp_action_vlan_pcp', |
| 56 | 'strip_vlan' : 'ofp_action_header', |
| 57 | 'set_dl_src' : 'ofp_action_dl_addr', |
| 58 | 'set_dl_dst' : 'ofp_action_dl_addr', |
| 59 | 'set_nw_src' : 'ofp_action_nw_addr', |
| 60 | 'set_nw_dst' : 'ofp_action_nw_addr', |
| 61 | 'set_nw_tos' : 'ofp_action_nw_tos', |
| 62 | 'set_tp_src' : 'ofp_action_tp_port', |
| 63 | 'set_tp_dst' : 'ofp_action_tp_port', |
| 64 | 'enqueue' : 'ofp_action_enqueue', |
| 65 | 'vendor' : 'ofp_action_vendor_header' |
| 66 | } |
| 67 | |
| 68 | template = """ |
| 69 | class action_--TYPE--(--PARENT_TYPE--): |
| 70 | \""" |
| 71 | Wrapper class for --TYPE-- action object |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 72 | |
| 73 | --DOC_INFO-- |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 74 | \""" |
Rich Lane | c52a1c4 | 2013-01-04 10:29:00 -0800 | [diff] [blame] | 75 | def __init__(self, **kwargs): |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 76 | --PARENT_TYPE--.__init__(self) |
| 77 | self.type = --ACTION_NAME-- |
| 78 | self.len = self.__len__() |
Rich Lane | c52a1c4 | 2013-01-04 10:29:00 -0800 | [diff] [blame] | 79 | for (k, v) in kwargs.items(): |
| 80 | if hasattr(self, k): |
| 81 | setattr(self, k, v) |
| 82 | else: |
| 83 | raise NameError("field %s does not exist in %s" % (k, self.__class__)) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 84 | def show(self, prefix=''): |
Dan Talayco | f4426d5 | 2010-03-12 13:06:31 -0800 | [diff] [blame] | 85 | outstr = prefix + "action_--TYPE--\\n" |
Dan Talayco | 46755fa | 2010-03-09 21:44:29 -0800 | [diff] [blame] | 86 | outstr += --PARENT_TYPE--.show(self, prefix) |
| 87 | return outstr |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 88 | """ |
| 89 | |
| 90 | if __name__ == '__main__': |
| 91 | for (t, parent) in action_class_map.items(): |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 92 | if not parent in class_to_members_map.keys(): |
| 93 | doc_info = "Unknown parent action class: " + parent |
| 94 | else: |
| 95 | doc_info = "Data members inherited from " + parent + ":\n" |
| 96 | for var in class_to_members_map[parent]: |
| 97 | doc_info += " @arg " + var + "\n" |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 98 | action_name = "OFPAT_" + t.upper() |
| 99 | to_print = re.sub('--TYPE--', t, template) |
| 100 | to_print = re.sub('--PARENT_TYPE--', parent, to_print) |
| 101 | to_print = re.sub('--ACTION_NAME--', action_name, to_print) |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 102 | to_print = re.sub('--DOC_INFO--', doc_info, to_print) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 103 | print to_print |
| 104 | |
| 105 | # Generate a list of action classes |
| 106 | print "action_class_list = (" |
| 107 | prev = None |
| 108 | for (t, parent) in action_class_map.items(): |
| 109 | if prev: |
| 110 | print " action_" + prev + "," |
| 111 | prev = t |
| 112 | print " action_" + prev + ")" |