From 285a644f392d444e159144604ddacef819674ba1 Mon Sep 17 00:00:00 2001 From: Mark Huang Date: Tue, 3 Oct 2006 19:32:41 +0000 Subject: [PATCH] - attribute types and attribute settings --- PLC/Attributes.py | 91 ++++++++++++++++++++++++++++++++++++++++++ PLC/SliceAttributes.py | 65 ++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 PLC/Attributes.py create mode 100644 PLC/SliceAttributes.py diff --git a/PLC/Attributes.py b/PLC/Attributes.py new file mode 100644 index 0000000..5bd3cf1 --- /dev/null +++ b/PLC/Attributes.py @@ -0,0 +1,91 @@ +from types import StringTypes + +from PLC.Faults import * +from PLC.Parameter import Parameter +from PLC.Table import Row, Table +from PLC.Roles import Roles + +class Attribute(Row): + """ + Representation of a row in the attributes table. To use, instantiate + with a dict of values. + """ + + table_name = 'attributes' + primary_key = 'attribute_id' + fields = { + 'attribute_id': Parameter(int, "Attribute identifier"), + 'name': Parameter(str, "Attribute name", max = 100), + 'description': Parameter(str, "Attribute description", max = 254), + 'min_role_id': Parameter(int, "Minimum (least powerful) role that can set or change this attribute"), + } + + def __init__(self, api, fields = {}): + Row.__init__(self, fields) + self.api = api + + def validate_name(self, name): + name = name.strip() + + if not name: + raise PLCInvalidArgument, "Attribute name must be set" + + conflicts = Attributes(self.api, [name]) + for attribute_id, attribute in conflicts.iteritems(): + if 'attribute_id' not in self or self['attribute_id'] != attribute_id: + raise PLCInvalidArgument, "Attribute name already in use" + + return name + + def validate_min_role_id(self, role_id): + roles = Roles(self.api) + if role_id not in roles: + raise PLCInvalidArgument, "Invalid role" + + return role_id + + def delete(self, commit = True): + """ + Delete existing slice attribute type. + """ + + assert 'attribute_id' in self + + # Clean up miscellaneous join tables + for table in ['attributes', 'slice_attribute']: + self.api.db.do("DELETE FROM %s" \ + " WHERE attribute_id = %d" % \ + (table, self['attribute_id']), self) + + if commit: + self.api.db.commit() + +class Attributes(Table): + """ + Representation of row(s) from the attributes table in the + database. + """ + + def __init__(self, api, attribute_id_or_name_list = None): + self.api = api + + sql = "SELECT %s FROM attributes" % \ + ", ".join(Attribute.fields) + + if attribute_id_or_name_list: + # Separate the list into integers and strings + attribute_ids = filter(lambda attribute_id: isinstance(attribute_id, (int, long)), + attribute_id_or_name_list) + names = filter(lambda name: isinstance(name, StringTypes), + attribute_id_or_name_list) + sql += " WHERE (False" + if attribute_ids: + sql += " OR attribute_id IN (%s)" % ", ".join(map(str, attribute_ids)) + if names: + sql += " OR name IN (%s)" % ", ".join(api.db.quote(names)) + sql += ")" + + rows = self.api.db.selectall(sql) + + for row in rows: + self[row['attribute_id']] = Attribute(api, row) diff --git a/PLC/SliceAttributes.py b/PLC/SliceAttributes.py new file mode 100644 index 0000000..bf51c29 --- /dev/null +++ b/PLC/SliceAttributes.py @@ -0,0 +1,65 @@ +from types import StringTypes + +from PLC.Faults import * +from PLC.Parameter import Parameter +from PLC.Table import Row, Table +from PLC.Attributes import Attribute, Attributes + +class SliceAttribute(Row): + """ + Representation of a row in the slice_attribute table. To use, + instantiate with a dict of values. + """ + + table_name = 'slice_attribute' + primary_key = 'slice_attribute_id' + fields = { + 'slice_attribute_id': Parameter(int, "Slice attribute identifier"), + 'slice_id': Parameter(int, "Slice identifier"), + 'node_id': Parameter(int, "Node identifier, if a sliver attribute"), + 'attribute_id': Attribute.fields['attribute_id'], + 'name': Attribute.fields['name'], + 'description': Attribute.fields['description'], + 'min_role_id': Attribute.fields['min_role_id'], + # XXX Arbitrary max, make configurable + 'value': Parameter(str, "Slice attribute value", max = 254), + } + + def __init__(self, api, fields = {}): + Row.__init__(self, fields) + self.api = api + + def delete(self, commit = True): + """ + Delete existing slice attribute. + """ + + assert 'slice_attribute_id' in self + + # Clean up miscellaneous join tables + for table in 'slice_attribute',: + self.api.db.do("DELETE FROM %s" \ + " WHERE slice_attribute_id = %d" % \ + (table, self['slice_attribute_id']), self) + + if commit: + self.api.db.commit() + +class SliceAttributes(dict): + """ + Representation of row(s) from the slice_attribute table in the + database. + """ + + def __init__(self, api, slice_attribute_id_list): + self.api = api + + sql = "SELECT %s FROM view_slice_attributes" % \ + ", ".join(SliceAttribute.fields) + + sql += " WHERE slice_attribute_id IN (%s)" % ", ".join(map(str, slice_attribute_id_list)) + + rows = self.api.db.selectall(sql) + + for row in rows: + self[row['slice_attribute_id']] = SliceAttribute(api, row) -- 2.43.0