# # Functions for interacting with the pcus table in the database # # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # from PLC.Faults import * from PLC.Parameter import Parameter from PLC.Debug import profile from PLC.Storage.AlchemyObject import AlchemyObj from PLC.Interfaces import valid_ip, Interface, Interfaces from PLC.Nodes import Node, Nodes class PCU(AlchemyObj): """ Representation of a row in the pcus table. To use, instantiate with a dict of values. """ tablename = 'pcus' join_tables = ['pcu_node'] fields = { 'pcu_id': Parameter(int, "PCU identifier", primary_key=True), 'site_id': Parameter(int, "Identifier of site where PCU is located"), 'hostname': Parameter(str, "PCU hostname", max = 254), 'ip': Parameter(str, "PCU IP address", max = 254), 'protocol': Parameter(str, "PCU protocol, e.g. ssh, https, telnet", max = 16, nullok = True), 'username': Parameter(str, "PCU username", max = 254, nullok = True), 'password': Parameter(str, "PCU username", max = 254, nullok = True), 'notes': Parameter(str, "Miscellaneous notes", max = 254, nullok = True), 'model': Parameter(str, "PCU model string", max = 32, nullok = True), 'node_ids': Parameter([int], "List of nodes that this PCU controls", joined=True), 'ports': Parameter([int], "List of the port numbers that each node is connected to", joined=True), 'last_updated': Parameter(int, "Date and time when node entry was created"), } def validate_ip(self, ip): if not valid_ip(ip): raise PLCInvalidArgument, "Invalid IP address " + ip return ip validate_last_updated = AlchemyObj.validate_timestamp def update_timestamp(self, col_name, commit = True): """ Update col_name field with current time """ pass def update_last_updated(self, commit = True): self.update_timestamp('last_updated', commit) def add_node(self, node, port, commit = True): """ Add node to existing PCU. """ assert 'pcu_id' in self assert isinstance(node, Node) assert isinstance(port, (int, long)) assert 'node_id' in node pcu_id = self['pcu_id'] node_id = node['node_id'] if node_id not in self['node_ids'] and port not in self['ports']: self.api.db.do("INSERT INTO pcu_node (pcu_id, node_id, port)" \ " VALUES(%(pcu_id)d, %(node_id)d, %(port)d)", locals()) if commit: self.api.db.commit() self['node_ids'].append(node_id) self['ports'].append(port) def remove_node(self, node, commit = True): """ Remove node from existing PCU. """ assert 'pcu_id' in self assert isinstance(node, Node) assert 'node_id' in node pcu_id = self['pcu_id'] node_id = node['node_id'] if node_id in self['node_ids']: i = self['node_ids'].index(node_id) port = self['ports'][i] self.api.db.do("DELETE FROM pcu_node" \ " WHERE pcu_id = %(pcu_id)d" \ " AND node_id = %(node_id)d", locals()) if commit: self.api.db.commit() self['node_ids'].remove(node_id) self['ports'].remove(port) def sync(self, commit, validate=True): AlchemyObj.sync(self, commit=commit, valiate=validate) if 'pcu_id' not in self: AlchemyObj.insert(self, dict(self)) else: AlchemyObj.delete(self, {'pcu_id': self['pcu_id']}, dict(self)) def delete(self, commit=True): AlchemyObj.delete(self, dict(self)) class PCUs(list): """ Representation of row(s) from the pcus table in the database. """ def __init__(self, api, pcu_filter = None, columns = None): if not pcu_filter: pcus = PCU().select() elif isinstance(pcu_filter, int): pcus = PCU().select(filter={'id': pcu_filter}) elif isinstance(pcu_filter, (list, tuple, set, int, long)): pcus = PCU().select(filter={'pcu_id': pcu_filter}) elif isinstance(pcu_filter, dict): pcus= PCU().select(filter=pcu_filter) else: raise PLCInvalidArgument, "Wrong pcu filter %r"%pcu_filter for pcu in pcus: self.append(pcu)