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
26 def discard(ns3, tid):
29 words = ["variable", "object", "probe", "adaptor", "wrapper",
30 "container", "derived", "simple"]
32 if rtype.lower().find(word) > -1:
35 bases = ["ns3::Scheduler", "ns3::SimulatorImpl"]
36 type_id = ns3.TypeId()
38 tid_base = type_id.LookupByName(base)
39 if tid.IsChildOf(tid_base):
44 def select_base_class(ns3, tid):
45 base_class_import = "from nepi.resources.ns3.ns3base import NS3Base"
46 base_class = "NS3Base"
50 if rtype == "ns3::Node":
51 base_class_import = "from nepi.resources.ns3.ns3node import NS3BaseNode"
52 base_class = "NS3BaseNode"
53 elif rtype == "ns3::Ipv4L3Protocol":
54 base_class_import = "from nepi.resources.ns3.ns3ipv4l3protocol import NS3BaseIpv4L3Protocol"
55 base_class = "NS3BaseIpv4L3Protocol"
57 type_id = ns3.TypeId()
59 bases = ["ns3::Application",
63 "ns3::PropagationLossModel",
64 "ns3::PropagationDelayModel",
65 "ns3::WifiRemoteStationManager",
69 "ns3::ErrorRateModel"]
72 tid_base = type_id.LookupByName(base)
73 if tid.IsChildOf(tid_base):
74 base_class = "NS3Base" + base.replace("ns3::", "")
75 base_module = "ns3" + base.replace("ns3::", "").lower()
76 base_class_import = "from nepi.resources.ns3.%s import %s " % (
77 base_module, base_class)
79 return (base_class_import, base_class)
82 ns3 = load_ns3_module()
84 type_id = ns3.TypeId()
86 tid_count = type_id.GetRegisteredN()
87 base = type_id.LookupByName("ns3::Object")
89 # Create a .py file using the ns-3 RM template for each ns-3 TypeId
90 for i in xrange(tid_count):
91 tid = type_id.GetRegistered(i)
96 if tid.MustHideFromDocumentation() or \
97 not tid.HasConstructor() or \
98 not tid.IsChildOf(base):
101 attributes = template_attributes(ns3, tid)
102 traces = template_traces(ns3, tid)
104 while ptid.HasParent():
105 ptid = ptid.GetParent()
106 attributes += template_attributes(ns3, ptid)
107 traces += template_traces(ns3, ptid)
109 attributes = "\n" + attributes if attributes else "pass"
110 traces = "\n" + traces if traces else "pass"
112 (base_class_import, base_class) = select_base_class(ns3, tid)
114 category = tid.GetGroupName()
116 rtype = tid.GetName()
117 classname = rtype.replace("ns3::", "NS3").replace("::","")
118 uncamm_rtype = re.sub('([a-z])([A-Z])', r'\1-\2', rtype).lower()
119 short_rtype = uncamm_rtype.replace("::","-")
121 d = os.path.dirname(os.path.realpath(__file__))
122 ftemp = open(os.path.join(d, "templates", "resource_manager_template.txt"), "r")
123 template = ftemp.read()
126 template = template. \
127 replace("<CLASS_NAME>", classname). \
128 replace("<RTYPE>", rtype). \
129 replace("<ATTRIBUTES>", attributes). \
130 replace("<TRACES>", traces). \
131 replace("<BASE_CLASS_IMPORT>", base_class_import). \
132 replace("<BASE_CLASS>", base_class). \
133 replace("<SHORT-RTYPE>", short_rtype)
135 fname = uncamm_rtype.replace('ns3::', ''). \
137 replace("-","_").lower() + ".py"
139 f = open(os.path.join(d, "classes", fname), "w")
140 print os.path.join(d, fname)
145 def template_attributes(ns3, tid):
146 d = os.path.dirname(os.path.realpath(__file__))
147 ftemp = open(os.path.join(d, "templates", "attribute_template.txt"), "r")
148 template = ftemp.read()
153 attr_count = tid.GetAttributeN()
154 for i in xrange(attr_count):
155 attr_info = tid.GetAttribute(i)
156 if not attr_info.accessor.HasGetter():
160 flags = attr_info.flags
161 if (flags & ns3.TypeId.ATTR_SET) != ns3.TypeId.ATTR_SET:
162 attr_flags = "Flags.Design"
163 elif (flags & ns3.TypeId.ATTR_CONSTRUCT) == ns3.TypeId.ATTR_CONSTRUCT:
164 attr_flags = "Flags.Construct"
166 attr_name = attr_info.name
167 checker = attr_info.checker
168 attr_help = attr_info.help.replace('"', '\\"').replace("'", "\\'")
169 value = attr_info.initialValue
170 attr_value = value.SerializeToString(checker)
171 attr_allowed = "None"
173 attr_type = "Types.String"
175 if isinstance(value, ns3.ObjectVectorValue):
177 elif isinstance(value, ns3.PointerValue):
179 elif isinstance(value, ns3.WaypointValue):
181 elif isinstance(value, ns3.BooleanValue):
182 attr_type = "Types.Bool"
183 attr_value = "True" if attr_value == "true" else "False"
184 elif isinstance(value, ns3.EnumValue):
185 attr_type = "Types.Enumerate"
186 allowed = checker.GetUnderlyingTypeInformation().split("|")
187 attr_allowed = "[%s]" % ",".join(map(lambda x: "\"%s\"" % x, allowed))
188 elif isinstance(value, ns3.DoubleValue):
189 attr_type = "Types.Double"
191 elif isinstance(value, ns3.UintegerValue):
192 attr_type = "Types.Integer"
195 attr_id = "attr_" + attr_name.lower().replace("-", "_")
196 attributes += template.replace("<ATTR_ID>", attr_id) \
197 .replace("<ATTR_NAME>", attr_name) \
198 .replace("<ATTR_HELP>", attr_help) \
199 .replace("<ATTR_TYPE>", attr_type) \
200 .replace("<ATTR_DEFAULT>", attr_value) \
201 .replace("<ATTR_ALLOWED>", attr_allowed) \
202 .replace("<ATTR_RANGE>", attr_range) \
203 .replace("<ATTR_FLAGS>", attr_flags)
207 def template_traces(ns3, tid):
208 d = os.path.dirname(os.path.realpath(__file__))
209 ftemp = open(os.path.join(d, "templates", "trace_template.txt"), "r")
210 template = ftemp.read()
215 trace_count = tid.GetTraceSourceN()
216 for i in xrange(trace_count):
217 trace_info = tid.GetTraceSource(i)
218 trace_name = trace_info.name
219 trace_help = trace_info.help.replace('"', '\\"').replace("'", "\\'")
221 trace_id = trace_name.lower()
222 traces += template.replace("<TRACE_ID>", trace_id) \
223 .replace("<TRACE_NAME>", trace_name) \
224 .replace("<TRACE_HELP>", trace_help)
228 if __name__ == "__main__":