2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
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;
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.
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/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
21 """ Allowed types for the Attribute value
31 """ Flags to characterize the scope of an Attribute
33 # Attribute value can not be read (it is hidden to the user)
36 # Attribute value can not be modified (it is not editable by the user)
39 # Attribute value can be modified only before deployment
42 # Attribute value will be used at deployment time for initial configuration
43 Construct = 1 << 3 # 8
45 # Attribute provides credentials to access resources
46 Credential = 1 << 4 | Design # 16 + 4
48 # Attribute is a filter used to discover resources
49 Filter = 1 << 5 | Design # 32 + 4
51 # Attribute Flag is reserved for internal RM usage (i.e. should be
52 # transparent to the user)
53 Reserved = 1 << 6 # 64
55 # Attribute global is set to all resources of rtype
59 class Attribute(object):
60 """ An Attribute exposes a configuration parameter of a resource
63 def __init__(self, name, help, type = Types.String,
64 flags = None, default = None, allowed = None,
65 range = None, set_hook = None):
67 :param name: Name of the Attribute
70 :param help: Description of the Attribute
73 :param type: The type expected for the Attribute value.
74 Should be one of Attribute.Types
77 :param flags: Defines Attribute behavior (i.e. whether it is read-only,
78 read and write, etc). This parameter must take its values from
79 Attribute.Flags. Flags values can be bitwised
82 :param default: Default value for the Attribute
83 :type default: Depends on the type of Attribute
85 :param allowed: List of values that the Attribute can take.
86 This parameter is only meaningful for Enumerate type Attributes
89 :param range: (max, min) tuple with range of possible values for
91 This parameter is only meaningful for Integer or Double type
93 :type range: (int, int) or (float, float)
95 :param set_hook: Function that will be executed whenever a new
96 value is set for the Attribute.
97 :type set_hook: function
103 self._flags = flags or 0
104 self._allowed = allowed
106 self._default = self._value = default
107 # callback to be invoked upon changing the
109 self.set_hook = set_hook
113 """ Returns the name of the Attribute """
118 """ Returns the default value of the Attribute """
123 """ Returns the type of the Attribute """
128 """ Returns the description of the Attribute """
133 """ Returns the flags of the Attribute """
138 """ Returns the set of allowed values for the Attribute """
143 """ Returns the range of allowed numerical values for the Attribute """
146 def has_flag(self, flag):
147 """ Returns True if the Attribute has the flag 'flag'
149 :param flag: Flag to be checked
152 return (self._flags & flag) == flag
155 """ Returns the value of the Attribute """
158 def set_value(self, value):
159 """ Configure a new value for the Attribute """
162 if self.type == Types.Enumerate:
163 valid = value in self._allowed
165 if self.type in [Types.Double, Types.Integer] and self.range:
166 (min, max) = self.range
170 valid = (value >= min and value <= max)
172 valid = valid and self.is_valid_value(value)
176 # Hook receives old value, new value
177 value = self.set_hook(self._value, value)
181 raise ValueError("Invalid value %s for attribute %s" %
182 (str(value), self.name))
184 value = property(get_value, set_value)
186 def is_valid_value(self, value):
187 """ Attribute subclasses will override this method to add
188 adequate validation"""
192 def has_changed(self):
193 """ Returns True if the value has changed from the default """
194 return self.value != self.default