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 # Force the load of ns3 libraries
21 from nepi.resources.ns3.ns3wrapper import load_ns3_module
27 ns3 = load_ns3_module()
29 type_id = ns3.TypeId()
31 tid_count = type_id.GetRegisteredN()
32 base = type_id.LookupByName("ns3::Object")
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)
38 if tid.MustHideFromDocumentation() or \
39 not tid.HasConstructor() or \
40 not tid.IsChildOf(base):
43 attributes = template_attributes(ns3, tid)
44 traces = template_traces(ns3, tid)
46 while ptid.HasParent():
47 ptid = ptid.GetParent()
48 attributes += template_attributes(ns3, ptid)
49 traces += template_traces(ns3, ptid)
51 attributes = "\n" + attributes if attributes else "pass"
52 traces = "\n" + traces if traces else "pass"
55 category = tid.GetGroupName()
57 base_class_import = "from nepi.resources.ns3.ns3base import NS3Base"
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("::","-")
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()
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)
78 fname = uncamm_rtype.replace('ns3::', ''). \
80 replace("-","_").lower() + ".py"
82 #f = open(os.path.join(d, "classes", fname), "w")
83 #print os.path.join(d, fname)
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()
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():
103 flags = attr_info.flags
104 if (flags & ns3.TypeId.ATTR_SET) != ns3.TypeId.ATTR_SET:
105 attr_flags = "Types.ExecReadOnly"
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"
114 attr_type = "Types.STRING"
116 if isinstance(value, ns3.ObjectVectorValue):
118 elif isinstance(value, ns3.PointerValue):
120 elif isinstance(value, ns3.WaypointValue):
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"
131 elif isinstance(value, ns3.UintegerValue):
132 attr_type = "Types.INTEGER"
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)
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()
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("'", "\\'")
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)
168 if __name__ == "__main__":