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()
56 base_class_import = "from nepi.resources.ns3.ns3base import NS3Base"
59 classname = rtype.replace("ns3::", "NS3").replace("::","")
60 uncamm_rtype = re.sub('([a-z])([A-Z])', r'\1-\2', rtype).lower()
61 short_rtype = uncamm_rtype.replace("::","-")
63 d = os.path.dirname(os.path.realpath(__file__))
64 ftemp = open(os.path.join(d, "templates", "resource_manager_template.txt"), "r")
65 template = ftemp.read()
68 template = template. \
69 replace("<CLASS_NAME>", classname). \
70 replace("<RTYPE>", rtype). \
71 replace("<ATTRIBUTES>", attributes). \
72 replace("<TRACES>", traces). \
73 replace("<BASE_CLASS_IMPORT>", base_class_import). \
74 replace("<BASE_CLASS>", base_class). \
75 replace("<SHORT-RTYPE>", short_rtype)
77 fname = uncamm_rtype.replace('ns3::', ''). \
79 replace("-","_").lower() + ".py"
81 #f = open(os.path.join(d, "classes", fname), "w")
82 #print os.path.join(d, fname)
87 def template_attributes(ns3, tid):
88 d = os.path.dirname(os.path.realpath(__file__))
89 ftemp = open(os.path.join(d, "templates", "attribute_template.txt"), "r")
90 template = ftemp.read()
95 attr_count = tid.GetAttributeN()
96 for i in xrange(attr_count):
97 attr_info = tid.GetAttribute(i)
98 if not attr_info.accessor.HasGetter():
102 flags = attr_info.flags
103 if (flags & ns3.TypeId.ATTR_SET) != ns3.TypeId.ATTR_SET:
104 attr_flags = "Types.ExecReadOnly"
106 attr_name = attr_info.name
107 checker = attr_info.checker
108 attr_help = attr_info.help.replace('"', '\\"').replace("'", "\\'")
109 value = attr_info.initialValue
110 attr_value = value.SerializeToString(checker)
111 attr_allowed = "None"
113 attr_type = "Types.STRING"
115 if isinstance(value, ns3.ObjectVectorValue):
117 elif isinstance(value, ns3.PointerValue):
119 elif isinstance(value, ns3.WaypointValue):
121 elif isinstance(value, ns3.BooleanValue):
122 attr_type = "Types.BOOL"
123 attr_value = "True" if attr_value == "true" else "False"
124 elif isinstance(value, ns3.EnumValue):
125 attr_type = "Types.ENUM"
126 attr_allowed = "[%s]"% checker.GetUnderlyingTypeInformation().replace("|", ",")
127 elif isinstance(value, ns3.DoubleValue):
128 attr_type = "Types.DOUBLE"
130 elif isinstance(value, ns3.UintegerValue):
131 attr_type = "Types.INTEGER"
134 attr_id = attr_name.lower()
135 attributes += template.replace("<ATTR_ID>", attr_id) \
136 .replace("<ATTR_NAME>", attr_name) \
137 .replace("<ATTR_HELP>", attr_help) \
138 .replace("<ATTR_TYPE>", attr_type) \
139 .replace("<ATTR_DEFAULT>", attr_value) \
140 .replace("<ATTR_ALLOWED>", attr_allowed) \
141 .replace("<ATTR_RANGE>", attr_range) \
142 .replace("<ATTR_FLAGS>", attr_flags)
146 def template_traces(ns3, tid):
147 d = os.path.dirname(os.path.realpath(__file__))
148 ftemp = open(os.path.join(d, "templates", "trace_template.txt"), "r")
149 template = ftemp.read()
154 trace_count = tid.GetTraceSourceN()
155 for i in xrange(trace_count):
156 trace_info = tid.GetTraceSource(i)
157 trace_name = trace_info.name
158 trace_help = trace_info.help.replace('"', '\\"').replace("'", "\\'")
160 trace_id = trace_name.lower()
161 traces += template.replace("<TRACE_ID>", trace_id) \
162 .replace("<TRACE_NAME>", trace_name) \
163 .replace("<TRACE_HELP>", trace_help)
167 if __name__ == "__main__":