2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
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.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from nepi.resources.ns3.ns3wrapper import load_ns3_module
26 ns3 = load_ns3_module()
28 type_id = ns3.TypeId()
30 tid_count = type_id.GetRegisteredN()
31 base = type_id.LookupByName("ns3::Object")
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)
37 if tid.MustHideFromDocumentation() or \
38 not tid.HasConstructor() or \
39 not tid.IsChildOf(base):
42 attributes = template_attributes(ns3, tid)
43 traces = template_traces(ns3, tid)
45 while ptid.HasParent():
46 ptid = ptid.GetParent()
47 attributes += template_attributes(ns3, ptid)
48 traces += template_traces(ns3, ptid)
50 attributes = "\n" + attributes if attributes else "pass"
51 traces = "\n" + traces if traces else "pass"
54 category = tid.GetGroupName()
55 classname = rtype.replace("ns3::", "NS3").replace("::","")
56 uncamm_rtype = re.sub('([a-z])([A-Z])', r'\1-\2', rtype).lower()
57 short_rtype = uncamm_rtype.replace("::","-")
59 d = os.path.dirname(os.path.realpath(__file__))
60 ftemp = open(os.path.join(d, "templates", "rm_template.txt"), "r")
61 template = ftemp.read()
64 template = template. \
65 replace("<CLASS_NAME>", classname). \
66 replace("<RTYPE>", rtype). \
67 replace("<ATTRIBUTES>", attributes). \
68 replace("<TRACES>", traces). \
69 replace("<SHORT-RTYPE>", short_rtype)
71 fname = uncamm_rtype.replace('ns3::', ''). \
73 replace("-","_").lower() + ".py"
75 f = open(os.path.join(d, fname), "w")
76 print os.path.join(d, fname)
81 def template_attributes(ns3, tid):
82 d = os.path.dirname(os.path.realpath(__file__))
83 ftemp = open(os.path.join(d, "templates", "attribute_template.txt"), "r")
84 template = ftemp.read()
89 attr_count = tid.GetAttributeN()
90 for i in xrange(attr_count):
91 attr_info = tid.GetAttribute(i)
92 if not attr_info.accessor.HasGetter():
96 flags = attr_info.flags
97 if (flags & ns3.TypeId.ATTR_SET) != ns3.TypeId.ATTR_SET:
98 attr_flags = "Types.ExecReadOnly"
100 attr_name = attr_info.name
101 checker = attr_info.checker
102 attr_help = attr_info.help
103 value = attr_info.initialValue
104 attr_value = value.SerializeToString(checker)
105 attr_allowed = "None"
107 attr_type = "Types.STRING"
109 if isinstance(value, ns3.ObjectVectorValue):
111 elif isinstance(value, ns3.PointerValue):
113 elif isinstance(value, ns3.WaypointValue):
115 elif isinstance(value, ns3.BooleanValue):
116 attr_type = "Types.BOOL"
117 attr_value = "True" if attr_value == "true" else "False"
118 elif isinstance(value, ns3.EnumValue):
119 attr_type = "Types.ENUM"
120 attr_allowed = "[%s]"% checker.GetUnderlyingTypeInformation().replace("|", ",")
121 elif isinstance(value, ns3.DoubleValue):
122 attr_type = "Types.DOUBLE"
124 elif isinstance(value, ns3.UintegerValue):
125 attr_type = "Types.INTEGER"
128 attr_id = attr_name.lower()
129 attributes += template.replace("<ATTR_ID>", attr_id) \
130 .replace("<ATTR_NAME>", attr_name) \
131 .replace("<ATTR_HELP>", attr_help) \
132 .replace("<ATTR_TYPE>", attr_type) \
133 .replace("<ATTR_DEFAULT>", attr_value) \
134 .replace("<ATTR_ALLOWED>", attr_allowed) \
135 .replace("<ATTR_RANGE>", attr_range) \
136 .replace("<ATTR_FLAGS>", attr_flags)
140 def template_traces(ns3, tid):
141 d = os.path.dirname(os.path.realpath(__file__))
142 ftemp = open(os.path.join(d, "templates", "trace_template.txt"), "r")
143 template = ftemp.read()
148 trace_count = tid.GetTraceSourceN()
149 for i in xrange(trace_count):
150 trace_info = tid.GetTraceSource(i)
151 trace_name = trace_info.name
152 trace_help = trace_info.help
154 trace_id = trace_name.lower()
155 traces += template.replace("<TRACE_ID>", trace_id) \
156 .replace("<TRACE_NAME>", trace_name) \
157 .replace("<TRACE_HELP>", trace_help)
161 if __name__ == "__main__":