add interface to pcus
[plcapi.git] / PLC / PCUs.py
1 #
2 # Functions for interacting with the pcus table in the database
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7 # $Id: PCUs.py,v 1.1 2006/09/06 15:36:07 mlhuang Exp $
8 #
9
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Debug import profile
13 from PLC.Table import Row, Table
14 from PLC.NodeNetworks import valid_ip, NodeNetwork, NodeNetworks
15
16 class PCU(Row):
17     """
18     Representation of a row in the pcus table. To use,
19     instantiate with a dict of values.
20     """
21
22     table_name = 'pcus'
23     primary_key = 'pcu_id'
24     fields = {
25         'pcu_id': Parameter(int, "PCU identifier"),
26         'site_id': Parameter(int, "Identifier of site where PCU is located"),
27         'hostname': Parameter(str, "PCU hostname", max = 254),
28         'ip': Parameter(str, "PCU IP address", max = 254),
29         'protocol': Parameter(str, "PCU protocol, e.g. ssh, https, telnet", max = 16),
30         'username': Parameter(str, "PCU username", max = 254),
31         'password': Parameter(str, "PCU username", max = 254),
32         'notes': Parameter(str, "Miscellaneous notes", max = 254),
33         'model': Parameter(str, "PCU model string", max = 32),
34         'node_ids': Parameter([int], "List of nodes that this PCU controls", ro = True),
35         'ports': Parameter([int], "List of the port numbers that each node is connected to", ro = True),
36         }
37
38     def __init__(self, api, fields = {}):
39         Row.__init__(self, fields)
40         self.api = api
41
42     def validate_ip(self, ip):
43         if not valid_ip(ip):
44             raise PLCInvalidArgument, "Invalid IP address " + ip
45         return ip
46
47     def delete(self, commit = True):
48         """
49         Delete existing PCU.
50         """
51
52         assert 'pcu_id' in self
53
54         # Clean up various join tables
55         for table in ['pcu_node', 'pcus']:
56             self.api.db.do("DELETE FROM " + table +
57                            " WHERE pcu_id = %(pcu_id)d",
58                            self)
59
60         if commit:
61             self.api.db.commit()
62
63 class PCUs(Table):
64     """
65     Representation of row(s) from the pcus table in the
66     database.
67     """
68
69     def __init__(self, api, pcu_ids = None):
70         self.api = api
71
72         # N.B.: Node IDs returned may be deleted.
73         sql = "SELECT %s FROM view_pcus" % \
74               ", ".join(PCU.fields)
75
76         if pcu_ids:
77             sql += " WHERE pcu_id IN (%s)" % ", ".join(map(str, pcu_ids))
78
79         rows = self.api.db.selectall(sql)
80
81         for row in rows:
82             self[row['pcu_id']] = pcu = PCU(api, row)
83             for aggregate in ['pcu_ids', 'ports']:
84                 if not pcu.has_key(aggregate) or pcu[aggregate] is None:
85                     pcu[aggregate] = []
86                 else:
87                     pcu[aggregate] = map(int, pcu[aggregate].split(','))