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 |
| 8 | sys.path.append("../../src/python/oftest/ofmsg") |
| 9 | from ofp import * |
| 10 | from ofp_aux 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 | |
| 15 | from ofp import * |
| 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 | \""" |
| 75 | def __init__(self): |
| 76 | --PARENT_TYPE--.__init__(self) |
| 77 | self.type = --ACTION_NAME-- |
| 78 | self.len = self.__len__() |
| 79 | def show(self, prefix=''): |
| 80 | print prefix + "action_--TYPE--" |
| 81 | --PARENT_TYPE--.show(self, prefix) |
| 82 | """ |
| 83 | |
| 84 | if __name__ == '__main__': |
| 85 | for (t, parent) in action_class_map.items(): |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 86 | if not parent in class_to_members_map.keys(): |
| 87 | doc_info = "Unknown parent action class: " + parent |
| 88 | else: |
| 89 | doc_info = "Data members inherited from " + parent + ":\n" |
| 90 | for var in class_to_members_map[parent]: |
| 91 | doc_info += " @arg " + var + "\n" |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 92 | action_name = "OFPAT_" + t.upper() |
| 93 | to_print = re.sub('--TYPE--', t, template) |
| 94 | to_print = re.sub('--PARENT_TYPE--', parent, to_print) |
| 95 | to_print = re.sub('--ACTION_NAME--', action_name, to_print) |
Dan Talayco | ac1cb81 | 2010-02-06 20:34:18 -0800 | [diff] [blame] | 96 | to_print = re.sub('--DOC_INFO--', doc_info, to_print) |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 97 | print to_print |
| 98 | |
| 99 | # Generate a list of action classes |
| 100 | print "action_class_list = (" |
| 101 | prev = None |
| 102 | for (t, parent) in action_class_map.items(): |
| 103 | if prev: |
| 104 | print " action_" + prev + "," |
| 105 | prev = t |
| 106 | print " action_" + prev + ")" |