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 with open(os.path.join(d, "templates", "resource_manager_template.txt"), "r") as ftemp:
131 template = ftemp.read()
133 template = template. \
134 replace("<CLASS_NAME>", classname). \
135 replace("<RTYPE>", rtype). \
136 replace("<ATTRIBUTES>", attributes). \
137 replace("<TRACES>", traces). \
138 replace("<BASE_CLASS_IMPORT>", base_class_import). \
139 replace("<BASE_CLASS>", base_class). \
140 replace("<SHORT-RTYPE>", short_rtype)
142 fname = uncamm_rtype.replace('ns3::', ''). \
144 replace("-","_").lower() + ".py"
146 with open(os.path.join(d, "classes", fname), "w") as f:
147 print(os.path.join(d, fname))
151 def template_attributes(ns3, tid):
152 d = os.path.dirname(os.path.realpath(__file__))
153 with open(os.path.join(d, "templates", "attribute_template.txt"), "r") as ftemp:
154 template = ftemp.read()
158 attr_count = tid.GetAttributeN()
159 for i in xrange(attr_count):
160 attr_info = tid.GetAttribute(i)
161 if not attr_info.accessor.HasGetter():
164 attr_flags = "Flags.Reserved"
165 flags = attr_info.flags
166 if (flags & ns3.TypeId.ATTR_CONSTRUCT) == ns3.TypeId.ATTR_CONSTRUCT:
167 attr_flags += " | Flags.Construct"
169 if (flags & ns3.TypeId.ATTR_GET) != ns3.TypeId.ATTR_GET:
170 attr_flags += " | Flags.NoRead"
171 elif (flags & ns3.TypeId.ATTR_SET) != ns3.TypeId.ATTR_SET:
172 attr_flags += " | Flags.NoWrite"
174 attr_name = attr_info.name
175 checker = attr_info.checker
176 attr_help = attr_info.help.replace('"', '\\"').replace("'", "\\'")
177 value = attr_info.initialValue
178 attr_value = value.SerializeToString(checker)
179 attr_allowed = "None"
181 attr_type = "Types.String"
183 if isinstance(value, ns3.ObjectVectorValue):
185 elif isinstance(value, ns3.PointerValue):
187 elif isinstance(value, ns3.WaypointValue):
189 elif isinstance(value, ns3.BooleanValue):
190 attr_type = "Types.Bool"
191 attr_value = "True" if attr_value == "true" else "False"
192 elif isinstance(value, ns3.EnumValue):
193 attr_type = "Types.Enumerate"
194 allowed = checker.GetUnderlyingTypeInformation().split("|")
195 attr_allowed = "[%s]" % ",".join(map(lambda x: "\"%s\"" % x, allowed))
196 elif isinstance(value, ns3.DoubleValue):
197 attr_type = "Types.Double"
199 elif isinstance(value, ns3.UintegerValue):
200 attr_type = "Types.Integer"
203 attr_id = "attr_" + attr_name.lower().replace("-", "_")
204 attributes += template.replace("<ATTR_ID>", attr_id) \
205 .replace("<ATTR_NAME>", attr_name) \
206 .replace("<ATTR_HELP>", attr_help) \
207 .replace("<ATTR_TYPE>", attr_type) \
208 .replace("<ATTR_DEFAULT>", attr_value) \
209 .replace("<ATTR_ALLOWED>", attr_allowed) \
210 .replace("<ATTR_RANGE>", attr_range) \
211 .replace("<ATTR_FLAGS>", attr_flags)
215 def template_traces(ns3, tid):
216 d = os.path.dirname(os.path.realpath(__file__))
217 with open(os.path.join(d, "templates", "trace_template.txt"), "r") as ftemp:
218 template = ftemp.read()
222 trace_count = tid.GetTraceSourceN()
223 for i in xrange(trace_count):
224 trace_info = tid.GetTraceSource(i)
225 trace_name = trace_info.name
226 trace_help = trace_info.help.replace('"', '\\"').replace("'", "\\'")
228 trace_id = trace_name.lower()
229 traces += template.replace("<TRACE_ID>", trace_id) \
230 .replace("<TRACE_NAME>", trace_name) \
231 .replace("<TRACE_HELP>", trace_help)
235 if __name__ == "__main__":