bf0a8536a3a23e391e75a318145e65a5671eaf17
[nepi.git] / src / nepi / execution / attribute.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 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 ### Attribute Types
21 class Types:
22     String  = "STRING"
23     Bool    = "BOOL"
24     Enumerate    = "ENUM"
25     Double  = "DOUBLE"
26     Integer = "INTEGER"
27
28 ### Attribute Flags
29 class Flags:
30     """ Differents flags to characterize an attribute
31
32     """
33     # Attribute value can not be read (it is hidden to the user) 
34     NoRead    = 1 # 1
35     
36     # Attribute value can not be modified (it is not editable by the user)
37     NoWrite   = 1 << 1 # 2
38     
39     # Attribute value can be modified only before deployment
40     Design  = 1 << 2 # 4
41
42     # Attribute value will be used at deployment time for initial configuration
43     Construct    = 1 << 3 #  8
44
45     # Attribute provides credentials to access resources
46     Credential  = 1 << 4  | Design # 16 + 4
47
48     # Attribute is a filter used to discover resources
49     Filter  = 1 << 5 | Design # 32 + 4
50
51     # Attribute Flag is reserved for internal RM usage (i.e. should be 
52     # transparent to the user)
53     Reserved  = 1 << 6 # 64
54
55     # Attribute global is set to all resources of rtype
56     Global  = 1 << 7 # 128
57
58
59 class Attribute(object):
60     """
61     .. class:: Class Args :
62
63         An Attribute reflects a configuration parameter for
64         a particular resource. Attributes might be read only or
65         not.
66       
67         :param name: Name of the attribute
68         :type name: str
69
70         :param help: Attribute description
71         :type help: str
72         
73         :param type: The type expected for the attribute value.
74                      Should be one of Attribute.Types .
75         :type type: str
76
77         :param flags: Defines attribute behavior (i.e. whether it is read-only,
78                 read and write, etc). This parameter should take its values from
79                 Attribute.Flags. Flags values can be bitwised.
80         :type flags: hex
81
82         :param default: Default value of the attribute
83         :type default: depends on the type of attribute
84         
85         :param allowed: List of values that the attribute can take. 
86                 This parameter is only meaningful for Enumerate type attributes.
87         :type allowed: list
88         
89         :param range: (max, min) tuple with range of possible values for
90                 attributes.
91                 This parameter is only meaningful for Integer or Double type
92                 attributes.
93         :type range: (int, int) or (float, float)
94         
95         :param set_hook: Function that will be executed whenever a new 
96                 value is set for the attribute.
97         :type set_hook: function
98
99     """
100     def __init__(self, name, help, type = Types.String,
101             flags = None, default = None, allowed = None,
102             range = None, set_hook = None):
103         self._name = name
104         self._help = help
105         self._type = type
106         self._flags = flags or 0
107         self._allowed = allowed
108         self._range = range
109         self._default = self._value = default
110         # callback to be invoked upon changing the 
111         # attribute value
112         self.set_hook = set_hook
113
114     @property
115     def name(self):
116         """ Returns the name of the attribute """
117         return self._name
118
119     @property
120     def default(self):
121         """ Returns the default value of the attribute """
122         return self._default
123
124     @property
125     def type(self):
126         """ Returns the type of the attribute """
127         return self._type
128
129     @property
130     def help(self):
131         """ Returns the help of the attribute """
132         return self._help
133
134     @property
135     def flags(self):
136         """ Returns the flags of the attribute """
137         return self._flags
138
139     @property
140     def allowed(self):
141         """ Returns the allowed value for this attribute """
142         return self._allowed
143
144     @property
145     def range(self):
146         """ Returns the range of the attribute """
147         return self._range
148
149     def has_flag(self, flag):
150         """ Returns true if the attribute has the flag 'flag'
151
152         :param flag: Flag to be checked
153         :type flag: Flags
154         """
155         return (self._flags & flag) == flag
156
157     def get_value(self):
158         """ Returns the value of the attribute """
159         return self._value
160
161     def set_value(self, value):
162         """ Change the value of the attribute after checking the type """
163         valid = True
164
165         if self.type == Types.Enumerate:
166             valid = value in self._allowed
167
168         if self.type in [Types.Double, Types.Integer] and self.range:
169             (min, max) = self.range
170
171             value = float(value)
172
173             valid = (value >= min and value <= max) 
174         
175         valid = valid and self.is_valid_value(value)
176
177         if valid: 
178             if self.set_hook:
179                 # Hook receives old value, new value
180                 value = self.set_hook(self._value, value)
181
182             self._value = value
183         else:
184             raise ValueError("Invalid value %s for attribute %s" %
185                     (str(value), self.name))
186
187     value = property(get_value, set_value)
188
189     def is_valid_value(self, value):
190         """ Attribute subclasses will override this method to add 
191         adequate validation"""
192         return True
193
194     @property
195     def has_changed(self):
196         """ Returns true if the value has changed from the default """
197         return self.value != self.default