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 as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
30 """ Differents flags to characterize 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
58 class Attribute(object):
60 .. class:: Class Args :
62 An Attribute reflects a configuration parameter for
63 a particular resource. Attributes might be read only or
66 :param name: Name of the attribute
69 :param help: Attribute description
72 :param type: The type expected for the attribute value.
73 Should be one of Attribute.Types .
76 :param flags: Defines attribute behavior (i.e. whether it is read-only,
77 read and write, etc). This parameter should take its values from
78 Attribute.Flags. Flags values can be bitwised.
81 :param default: Default value of the attribute
82 :type default: depends on the type of attribute
84 :param allowed: List of values that the attribute can take.
85 This parameter is only meaningful for Enumerate type attributes.
88 :param range: (max, min) tuple with range of possible values for
90 This parameter is only meaningful for Integer or Double type
92 :type range: (int, int) or (float, float)
94 :param set_hook: Function that will be executed whenever a new
95 value is set for the attribute.
96 :type set_hook: function
99 def __init__(self, name, help, type = Types.String,
100 flags = None, default = None, allowed = None,
101 range = None, set_hook = None):
105 self._flags = flags or 0
106 self._allowed = allowed
108 self._default = self._value = default
109 # callback to be invoked upon changing the
111 self.set_hook = set_hook
115 """ Returns the name of the attribute """
120 """ Returns the default value of the attribute """
125 """ Returns the type of the attribute """
130 """ Returns the help of the attribute """
135 """ Returns the flags of the attribute """
140 """ Returns the allowed value for this attribute """
145 """ Returns the range of the attribute """
148 def has_flag(self, flag):
149 """ Returns true if the attribute has the flag 'flag'
151 :param flag: Flag to be checked
154 return (self._flags & flag) == flag
157 """ Returns the value of the attribute """
160 def set_value(self, value):
161 """ Change the value of the attribute after checking the type """
164 if self.type == Types.Enumerate:
165 valid = value in self._allowed
167 if self.type in [Types.Double, Types.Integer] and self.range:
168 (min, max) = self.range
172 valid = (value >= min and value <= max)
174 valid = valid and self.is_valid_value(value)
178 # Hook receives old value, new value
179 value = self.set_hook(self._value, value)
183 raise ValueError("Invalid value %s for attribute %s" %
184 (str(value), self.name))
186 value = property(get_value, set_value)
188 def is_valid_value(self, value):
189 """ Attribute subclasses will override this method to add
190 adequate validation"""
194 def has_changed(self):
195 """ Returns true if the value has changed from the default """
196 return self.value != self.default