Cleaning up RM template
[nepi.git] / doc / templates / template_rm.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 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 # Authors: Alina Quereilhac <alina.quereilhac@inria.fr>
19 #          Julien Tribino <julien.tribino@inria.fr>
20
21
22 from nepi.execution.attribute import Attribute, Flags, Types
23 from nepi.execution.resource import ResourceManager, clsinit_copy, \
24         ResourceState
25
26 #clsinit_copy is used to inherit attributes from the parent class
27 @clsinit_copy
28 class RMClass(ResourceManager):
29     # Name that will be used in the NEPI script to identify the resource type
30     _rtype = "platform::RMType" 
31     # User friendly description of the RM 
32     _help = "Describes what this RM does"
33     # Name of the platform this RM belongs to 
34     _platform = "platform" 
35     # list of valid connection for this RM
36     _authorized_connections = ["platform::AnotherRMType1" , "platform::AnotherRMType2"] 
37
38     @classmethod
39     def _register_attributes(cls):
40         """
41         This method is used to register all the attribute of this RM. Check the
42         file src/execution/attribute.py to see all the fields of this class
43         """
44
45         attribute1 = Attribute("nameOfAttribute1", "Description of Attribute 1",
46                 flags = Flags.Design)
47
48         attribute2 = Attribute("nameOfAttribute2", "Description of Attribute 2",
49                 flags = Flags.Design)
50
51         cls._register_attribute(attribute1)
52         cls._register_attribute(attribute2)
53
54     def __init__(self, ec, guid):
55         """
56         Declares and initializes variables of the RM that are not Attributes.
57         Attributes represent resource configuration exposed to the user, 
58         other class variables can declared here for RM internal use.
59         """
60         
61         super(RMClass, self).__init__(ec, guid)
62     
63     def log_message(self, msg):
64         """
65         Prints a log message adding a identification prefix.
66
67         The log message for the RMClass can be redefined here. 
68         It can be used to add information about related resources such as
69         the hostname of the node RM associated to an application RM.
70         """
71         
72         return " %s guid %d - %s " % (self._rtype, self.guid, msg)
73
74     def valid_connection(self, guid):
75         """
76         Checks whether the RMClass instance can be connected to the 
77         other RM corresponding to the given guid.
78         """
79         
80         rm = self.ec.get_resource(guid)
81
82         if rm.get_rtype() not in self._authorized_connections:
83             msg = ("Connection between %s %s and %s %s refused: "
84                     "An Application can be connected only to a Node" ) % \
85                 (self.get_rtype(), self._guid, rm.get_rtype(), guid)
86
87             return False
88
89         elif len(self.connections) != 0 :
90             msg = ("Connection between %s %s and %s %s refused: "
91                     "This Application is already connected" ) % \
92                 (self.get_rtype(), self._guid, rm.get_rtype(), guid)
93             self.debug(msg)
94
95             return False
96
97         return True
98
99     def do_discover(self):
100         """
101         Perform actions required to discover resources matching some criteria
102         specified by the user through the configuration of Attributes. 
103         """
104         
105         super(RMClass, self).do_discover()
106
107     def do_reserve(self):
108         """
109         Perform actions required to reserve resources matching some criteria
110         specified by the user through the configuration of Attributes. 
111         """
112         
113         super(RMClass, self).do_reserve()
114
115     def do_provision(self):
116         """
117         Perform actions required to provision a resource in the platform,
118         matching the criteria specified by the user.
119         """
120
121         super(RMClass, self).do_provision()
122
123     def do_deploy(self):
124         """
125         Perform actions required to deploy a resource in the platform.
126         
127         Deploying a resource most frequently involves invoking the 
128         do_discover and do_provision methods. In order to deploy a
129         resource it might be necessary wait until other associated
130         resource is in a given state, as in the following example:
131
132         other_rm_list = self.get_connected(OtherRMClass.get_rtype())
133         other_rm = other_rm_list[0]
134
135         if other_rm.state < ResourceState.READY:
136             self.ec.schedule(self.reschedule_delay, self.deploy)
137         else:
138             self.do_discover()
139             self.do_provision()
140
141             super(RMClass, self).do_deploy()
142  
143         """
144
145         super(RMClass, self).do_deploy()
146
147     def do_start(self):
148         """
149         Perform actions required to start a resource in the platform.
150         """
151
152         super(RMClass, self).do_start()
153
154     def do_stop(self):
155         """
156         Perform actions required to stop a resource in the platform.
157         """
158
159         super(RMClass, self).do_stop()
160
161     def do_release(self):
162         """
163         Perform actions required to release a resource in the platform.
164         """
165
166         super(RMClass, self).do_release()
167
168     @property
169     def state(self):
170         """ 
171         Returns the state of the RM.
172         """
173
174         return super(RMClass, self).state
175
176