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