016cfa7fe290ef79609501c9a6da025ab02ac563
[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
21 # Instructions. Run with:
22 #
23 #  PYTHONPATH=$PYTHONPATH:~/repos/nepi/src python src/nepi/resources/ns3/resource_manager_generator.py
24 #
25
26 # Force the load of ns3 libraries
27 from nepi.resources.ns3.ns3wrapper import load_ns3_module
28
29 import os
30 import re
31
32 adapted_types = ["ns3::Node",
33         "ns3::Icmpv4L4Protocol",
34         "ns3::ArpL3Protocol",
35         "ns3::Ipv4L3Protocol",
36         "ns3::PropagationLossModel",
37         "ns3::MobilityModel",
38         "ns3::PropagationDelayModel",
39         "ns3::WifiRemoteStationManager",
40         "ns3::WifiNetDevice",
41         "ns3::WifiChannel",
42         "ns3::WifiPhy",
43         "ns3::WifiMac",
44         "ns3::ErrorModel",
45         "ns3::ErrorRateModel",
46         "ns3::Application", 
47         #"ns3::DceApplication", 
48         "ns3::NetDevice",
49         "ns3::Channel",
50         "ns3::Queue"]
51
52 base_types = ["ns3::IpL4Protocol"]
53
54 def select_base_class(ns3, tid): 
55     base_class_import = None
56     base_class = None
57    
58     rtype = tid.GetName()
59
60     type_id = ns3.TypeId()
61
62     for type_name in adapted_types:
63         tid_base = type_id.LookupByName(type_name)
64         if type_name == rtype or tid.IsChildOf(tid_base):
65             base_class = "NS3Base" + type_name.replace("ns3::", "")
66             base_module = "ns3" + type_name.replace("ns3::", "").lower()
67             base_class_import = "from nepi.resources.ns3.%s import %s " % (
68                     base_module, base_class)
69             return (base_class_import, base_class)
70
71     base_class_import = "from nepi.resources.ns3.ns3base import NS3Base"
72     base_class = "NS3Base"
73
74     for type_name in base_types:
75         tid_base = type_id.LookupByName(type_name)
76         if type_name == rtype or tid.IsChildOf(tid_base):
77             return (base_class_import, base_class)
78
79     return (None, None)
80
81 def create_ns3_rms():
82     ns3 = load_ns3_module()
83
84     type_id = ns3.TypeId()
85     
86     tid_count = type_id.GetRegisteredN()
87     base = type_id.LookupByName("ns3::Object")
88
89     # Create a .py file using the ns-3 RM template for each ns-3 TypeId
90     for i in xrange(tid_count):
91         tid = type_id.GetRegistered(i)
92         
93         (base_class_import, base_class) = select_base_class(ns3, tid)
94         if not base_class:
95             continue
96         
97         if tid.MustHideFromDocumentation() or \
98                 not tid.HasConstructor() or \
99                 not tid.IsChildOf(base): 
100             continue
101        
102         attributes = template_attributes(ns3, tid)
103         traces = template_traces(ns3, tid)
104         ptid = tid
105         while ptid.HasParent():
106             ptid = ptid.GetParent()
107             attributes += template_attributes(ns3, ptid)
108             traces += template_traces(ns3, ptid)
109
110         attributes = "\n" + attributes if attributes else "pass"
111         traces = "\n" + traces if traces else "pass"
112
113         category = tid.GetGroupName()
114
115         rtype = tid.GetName()
116         classname = rtype.replace("ns3::", "NS3").replace("::","")
117         uncamm_rtype = re.sub('([a-z])([A-Z])', r'\1-\2', rtype).lower()
118         short_rtype = uncamm_rtype.replace("::","-")
119
120         d = os.path.dirname(os.path.realpath(__file__))
121         ftemp = open(os.path.join(d, "templates", "resource_manager_template.txt"), "r")
122         template = ftemp.read()
123         ftemp.close()
124
125         template = template. \
126                 replace("<CLASS_NAME>", classname). \
127                 replace("<RTYPE>", rtype). \
128                 replace("<ATTRIBUTES>", attributes). \
129                 replace("<TRACES>", traces). \
130                 replace("<BASE_CLASS_IMPORT>", base_class_import). \
131                 replace("<BASE_CLASS>", base_class). \
132                 replace("<SHORT-RTYPE>", short_rtype)
133
134         fname = uncamm_rtype.replace('ns3::', ''). \
135                 replace('::', ''). \
136                 replace("-","_").lower() + ".py"
137
138         f = open(os.path.join(d, "classes", fname), "w")
139         print os.path.join(d, fname)
140         print template
141         f.write(template)
142         f.close()
143
144 def template_attributes(ns3, tid): 
145     d = os.path.dirname(os.path.realpath(__file__))
146     ftemp = open(os.path.join(d, "templates", "attribute_template.txt"), "r")
147     template = ftemp.read()
148     ftemp.close()
149
150     attributes = ""
151
152     attr_count = tid.GetAttributeN()
153     for i in xrange(attr_count):
154         attr_info = tid.GetAttribute(i)
155         if not attr_info.accessor.HasGetter():
156             continue
157
158         attr_flags = "Flags.Reserved"
159         flags = attr_info.flags
160         if (flags & ns3.TypeId.ATTR_CONSTRUCT) == ns3.TypeId.ATTR_CONSTRUCT:
161             attr_flags += " | Flags.Construct"
162         else:
163             if (flags & ns3.TypeId.ATTR_GET) != ns3.TypeId.ATTR_GET:
164                 attr_flags += " | Flags.NoRead"
165             elif (flags & ns3.TypeId.ATTR_SET) != ns3.TypeId.ATTR_SET:
166                 attr_flags += " | Flags.NoWrite"
167
168         attr_name = attr_info.name
169         checker = attr_info.checker
170         attr_help = attr_info.help.replace('"', '\\"').replace("'", "\\'")
171         value = attr_info.initialValue
172         attr_value = value.SerializeToString(checker)
173         attr_allowed = "None"
174         attr_range = "None"
175         attr_type = "Types.String"
176
177         if isinstance(value, ns3.ObjectVectorValue):
178             continue
179         elif isinstance(value, ns3.PointerValue):
180             continue
181         elif isinstance(value, ns3.WaypointValue):
182             continue
183         elif isinstance(value, ns3.BooleanValue):
184             attr_type = "Types.Bool"
185             attr_value = "True" if attr_value == "true" else "False"
186         elif isinstance(value, ns3.EnumValue):
187             attr_type = "Types.Enumerate"
188             allowed = checker.GetUnderlyingTypeInformation().split("|")
189             attr_allowed = "[%s]" % ",".join(map(lambda x: "\"%s\"" % x, allowed))
190         elif isinstance(value, ns3.DoubleValue):
191             attr_type = "Types.Double"
192             # TODO: range
193         elif isinstance(value, ns3.UintegerValue):
194             attr_type = "Types.Integer"
195             # TODO: range
196
197         attr_id = "attr_" + attr_name.lower().replace("-", "_")
198         attributes += template.replace("<ATTR_ID>", attr_id) \
199                 .replace("<ATTR_NAME>", attr_name) \
200                 .replace("<ATTR_HELP>", attr_help) \
201                 .replace("<ATTR_TYPE>", attr_type) \
202                 .replace("<ATTR_DEFAULT>", attr_value) \
203                 .replace("<ATTR_ALLOWED>", attr_allowed) \
204                 .replace("<ATTR_RANGE>", attr_range) \
205                 .replace("<ATTR_FLAGS>", attr_flags) 
206
207     return attributes
208
209 def template_traces(ns3, tid): 
210     d = os.path.dirname(os.path.realpath(__file__))
211     ftemp = open(os.path.join(d, "templates", "trace_template.txt"), "r")
212     template = ftemp.read()
213     ftemp.close()
214
215     traces = ""
216
217     trace_count = tid.GetTraceSourceN()
218     for i in xrange(trace_count):
219         trace_info = tid.GetTraceSource(i)
220         trace_name = trace_info.name
221         trace_help = trace_info.help.replace('"', '\\"').replace("'", "\\'")
222
223         trace_id = trace_name.lower()
224         traces += template.replace("<TRACE_ID>", trace_id) \
225                 .replace("<TRACE_NAME>", trace_name) \
226                 .replace("<TRACE_HELP>", trace_help) 
227
228     return traces
229
230 if __name__ == "__main__":
231     create_ns3_rms()