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
22 from nepi.execution.attribute import Flags
23 from nepi.execution.trace import TraceAttr
26 class NS3Base(ResourceManager):
27 _rtype = "abstract::ns3::Object"
30 def __init__(self, ec, guid):
31 super(NS3Base, self).__init__(ec, guid)
33 self._connected = set()
34 self._trace_filename = dict()
39 return self._connected
47 return self.node.simulation
52 from nepi.resources.ns3.ns3node import NS3BaseNode
53 nodes = self.get_connected(NS3BaseNode.get_rtype())
54 if nodes: self._node = 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 not 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.state < ResourceState.READY:
109 def do_provision(self):
110 self._instantiate_object()
111 self._connect_object()
112 self._configure_object()
114 self.info("Provisioning finished")
116 super(NS3Base, self).do_provision()
120 self.debug("---- RESCHEDULING DEPLOY ----" )
121 self.ec.schedule(reschedule_delay, self.deploy)
129 if self.state == ResourceState.READY:
130 # No need to do anything, simulation.Run() will start every object
131 self.info("Starting")
135 self.error(msg, out, err)
136 raise RuntimeError, msg
139 if self.state == ResourceState.STARTED:
140 # No need to do anything, simulation.Destroy() will stop every object
141 self.info("Stopping command '%s'" % command)
149 if self.state in [ResourceState.READY, ResourceState.STARTED] and \
150 self.has_flag(name, Flags.Reserved) and \
151 not self.has_flag(name, Flags.NoRead):
152 return self.simulation.ns3_get(self.uuid, name)
154 value = super(NS3Base, self).get(name)
158 def set(self, name, value):
159 if self.state in [ResourceState.READY, ResourceState.STARTED] and \
160 self.has_flag(name, Flags.Reserved) and \
161 not (self.has_flag(Flags.NoWrite) or self.has_flag(name, Flags.Design)):
162 self.simulation.ns3_set(self.uuid, name, value)
164 value = super(NS3Base, self).set(name, value)