- add_person, remove_person: fix case when person is already part of/no longer
[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.4 2006/09/25 14:55:43 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 sync().
37     """
38
39     table_name = 'nodenetworks'
40     primary_key = 'nodenetwork_id'
41     fields = {
42         'nodenetwork_id': Parameter(int, "Node interface identifier"),
43         'method': Parameter(str, "Addressing method (e.g., 'static' or 'dhcp')"),
44         'type': Parameter(str, "Address type (e.g., 'ipv4')"),
45         'ip': Parameter(str, "IP address"),
46         'mac': Parameter(str, "MAC address"),
47         'gateway': Parameter(str, "IP address of primary gateway"),
48         'network': Parameter(str, "Subnet address"),
49         'broadcast': Parameter(str, "Network broadcast address"),
50         'netmask': Parameter(str, "Subnet mask"),
51         'dns1': Parameter(str, "IP address of primary DNS server"),
52         'dns2': Parameter(str, "IP address of secondary DNS server"),
53         # XXX Should be an int (bps)
54         'bwlimit': Parameter(str, "Bandwidth limit"),
55         'hostname': Parameter(str, "(Optional) Hostname"),
56         'node_id': Parameter(int, "Node associated with this interface (if any)"),
57         'is_primary': Parameter(bool, "Is the primary interface for this node"),
58         }
59
60     methods = ['static', 'dhcp', 'proxy', 'tap', 'ipmi', 'unknown']
61
62     types = ['ipv4']
63
64     bwlimits = ['-1',
65                 '100kbit', '250kbit', '500kbit',
66                 '1mbit', '2mbit', '5mbit',
67                 '10mbit', '20mbit', '50mbit',
68                 '100mbit']
69
70     def __init__(self, api, fields = {}):
71         Row.__init__(self, fields)
72         self.api = api
73
74     def validate_method(self, method):
75         if method not in self.methods:
76             raise PLCInvalidArgument, "Invalid addressing method"
77         return method
78
79     def validate_type(self, type):
80         if type not in self.types:
81             raise PLCInvalidArgument, "Invalid address type"
82         return type
83
84     def validate_ip(self, ip):
85         if ip:
86             try:
87                 ip = socket.inet_ntoa(socket.inet_aton(ip))
88             except socket.error:
89                 raise PLCInvalidArgument, "Invalid IP address " + ip
90
91         return ip
92
93     def validate_mac(self, mac):
94         try:
95             bytes = mac.split(":")
96             if len(bytes) < 6:
97                 raise Exception
98             for i, byte in enumerate(bytes):
99                 byte = int(byte, 16)
100                 if byte < 0 or byte > 255:
101                     raise Exception
102                 bytes[i] = "%02x" % byte
103             mac = ":".join(bytes)
104         except:
105             raise PLCInvalidArgument, "Invalid MAC address"
106
107         return mac
108
109     validate_gateway = validate_ip
110     validate_network = validate_ip
111     validate_broadcast = validate_ip
112     validate_netmask = validate_ip
113     validate_dns1 = validate_ip
114     validate_dns2 = validate_ip
115
116     def validate_bwlimit(self, bwlimit):
117         if bwlimit not in self.bwlimits:
118             raise PLCInvalidArgument, "Invalid bandwidth limit"
119         return bwlimit
120
121     def validate_hostname(self, hostname):
122         # Optional
123         if not hostname:
124             return hostname
125
126         # Validate hostname, and check for conflicts with a node hostname
127         return PLC.Nodes.Node.validate_hostname(self, hostname)
128
129     def validate(self):
130         """
131         Flush changes back to the database.
132         """
133
134         # Basic validation
135         Row.validate(self)
136
137         assert 'method' in self
138         method = self['method']
139
140         if method == "proxy" or method == "tap":
141             if 'mac' in self and self['mac']:
142                 raise PLCInvalidArgument, "For %s method, mac should not be specified" % method
143             if 'ip' not in self or not self['ip']:
144                 raise PLCInvalidArgument, "For %s method, ip is required" % method
145             if method == "tap" and ('gateway' not in self or not self['gateway']):
146                 raise PLCInvalidArgument, "For tap method, gateway is required and should be " \
147                       "the IP address of the node that proxies for this address"
148             # Should check that the proxy address is reachable, but
149             # there's no way to tell if the only primary interface is
150             # DHCP!
151
152         elif method == "static":
153             for key in ['ip', 'gateway', 'network', 'broadcast', 'netmask', 'dns1']:
154                 if key not in self or not self[key]:
155                     raise PLCInvalidArgument, "For static method, %s is required" % key
156                 globals()[key] = self[key]
157             if not in_same_network(ip, network, netmask):
158                 raise PLCInvalidArgument, "IP address %s is inconsistent with network %s/%s" % \
159                       (ip, network, netmask)
160             if not in_same_network(broadcast, network, netmask):
161                 raise PLCInvalidArgument, "Broadcast address %s is inconsistent with network %s/%s" % \
162                       (broadcast, network, netmask)
163             if not in_same_network(ip, gateway, netmask):
164                 raise PLCInvalidArgument, "Gateway %s is not reachable from %s/%s" % \
165                       (gateway, ip, netmask)
166
167         elif method == "ipmi":
168             if 'ip' not in self or not self['ip']:
169                 raise PLCInvalidArgument, "For ipmi method, ip is required"
170
171     def delete(self, commit = True):
172         """
173         Delete existing nodenetwork.
174         """
175
176         assert 'nodenetwork_id' in self
177
178         # Delete ourself
179         self.api.db.do("DELETE FROM nodenetworks" \
180                        " WHERE nodenetwork_id = %d" % \
181                        self['nodenetwork_id'])
182         
183         if commit:
184             self.api.db.commit()
185
186 class NodeNetworks(Table):
187     """
188     Representation of row(s) from the nodenetworks table in the
189     database.
190     """
191
192     def __init__(self, api, nodenetwork_id_or_hostname_list = None):
193         self.api = api
194
195         sql = "SELECT %s FROM nodenetworks" % \
196               ", ".join(NodeNetwork.fields)
197
198         if nodenetwork_id_or_hostname_list:
199             # Separate the list into integers and strings
200             nodenetwork_ids = filter(lambda nodenetwork_id: isinstance(nodenetwork_id, (int, long)),
201                                      nodenetwork_id_or_hostname_list)
202             hostnames = filter(lambda hostname: isinstance(hostname, StringTypes),
203                                nodenetwork_id_or_hostname_list)
204             sql += " WHERE (False"
205             if nodenetwork_ids:
206                 sql += " OR nodenetwork_id IN (%s)" % ", ".join(map(str, nodenetwork_ids))
207             if hostnames:
208                 sql += " OR hostname IN (%s)" % ", ".join(api.db.quote(hostnames)).lower()
209             sql += ")"
210
211         rows = self.api.db.selectall(sql)
212
213         for row in rows:
214             self[row['nodenetwork_id']] = NodeNetwork(api, row)