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.5 2006/10/24 20:02:22 mlhuang Exp $
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 from PLC.Nodes import Node, Nodes
19 Representation of a row in the pcus table. To use,
20 instantiate with a dict of values.
24 primary_key = 'pcu_id'
25 join_tables = ['pcu_node']
27 'pcu_id': Parameter(int, "PCU identifier"),
28 'site_id': Parameter(int, "Identifier of site where PCU is located"),
29 'hostname': Parameter(str, "PCU hostname", max = 254),
30 'ip': Parameter(str, "PCU IP address", max = 254),
31 'protocol': Parameter(str, "PCU protocol, e.g. ssh, https, telnet", max = 16),
32 'username': Parameter(str, "PCU username", max = 254),
33 'password': Parameter(str, "PCU username", max = 254),
34 'notes': Parameter(str, "Miscellaneous notes", max = 254),
35 'model': Parameter(str, "PCU model string", max = 32),
36 'node_ids': Parameter([int], "List of nodes that this PCU controls", ro = True),
37 'ports': Parameter([int], "List of the port numbers that each node is connected to", ro = True),
40 def validate_ip(self, ip):
42 raise PLCInvalidArgument, "Invalid IP address " + ip
45 def add_node(self, node, port, commit = True):
47 Add node to existing PCU.
50 assert 'pcu_id' in self
51 assert isinstance(node, Node)
52 assert isinstance(port, (int, long))
53 assert 'node_id' in node
55 pcu_id = self['pcu_id']
56 node_id = node['node_id']
58 if node_id not in self['node_ids'] and port not in self['ports']:
59 self.api.db.do("INSERT INTO pcu_node (pcu_id, node_id, port)" \
60 " VALUES(%(pcu_id)d, %(node_id)d, %(port)d)",
66 self['node_ids'].append(node_id)
67 self['ports'].append(port)
69 def remove_node(self, node, commit = True):
71 Remove node from existing PCU.
74 assert 'pcu_id' in self
75 assert isinstance(node, Node)
76 assert 'node_id' in node
78 pcu_id = self['pcu_id']
79 node_id = node['node_id']
81 if node_id in self['node_ids']:
82 i = self['node_ids'].index(node_id)
83 port = self['ports'][i]
85 self.api.db.do("DELETE FROM pcu_node" \
86 " WHERE pcu_id = %(pcu_id)d" \
87 " AND node_id = %(node_id)d",
93 self['node_ids'].remove(node_id)
94 self['ports'].remove(port)
98 Representation of row(s) from the pcus table in the
102 def __init__(self, api, pcu_ids = None):
105 # N.B.: Node IDs returned may be deleted.
106 sql = "SELECT %s FROM view_pcus" % \
107 ", ".join(PCU.fields)
110 sql += " WHERE pcu_id IN (%s)" % ", ".join(map(str, pcu_ids))
112 rows = self.api.db.selectall(sql)
115 self[row['pcu_id']] = pcu = PCU(api, row)
116 for aggregate in ['node_ids', 'ports']:
117 if not pcu.has_key(aggregate) or pcu[aggregate] is None:
120 pcu[aggregate] = map(int, pcu[aggregate].split(','))