applied the except and raise fixers to the master branch to close the gap with py3
[nepi.git] / src / nepi / resources / netns / netnsbase.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 NetNSBase(ResourceManager):
26     _rtype = "abstract::netns::Object"
27     _platform = "netns"
28
29     def __init__(self, ec, guid):
30         super(NetNSBase, self).__init__(ec, guid)
31         self._uuid = None
32         self._connected = set()
33         self._trace_filename = dict()
34
35     @property
36     def connected(self):
37         return self._connected
38
39     @property
40     def uuid(self):
41         return self._uuid
42
43     def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
44         filename = self._trace_filename.get(name)
45         if not filename:
46             self.error("Can not resolve trace %s. Did you enabled it?" % name)
47             return ""
48
49         return self.emulation.trace(filename, attr, block, offset)
50
51     @property
52     def _rms_to_wait(self):
53         """ Returns the collection of RMs that this RM needs to
54         wait for before start
55
56         This method should be overriden to wait for other
57         objects to be deployed before proceeding with the deployment
58
59         """
60         raise RuntimeError("No dependencies defined!")
61
62     def _instantiate_object(self):
63         pass
64
65     def _wait_rms(self):
66         rms = set()
67         for rm in self._rms_to_wait:
68             if rm is not None:
69                 rms.add(rm)
70
71         """ Returns True if dependent RMs are not yer READY, False otherwise"""
72         for rm in rms:
73             if rm.state < ResourceState.READY:
74                 return True
75         return False
76
77     def do_provision(self):
78         self._instantiate_object()
79       
80         self.info("Provisioning finished")
81
82         super(NetNSBase, self).do_provision()
83
84     def do_deploy(self):
85         if self._wait_rms():
86             self.debug("---- RESCHEDULING DEPLOY ----" )
87             self.ec.schedule(self.reschedule_delay, self.deploy)
88         else:
89             self.do_discover()
90             self.do_provision()
91
92             self.set_ready()
93
94     def do_start(self):
95         if self.state == ResourceState.READY:
96             # No need to do anything, emulation.Run() will start every object
97             self.info("Starting")
98             self.set_started()
99         else:
100             msg = " Failed "
101             self.error(msg, out, err)
102             raise RuntimeError(msg)
103
104     def do_stop(self):
105         if self.state == ResourceState.STARTED:
106             # No need to do anything, emulation.Destroy() will stop every object
107             self.set_stopped()
108     
109     @property
110     def state(self):
111         return self._state
112
113     def get(self, name):
114         #flags = Flags.NoWrite | Flags.NoRead | Flags.Design
115         flags = Flags.Design
116         if self._state in [ResourceState.READY, ResourceState.STARTED] \
117                 and not self.has_flag(name, flags):
118             return self.emulation.emu_get(self.uuid, name)
119         
120         value = super(NetNSBase, self).get(name)
121         return value
122
123     def set(self, name, value):
124         flags = Flags.Design
125         if (self._state > ResourceState.NEW and \
126                 self.has_flag(name, Flags.Design)) or \
127                 self.has_flag(name, Flags.NoWrite):
128             out = err = ""
129             msg = " Cannot change Design only attribue %s" % name
130             self.error(msg, out, err)
131             return 
132
133         if self._state in [ResourceState.READY, ResourceState.STARTED]:
134             self.emulation.emu_set(self.uuid, name, value)
135         
136         value = super(NetNSBase, self).set(name, value)
137
138         return value
139