- remove optional sub-parameter; we use these fields in both Add() and
[plcapi.git] / PLC / PCUs.py
index 155ac9a..57ec44c 100644 (file)
@@ -4,7 +4,7 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id: PCUs.py,v 1.1 2006/09/06 15:36:07 mlhuang Exp $
+# $Id: PCUs.py,v 1.5 2006/10/24 20:02:22 mlhuang Exp $
 #
 
 from PLC.Faults import *
@@ -12,6 +12,7 @@ from PLC.Parameter import Parameter
 from PLC.Debug import profile
 from PLC.Table import Row, Table
 from PLC.NodeNetworks import valid_ip, NodeNetwork, NodeNetworks
+from PLC.Nodes import Node, Nodes
 
 class PCU(Row):
     """
@@ -21,6 +22,7 @@ class PCU(Row):
 
     table_name = 'pcus'
     primary_key = 'pcu_id'
+    join_tables = ['pcu_node']
     fields = {
         'pcu_id': Parameter(int, "PCU identifier"),
         'site_id': Parameter(int, "Identifier of site where PCU is located"),
@@ -35,30 +37,61 @@ class PCU(Row):
         'ports': Parameter([int], "List of the port numbers that each node is connected to", ro = True),
         }
 
-    def __init__(self, api, fields = {}):
-        Row.__init__(self, fields)
-        self.api = api
-
     def validate_ip(self, ip):
         if not valid_ip(ip):
             raise PLCInvalidArgument, "Invalid IP address " + ip
         return ip
 
-    def delete(self, commit = True):
+    def add_node(self, node, port, commit = True):
+        """
+        Add node to existing PCU.
+        """
+
+        assert 'pcu_id' in self
+        assert isinstance(node, Node)
+        assert isinstance(port, (int, long))
+        assert 'node_id' in node
+
+        pcu_id = self['pcu_id']
+        node_id = node['node_id']
+
+        if node_id not in self['node_ids'] and port not in self['ports']:
+            self.api.db.do("INSERT INTO pcu_node (pcu_id, node_id, port)" \
+                           " VALUES(%(pcu_id)d, %(node_id)d, %(port)d)",
+                           locals())
+
+            if commit:
+                self.api.db.commit()
+
+            self['node_ids'].append(node_id)
+            self['ports'].append(port)
+
+    def remove_node(self, node, commit = True):
         """
-        Delete existing PCU.
+        Remove node from existing PCU.
         """
 
         assert 'pcu_id' in self
+        assert isinstance(node, Node)
+        assert 'node_id' in node
+
+        pcu_id = self['pcu_id']
+        node_id = node['node_id']
+
+        if node_id in self['node_ids']:
+            i = self['node_ids'].index(node_id)
+            port = self['ports'][i]
+
+            self.api.db.do("DELETE FROM pcu_node" \
+                           " WHERE pcu_id = %(pcu_id)d" \
+                           " AND node_id = %(node_id)d",
+                           locals())
 
-        # Clean up various join tables
-        for table in ['pcu_node', 'pcus']:
-            self.api.db.do("DELETE FROM " + table +
-                           " WHERE pcu_id = %(pcu_id)d",
-                           self)
+            if commit:
+                self.api.db.commit()
 
-        if commit:
-            self.api.db.commit()
+            self['node_ids'].remove(node_id)
+            self['ports'].remove(port)
 
 class PCUs(Table):
     """
@@ -80,7 +113,7 @@ class PCUs(Table):
 
         for row in rows:
             self[row['pcu_id']] = pcu = PCU(api, row)
-            for aggregate in ['pcu_ids', 'ports']:
+            for aggregate in ['node_ids', 'ports']:
                 if not pcu.has_key(aggregate) or pcu[aggregate] is None:
                     pcu[aggregate] = []
                 else: