Cache:
[plcapi.git] / PLC / Slices.py
index 56c98d9..5e2b2e2 100644 (file)
@@ -24,7 +24,6 @@ class Slice(Row):
     fields = {
         'slice_id': Parameter(int, "Slice identifier"),
         'site_id': Parameter(int, "Identifier of the site to which this slice belongs"),
-        'peer_id': Parameter(int, "Peer at which this slice was created", nullok = True),
         'name': Parameter(str, "Slice name", max = 32),
         'instantiation': Parameter(str, "Slice instantiation state"),
         'url': Parameter(str, "URL further describing this slice", max = 254, nullok = True),
@@ -36,7 +35,20 @@ class Slice(Row):
         '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),
         'slice_attribute_ids': Parameter([int], "List of slice attributes", ro = True),
+        'peer_id': Parameter(int, "Peer at which this slice was created", nullok = True),
         }
+    # for Cache
+    class_key = 'name'
+    foreign_fields = ['instantiation', 'url', 'description', 'max_nodes', 'expires']
+    foreign_xrefs = [
+        {'field': 'node_ids' ,         'class': 'Node',   'table': 'slice_node' },
+       {'field': 'person_ids',        'class': 'Person', 'table': 'slice_person'},
+       {'field': 'creator_person_id', 'class': 'Person', 'table': 'unused-on-direct-refs'},
+        {'field': 'site_id',           'class': 'Site',   'table': 'unused-on-direct-refs'},
+    ]
+    # forget about this one, it is read-only anyway
+    # handling it causes Cache to re-sync all over again 
+    # 'created'
 
     def validate_name(self, name):
         # N.B.: Responsibility of the caller to ensure that login_base
@@ -55,7 +67,7 @@ class Slice(Row):
         conflicts = Slices(self.api, [name])
         for slice in conflicts:
             if 'slice_id' not in self or self['slice_id'] != slice['slice_id']:
-                raise PLCInvalidArgument, "Slice name already in use"
+                raise PLCInvalidArgument, "Slice name already in use, %s"%name
 
         return name
 
@@ -66,13 +78,14 @@ class Slice(Row):
 
         return instantiation
 
+    # timestamps
+    def validate_created (self, timestamp):
+       return self.validate_timestamp (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"
-
-        return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(expires))
+       return self.validate_timestamp (expires,True)
 
     def add_person(self, person, commit = True):
         """
@@ -176,36 +189,6 @@ class Slice(Row):
             self['node_ids'].remove(node_id)
             node['slice_ids'].remove(slice_id)
 
-    ########## for foreign slices update, from ForeignSlices
-    def purge_slice_node (self,commit=True):
-        sql = "DELETE FROM slice_node WHERE slice_id=%d"%self['slice_id']
-        self.api.db.do(sql)
-        if commit:
-            self.api.db.commit()
-
-    def add_slice_nodes (self, node_ids, commit=True):
-        slice_id = self['slice_id']
-        ### xxx needs to be optimized
-        ### tried to figure a way to use a single sql statement
-        ### like: insert into table (x,y) values (1,2),(3,4);
-        ### but apparently this is not supported under postgresql
-        for node_id in node_ids:
-            sql="INSERT INTO slice_node VALUES (%d,%d)"%(slice_id,node_id)
-            self.api.db.do(sql)
-        if commit:
-            self.api.db.commit()
-
-    def update_slice_nodes (self, node_ids):
-        # xxx to be optimized
-        # we could compute the (set) difference between
-        # current and updated set of node_ids
-        # and invoke the DB only based on that
-        #
-        # for now : clean all entries for this slice
-        self.purge_slice_node()
-        # and re-install new list
-        self.add_slice_nodes (node_ids)
-
     ##########
     def sync(self, commit = True):
         """