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>
21 # Instructions. Run with:
23 # PYTHONPATH=$PYTHONPATH:~/repos/nepi/src python src/nepi/resources/ns3/resource_manager_generator.py
26 # Force the load of ns3 libraries
27 from nepi.resources.ns3.ns3wrapper import load_ns3_module
32 adapted_types = ["ns3::Node",
33 "ns3::Icmpv4L4Protocol",
35 "ns3::Ipv4L3Protocol",
36 "ns3::PropagationLossModel",
38 "ns3::PropagationDelayModel",
39 "ns3::WifiRemoteStationManager",
45 "ns3::ErrorRateModel",
47 #"ns3::DceApplication",
52 base_types = ["ns3::IpL4Protocol"]
54 def select_base_class(ns3, tid):
55 base_class_import = None
60 type_id = ns3.TypeId()
62 for type_name in adapted_types:
63 tid_base = type_id.LookupByName(type_name)
64 if type_name == rtype or tid.IsChildOf(tid_base):
65 base_class = "NS3Base" + type_name.replace("ns3::", "")
66 base_module = "ns3" + type_name.replace("ns3::", "").lower()
67 base_class_import = "from nepi.resources.ns3.%s import %s " % (
68 base_module, base_class)
69 return (base_class_import, base_class)
71 base_class_import = "from nepi.resources.ns3.ns3base import NS3Base"
72 base_class = "NS3Base"
74 for type_name in base_types:
75 tid_base = type_id.LookupByName(type_name)
76 if type_name == rtype or tid.IsChildOf(tid_base):
77 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)
93 (base_class_import, base_class) = select_base_class(ns3, tid)
97 if tid.MustHideFromDocumentation() or \
98 not tid.HasConstructor() or \
99 not tid.IsChildOf(base):
102 attributes = template_attributes(ns3, tid)
103 traces = template_traces(ns3, tid)
105 while ptid.HasParent():
106 ptid = ptid.GetParent()
107 attributes += template_attributes(ns3, ptid)
108 traces += template_traces(ns3, ptid)
110 attributes = "\n" + attributes if attributes else "pass"
111 traces = "\n" + traces if traces else "pass"
113 category = tid.GetGroupName()
115 rtype = tid.GetName()
116 classname = rtype.replace("ns3::", "NS3").replace("::","")
117 uncamm_rtype = re.sub('([a-z])([A-Z])', r'\1-\2', rtype).lower()
118 short_rtype = uncamm_rtype.replace("::","-")
120 d = os.path.dirname(os.path.realpath(__file__))
121 ftemp = open(os.path.join(d, "templates", "resource_manager_template.txt"), "r")
122 template = ftemp.read()
125 template = template. \
126 replace("<CLASS_NAME>", classname). \
127 replace("<RTYPE>", rtype). \
128 replace("<ATTRIBUTES>", attributes). \
129 replace("<TRACES>", traces). \
130 replace("<BASE_CLASS_IMPORT>", base_class_import). \
131 replace("<BASE_CLASS>", base_class). \
132 replace("<SHORT-RTYPE>", short_rtype)
134 fname = uncamm_rtype.replace('ns3::', ''). \
136 replace("-","_").lower() + ".py"
138 f = open(os.path.join(d, "classes", fname), "w")
139 print os.path.join(d, fname)
144 def template_attributes(ns3, tid):
145 d = os.path.dirname(os.path.realpath(__file__))
146 ftemp = open(os.path.join(d, "templates", "attribute_template.txt"), "r")
147 template = ftemp.read()
152 attr_count = tid.GetAttributeN()
153 for i in xrange(attr_count):
154 attr_info = tid.GetAttribute(i)
155 if not attr_info.accessor.HasGetter():
158 attr_flags = "Flags.Reserved"
159 flags = attr_info.flags
160 if (flags & ns3.TypeId.ATTR_CONSTRUCT) == ns3.TypeId.ATTR_CONSTRUCT:
161 attr_flags += " | Flags.Construct"
163 if (flags & ns3.TypeId.ATTR_GET) != ns3.TypeId.ATTR_GET:
164 attr_flags += " | Flags.NoRead"
165 elif (flags & ns3.TypeId.ATTR_SET) != ns3.TypeId.ATTR_SET:
166 attr_flags += " | Flags.NoWrite"
168 attr_name = attr_info.name
169 checker = attr_info.checker
170 attr_help = attr_info.help.replace('"', '\\"').replace("'", "\\'")
171 value = attr_info.initialValue
172 attr_value = value.SerializeToString(checker)
173 attr_allowed = "None"
175 attr_type = "Types.String"
177 if isinstance(value, ns3.ObjectVectorValue):
179 elif isinstance(value, ns3.PointerValue):
181 elif isinstance(value, ns3.WaypointValue):
183 elif isinstance(value, ns3.BooleanValue):
184 attr_type = "Types.Bool"
185 attr_value = "True" if attr_value == "true" else "False"
186 elif isinstance(value, ns3.EnumValue):
187 attr_type = "Types.Enumerate"
188 allowed = checker.GetUnderlyingTypeInformation().split("|")
189 attr_allowed = "[%s]" % ",".join(map(lambda x: "\"%s\"" % x, allowed))
190 elif isinstance(value, ns3.DoubleValue):
191 attr_type = "Types.Double"
193 elif isinstance(value, ns3.UintegerValue):
194 attr_type = "Types.Integer"
197 attr_id = "attr_" + attr_name.lower().replace("-", "_")
198 attributes += template.replace("<ATTR_ID>", attr_id) \
199 .replace("<ATTR_NAME>", attr_name) \
200 .replace("<ATTR_HELP>", attr_help) \
201 .replace("<ATTR_TYPE>", attr_type) \
202 .replace("<ATTR_DEFAULT>", attr_value) \
203 .replace("<ATTR_ALLOWED>", attr_allowed) \
204 .replace("<ATTR_RANGE>", attr_range) \
205 .replace("<ATTR_FLAGS>", attr_flags)
209 def template_traces(ns3, tid):
210 d = os.path.dirname(os.path.realpath(__file__))
211 ftemp = open(os.path.join(d, "templates", "trace_template.txt"), "r")
212 template = ftemp.read()
217 trace_count = tid.GetTraceSourceN()
218 for i in xrange(trace_count):
219 trace_info = tid.GetTraceSource(i)
220 trace_name = trace_info.name
221 trace_help = trace_info.help.replace('"', '\\"').replace("'", "\\'")
223 trace_id = trace_name.lower()
224 traces += template.replace("<TRACE_ID>", trace_id) \
225 .replace("<TRACE_NAME>", trace_name) \
226 .replace("<TRACE_HELP>", trace_help)
230 if __name__ == "__main__":