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