rename attributes to slice_attribute_types
[plcapi.git] / PLC / SliceAttributes.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.SliceAttributeTypes import SliceAttributeType, SliceAttributeTypes
7
8 class SliceAttribute(Row):
9     """
10     Representation of a row in the slice_attribute table. To use,
11     instantiate with a dict of values.
12     """
13
14     table_name = 'slice_attribute'
15     primary_key = 'slice_attribute_id'
16     fields = {
17         'slice_attribute_id': Parameter(int, "Slice attribute identifier"),
18         'slice_id': Parameter(int, "Slice identifier"),
19         'node_id': Parameter(int, "Node identifier, if a sliver attribute"),
20         'attribute_type_id': SliceAttributeType.fields['attribute_type_id'],
21         'name': SliceAttributeType.fields['name'],
22         'description': SliceAttributeType.fields['description'],
23         'min_role_id': SliceAttributeType.fields['min_role_id'],
24         # XXX Arbitrary max, make configurable
25         'value': Parameter(str, "Slice attribute value", max = 254),
26         }
27
28     def __init__(self, api, fields = {}):
29         Row.__init__(self, fields)
30         self.api = api
31
32     def delete(self, commit = True):
33         """
34         Delete existing slice attribute.
35         """
36
37         assert 'slice_attribute_id' in self
38
39         # Clean up miscellaneous join tables
40         for table in 'slice_attribute',:
41             self.api.db.do("DELETE FROM %s" \
42                            " WHERE slice_attribute_id = %d" % \
43                            (table, self['slice_attribute_id']), self)
44
45         if commit:
46             self.api.db.commit()
47
48 class SliceAttributes(dict):
49     """
50     Representation of row(s) from the slice_attribute table in the
51     database.
52     """
53
54     def __init__(self, api, slice_attribute_id_list):
55         self.api = api
56
57         sql = "SELECT %s FROM view_slice_attributes" % \
58               ", ".join(SliceAttribute.fields)
59
60         if slice_attribute_id_list:
61             sql += " WHERE slice_attribute_id IN (%s)" % ", ".join(map(str, slice_attribute_id_list))
62
63         rows = self.api.db.selectall(sql)
64  
65         for row in rows:
66             self[row['slice_attribute_id']] = SliceAttribute(api, row)