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