Working on automatic generation of ns-3 RMs
[nepi.git] / src / nepi / resources / ns3 / resource_manager_generator.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.resources.ns3.ns3wrapper import load_ns3_module
21
22 import os
23 import re
24
25 def create_ns3_rms():
26     ns3 = load_ns3_module()
27
28     type_id = ns3.TypeId()
29     
30     tid_count = type_id.GetRegisteredN()
31     base = type_id.LookupByName("ns3::Object")
32
33     # Create a .py file using the ns-3 RM template for each ns-3 TypeId
34     for i in xrange(tid_count):
35         tid = type_id.GetRegistered(i)
36         
37         if tid.MustHideFromDocumentation() or \
38                 not tid.HasConstructor() or \
39                 not tid.IsChildOf(base): 
40             continue
41        
42         attributes = template_attributes(ns3, tid)
43         traces = template_traces(ns3, tid)
44         ptid = tid
45         while ptid.HasParent():
46             ptid = ptid.GetParent()
47             attributes += template_attributes(ns3, ptid)
48             traces += template_traces(ns3, ptid)
49
50         attributes = "\n" + attributes if attributes else "pass"
51         traces = "\n" + traces if traces else "pass"
52
53         rtype = tid.GetName()
54         category = tid.GetGroupName()
55
56         base_class_import = "from nepi.resources.ns3.ns3base import NS3Base"
57         base_clas = "NS3Base"
58  
59         classname = rtype.replace("ns3::", "NS3").replace("::","")
60         uncamm_rtype = re.sub('([a-z])([A-Z])', r'\1-\2', rtype).lower()
61         short_rtype = uncamm_rtype.replace("::","-")
62
63         d = os.path.dirname(os.path.realpath(__file__))
64         ftemp = open(os.path.join(d, "templates", "resource_manager_template.txt"), "r")
65         template = ftemp.read()
66         ftemp.close()
67
68         template = template. \
69                 replace("<CLASS_NAME>", classname). \
70                 replace("<RTYPE>", rtype). \
71                 replace("<ATTRIBUTES>", attributes). \
72                 replace("<TRACES>", traces). \
73                 replace("<BASE_CLASS_IMPORT>", base_class_import). \
74                 replace("<BASE_CLASS>", base_class). \
75                 replace("<SHORT-RTYPE>", short_rtype)
76
77         fname = uncamm_rtype.replace('ns3::', ''). \
78                 replace('::', ''). \
79                 replace("-","_").lower() + ".py"
80
81         #f = open(os.path.join(d, "classes", fname), "w")
82         #print os.path.join(d, fname)
83         #print template
84         #f.write(template)
85         #f.close()
86
87 def template_attributes(ns3, tid): 
88     d = os.path.dirname(os.path.realpath(__file__))
89     ftemp = open(os.path.join(d, "templates", "attribute_template.txt"), "r")
90     template = ftemp.read()
91     ftemp.close()
92
93     attributes = ""
94
95     attr_count = tid.GetAttributeN()
96     for i in xrange(attr_count):
97         attr_info = tid.GetAttribute(i)
98         if not attr_info.accessor.HasGetter():
99             continue
100
101         attr_flags = "None"
102         flags = attr_info.flags
103         if (flags & ns3.TypeId.ATTR_SET) != ns3.TypeId.ATTR_SET:
104             attr_flags = "Types.ExecReadOnly"
105
106         attr_name = attr_info.name
107         checker = attr_info.checker
108         attr_help = attr_info.help.replace('"', '\\"').replace("'", "\\'")
109         value = attr_info.initialValue
110         attr_value = value.SerializeToString(checker)
111         attr_allowed = "None"
112         attr_range = "None"
113         attr_type = "Types.STRING"
114
115         if isinstance(value, ns3.ObjectVectorValue):
116             continue
117         elif isinstance(value, ns3.PointerValue):
118             continue
119         elif isinstance(value, ns3.WaypointValue):
120             continue
121         elif isinstance(value, ns3.BooleanValue):
122             attr_type = "Types.BOOL"
123             attr_value = "True" if attr_value == "true" else "False"
124         elif isinstance(value, ns3.EnumValue):
125             attr_type = "Types.ENUM"
126             attr_allowed = "[%s]"% checker.GetUnderlyingTypeInformation().replace("|", ",")
127         elif isinstance(value, ns3.DoubleValue):
128             attr_type = "Types.DOUBLE"
129             # TODO: range
130         elif isinstance(value, ns3.UintegerValue):
131             attr_type = "Types.INTEGER"
132             # TODO: range
133
134         attr_id = attr_name.lower()
135         attributes += template.replace("<ATTR_ID>", attr_id) \
136                 .replace("<ATTR_NAME>", attr_name) \
137                 .replace("<ATTR_HELP>", attr_help) \
138                 .replace("<ATTR_TYPE>", attr_type) \
139                 .replace("<ATTR_DEFAULT>", attr_value) \
140                 .replace("<ATTR_ALLOWED>", attr_allowed) \
141                 .replace("<ATTR_RANGE>", attr_range) \
142                 .replace("<ATTR_FLAGS>", attr_flags) 
143
144     return attributes
145
146 def template_traces(ns3, tid): 
147     d = os.path.dirname(os.path.realpath(__file__))
148     ftemp = open(os.path.join(d, "templates", "trace_template.txt"), "r")
149     template = ftemp.read()
150     ftemp.close()
151
152     traces = ""
153
154     trace_count = tid.GetTraceSourceN()
155     for i in xrange(trace_count):
156         trace_info = tid.GetTraceSource(i)
157         trace_name = trace_info.name
158         trace_help = trace_info.help.replace('"', '\\"').replace("'", "\\'")
159
160         trace_id = trace_name.lower()
161         traces += template.replace("<TRACE_ID>", trace_id) \
162                 .replace("<TRACE_NAME>", trace_name) \
163                 .replace("<TRACE_HELP>", trace_help) 
164
165     return traces
166
167 if __name__ == "__main__":
168     create_ns3_rms()