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