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 version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 # Instructions to automatically generate ns-3 ResourceManagers
22 # Configure the ns-3 enviorment (e.g.):
24 # export PYTHONPATH=~/.nepi/nepi-usr/bin/ns-3/ns-3.20/optimized/build/lib/python/site-packages
25 # export LD_LIBRARY_PATH=~/.nepi/nepi-usr/bin/ns-3/ns-3.20/optimized/build/lib
27 # Run the RM generator:
29 # PYTHONPATH=$PYTHONPATH:~/repos/nepi/src python src/nepi/resources/ns3/resource_manager_generator.py
32 from __future__ import print_function
34 # Force the load of ns3 libraries
35 from nepi.resources.ns3.ns3wrapper import load_ns3_module
40 adapted_types = ["ns3::Node",
41 "ns3::Icmpv4L4Protocol",
43 "ns3::Ipv4L3Protocol",
44 "ns3::PropagationLossModel",
46 "ns3::PropagationDelayModel",
47 "ns3::WifiRemoteStationManager",
53 "ns3::ErrorRateModel",
56 #"ns3::DceApplication",
61 base_types = ["ns3::IpL4Protocol"]
63 def select_base_class(ns3, tid):
64 base_class_import = None
69 type_id = ns3.TypeId()
71 for type_name in adapted_types:
72 tid_base = type_id.LookupByName(type_name)
73 if type_name == rtype or tid.IsChildOf(tid_base):
74 base_class = "NS3Base" + type_name.replace("ns3::", "")
75 base_module = "ns3" + type_name.replace("ns3::", "").lower()
76 base_class_import = "from nepi.resources.ns3.%s import %s " % (
77 base_module, base_class)
78 return (base_class_import, base_class)
80 base_class_import = "from nepi.resources.ns3.ns3base import NS3Base"
81 base_class = "NS3Base"
83 for type_name in base_types:
84 tid_base = type_id.LookupByName(type_name)
85 if type_name == rtype or tid.IsChildOf(tid_base):
86 return (base_class_import, base_class)
91 ns3 = load_ns3_module()
93 type_id = ns3.TypeId()
95 tid_count = type_id.GetRegisteredN()
96 base = type_id.LookupByName("ns3::Object")
98 # Create a .py file using the ns-3 RM template for each ns-3 TypeId
99 for i in xrange(tid_count):
100 tid = type_id.GetRegistered(i)
102 (base_class_import, base_class) = select_base_class(ns3, tid)
106 if tid.MustHideFromDocumentation() or \
107 not tid.HasConstructor() or \
108 not tid.IsChildOf(base):
111 attributes = template_attributes(ns3, tid)
112 traces = template_traces(ns3, tid)
114 while ptid.HasParent():
115 ptid = ptid.GetParent()
116 attributes += template_attributes(ns3, ptid)
117 traces += template_traces(ns3, ptid)
119 attributes = "\n" + attributes if attributes else "pass"
120 traces = "\n" + traces if traces else "pass"
122 category = tid.GetGroupName()
124 rtype = tid.GetName()
125 classname = rtype.replace("ns3::", "NS3").replace("::","")
126 uncamm_rtype = re.sub('([a-z])([A-Z])', r'\1-\2', rtype).lower()
127 short_rtype = uncamm_rtype.replace("::","-")
129 d = os.path.dirname(os.path.realpath(__file__))
130 ftemp = open(os.path.join(d, "templates", "resource_manager_template.txt"), "r")
131 template = ftemp.read()
134 template = template. \
135 replace("<CLASS_NAME>", classname). \
136 replace("<RTYPE>", rtype). \
137 replace("<ATTRIBUTES>", attributes). \
138 replace("<TRACES>", traces). \
139 replace("<BASE_CLASS_IMPORT>", base_class_import). \
140 replace("<BASE_CLASS>", base_class). \
141 replace("<SHORT-RTYPE>", short_rtype)
143 fname = uncamm_rtype.replace('ns3::', ''). \
145 replace("-","_").lower() + ".py"
147 f = open(os.path.join(d, "classes", fname), "w")
148 print(os.path.join(d, fname))
153 def template_attributes(ns3, tid):
154 d = os.path.dirname(os.path.realpath(__file__))
155 ftemp = open(os.path.join(d, "templates", "attribute_template.txt"), "r")
156 template = ftemp.read()
161 attr_count = tid.GetAttributeN()
162 for i in xrange(attr_count):
163 attr_info = tid.GetAttribute(i)
164 if not attr_info.accessor.HasGetter():
167 attr_flags = "Flags.Reserved"
168 flags = attr_info.flags
169 if (flags & ns3.TypeId.ATTR_CONSTRUCT) == ns3.TypeId.ATTR_CONSTRUCT:
170 attr_flags += " | Flags.Construct"
172 if (flags & ns3.TypeId.ATTR_GET) != ns3.TypeId.ATTR_GET:
173 attr_flags += " | Flags.NoRead"
174 elif (flags & ns3.TypeId.ATTR_SET) != ns3.TypeId.ATTR_SET:
175 attr_flags += " | Flags.NoWrite"
177 attr_name = attr_info.name
178 checker = attr_info.checker
179 attr_help = attr_info.help.replace('"', '\\"').replace("'", "\\'")
180 value = attr_info.initialValue
181 attr_value = value.SerializeToString(checker)
182 attr_allowed = "None"
184 attr_type = "Types.String"
186 if isinstance(value, ns3.ObjectVectorValue):
188 elif isinstance(value, ns3.PointerValue):
190 elif isinstance(value, ns3.WaypointValue):
192 elif isinstance(value, ns3.BooleanValue):
193 attr_type = "Types.Bool"
194 attr_value = "True" if attr_value == "true" else "False"
195 elif isinstance(value, ns3.EnumValue):
196 attr_type = "Types.Enumerate"
197 allowed = checker.GetUnderlyingTypeInformation().split("|")
198 attr_allowed = "[%s]" % ",".join(map(lambda x: "\"%s\"" % x, allowed))
199 elif isinstance(value, ns3.DoubleValue):
200 attr_type = "Types.Double"
202 elif isinstance(value, ns3.UintegerValue):
203 attr_type = "Types.Integer"
206 attr_id = "attr_" + attr_name.lower().replace("-", "_")
207 attributes += template.replace("<ATTR_ID>", attr_id) \
208 .replace("<ATTR_NAME>", attr_name) \
209 .replace("<ATTR_HELP>", attr_help) \
210 .replace("<ATTR_TYPE>", attr_type) \
211 .replace("<ATTR_DEFAULT>", attr_value) \
212 .replace("<ATTR_ALLOWED>", attr_allowed) \
213 .replace("<ATTR_RANGE>", attr_range) \
214 .replace("<ATTR_FLAGS>", attr_flags)
218 def template_traces(ns3, tid):
219 d = os.path.dirname(os.path.realpath(__file__))
220 ftemp = open(os.path.join(d, "templates", "trace_template.txt"), "r")
221 template = ftemp.read()
226 trace_count = tid.GetTraceSourceN()
227 for i in xrange(trace_count):
228 trace_info = tid.GetTraceSource(i)
229 trace_name = trace_info.name
230 trace_help = trace_info.help.replace('"', '\\"').replace("'", "\\'")
232 trace_id = trace_name.lower()
233 traces += template.replace("<TRACE_ID>", trace_id) \
234 .replace("<TRACE_NAME>", trace_name) \
235 .replace("<TRACE_HELP>", trace_help)
239 if __name__ == "__main__":