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