Adding base RMs for ns-3
[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 select_base_class(ns3, tid): 
27     rtype = tid.GetName()
28
29     type_id = ns3.TypeId()
30     appbase = type_id.LookupByName("ns3::Application")
31     devicebase = type_id.LookupByName("ns3::NetDevice")
32     channelbase = type_id.LookupByName("ns3::Channel")
33     queuebase = type_id.LookupByName("ns3::Queue")
34     lossbase = type_id.LookupByName("ns3::PropagationLossModel")
35     delaybase = type_id.LookupByName("ns3::PropagationDelayModel")
36     managerbase = type_id.LookupByName("ns3::WifiRemoteStationManager")
37
38     if tid.IsChiledOf(appbase):
39        base_class_import = "from nepi.resources.ns3.ns3application import NS3BaseApplication"
40        base_class = "NS3BaseApplication"
41     elif tid.IsChiledOf(devicebase):
42        base_class_import = "from nepi.resources.ns3.ns3device import NS3BaseNetDevice"
43        base_class = "NS3BaseNetDevice"
44     elif tid.IsChiledOf(channelbase):
45        base_class_import = "from nepi.resources.ns3.ns3channel import NS3BaseChannel"
46        base_class = "NS3BaseChannel"
47     elif tid.IsChiledOf(queuebase):
48        base_class_import = "from nepi.resources.ns3.ns3queue import NS3BaseQueue"
49        base_class = "NS3BaseQueue"
50     elif tid.IsChiledOf(lossbase):
51        base_class_import = "from nepi.resources.ns3.ns3loss import NS3BasePropagationLossModel"
52        base_class = "NS3BasePropagationLossDelay"
53     elif tid.IsChiledOf(delaybase):
54        base_class_import = "from nepi.resources.ns3.ns3delay import NS3BasePropagationDelayModel"
55        base_class = "NS3BasePropagationDelayModel"
56     elif tid.IsChiledOf(managerbase):
57        base_class_import = "from nepi.resources.ns3.ns3manager import NS3BaseWifiRemoteStationManager"
58        base_class = "NS3BaseWifiRemoteStationManager"
59     elif rtype == "ns3::Node":
60        base_class_import = "from nepi.resources.ns3.ns3node import NS3BaseNode"
61        base_class = "NS3BaseNode"
62     elif rtype == "ns3::Ipv4L3Protocol":
63        base_class_import = "from nepi.resources.ns3.ns3ipv4protocol import NS3BaseIpV4Protocol"
64        base_class = "NS3BaseIpV4Protocol"
65     else:
66        base_class_import = "from nepi.resources.ns3.ns3base import NS3Base"
67        base_class = "NS3Base"
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 tid.MustHideFromDocumentation() or \
84                 not tid.HasConstructor() or \
85                 not tid.IsChildOf(base): 
86             continue
87        
88         attributes = template_attributes(ns3, tid)
89         traces = template_traces(ns3, tid)
90         ptid = tid
91         while ptid.HasParent():
92             ptid = ptid.GetParent()
93             attributes += template_attributes(ns3, ptid)
94             traces += template_traces(ns3, ptid)
95
96         attributes = "\n" + attributes if attributes else "pass"
97         traces = "\n" + traces if traces else "pass"
98
99         (base_class_import, base_class) = select_base_class(ns3, tid)
100
101         rtype = tid.GetName()
102         category = tid.GetGroupName()
103
104         classname = rtype.replace("ns3::", "NS3").replace("::","")
105         uncamm_rtype = re.sub('([a-z])([A-Z])', r'\1-\2', rtype).lower()
106         short_rtype = uncamm_rtype.replace("::","-")
107
108         d = os.path.dirname(os.path.realpath(__file__))
109         ftemp = open(os.path.join(d, "templates", "resource_manager_template.txt"), "r")
110         template = ftemp.read()
111         ftemp.close()
112
113         template = template. \
114                 replace("<CLASS_NAME>", classname). \
115                 replace("<RTYPE>", rtype). \
116                 replace("<ATTRIBUTES>", attributes). \
117                 replace("<TRACES>", traces). \
118                 replace("<BASE_CLASS_IMPORT>", base_class_import). \
119                 replace("<BASE_CLASS>", base_class). \
120                 replace("<SHORT-RTYPE>", short_rtype)
121
122         fname = uncamm_rtype.replace('ns3::', ''). \
123                 replace('::', ''). \
124                 replace("-","_").lower() + ".py"
125
126         #f = open(os.path.join(d, "classes", fname), "w")
127         #print os.path.join(d, fname)
128         #print template
129         #f.write(template)
130         #f.close()
131
132 def template_attributes(ns3, tid): 
133     d = os.path.dirname(os.path.realpath(__file__))
134     ftemp = open(os.path.join(d, "templates", "attribute_template.txt"), "r")
135     template = ftemp.read()
136     ftemp.close()
137
138     attributes = ""
139
140     attr_count = tid.GetAttributeN()
141     for i in xrange(attr_count):
142         attr_info = tid.GetAttribute(i)
143         if not attr_info.accessor.HasGetter():
144             continue
145
146         attr_flags = "None"
147         flags = attr_info.flags
148         if (flags & ns3.TypeId.ATTR_SET) != ns3.TypeId.ATTR_SET:
149             attr_flags = "Types.ExecReadOnly"
150
151         attr_name = attr_info.name
152         checker = attr_info.checker
153         attr_help = attr_info.help.replace('"', '\\"').replace("'", "\\'")
154         value = attr_info.initialValue
155         attr_value = value.SerializeToString(checker)
156         attr_allowed = "None"
157         attr_range = "None"
158         attr_type = "Types.STRING"
159
160         if isinstance(value, ns3.ObjectVectorValue):
161             continue
162         elif isinstance(value, ns3.PointerValue):
163             continue
164         elif isinstance(value, ns3.WaypointValue):
165             continue
166         elif isinstance(value, ns3.BooleanValue):
167             attr_type = "Types.BOOL"
168             attr_value = "True" if attr_value == "true" else "False"
169         elif isinstance(value, ns3.EnumValue):
170             attr_type = "Types.ENUM"
171             attr_allowed = "[%s]"% checker.GetUnderlyingTypeInformation().replace("|", ",")
172         elif isinstance(value, ns3.DoubleValue):
173             attr_type = "Types.DOUBLE"
174             # TODO: range
175         elif isinstance(value, ns3.UintegerValue):
176             attr_type = "Types.INTEGER"
177             # TODO: range
178
179         attr_id = attr_name.lower()
180         attributes += template.replace("<ATTR_ID>", attr_id) \
181                 .replace("<ATTR_NAME>", attr_name) \
182                 .replace("<ATTR_HELP>", attr_help) \
183                 .replace("<ATTR_TYPE>", attr_type) \
184                 .replace("<ATTR_DEFAULT>", attr_value) \
185                 .replace("<ATTR_ALLOWED>", attr_allowed) \
186                 .replace("<ATTR_RANGE>", attr_range) \
187                 .replace("<ATTR_FLAGS>", attr_flags) 
188
189     return attributes
190
191 def template_traces(ns3, tid): 
192     d = os.path.dirname(os.path.realpath(__file__))
193     ftemp = open(os.path.join(d, "templates", "trace_template.txt"), "r")
194     template = ftemp.read()
195     ftemp.close()
196
197     traces = ""
198
199     trace_count = tid.GetTraceSourceN()
200     for i in xrange(trace_count):
201         trace_info = tid.GetTraceSource(i)
202         trace_name = trace_info.name
203         trace_help = trace_info.help.replace('"', '\\"').replace("'", "\\'")
204
205         trace_id = trace_name.lower()
206         traces += template.replace("<TRACE_ID>", trace_id) \
207                 .replace("<TRACE_NAME>", trace_name) \
208                 .replace("<TRACE_HELP>", trace_help) 
209
210     return traces
211
212 if __name__ == "__main__":
213     create_ns3_rms()