blob: bde0291fa37ad76b7390d70ec4011099f0297395 [file] [log] [blame]
Dan Talaycof75360a2010-02-05 22:22:54 -08001#!/usr/bin/python
2#
3# This python script generates action 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 action 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# Action subclasses
22#
23################################################################
24
25action_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
35action_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]
50action_types.sort()
51
52action_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
68template = """
Rich Lane95271012013-01-04 15:24:51 -080069class --TYPE--(--PARENT_TYPE--):
Dan Talaycof75360a2010-02-05 22:22:54 -080070 \"""
71 Wrapper class for --TYPE-- action object
Dan Talaycoac1cb812010-02-06 20:34:18 -080072
73 --DOC_INFO--
Dan Talaycof75360a2010-02-05 22:22:54 -080074 \"""
Rich Lanec52a1c42013-01-04 10:29:00 -080075 def __init__(self, **kwargs):
Dan Talaycof75360a2010-02-05 22:22:54 -080076 --PARENT_TYPE--.__init__(self)
77 self.type = --ACTION_NAME--
78 self.len = self.__len__()
Rich Lanec52a1c42013-01-04 10:29:00 -080079 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 Talaycof75360a2010-02-05 22:22:54 -080084 def show(self, prefix=''):
Dan Talaycof4426d52010-03-12 13:06:31 -080085 outstr = prefix + "action_--TYPE--\\n"
Dan Talayco46755fa2010-03-09 21:44:29 -080086 outstr += --PARENT_TYPE--.show(self, prefix)
87 return outstr
Rich Lane95271012013-01-04 15:24:51 -080088
89action_--TYPE-- = --TYPE-- # for backwards compatibility
Dan Talaycof75360a2010-02-05 22:22:54 -080090"""
91
92if __name__ == '__main__':
93 for (t, parent) in action_class_map.items():
Dan Talaycoac1cb812010-02-06 20:34:18 -080094 if not parent in class_to_members_map.keys():
95 doc_info = "Unknown parent action class: " + parent
96 else:
97 doc_info = "Data members inherited from " + parent + ":\n"
98 for var in class_to_members_map[parent]:
99 doc_info += " @arg " + var + "\n"
Dan Talaycof75360a2010-02-05 22:22:54 -0800100 action_name = "OFPAT_" + t.upper()
101 to_print = re.sub('--TYPE--', t, template)
102 to_print = re.sub('--PARENT_TYPE--', parent, to_print)
103 to_print = re.sub('--ACTION_NAME--', action_name, to_print)
Dan Talaycoac1cb812010-02-06 20:34:18 -0800104 to_print = re.sub('--DOC_INFO--', doc_info, to_print)
Dan Talaycof75360a2010-02-05 22:22:54 -0800105 print to_print
106
107 # Generate a list of action classes
108 print "action_class_list = ("
109 prev = None
110 for (t, parent) in action_class_map.items():
111 if prev:
112 print " action_" + prev + ","
113 prev = t
114 print " action_" + prev + ")"