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"),
42 def validate_ip(self, ip):
44 raise PLCInvalidArgument, "Invalid IP address " + ip
47 def add_node(self, node, port, commit = True):
49 Add node to existing PCU.
52 assert 'pcu_id' in self
53 assert isinstance(node, Node)
54 assert isinstance(port, (int, long))
55 assert 'node_id' in node
57 pcu_id = self['pcu_id']
58 node_id = node['node_id']
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)",
68 self['node_ids'].append(node_id)
69 self['ports'].append(port)
71 def remove_node(self, node, commit = True):
73 Remove node from existing PCU.
76 assert 'pcu_id' in self
77 assert isinstance(node, Node)
78 assert 'node_id' in node
80 pcu_id = self['pcu_id']
81 node_id = node['node_id']
83 if node_id in self['node_ids']:
84 i = self['node_ids'].index(node_id)
85 port = self['ports'][i]
87 self.api.db.do("DELETE FROM pcu_node" \
88 " WHERE pcu_id = %(pcu_id)d" \
89 " AND node_id = %(node_id)d",
95 self['node_ids'].remove(node_id)
96 self['ports'].remove(port)
100 Representation of row(s) from the pcus table in the
104 def __init__(self, api, pcu_filter = None, columns = None):
105 Table.__init__(self, api, PCU, columns)
107 sql = "SELECT %s FROM view_pcus WHERE True" % \
108 ", ".join(self.columns)
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)