first draft for ilinks : typed and valued links between interfaces
[plcapi.git] / PLC / Ilinks.py
diff --git a/PLC/Ilinks.py b/PLC/Ilinks.py
new file mode 100644 (file)
index 0000000..f6c9586
--- /dev/null
@@ -0,0 +1,53 @@
+#
+# Thierry Parmentelat - INRIA
+#
+# $Revision: 9423 $
+#
+from PLC.Faults import *
+from PLC.Parameter import Parameter
+from PLC.Filter import Filter
+from PLC.Table import Row, Table
+from PLC.Interfaces import Interface, Interfaces
+from PLC.IlinkTypes import IlinkType, IlinkTypes
+
+class Ilink(Row):
+    """
+    Representation of a row in the ilink table.
+    To use, instantiate with a dict of values.
+    """
+
+    table_name = 'ilink'
+    primary_key = 'ilink_id'
+    fields = {
+        'ilink_id': Parameter(int, "ilink identifier"),
+        'ilink_type_id': IlinkType.fields['ilink_type_id'],
+        'src_interface_id': Parameter(int, "source interface identifier"),
+        'dst_interface_id': Parameter(int, "destination interface identifier"),
+        'value': Parameter( str, "optional ilink value"),
+        }
+
+class Ilinks(Table):
+    """
+    Representation of row(s) from the ilink table in the
+    database.
+    """
+
+    def __init__(self, api, ilink_filter = None, columns = None):
+        Table.__init__(self, api, Ilink, columns)
+
+        sql = "SELECT %s FROM view_ilinks WHERE True" % \
+              ", ".join(self.columns)
+
+        if ilink_filter is not None:
+            if isinstance(ilink_filter, (list, tuple, set)):
+                ilink_filter = Filter(Ilink.fields, {'ilink_id': ilink_filter})
+            elif isinstance(ilink_filter, dict):
+                ilink_filter = Filter(Ilink.fields, ilink_filter)
+            elif isinstance(ilink_filter, int):
+                ilink_filter = Filter(Ilink.fields, {'ilink_id': [ilink_filter]})
+            else:
+                raise PLCInvalidArgument, "Wrong ilink filter %r"%ilink_filter
+            sql += " AND (%s) %s" % ilink_filter.sql(api)
+
+
+        self.selectall(sql)