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