Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / PLC / PCUs.py
1 #
2 # Functions for interacting with the pcus table in the database
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7 # $Id: PCUs.py 5574 2007-10-25 20:33:17Z thierry $
8 #
9
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
17
18 class PCU(Row):
19     """
20     Representation of a row in the pcus table. To use,
21     instantiate with a dict of values.
22     """
23
24     table_name = 'pcus'
25     primary_key = 'pcu_id'
26     join_tables = ['pcu_node']
27     fields = {
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"),
39         }
40
41     def validate_ip(self, ip):
42         if not valid_ip(ip):
43             raise PLCInvalidArgument, "Invalid IP address " + ip
44         return ip
45
46     def add_node(self, node, port, commit = True):
47         """
48         Add node to existing PCU.
49         """
50
51         assert 'pcu_id' in self
52         assert isinstance(node, Node)
53         assert isinstance(port, (int, long))
54         assert 'node_id' in node
55
56         pcu_id = self['pcu_id']
57         node_id = node['node_id']
58
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)",
62                            locals())
63
64             if commit:
65                 self.api.db.commit()
66
67             self['node_ids'].append(node_id)
68             self['ports'].append(port)
69
70     def remove_node(self, node, commit = True):
71         """
72         Remove node from existing PCU.
73         """
74
75         assert 'pcu_id' in self
76         assert isinstance(node, Node)
77         assert 'node_id' in node
78
79         pcu_id = self['pcu_id']
80         node_id = node['node_id']
81
82         if node_id in self['node_ids']:
83             i = self['node_ids'].index(node_id)
84             port = self['ports'][i]
85
86             self.api.db.do("DELETE FROM pcu_node" \
87                            " WHERE pcu_id = %(pcu_id)d" \
88                            " AND node_id = %(node_id)d",
89                            locals())
90
91             if commit:
92                 self.api.db.commit()
93
94             self['node_ids'].remove(node_id)
95             self['ports'].remove(port)
96
97 class PCUs(Table):
98     """
99     Representation of row(s) from the pcus table in the
100     database.
101     """
102
103     def __init__(self, api, pcu_filter = None, columns = None):
104         Table.__init__(self, api, PCU, columns)
105
106         sql = "SELECT %s FROM view_pcus WHERE True" % \
107               ", ".join(self.columns)
108
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) %s" % pcu_filter.sql(api)
115
116         self.selectall(sql)