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