2 # Functions for interacting with the pcus table in the database
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
7 # $Id: PCUs.py,v 1.9 2006/11/09 03:07:42 mlhuang Exp $
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Filter import Filter
13 from PLC.Debug import profile
14 from PLC.Table import Row, Table
15 from PLC.NodeNetworks import valid_ip, NodeNetwork, NodeNetworks
16 from PLC.Nodes import Node, Nodes
20 Representation of a row in the pcus table. To use,
21 instantiate with a dict of values.
25 primary_key = 'pcu_id'
26 join_tables = ['pcu_node']
28 'pcu_id': Parameter(int, "PCU identifier"),
29 'site_id': Parameter(int, "Identifier of site where PCU is located"),
30 'hostname': Parameter(str, "PCU hostname", max = 254),
31 'ip': Parameter(str, "PCU IP address", max = 254),
32 'protocol': Parameter(str, "PCU protocol, e.g. ssh, https, telnet", max = 16, nullok = True),
33 'username': Parameter(str, "PCU username", max = 254, nullok = True),
34 'password': Parameter(str, "PCU username", max = 254, nullok = True),
35 'notes': Parameter(str, "Miscellaneous notes", max = 254, nullok = True),
36 'model': Parameter(str, "PCU model string", max = 32, nullok = True),
37 'node_ids': Parameter([int], "List of nodes that this PCU controls"),
38 'ports': Parameter([int], "List of the port numbers that each node is connected to"),
41 def validate_ip(self, ip):
43 raise PLCInvalidArgument, "Invalid IP address " + ip
46 def add_node(self, node, port, commit = True):
48 Add node to existing PCU.
51 assert 'pcu_id' in self
52 assert isinstance(node, Node)
53 assert isinstance(port, (int, long))
54 assert 'node_id' in node
56 pcu_id = self['pcu_id']
57 node_id = node['node_id']
59 if node_id not in self['node_ids'] and port not in self['ports']:
60 self.api.db.do("INSERT INTO pcu_node (pcu_id, node_id, port)" \
61 " VALUES(%(pcu_id)d, %(node_id)d, %(port)d)",
67 self['node_ids'].append(node_id)
68 self['ports'].append(port)
70 def remove_node(self, node, commit = True):
72 Remove node from existing PCU.
75 assert 'pcu_id' in self
76 assert isinstance(node, Node)
77 assert 'node_id' in node
79 pcu_id = self['pcu_id']
80 node_id = node['node_id']
82 if node_id in self['node_ids']:
83 i = self['node_ids'].index(node_id)
84 port = self['ports'][i]
86 self.api.db.do("DELETE FROM pcu_node" \
87 " WHERE pcu_id = %(pcu_id)d" \
88 " AND node_id = %(node_id)d",
94 self['node_ids'].remove(node_id)
95 self['ports'].remove(port)
99 Representation of row(s) from the pcus table in the
103 def __init__(self, api, pcu_filter = None, columns = None):
104 Table.__init__(self, api, PCU, columns)
106 sql = "SELECT %s FROM view_pcus WHERE True" % \
107 ", ".join(self.columns)
109 if pcu_filter is not None:
110 if isinstance(pcu_filter, (list, tuple, set)):
111 pcu_filter = Filter(PCU.fields, {'pcu_id': pcu_filter})
112 elif isinstance(pcu_filter, dict):
113 pcu_filter = Filter(PCU.fields, pcu_filter)
114 sql += " AND (%s)" % pcu_filter.sql(api)