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 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>
19 from nepi.execution.resource import ResourceManager, clsinit_copy, \
21 from nepi.execution.attribute import Flags
22 from nepi.execution.trace import TraceAttr
25 class NS3Base(ResourceManager):
26 _rtype = "abstract::ns3::Object"
29 def __init__(self, ec, guid):
30 super(NS3Base, self).__init__(ec, guid)
32 self._connected = set()
33 self._trace_filename = dict()
38 return self._connected
46 return self.node.simulation
51 from nepi.resources.ns3.ns3node import NS3BaseNode
52 nodes = self.get_connected(NS3BaseNode.get_rtype())
53 if nodes: self._node = nodes[0]
57 def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
58 filename = self._trace_filename.get(name)
60 self.error("Can not resolve trace %s. Did you enabled it?" % name)
63 return self.simulation.trace(filename, attr, block, offset)
66 def _rms_to_wait(self):
67 """ Returns the collection of ns-3 RMs that this RM needs to
70 This method should be overriden to wait for other ns-3
71 objects to be deployed before proceeding with the deployment
76 if node: rms.add(node)
79 def _instantiate_object(self):
84 for attr in self._attrs.values():
85 if not ( attr.has_flag(Flags.Construct) and attr.has_changed ):
88 kwargs[attr.name] = attr._value
90 self._uuid = self.simulation.factory(self.get_rtype(), **kwargs)
92 def _configure_object(self):
95 def _connect_object(self):
97 if node and node.uuid not in self.connected:
98 self.simulation.invoke(node.uuid, "AggregateObject", self.uuid)
99 self._connected.add(node.uuid)
102 """ Returns True if dependent RMs are not yer READY, False otherwise"""
103 for rm in self._rms_to_wait:
104 if rm.state < ResourceState.READY:
108 def do_provision(self):
109 self._instantiate_object()
110 self._connect_object()
111 self._configure_object()
113 self.info("Provisioning finished")
115 super(NS3Base, self).do_provision()
119 self.debug("---- RESCHEDULING DEPLOY ----" )
120 self.ec.schedule(self.reschedule_delay, self.deploy)
128 if self.state == ResourceState.READY:
129 # No need to do anything, simulation.Run() will start every object
130 self.info("Starting")
134 self.error(msg, out, err)
135 raise RuntimeError(msg)
138 if self.state == ResourceState.STARTED:
139 # No need to do anything, simulation.Destroy() will stop every object
140 self.info("Stopping")
148 if self.state in [ResourceState.READY, ResourceState.STARTED] and \
149 self.has_flag(name, Flags.Reserved) and \
150 not self.has_flag(name, Flags.NoRead):
151 return self.simulation.ns3_get(self.uuid, name)
153 value = super(NS3Base, self).get(name)
157 def set(self, name, value):
158 if self.state in [ResourceState.READY, ResourceState.STARTED] and \
159 self.has_flag(name, Flags.Reserved) and \
160 not (self.has_flag(Flags.NoWrite) or self.has_flag(name, Flags.Design)):
161 self.simulation.ns3_set(self.uuid, name, value)
163 value = super(NS3Base, self).set(name, value)