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