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