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
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
21 Representation of a row in the pcus table. To use,
22 instantiate with a dict of values.
26 primary_key = 'pcu_id'
27 join_tables = ['pcu_node']
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 'last_updated': Parameter(int, "Date and time when node entry was created", ro = True),
43 def validate_ip(self, ip):
45 raise PLCInvalidArgument, "Invalid IP address " + ip
48 validate_last_updated = Row.validate_timestamp
50 def update_timestamp(self, col_name, commit = True):
52 Update col_name field with current time
55 assert 'pcu_id' in self
56 assert self.table_name
58 self.api.db.do("UPDATE %s SET %s = CURRENT_TIMESTAMP " % (self.table_name, col_name) + \
59 " where pcu_id = %d" % (self['pcu_id']) )
62 def update_last_updated(self, commit = True):
63 self.update_timestamp('last_updated', commit)
65 def add_node(self, node, port, commit = True):
67 Add node to existing PCU.
70 assert 'pcu_id' in self
71 assert isinstance(node, Node)
72 assert isinstance(port, (int, long))
73 assert 'node_id' in node
75 pcu_id = self['pcu_id']
76 node_id = node['node_id']
78 if node_id not in self['node_ids'] and port not in self['ports']:
79 self.api.db.do("INSERT INTO pcu_node (pcu_id, node_id, port)" \
80 " VALUES(%(pcu_id)d, %(node_id)d, %(port)d)",
86 self['node_ids'].append(node_id)
87 self['ports'].append(port)
89 def remove_node(self, node, commit = True):
91 Remove node from existing PCU.
94 assert 'pcu_id' in self
95 assert isinstance(node, Node)
96 assert 'node_id' in node
98 pcu_id = self['pcu_id']
99 node_id = node['node_id']
101 if node_id in self['node_ids']:
102 i = self['node_ids'].index(node_id)
103 port = self['ports'][i]
105 self.api.db.do("DELETE FROM pcu_node" \
106 " WHERE pcu_id = %(pcu_id)d" \
107 " AND node_id = %(node_id)d",
113 self['node_ids'].remove(node_id)
114 self['ports'].remove(port)
118 Representation of row(s) from the pcus table in the
122 def __init__(self, api, pcu_filter = None, columns = None):
123 Table.__init__(self, api, PCU, columns)
125 sql = "SELECT %s FROM view_pcus WHERE True" % \
126 ", ".join(self.columns)
128 if pcu_filter is not None:
129 if isinstance(pcu_filter, (list, tuple, set, int, long)):
130 pcu_filter = Filter(PCU.fields, {'pcu_id': pcu_filter})
131 elif isinstance(pcu_filter, dict):
132 pcu_filter = Filter(PCU.fields, pcu_filter)
134 raise PLCInvalidArgument, "Wrong pcu filter %r"%pcu_filter
135 sql += " AND (%s) %s" % pcu_filter.sql(api)