1 from types import StringTypes
5 from PLC.Faults import *
6 from PLC.Parameter import Parameter
7 from PLC.Filter import Filter
8 from PLC.Debug import profile
9 from PLC.Table import Row, Table
10 from PLC.SliceInstantiations import SliceInstantiation, SliceInstantiations
11 from PLC.Nodes import Node, Nodes
12 from PLC.Persons import Person, Persons
16 Representation of a row in the slices table. To use, optionally
17 instantiate with a dict of values. Update as you would a
18 dict. Commit to the database with sync().To use, instantiate
19 with a dict of values.
23 primary_key = 'slice_id'
24 join_tables = ['slice_node', 'slice_person', 'slice_attribute', 'peer_slice']
26 'slice_id': Parameter(int, "Slice identifier"),
27 'site_id': Parameter(int, "Identifier of the site to which this slice belongs"),
28 'name': Parameter(str, "Slice name", max = 32),
29 'instantiation': Parameter(str, "Slice instantiation state"),
30 'url': Parameter(str, "URL further describing this slice", max = 254, nullok = True),
31 'description': Parameter(str, "Slice description", max = 2048, nullok = True),
32 'max_nodes': Parameter(int, "Maximum number of nodes that can be assigned to this slice"),
33 'creator_person_id': Parameter(int, "Identifier of the account that created this slice"),
34 'created': Parameter(int, "Date and time when slice was created, in seconds since UNIX epoch", ro = True),
35 'expires': Parameter(int, "Date and time when slice expires, in seconds since UNIX epoch"),
36 'node_ids': Parameter([int], "List of nodes in this slice", ro = True),
37 'person_ids': Parameter([int], "List of accounts that can use this slice", ro = True),
38 'slice_attribute_ids': Parameter([int], "List of slice attributes", ro = True),
39 'peer_id': Parameter(int, "Peer to which this slice belongs", nullok = True),
40 'peer_slice_id': Parameter(int, "Foreign slice identifier at peer", nullok = True),
44 foreign_fields = ['instantiation', 'url', 'description', 'max_nodes', 'expires']
46 {'field': 'node_ids' , 'class': 'Node', 'table': 'slice_node' },
47 {'field': 'person_ids', 'class': 'Person', 'table': 'slice_person'},
48 {'field': 'creator_person_id', 'class': 'Person', 'table': 'unused-on-direct-refs'},
49 {'field': 'site_id', 'class': 'Site', 'table': 'unused-on-direct-refs'},
51 # forget about this one, it is read-only anyway
52 # handling it causes Cache to re-sync all over again
55 def validate_name(self, name):
56 # N.B.: Responsibility of the caller to ensure that login_base
57 # portion of the slice name corresponds to a valid site, if
61 # 2. Begins with login_base (letters or numbers).
62 # 3. Then single underscore after login_base.
63 # 4. Then letters, numbers, or underscores.
64 good_name = r'^[a-z0-9]+_[a-z0-9_]+$'
66 not re.match(good_name, name):
67 raise PLCInvalidArgument, "Invalid slice name"
69 conflicts = Slices(self.api, [name])
70 for slice in conflicts:
71 if 'slice_id' not in self or self['slice_id'] != slice['slice_id']:
72 raise PLCInvalidArgument, "Slice name already in use, %s"%name
76 def validate_instantiation(self, instantiation):
77 instantiations = [row['instantiation'] for row in SliceInstantiations(self.api)]
78 if instantiation not in instantiations:
79 raise PLCInvalidArgument, "No such instantiation state"
83 validate_created = Row.validate_timestamp
85 def validate_expires(self, expires):
86 # N.B.: Responsibility of the caller to ensure that expires is
87 # not too far into the future.
88 check_future = not ('is_deleted' in self and self['is_deleted'])
89 return Row.validate_timestamp(self, expires, check_future = check_future)
91 add_person = Row.add_object(Person, 'slice_person')
92 remove_person = Row.remove_object(Person, 'slice_person')
94 add_node = Row.add_object(Node, 'slice_node')
95 remove_node = Row.remove_object(Node, 'slice_node')
97 def sync(self, commit = True):
99 Add or update a slice.
102 # Before a new slice is added, delete expired slices
103 if 'slice_id' not in self:
104 expired = Slices(self.api, expires = -int(time.time()))
105 for slice in expired:
108 Row.sync(self, commit)
110 def delete(self, commit = True):
112 Delete existing slice.
115 assert 'slice_id' in self
117 # Clean up miscellaneous join tables
118 for table in self.join_tables:
119 self.api.db.do("DELETE FROM %s WHERE slice_id = %d" % \
120 (table, self['slice_id']))
123 self['is_deleted'] = True
128 Representation of row(s) from the slices table in the
132 def __init__(self, api, slice_filter = None, columns = None, expires = int(time.time())):
133 Table.__init__(self, api, Slice, columns)
135 sql = "SELECT %s FROM view_slices WHERE is_deleted IS False" % \
136 ", ".join(self.columns)
138 if expires is not None:
140 sql += " AND expires > %(expires)d"
143 sql += " AND expires < %(expires)d"
145 if slice_filter is not None:
146 if isinstance(slice_filter, (list, tuple, set)):
147 # Separate the list into integers and strings
148 ints = filter(lambda x: isinstance(x, (int, long)), slice_filter)
149 strs = filter(lambda x: isinstance(x, StringTypes), slice_filter)
150 slice_filter = Filter(Slice.fields, {'slice_id': ints, 'name': strs})
151 sql += " AND (%s)" % slice_filter.sql(api, "OR")
152 elif isinstance(slice_filter, dict):
153 slice_filter = Filter(Slice.fields, slice_filter)
154 sql += " AND (%s)" % slice_filter.sql(api, "AND")
156 self.selectall(sql, locals())