svn keywords
[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$
8 # $URL$
9 #
10
11 from PLC.Faults import *
12 from PLC.Parameter import Parameter
13 from PLC.Filter import Filter
14 from PLC.Debug import profile
15 from PLC.Table import Row, Table
16 from PLC.Interfaces import valid_ip, Interface, Interfaces
17 from PLC.Nodes import Node, Nodes
18
19 class PCU(Row):
20     """
21     Representation of a row in the pcus table. To use,
22     instantiate with a dict of values.
23     """
24
25     table_name = 'pcus'
26     primary_key = 'pcu_id'
27     join_tables = ['pcu_node']
28     fields = {
29         'pcu_id': Parameter(int, "PCU identifier"),
30         'site_id': Parameter(int, "Identifier of site where PCU is located"),
31         'hostname': Parameter(str, "PCU hostname", max = 254),
32         'ip': Parameter(str, "PCU IP address", max = 254),
33         'protocol': Parameter(str, "PCU protocol, e.g. ssh, https, telnet", max = 16, nullok = True),
34         'username': Parameter(str, "PCU username", max = 254, nullok = True),
35         'password': Parameter(str, "PCU username", max = 254, nullok = True),
36         'notes': Parameter(str, "Miscellaneous notes", max = 254, nullok = True),
37         'model': Parameter(str, "PCU model string", max = 32, nullok = True),
38         'node_ids': Parameter([int], "List of nodes that this PCU controls"),
39         'ports': Parameter([int], "List of the port numbers that each node is connected to"),
40         }
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 add_node(self, node, port, commit = True):
48         """
49         Add node to existing PCU.
50         """
51
52         assert 'pcu_id' in self
53         assert isinstance(node, Node)
54         assert isinstance(port, (int, long))
55         assert 'node_id' in node
56
57         pcu_id = self['pcu_id']
58         node_id = node['node_id']
59
60         if node_id not in self['node_ids'] and port not in self['ports']:
61             self.api.db.do("INSERT INTO pcu_node (pcu_id, node_id, port)" \
62                            " VALUES(%(pcu_id)d, %(node_id)d, %(port)d)",
63                            locals())
64
65             if commit:
66                 self.api.db.commit()
67
68             self['node_ids'].append(node_id)
69             self['ports'].append(port)
70
71     def remove_node(self, node, commit = True):
72         """
73         Remove node from existing PCU.
74         """
75
76         assert 'pcu_id' in self
77         assert isinstance(node, Node)
78         assert 'node_id' in node
79
80         pcu_id = self['pcu_id']
81         node_id = node['node_id']
82
83         if node_id in self['node_ids']:
84             i = self['node_ids'].index(node_id)
85             port = self['ports'][i]
86
87             self.api.db.do("DELETE FROM pcu_node" \
88                            " WHERE pcu_id = %(pcu_id)d" \
89                            " AND node_id = %(node_id)d",
90                            locals())
91
92             if commit:
93                 self.api.db.commit()
94
95             self['node_ids'].remove(node_id)
96             self['ports'].remove(port)
97
98 class PCUs(Table):
99     """
100     Representation of row(s) from the pcus table in the
101     database.
102     """
103
104     def __init__(self, api, pcu_filter = None, columns = None):
105         Table.__init__(self, api, PCU, columns)
106
107         sql = "SELECT %s FROM view_pcus WHERE True" % \
108               ", ".join(self.columns)
109
110         if pcu_filter is not None:
111             if isinstance(pcu_filter, (list, tuple, set)):
112                 pcu_filter = Filter(PCU.fields, {'pcu_id': pcu_filter})
113             elif isinstance(pcu_filter, dict):
114                 pcu_filter = Filter(PCU.fields, pcu_filter)
115             sql += " AND (%s) %s" % pcu_filter.sql(api)
116
117         self.selectall(sql)