Merge from trunk
[plcapi.git] / trunk / PLC / SliceInstantiations.py
diff --git a/trunk/PLC/SliceInstantiations.py b/trunk/PLC/SliceInstantiations.py
new file mode 100644 (file)
index 0000000..4996a1e
--- /dev/null
@@ -0,0 +1,53 @@
+#
+# Functions for interacting with the slice_instantiations table in the database
+#
+# Mark Huang <mlhuang@cs.princeton.edu>
+# Copyright (C) 2006 The Trustees of Princeton University
+#
+# $Id$
+#
+
+from PLC.Faults import *
+from PLC.Parameter import Parameter
+from PLC.Table import Row, Table
+
+class SliceInstantiation(Row):
+    """
+    Representation of a row in the slice_instantiations table. To use,
+    instantiate with a dict of values.
+    """
+
+    table_name = 'slice_instantiations'
+    primary_key = 'instantiation'
+    join_tables = ['slices']
+    fields = {
+        'instantiation': Parameter(str, "Slice instantiation state", max = 100),
+        }
+
+    def validate_instantiation(self, instantiation):
+       # Make sure name is not blank
+        if not len(instantiation):
+            raise PLCInvalidArgument, "Slice instantiation state name must be specified"
+       
+       # Make sure slice instantiation does not alredy exist
+       conflicts = SliceInstantiations(self.api, [instantiation])
+        if conflicts:
+            raise PLCInvalidArgument, "Slice instantiation state name already in use"
+
+       return instantiation
+        
+class SliceInstantiations(Table):
+    """
+    Representation of the slice_instantiations table in the database.
+    """
+
+    def __init__(self, api, instantiations = None):
+        Table.__init__(self, api, SliceInstantiation)
+
+        sql = "SELECT %s FROM slice_instantiations" % \
+              ", ".join(SliceInstantiation.fields)
+        
+        if instantiations:
+            sql += " WHERE instantiation IN (%s)" % ", ".join(map(api.db.quote, instantiations))
+
+        self.selectall(sql)