X-Git-Url: http://git.onelab.eu/?p=plcapi.git;a=blobdiff_plain;f=PLC%2FSlices.py;h=e5a51ebed58cf195c4293a4c06c8a54ae08743e3;hp=1b6ee9216084553b7f8ab7a5340b6fbf418a95fc;hb=13901cd4465288b634103c1997a5653500f2b5cc;hpb=d1aa90df9b9dd21d774b7b92ba966d06bb3d9f85 diff --git a/PLC/Slices.py b/PLC/Slices.py index 1b6ee92..e5a51eb 100644 --- a/PLC/Slices.py +++ b/PLC/Slices.py @@ -1,12 +1,17 @@ from types import StringTypes import time +import re from PLC.Faults import * -from PLC.Parameter import Parameter +from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.Debug import profile from PLC.Table import Row, Table -from PLC.SliceInstantiations import SliceInstantiations -import PLC.Persons +from PLC.SliceInstantiations import SliceInstantiation, SliceInstantiations +from PLC.Nodes import Node +from PLC.Persons import Person, Persons +from PLC.SliceTags import SliceTag +from PLC.Timestamp import Timestamp class Slice(Row): """ @@ -16,208 +21,214 @@ class Slice(Row): with a dict of values. """ + table_name = 'slices' + primary_key = 'slice_id' + join_tables = ['slice_node', 'slice_person', 'slice_tag', 'peer_slice', 'node_slice_whitelist', 'leases', ] fields = { - 'slice_id': Parameter(int, "Slice type"), + 'slice_id': Parameter(int, "Slice identifier"), 'site_id': Parameter(int, "Identifier of the site to which this slice belongs"), - 'name': Parameter(str, "Slice name", max = 32), + 'name': Parameter(str, "Slice name", max = 64), 'instantiation': Parameter(str, "Slice instantiation state"), - 'url': Parameter(str, "URL further describing this slice", max = 254), - 'description': Parameter(str, "Slice description", max = 2048), + 'url': Parameter(str, "URL further describing this slice", max = 254, nullok = True), + 'description': Parameter(str, "Slice description", max = 2048, nullok = True), 'max_nodes': Parameter(int, "Maximum number of nodes that can be assigned to this slice"), 'creator_person_id': Parameter(int, "Identifier of the account that created this slice"), 'created': Parameter(int, "Date and time when slice was created, in seconds since UNIX epoch", ro = True), 'expires': Parameter(int, "Date and time when slice expires, in seconds since UNIX epoch"), 'node_ids': Parameter([int], "List of nodes in this slice", ro = True), 'person_ids': Parameter([int], "List of accounts that can use this slice", ro = True), - 'attribute_ids': Parameter([int], "List of slice attributes", ro = True), + 'slice_tag_ids': Parameter([int], "List of slice attributes", ro = True), + 'peer_id': Parameter(int, "Peer to which this slice belongs", nullok = True), + 'peer_slice_id': Parameter(int, "Foreign slice identifier at peer", nullok = True), + } + related_fields = { + 'persons': [Mixed(Parameter(int, "Person identifier"), + Parameter(str, "Email address"))], + 'nodes': [Mixed(Parameter(int, "Node identifier"), + Parameter(str, "Fully qualified hostname"))] } - def __init__(self, api, fields): - Row.__init__(self, fields) - self.api = api + view_tags_name="view_slice_tags" + tags = {} def validate_name(self, name): # N.B.: Responsibility of the caller to ensure that login_base # portion of the slice name corresponds to a valid site, if # desired. + + # 1. Lowercase. + # 2. Begins with login_base (letters or numbers). + # 3. Then single underscore after login_base. + # 4. Then letters, numbers, or underscores. + good_name = r'^[a-z0-9\.]+_[a-zA-Z0-9_\.]+$' + if not name or \ + not re.match(good_name, name): + raise PLCInvalidArgument, "Invalid slice name" + conflicts = Slices(self.api, [name]) - for slice_id, slice in conflicts.iteritems(): - if 'slice_id' not in self or self['slice_id'] != slice_id: - raise PLCInvalidArgument, "Slice name already in use" + for slice in conflicts: + if 'slice_id' not in self or self['slice_id'] != slice['slice_id']: + raise PLCInvalidArgument, "Slice name already in use, %s"%name return name def validate_instantiation(self, instantiation): - instantiations = SliceInstantiations(self.api) + instantiations = [row['instantiation'] for row in SliceInstantiations(self.api)] if instantiation not in instantiations: raise PLCInvalidArgument, "No such instantiation state" - return state + return instantiation + + validate_created = Row.validate_timestamp def validate_expires(self, expires): # N.B.: Responsibility of the caller to ensure that expires is # not too far into the future. - if expires < time.time(): - raise PLCInvalidArgument, "Expiration date must be in the future" + check_future = not ('is_deleted' in self and self['is_deleted']) + return Timestamp.sql_validate( expires, check_future = check_future) - return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(expires)) + add_person = Row.add_object(Person, 'slice_person') + remove_person = Row.remove_object(Person, 'slice_person') - def validate_creator_person_id(self, person_id): - persons = PLC.Persons.Persons(self.api, [person_id]) - if not persons: - raise PLCInvalidArgument, "Invalid creator" + add_node = Row.add_object(Node, 'slice_node') + remove_node = Row.remove_object(Node, 'slice_node') - return person_id + add_to_node_whitelist = Row.add_object(Node, 'node_slice_whitelist') + delete_from_node_whitelist = Row.remove_object(Node, 'node_slice_whitelist') - def add_person(self, person, commit = True): + def associate_persons(self, auth, field, value): """ - Add person to existing slice. + Adds persons found in value list to this slice (using AddPersonToSlice). + Deletes persons not found in value list from this slice (using DeletePersonFromSlice). """ + assert 'person_ids' in self assert 'slice_id' in self - assert isinstance(person, PLC.Persons.Person) - assert 'person_id' in person + assert isinstance(value, list) - slice_id = self['slice_id'] - person_id = person['person_id'] - self.api.db.do("INSERT INTO slice_person (person_id, slice_id)" \ - " VALUES(%(person_id)d, %(slice_id)d)", - locals()) + (person_ids, emails) = self.separate_types(value)[0:2] - if commit: - self.api.db.commit() + # Translate emails into person_ids + if emails: + persons = Persons(self.api, emails, ['person_id']).dict('person_id') + person_ids += persons.keys() - if 'person_ids' in self and person_id not in self['person_ids']: - self['person_ids'].append(person_id) + # Add new ids, remove stale ids + if self['person_ids'] != person_ids: + from PLC.Methods.AddPersonToSlice import AddPersonToSlice + from PLC.Methods.DeletePersonFromSlice import DeletePersonFromSlice + new_persons = set(person_ids).difference(self['person_ids']) + stale_persons = set(self['person_ids']).difference(person_ids) - if 'slice_ids' in person and slice_id not in person['slice_ids']: - person['slice_ids'].append(slice_id) + for new_person in new_persons: + AddPersonToSlice.__call__(AddPersonToSlice(self.api), auth, new_person, self['slice_id']) + for stale_person in stale_persons: + DeletePersonFromSlice.__call__(DeletePersonFromSlice(self.api), auth, stale_person, self['slice_id']) - def remove_person(self, person, commit = True): + def associate_nodes(self, auth, field, value): """ - Remove person from existing slice. + Adds nodes found in value list to this slice (using AddSliceToNodes). + Deletes nodes not found in value list from this slice (using DeleteSliceFromNodes). """ - assert 'slice_id' in self - assert isinstance(person, PLC.Persons.Person) - assert 'person_id' in person - - slice_id = self['slice_id'] - person_id = person['person_id'] - self.api.db.do("DELETE FROM slice_person" \ - " WHERE person_id = %(person_id)d" \ - " AND slice_id = %(slice_id)d", - locals()) + from PLC.Nodes import Nodes - if commit: - self.api.db.commit() - - if 'person_ids' in self and person_id in self['person_ids']: - self['person_ids'].remove(person_id) - - if 'slice_ids' in person and slice_id in person['slice_ids']: - person['slice_ids'].remove(slice_id) - - def add_node(self, node, commit = True): + assert 'node_ids' in self + assert 'slice_id' in self + assert isinstance(value, list) + + (node_ids, hostnames) = self.separate_types(value)[0:2] + + # Translate hostnames into node_ids + if hostnames: + nodes = Nodes(self.api, hostnames, ['node_id']).dict('node_id') + node_ids += nodes.keys() + + # Add new ids, remove stale ids + if self['node_ids'] != node_ids: + from PLC.Methods.AddSliceToNodes import AddSliceToNodes + from PLC.Methods.DeleteSliceFromNodes import DeleteSliceFromNodes + new_nodes = set(node_ids).difference(self['node_ids']) + stale_nodes = set(self['node_ids']).difference(node_ids) + + if new_nodes: + AddSliceToNodes.__call__(AddSliceToNodes(self.api), auth, self['slice_id'], list(new_nodes)) + if stale_nodes: + DeleteSliceFromNodes.__call__(DeleteSliceFromNodes(self.api), auth, self['slice_id'], list(stale_nodes)) + def associate_slice_tags(self, auth, fields, value): """ - Add node to existing slice. + Deletes slice_tag_ids not found in value list (using DeleteSliceTag). + Adds slice_tags if slice_fields w/o slice_id is found (using AddSliceTag). + Updates slice_tag if slice_fields w/ slice_id is found (using UpdateSlceiAttribute). """ - assert 'slice_id' in self - assert isinstance(node, PLC.Nodes.Node) - assert 'node_id' in node + assert 'slice_tag_ids' in self + assert isinstance(value, list) - slice_id = self['slice_id'] - node_id = node['node_id'] - self.api.db.do("INSERT INTO slice_node (node_id, slice_id)" \ - " VALUES(%(node_id)d, %(slice_id)d)", - locals()) + (attribute_ids, blank, attributes) = self.separate_types(value) - if commit: - self.api.db.commit() + # There is no way to add attributes by id. They are + # associated with a slice when they are created. + # So we are only looking to delete here + if self['slice_tag_ids'] != attribute_ids: + from PLC.Methods.DeleteSliceTag import DeleteSliceTag + stale_attributes = set(self['slice_tag_ids']).difference(attribute_ids) - if 'node_ids' in self and node_id not in self['node_ids']: - self['node_ids'].append(node_id) + for stale_attribute in stale_attributes: + DeleteSliceTag.__call__(DeleteSliceTag(self.api), auth, stale_attribute['slice_tag_id']) - if 'slice_ids' in node and slice_id not in node['slice_ids']: - node['slice_ids'].append(slice_id) + # If dictionary exists, we are either adding new + # attributes or updating existing ones. + if attributes: + from PLC.Methods.AddSliceTag import AddSliceTag + from PLC.Methods.UpdateSliceTag import UpdateSliceTag - def remove_node(self, node, commit = True): - """ - Remove node from existing slice. - """ + added_attributes = filter(lambda x: 'slice_tag_id' not in x, attributes) + updated_attributes = filter(lambda x: 'slice_tag_id' in x, attributes) - assert 'slice_id' in self - assert isinstance(node, PLC.Nodes.Node) - assert 'node_id' in node + for added_attribute in added_attributes: + if 'tag_type' in added_attribute: + type = added_attribute['tag_type'] + elif 'tag_type_id' in added_attribute: + type = added_attribute['tag_type_id'] + else: + raise PLCInvalidArgument, "Must specify tag_type or tag_type_id" - slice_id = self['slice_id'] - node_id = node['node_id'] - self.api.db.do("DELETE FROM slice_node" \ - " WHERE node_id = %(node_id)d" \ - " AND slice_id = %(slice_id)d", - locals()) + if 'value' in added_attribute: + value = added_attribute['value'] + else: + raise PLCInvalidArgument, "Must specify a value" - if commit: - self.api.db.commit() + if 'node_id' in added_attribute: + node_id = added_attribute['node_id'] + else: + node_id = None - if 'node_ids' in self and node_id in self['node_ids']: - self['node_ids'].remove(node_id) + if 'nodegroup_id' in added_attribute: + nodegroup_id = added_attribute['nodegroup_id'] + else: + nodegroup_id = None - if 'slice_ids' in node and slice_id in node['slice_ids']: - node['slice_ids'].remove(slice_id) + AddSliceTag.__call__(AddSliceTag(self.api), auth, self['slice_id'], type, value, node_id, nodegroup_id) + for updated_attribute in updated_attributes: + attribute_id = updated_attribute.pop('slice_tag_id') + if attribute_id not in self['slice_tag_ids']: + raise PLCInvalidArgument, "Attribute doesnt belong to this slice" + else: + UpdateSliceTag.__call__(UpdateSliceTag(self.api), auth, attribute_id, updated_attribute) def sync(self, commit = True): """ - Flush changes back to the database. + Add or update a slice. """ - try: - if not self['name']: - raise KeyError - except KeyError: - raise PLCInvalidArgument, "Slice name must be specified" - - self.validate() - - # Fetch a new slice_id if necessary + # Before a new slice is added, delete expired slices if 'slice_id' not in self: - # N.B.: Responsibility of the caller to ensure that - # max_slices is not exceeded. - rows = self.api.db.selectall("SELECT NEXTVAL('slices_slice_id_seq') AS slice_id") - if not rows: - raise PLCDBError, "Unable to fetch new slice_id" - self['slice_id'] = rows[0]['slice_id'] - insert = True - else: - insert = False - - # Filter out fields that cannot be set or updated directly - slices_fields = self.api.db.fields('slices') - fields = dict(filter(lambda (key, value): \ - key in slices_fields and \ - (key not in self.fields or not self.fields[key].ro), - self.items())) - - # Parameterize for safety - keys = fields.keys() - values = [self.api.db.param(key, value) for (key, value) in fields.items()] - - if insert: - # Insert new row in slices table - sql = "INSERT INTO slices (%s) VALUES (%s)" % \ - (", ".join(keys), ", ".join(values)) - else: - # Update existing row in slices table - columns = ["%s = %s" % (key, value) for (key, value) in zip(keys, values)] - sql = "UPDATE slices SET " + \ - ", ".join(columns) + \ - " WHERE slice_id = %(slice_id)d" - - self.api.db.do(sql, fields) - - if commit: - self.api.db.commit() + expired = Slices(self.api, expires = -int(time.time())) + for slice in expired: + slice.delete(commit) + + Row.sync(self, commit) def delete(self, commit = True): """ @@ -227,46 +238,59 @@ class Slice(Row): assert 'slice_id' in self # Clean up miscellaneous join tables - for table in ['slice_node', 'slice_person', 'slice_attribute']: - self.api.db.do("DELETE FROM %s" \ - " WHERE slice_id = %d" % \ - (table, self['slice_id']), self) + for table in self.join_tables: + self.api.db.do("DELETE FROM %s WHERE slice_id = %d" % \ + (table, self['slice_id'])) # Mark as deleted self['is_deleted'] = True self.sync(commit) + class Slices(Table): """ Representation of row(s) from the slices table in the database. """ - def __init__(self, api, slice_id_or_name_list = None, fields = Slice.fields): - self.api = api - - sql = "SELECT %s FROM view_slices WHERE is_deleted IS False" % \ - ", ".join(fields) - - if slice_id_or_name_list: - # Separate the list into integers and strings - slice_ids = filter(lambda slice_id: isinstance(slice_id, (int, long)), - slice_id_or_name_list) - names = filter(lambda name: isinstance(name, StringTypes), - slice_id_or_name_list) - sql += " AND (False" - if slice_ids: - sql += " OR slice_id IN (%s)" % ", ".join(map(str, slice_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['slice_id']] = slice = Slice(api, row) - for aggregate in 'person_ids', 'slice_ids', 'attribute_ids': - if not slice.has_key(aggregate) or slice[aggregate] is None: - slice[aggregate] = [] - else: - slice[aggregate] = map(int, slice[aggregate].split(',')) + def __init__(self, api, slice_filter = None, columns = None, expires = int(time.time())): + Table.__init__(self, api, Slice, columns) + + # the view that we're selecting upon: start with view_slices + view = "view_slices" + # as many left joins as requested tags + for tagname in self.tag_columns: + view= "%s left join %s using (%s)"%(view,Slice.tagvalue_view_name(tagname), + Slice.primary_key) + + sql = "SELECT %s FROM %s WHERE is_deleted IS False" % \ + (", ".join(self.columns.keys()+self.tag_columns.keys()),view) + + if expires is not None: + if expires >= 0: + sql += " AND expires > %d" % expires + else: + expires = -expires + sql += " AND expires < %d" % expires + + if slice_filter is not None: + if isinstance(slice_filter, (list, tuple, set)): + # Separate the list into integers and strings + ints = filter(lambda x: isinstance(x, (int, long)), slice_filter) + strs = filter(lambda x: isinstance(x, StringTypes), slice_filter) + slice_filter = Filter(Slice.fields, {'slice_id': ints, 'name': strs}) + sql += " AND (%s) %s" % slice_filter.sql(api, "OR") + elif isinstance(slice_filter, dict): + allowed_fields=dict(Slice.fields.items()+Slice.tags.items()) + slice_filter = Filter(allowed_fields, slice_filter) + sql += " AND (%s) %s" % slice_filter.sql(api, "AND") + elif isinstance (slice_filter, StringTypes): + slice_filter = Filter(Slice.fields, {'name':slice_filter}) + sql += " AND (%s) %s" % slice_filter.sql(api, "AND") + elif isinstance (slice_filter, (int, long)): + slice_filter = Filter(Slice.fields, {'slice_id':slice_filter}) + sql += " AND (%s) %s" % slice_filter.sql(api, "AND") + else: + raise PLCInvalidArgument, "Wrong slice filter %r"%slice_filter + + self.selectall(sql)