rename attributes to slice_attribute_types
[plcapi.git] / PLC / SliceAttributeTypes.py
1 from types import StringTypes
2
3 from PLC.Faults import *
4 from PLC.Parameter import Parameter
5 from PLC.Table import Row, Table
6 from PLC.Roles import Role, Roles
7
8 class SliceAttributeType(Row):
9     """
10     Representation of a row in the slice_attribute_types table. To
11     use, instantiate with a dict of values.
12     """
13
14     table_name = 'slice_attribute_types'
15     primary_key = 'attribute_type_id'
16     fields = {
17         'attribute_type_id': Parameter(int, "Slice attribute type identifier"),
18         'name': Parameter(str, "Slice attribute type name", max = 100),
19         'description': Parameter(str, "Slice attribute type description", max = 254),
20         'min_role_id': Parameter(int, "Minimum (least powerful) role that can set or change this attribute"),
21         }
22
23     def __init__(self, api, fields = {}):
24         Row.__init__(self, fields)
25         self.api = api
26
27     def validate_name(self, name):
28         name = name.strip()
29
30         if not name:
31             raise PLCInvalidArgument, "Slice attribute type name must be set"
32
33         conflicts = SliceAttributeTypes(self.api, [name])
34         for attribute_type_id, attribute in conflicts.iteritems():
35             if 'attribute_type_id' not in self or self['attribute_type_id'] != attribute_type_id:
36                 raise PLCInvalidArgument, "Slice attribute type name already in use"
37
38         return name
39
40     def validate_min_role_id(self, role_id):
41         roles = Roles(self.api)
42         if role_id not in roles:
43             raise PLCInvalidArgument, "Invalid role"
44
45         return role_id
46
47     def delete(self, commit = True):
48         """
49         Delete existing slice attribute type.
50         """
51
52         assert 'attribute_type_id' in self
53
54         # Clean up miscellaneous join tables
55         for table in ['slice_attribute_types', 'slice_attribute']:
56             self.api.db.do("DELETE FROM %s" \
57                            " WHERE attribute_type_id = %d" % \
58                            (table, self['attribute_type_id']), self)
59
60         if commit:
61             self.api.db.commit()
62
63 class SliceAttributeTypes(Table):
64     """
65     Representation of row(s) from the slice_attribute_types table in the
66     database.
67     """
68
69     def __init__(self, api, attribute_type_id_or_name_list = None):
70         self.api = api
71
72         sql = "SELECT %s FROM slice_attribute_types" % \
73               ", ".join(SliceAttributeType.fields)
74
75         if attribute_type_id_or_name_list:
76             # Separate the list into integers and strings
77             attribute_type_ids = filter(lambda attribute_type_id: isinstance(attribute_type_id, (int, long)),
78                                    attribute_type_id_or_name_list)
79             names = filter(lambda name: isinstance(name, StringTypes),
80                            attribute_type_id_or_name_list)
81             sql += " WHERE (False"
82             if attribute_type_ids:
83                 sql += " OR attribute_type_id IN (%s)" % ", ".join(map(str, attribute_type_ids))
84             if names:
85                 sql += " OR name IN (%s)" % ", ".join(api.db.quote(names))
86             sql += ")"
87
88         rows = self.api.db.selectall(sql)
89  
90         for row in rows:
91             self[row['attribute_type_id']] = SliceAttributeType(api, row)