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 select_base_class(ns3, tid):
27 base_class_import = base_class = None
31 if rtype == "ns3::Node":
32 base_class_import = "from nepi.resources.ns3.ns3node import NS3BaseNode"
33 base_class = "NS3BaseNode"
34 elif rtype == "ns3::Ipv4L3Protocol":
35 base_class_import = "from nepi.resources.ns3.ns3ipv4protocol import NS3BaseIpv4L3Protocol"
36 base_class = "NS3BaseIpv4L3Protocol"
38 base_class_import = "from nepi.resources.ns3.ns3base import NS3Base"
39 base_class = "NS3Base"
43 type_id = ns3.TypeId()
45 bases = ["ns3::Application",
49 "ns3::PropagationLossModel",
50 "ns3::PropagationDelayModel",
51 "ns3::WifiRemoteStationManager",
55 "ns3::ErrorRateModel"]
58 tid_base = type_id.LookupByName(base)
59 if tid.IsChildOf(tid_base):
60 base_class = "NS3Base" + base.replace("ns3::", "")
61 base_module = "ns3" + base.replace("ns3::", "").lower()
62 base_class_import = "from nepi.resources.ns3.ns3application import %s " % (
63 base_module, base_class)
65 return (base_class_import, base_class)
68 ns3 = load_ns3_module()
70 type_id = ns3.TypeId()
72 tid_count = type_id.GetRegisteredN()
73 base = type_id.LookupByName("ns3::Object")
75 # Create a .py file using the ns-3 RM template for each ns-3 TypeId
76 for i in xrange(tid_count):
77 tid = type_id.GetRegistered(i)
79 if tid.MustHideFromDocumentation() or \
80 not tid.HasConstructor() or \
81 not tid.IsChildOf(base):
84 attributes = template_attributes(ns3, tid)
85 traces = template_traces(ns3, tid)
87 while ptid.HasParent():
88 ptid = ptid.GetParent()
89 attributes += template_attributes(ns3, ptid)
90 traces += template_traces(ns3, ptid)
92 attributes = "\n" + attributes if attributes else "pass"
93 traces = "\n" + traces if traces else "pass"
95 (base_class_import, base_class) = select_base_class(ns3, tid)
98 category = tid.GetGroupName()
100 classname = rtype.replace("ns3::", "NS3").replace("::","")
101 uncamm_rtype = re.sub('([a-z])([A-Z])', r'\1-\2', rtype).lower()
102 short_rtype = uncamm_rtype.replace("::","-")
104 d = os.path.dirname(os.path.realpath(__file__))
105 ftemp = open(os.path.join(d, "templates", "resource_manager_template.txt"), "r")
106 template = ftemp.read()
109 template = template. \
110 replace("<CLASS_NAME>", classname). \
111 replace("<RTYPE>", rtype). \
112 replace("<ATTRIBUTES>", attributes). \
113 replace("<TRACES>", traces). \
114 replace("<BASE_CLASS_IMPORT>", base_class_import). \
115 replace("<BASE_CLASS>", base_class). \
116 replace("<SHORT-RTYPE>", short_rtype)
118 fname = uncamm_rtype.replace('ns3::', ''). \
120 replace("-","_").lower() + ".py"
122 f = open(os.path.join(d, "classes", fname), "w")
123 print os.path.join(d, fname)
128 def template_attributes(ns3, tid):
129 d = os.path.dirname(os.path.realpath(__file__))
130 ftemp = open(os.path.join(d, "templates", "attribute_template.txt"), "r")
131 template = ftemp.read()
136 attr_count = tid.GetAttributeN()
137 for i in xrange(attr_count):
138 attr_info = tid.GetAttribute(i)
139 if not attr_info.accessor.HasGetter():
143 flags = attr_info.flags
144 if (flags & ns3.TypeId.ATTR_SET) != ns3.TypeId.ATTR_SET:
145 attr_flags = "Types.ExecReadOnly"
147 attr_name = attr_info.name
148 checker = attr_info.checker
149 attr_help = attr_info.help.replace('"', '\\"').replace("'", "\\'")
150 value = attr_info.initialValue
151 attr_value = value.SerializeToString(checker)
152 attr_allowed = "None"
154 attr_type = "Types.STRING"
156 if isinstance(value, ns3.ObjectVectorValue):
158 elif isinstance(value, ns3.PointerValue):
160 elif isinstance(value, ns3.WaypointValue):
162 elif isinstance(value, ns3.BooleanValue):
163 attr_type = "Types.BOOL"
164 attr_value = "True" if attr_value == "true" else "False"
165 elif isinstance(value, ns3.EnumValue):
166 attr_type = "Types.ENUM"
167 attr_allowed = "[%s]"% checker.GetUnderlyingTypeInformation().replace("|", ",")
168 elif isinstance(value, ns3.DoubleValue):
169 attr_type = "Types.DOUBLE"
171 elif isinstance(value, ns3.UintegerValue):
172 attr_type = "Types.INTEGER"
175 attr_id = attr_name.lower()
176 attributes += template.replace("<ATTR_ID>", attr_id) \
177 .replace("<ATTR_NAME>", attr_name) \
178 .replace("<ATTR_HELP>", attr_help) \
179 .replace("<ATTR_TYPE>", attr_type) \
180 .replace("<ATTR_DEFAULT>", attr_value) \
181 .replace("<ATTR_ALLOWED>", attr_allowed) \
182 .replace("<ATTR_RANGE>", attr_range) \
183 .replace("<ATTR_FLAGS>", attr_flags)
187 def template_traces(ns3, tid):
188 d = os.path.dirname(os.path.realpath(__file__))
189 ftemp = open(os.path.join(d, "templates", "trace_template.txt"), "r")
190 template = ftemp.read()
195 trace_count = tid.GetTraceSourceN()
196 for i in xrange(trace_count):
197 trace_info = tid.GetTraceSource(i)
198 trace_name = trace_info.name
199 trace_help = trace_info.help.replace('"', '\\"').replace("'", "\\'")
201 trace_id = trace_name.lower()
202 traces += template.replace("<TRACE_ID>", trace_id) \
203 .replace("<TRACE_NAME>", trace_name) \
204 .replace("<TRACE_HELP>", trace_help)
208 if __name__ == "__main__":