3 # Thierry Parmentelat - INRIA
7 from types import StringTypes
9 from PLC.Faults import *
10 from PLC.Parameter import Parameter
11 from PLC.Filter import Filter
12 from PLC.Table import Row, Table
13 from PLC.Roles import Role, Roles
15 # xxx todo : deleting a tag type should delete the related nodegroup(s)
20 Representation of a row in the tag_types table.
23 table_name = 'tag_types'
24 primary_key = 'tag_type_id'
25 join_tables = ['node_tag', 'interface_tag', 'slice_tag' ]
27 'tag_type_id': Parameter(int, "Node tag type identifier"),
28 'tagname': Parameter(str, "Node tag type name", max = 100),
29 'description': Parameter(str, "Node tag type description", max = 254),
30 'category' : Parameter (str, "Node tag category", max=64, optional=True),
31 'min_role_id': Parameter(int, "Minimum (least powerful) role that can set or change this attribute"),
34 def validate_name(self, name):
36 raise PLCInvalidArgument, "node tag type name must be set"
38 conflicts = TagTypes(self.api, [name])
39 for tag_type in conflicts:
40 if 'tag_type_id' not in self or \
41 self['tag_type_id'] != tag_type['tag_type_id']:
42 raise PLCInvalidArgument, "node tag type name already in use"
46 def validate_min_role_id(self, role_id):
47 roles = [row['role_id'] for row in Roles(self.api)]
48 if role_id not in roles:
49 raise PLCInvalidArgument, "Invalid role"
53 class TagTypes(Table):
55 Representation of row(s) from the tag_types table
59 def __init__(self, api, tag_type_filter = None, columns = None):
60 Table.__init__(self, api, TagType, columns)
62 sql = "SELECT %s FROM tag_types WHERE True" % \
63 ", ".join(self.columns)
65 if tag_type_filter is not None:
66 if isinstance(tag_type_filter, (list, tuple, set)):
67 # Separate the list into integers and strings
68 ints = filter(lambda x: isinstance(x, (int, long)), tag_type_filter)
69 strs = filter(lambda x: isinstance(x, StringTypes), tag_type_filter)
70 tag_type_filter = Filter(TagType.fields, {'tag_type_id': ints, 'tagname': strs})
71 sql += " AND (%s) %s" % tag_type_filter.sql(api, "OR")
72 elif isinstance(tag_type_filter, dict):
73 tag_type_filter = Filter(TagType.fields, tag_type_filter)
74 sql += " AND (%s) %s" % tag_type_filter.sql(api, "AND")
75 elif isinstance (tag_type_filter, StringTypes):
76 tag_type_filter = Filter(TagType.fields, {'tagname':[tag_type_filter]})
77 sql += " AND (%s) %s" % tag_type_filter.sql(api, "AND")
79 raise PLCInvalidArgument, "Wrong node tag type filter %r"%tag_type_filter