Adding trace support for ns3 RMs
[nepi.git] / src / nepi / resources / ns3 / ns3base.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 INRIA
4 #
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.
9 #
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.
14 #
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/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.resource import ResourceManager, clsinit_copy, \
21         ResourceState, reschedule_delay
22
23 from nepi.execution.attribute import Flags
24 from nepi.execution.trace import TraceAttr
25
26 reschedule_delay = "2s"
27
28 @clsinit_copy
29 class NS3Base(ResourceManager):
30     _rtype = "abstract::ns3::Object"
31     _backend_type = "ns3"
32
33     def __init__(self, ec, guid):
34         super(NS3Base, self).__init__(ec, guid)
35         self._uuid = None
36         self._connected = set()
37         self._trace_filename = dict()
38
39     @property
40     def connected(self):
41         return self._connected
42
43     @property
44     def uuid(self):
45         return self._uuid
46
47     @property
48     def simulation(self):
49         return self.node.simulation
50
51     @property
52     def node(self):
53         from nepi.resources.ns3.ns3node import NS3BaseNode
54         nodes = self.get_connected(NS3BaseNode.get_rtype())
55         if nodes: return nodes[0]
56         return None
57
58     def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
59         filename = self._trace_filename.get(name)
60         if not filename:
61             self.error("Can resolve trace %s. Did you enabled it?" % name)
62             return ""
63
64         return self.simulation.trace(filename, attr, block, offset)
65
66     @property
67     def _rms_to_wait(self):
68         """ Returns the collection of ns-3 RMs that this RM needs to
69         wait for before start
70
71         This method should be overriden to wait for other ns-3
72         objects to be deployed before proceeding with the deployment
73
74         """
75         rms = set()
76         node = self.node
77         if node: rms.add(node)
78         return rms
79
80     def _instantiate_object(self):
81         if self.uuid:
82             return 
83
84         kwargs = dict()
85         for attr in self._attrs.values():
86             if not (attr.has_changed() and attr.has_flag(Flags.Construct)):
87                 continue
88
89             kwargs[attr.name] = attr.value
90
91         self._uuid = self.simulation.factory(self.get_rtype(), **kwargs)
92
93     def _configure_object(self):
94         pass
95
96     def _connect_object(self):
97         node = self.node
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)
101
102     def _wait_rms(self):
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:
106                 return True
107         return False
108
109     def do_provision(self):
110         # TODO: create run dir for ns3 object !!!!
111         # self.simulation.node.mkdir(self.run_home)
112
113         self._instantiate_object()
114         self._connect_object()
115         self._configure_object()
116       
117         self.info("Provisioning finished")
118
119         super(NS3Base, self).do_provision()
120
121     def do_deploy(self):
122         if self._wait_rms():
123             self.debug("---- RESCHEDULING DEPLOY ----" )
124             self.ec.schedule(reschedule_delay, self.deploy)
125         else:
126             self.do_discover()
127             self.do_provision()
128
129             self.set_ready()
130
131     def do_start(self):
132         if self.state == ResourceState.READY:
133             # No need to do anything, simulation.Run() will start every object
134             self.info("Starting")
135             self.set_started()
136         else:
137             msg = " Failed "
138             self.error(msg, out, err)
139             raise RuntimeError, msg
140
141     def do_stop(self):
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)
145             self.set_stopped()
146     
147     @property
148     def state(self):
149         return self._state
150