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