- updated some validate functions
[plcapi.git] / PLC / NodeNetworks.py
1 #
2 # Functions for interacting with the nodenetworks table in the database
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7 # $Id: NodeNetworks.py,v 1.1 2006/09/06 15:36:07 mlhuang Exp $
8 #
9
10 from types import StringTypes
11 import socket
12 import struct
13
14 from PLC.Faults import *
15 from PLC.Parameter import Parameter
16 from PLC.Debug import profile
17 from PLC.Table import Row, Table
18 import PLC.Nodes
19
20 def in_same_network(address1, address2, netmask):
21     """
22     Returns True if two IPv4 addresses are in the same network. Faults
23     if an address is invalid.
24     """
25
26     address1 = struct.unpack('>L', socket.inet_aton(address1))[0]
27     address2 = struct.unpack('>L', socket.inet_aton(address2))[0]
28     netmask = struct.unpack('>L', socket.inet_aton(netmask))[0]
29
30     return (address1 & netmask) == (address2 & netmask)
31
32 class NodeNetwork(Row):
33     """
34     Representation of a row in the nodenetworks table. To use, optionally
35     instantiate with a dict of values. Update as you would a
36     dict. Commit to the database with flush().
37     """
38
39     fields = {
40         'nodenetwork_id': Parameter(int, "Node interface identifier"),
41         'method': Parameter(str, "Addressing method (e.g., 'static' or 'dhcp')"),
42         'type': Parameter(str, "Address type (e.g., 'ipv4')"),
43         'ip': Parameter(str, "IP address"),
44         'mac': Parameter(str, "MAC address"),
45         'gateway': Parameter(str, "IP address of primary gateway"),
46         'network': Parameter(str, "Subnet address"),
47         'broadcast': Parameter(str, "Network broadcast address"),
48         'netmask': Parameter(str, "Subnet mask"),
49         'dns1': Parameter(str, "IP address of primary DNS server"),
50         'dns2': Parameter(str, "IP address of secondary DNS server"),
51         # XXX Should be an int (bps)
52         'bwlimit': Parameter(str, "Bandwidth limit"),
53         'hostname': Parameter(str, "(Optional) Hostname"),
54         }
55
56     # These fields are derived from join tables and are not
57     # actually in the nodenetworks table.
58     join_fields = {
59         'node_id': Parameter(int, "Node associated with this interface (if any)"),
60         'is_primary': Parameter(bool, "Is the primary interface for this node"),
61         }
62
63     all_fields = dict(join_fields.items() + fields.items())
64
65     methods = ['static', 'dhcp', 'proxy', 'tap', 'ipmi', 'unknown']
66
67     types = ['ipv4']
68
69     bwlimits = ['-1',
70                 '100kbit', '250kbit', '500kbit',
71                 '1mbit', '2mbit', '5mbit',
72                 '10mbit', '20mbit', '50mbit',
73                 '100mbit']
74
75     def __init__(self, api, fields):
76         Row.__init__(self, fields)
77         self.api = api
78
79     def validate_method(self, method):
80         if method not in self.methods:
81             raise PLCInvalidArgument, "Invalid addressing method"
82         return method
83
84     def validate_type(self, type):
85         if type not in self.types:
86             raise PLCInvalidArgument, "Invalid address type"
87         return type
88
89     def validate_ip(self, ip):
90         try:
91             ip = socket.inet_ntoa(socket.inet_aton(ip))
92         except socket.error:
93             raise PLCInvalidArgument, "Invalid IP address " + ip
94
95         return ip
96
97     def validate_mac(self, mac):
98         try:
99             bytes = mac.split(":")
100             if len(bytes) < 6:
101                 raise Exception
102             for i, byte in enumerate(bytes):
103                 byte = int(byte, 16)
104                 if byte < 0 or byte > 255:
105                     raise Exception
106                 bytes[i] = "%02x" % byte
107             mac = ":".join(bytes)
108         except:
109             raise PLCInvalidArgument, "Invalid MAC address"
110
111         return mac
112
113     validate_gateway = validate_ip
114     validate_network = validate_ip
115     validate_broadcast = validate_ip
116     validate_netmask = validate_ip
117     validate_dns1 = validate_ip
118     validate_dns2 = validate_ip
119
120     def validate_bwlimit(self, bwlimit):
121         if bwlimit not in self.bwlimits:
122             raise PLCInvalidArgument, "Invalid bandwidth limit"
123         return bwlimit
124
125     def validate_hostname(self, hostname):
126         # Optional
127         if not hostname:
128             return hostname
129
130         # Validate hostname, and check for conflicts with a node hostname
131         return PLC.Nodes.Node.validate_hostname(self, hostname)
132
133     def flush(self, commit = True):
134         """
135         Flush changes back to the database.
136         """
137
138         # Validate all specified fields
139         self.validate()
140
141         try:
142             method = self['method']
143             self['type']
144         except KeyError:
145             raise PLCInvalidArgument, "method and type must both be specified"
146
147         if method == "proxy" or method == "tap":
148             if 'mac' in self:
149                 raise PLCInvalidArgument, "For %s method, mac should not be specified" % method
150             if 'ip' not in self:
151                 raise PLCInvalidArgument, "For %s method, ip is required" % method
152             if method == "tap" and 'gateway' not in self:
153                 raise PLCInvalidArgument, "For tap method, gateway is required and should be " \
154                       "the IP address of the node that proxies for this address"
155             # Should check that the proxy address is reachable, but
156             # there's no way to tell if the only primary interface is
157             # DHCP!
158
159         elif method == "static":
160             for key in ['ip', 'gateway', 'network', 'broadcast', 'netmask', 'dns1']:
161                 if key not in self:
162                     raise PLCInvalidArgument, "For static method, %s is required" % key
163                 locals()[key] = self[key]
164             if not in_same_network(ip, network, netmask):
165                 raise PLCInvalidArgument, "IP address %s is inconsistent with network %s/%s" % \
166                       (ip, network, netmask)
167             if not in_same_network(broadcast, network, netmask):
168                 raise PLCInvalidArgument, "Broadcast address %s is inconsistent with network %s/%s" % \
169                       (broadcast, network, netmask)
170             if not in_same_network(ip, gateway, netmask):
171                 raise PLCInvalidArgument, "Gateway %s is not reachable from %s/%s" % \
172                       (gateway, ip, netmask)
173
174         elif method == "ipmi":
175             if 'ip' not in self:
176                 raise PLCInvalidArgument, "For ipmi method, ip is required"
177
178         # Fetch a new nodenetwork_id if necessary
179         if 'nodenetwork_id' not in self:
180             rows = self.api.db.selectall("SELECT NEXTVAL('nodenetworks_nodenetwork_id_seq') AS nodenetwork_id")
181             if not rows:
182                 raise PLCDBError("Unable to fetch new nodenetwork_id")
183             self['nodenetwork_id'] = rows[0]['nodenetwork_id']
184             insert = True
185         else:
186             insert = False
187
188         # Filter out fields that cannot be set or updated directly
189         fields = dict(filter(lambda (key, value): key in self.fields,
190                              self.items()))
191
192         # Parameterize for safety
193         keys = fields.keys()
194         values = [self.api.db.param(key, value) for (key, value) in fields.items()]
195
196         if insert:
197             # Insert new row in nodenetworks table
198             sql = "INSERT INTO nodenetworks (%s) VALUES (%s)" % \
199                   (", ".join(keys), ", ".join(values))
200         else:
201             # Update existing row in sites table
202             columns = ["%s = %s" % (key, value) for (key, value) in zip(keys, values)]
203             sql = "UPDATE nodenetworks SET " + \
204                   ", ".join(columns) + \
205                   " WHERE nodenetwork_id = %(nodenetwork_id)d"
206
207         self.api.db.do(sql, fields)
208
209         if commit:
210             self.api.db.commit()
211
212     def delete(self, commit = True):
213         """
214         Delete existing nodenetwork.
215         """
216
217         assert 'nodenetwork_id' in self
218
219         # Delete ourself
220         for table in ['node_nodenetworks', 'nodenetworks']:
221             self.api.db.do("DELETE FROM %s" \
222                            " WHERE nodenetwork_id = %d" % \
223                            (table, self['nodenetwork_id']))
224         
225         if commit:
226             self.api.db.commit()
227
228 class NodeNetworks(Table):
229     """
230     Representation of row(s) from the nodenetworks table in the
231     database.
232     """
233
234     def __init__(self, api, nodenetwork_id_or_hostname_list = None):
235         self.api = api
236
237         # N.B.: Node IDs returned may be deleted.
238         sql = "SELECT nodenetworks.*" \
239               ", node_nodenetworks.node_id" \
240               ", node_nodenetworks.is_primary" \
241               " FROM nodenetworks" \
242               " LEFT JOIN node_nodenetworks USING (nodenetwork_id)"
243
244         if nodenetwork_id_or_hostname_list:
245             # Separate the list into integers and strings
246             nodenetwork_ids = filter(lambda nodenetwork_id: isinstance(nodenetwork_id, (int, long)),
247                                      nodenetwork_id_or_hostname_list)
248             hostnames = filter(lambda hostname: isinstance(hostname, StringTypes),
249                            nodenetwork_id_or_hostname_list)
250             sql += " WHERE (False"
251             if nodenetwork_ids:
252                 sql += " OR nodenetwork_id IN (%s)" % ", ".join(map(str, nodenetwork_ids))
253             if hostnames:
254                 sql += " OR hostname IN (%s)" % ", ".join(api.db.quote(hostnames)).lower()
255             sql += ")"
256
257         rows = self.api.db.selectall(sql)
258         for row in rows:
259             if self.has_key(row['nodenetwork_id']):
260                 nodenetwork = self[row['nodenetwork_id']]
261                 nodenetwork.update(row)
262             else:
263                 self[row['nodenetwork_id']] = NodeNetwork(api, row)