persons get connected to slices. GetSlivers reports the right keys
[plcapi.git] / PLC / Slices.py
index 13e0842..234526f 100644 (file)
@@ -9,7 +9,6 @@ from PLC.Debug import profile
 from PLC.Table import Row, Table
 from PLC.SliceInstantiations import SliceInstantiations
 from PLC.Nodes import Node, Nodes
-from PLC.ForeignNodes import ForeignNode, ForeignNodes
 import PLC.Persons
 
 class Slice(Row):
@@ -25,6 +24,7 @@ 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),
@@ -37,6 +37,14 @@ class Slice(Row):
         '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),
         }
+    # for Cache
+    class_key = 'name'
+    foreign_fields = ['instantiation', 'url', 'description',
+                         'max_nodes', 'created', 'expires']
+    foreign_xrefs = { 
+       'Node' :   { 'field' : 'node_ids' , 'table': 'slice_node' },
+       'Person' : { 'field': 'person_ids', 'table' : 'slice_person'},
+    }
 
     def validate_name(self, name):
         # N.B.: Responsibility of the caller to ensure that login_base
@@ -53,14 +61,14 @@ class Slice(Row):
             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"
 
@@ -125,16 +133,13 @@ class Slice(Row):
             self['person_ids'].remove(person_id)
             person['slice_ids'].remove(slice_id)
 
-    def add_node(self, node, is_foreign_node = False, commit = True):
+    def add_node(self, node, commit = True):
         """
         Add node to existing slice.
         """
 
         assert 'slice_id' in self
-        if not is_foreign_node:
-            assert isinstance(node, Node)
-        else:
-            assert isinstance(node, ForeignNode)
+        assert isinstance(node, Node)
         assert 'node_id' in node
 
         slice_id = self['slice_id']
@@ -179,6 +184,7 @@ class Slice(Row):
             self['node_ids'].remove(node_id)
             node['slice_ids'].remove(slice_id)
 
+    ##########
     def sync(self, commit = True):
         """
         Add or update a slice.
@@ -186,7 +192,7 @@ class Slice(Row):
 
         # Before a new slice is added, delete expired slices
         if 'slice_id' not in self:
-            expired = Slices(self.api, expires = -int(time.time())).values()
+            expired = Slices(self.api, expires = -int(time.time()))
             for slice in expired:
                 slice.delete(commit)
 
@@ -215,11 +221,11 @@ class Slices(Table):
     database.
     """
 
-    def __init__(self, api, slice_filter = None, expires = int(time.time())):
-        Table.__init__(self, api, Slice)
+    def __init__(self, api, slice_filter = None, columns = None, expires = int(time.time())):
+        Table.__init__(self, api, Slice, columns)
 
         sql = "SELECT %s FROM view_slices WHERE is_deleted IS False" % \
-              ", ".join(Slice.fields)
+              ", ".join(self.columns)
 
         if expires is not None:
             if expires >= 0: