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()
38 return self._connected
46 return self.node.simulation
50 from nepi.resources.ns3.ns3node import NS3BaseNode
51 nodes = self.get_connected(NS3BaseNode.get_rtype())
52 if nodes: return nodes[0]
55 def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
56 filename = self._trace_filename.get(name)
58 self.error("Can not resolve trace %s. Did you enabled it?" % name)
61 return self.simulation.trace(filename, attr, block, offset)
64 def _rms_to_wait(self):
65 """ Returns the collection of ns-3 RMs that this RM needs to
68 This method should be overriden to wait for other ns-3
69 objects to be deployed before proceeding with the deployment
74 if node: rms.add(node)
77 def _instantiate_object(self):
82 for attr in self._attrs.values():
83 if not ( attr.has_flag(Flags.Construct) and attr.has_changed() ):
86 kwargs[attr.name] = attr._value
88 self._uuid = self.simulation.factory(self.get_rtype(), **kwargs)
90 def _configure_object(self):
93 def _connect_object(self):
95 if node and node.uuid not in self.connected:
96 self.simulation.invoke(node.uuid, "AggregateObject", self.uuid)
97 self._connected.add(node.uuid)
100 """ Returns True if dependent RMs are not yer READY, False otherwise"""
101 for rm in self._rms_to_wait:
102 if rm.state < ResourceState.READY:
106 def do_provision(self):
107 self._instantiate_object()
108 self._connect_object()
109 self._configure_object()
111 self.info("Provisioning finished")
113 super(NS3Base, self).do_provision()
117 self.debug("---- RESCHEDULING DEPLOY ----" )
118 self.ec.schedule(reschedule_delay, self.deploy)
126 if self.state == ResourceState.READY:
127 # No need to do anything, simulation.Run() will start every object
128 self.info("Starting")
132 self.error(msg, out, err)
133 raise RuntimeError, msg
136 if self.state == ResourceState.STARTED:
137 # No need to do anything, simulation.Destroy() will stop every object
138 self.info("Stopping command '%s'" % command)
146 if self.state in [ResourceState.READY, ResourceState.STARTED] and \
147 self.has_flag(name, Flags.Reserved) and \
148 not self.has_flag(name, Flags.NoRead):
149 return self.simulation.ns3_get(self.uuid, name)
151 value = super(NS3Base, self).get(name)
155 def set(self, name, value):
156 if self.state in [ResourceState.READY, ResourceState.STARTED] and \
157 self.has_flag(name, Flags.Reserved) and \
158 not (self.has_flag(Flags.NoWrite) or self.has_flag(name, Flags.Design)):
159 self.simulation.ns3_set(self.uuid, name, value)
161 value = super(NS3Base, self).set(name, value)