2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2014 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 from nepi.execution.resource import ResourceManager, clsinit_copy, \
21 ResourceState, reschedule_delay
23 from nepi.execution.attribute import Flags
24 from nepi.execution.trace import TraceAttr
26 reschedule_delay = "1s"
29 class NS3Base(ResourceManager):
30 _rtype = "abstract::ns3::Object"
33 def __init__(self, ec, guid):
34 super(NS3Base, self).__init__(ec, guid)
36 self._connected = set()
37 self._trace_filename = dict()
41 return self._connected
49 return self.node.simulation
53 from nepi.resources.ns3.ns3node import NS3BaseNode
54 nodes = self.get_connected(NS3BaseNode.get_rtype())
55 if nodes: return nodes[0]
58 def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
59 filename = self._trace_filename.get(name)
61 self.error("Can resolve trace %s. Did you enabled it?" % name)
64 return self.simulation.trace(filename, attr, block, offset)
67 def _rms_to_wait(self):
68 """ Returns the collection of ns-3 RMs that this RM needs to
71 This method should be overriden to wait for other ns-3
72 objects to be deployed before proceeding with the deployment
77 if node: rms.add(node)
80 def _instantiate_object(self):
85 for attr in self._attrs.values():
86 if not ( attr.has_flag(Flags.Construct) and attr.has_changed() ):
89 kwargs[attr.name] = attr._value
91 self._uuid = self.simulation.factory(self.get_rtype(), **kwargs)
93 def _configure_object(self):
96 def _connect_object(self):
98 if node and node.uuid not in self.connected:
99 self.simulation.invoke(node.uuid, "AggregateObject", self.uuid)
100 self._connected.add(node.uuid)
103 """ Returns True if dependent RMs are not yer READY, False otherwise"""
104 for rm in self._rms_to_wait:
105 if rm and rm.state < ResourceState.READY:
109 def do_provision(self):
110 # TODO: create run dir for ns3 object !!!!
111 # self.simulation.node.mkdir(self.run_home)
113 self._instantiate_object()
114 self._connect_object()
115 self._configure_object()
117 self.info("Provisioning finished")
119 super(NS3Base, self).do_provision()
123 self.debug("---- RESCHEDULING DEPLOY ----" )
124 self.ec.schedule(reschedule_delay, self.deploy)
132 if self.state == ResourceState.READY:
133 # No need to do anything, simulation.Run() will start every object
134 self.info("Starting")
138 self.error(msg, out, err)
139 raise RuntimeError, msg
142 if self.state == ResourceState.STARTED:
143 # No need to do anything, simulation.Destroy() will stop every object
144 self.info("Stopping command '%s'" % command)
152 if self.state in [ResourceState.READY, ResourceState.STARTED] and \
153 self.has_flag(name, Flags.Reserved) and \
154 not self.has_flag(name, Flags.NoRead):
155 return self.simulation.ns3_get(self.uuid, name)
157 value = super(NS3Base, self).get(name)
161 def set(self, name, value):
162 if self.state in [ResourceState.READY, ResourceState.STARTED] and \
163 self.has_flag(name, Flags.Reserved) and \
164 not (self.has_flag(Flags.NoWrite) or self.has_flag(name, Flags.Design)):
165 self.simulation.ns3_set(self.uuid, name, value)
167 value = super(NS3Base, self).set(name, value)