Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / PLC / PCUProtocolTypes.py
1 #
2 # Functions for interacting with the pcu_type_port table in the database
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7 # $Id: 
8 #
9
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Table import Row, Table
13 from PLC.Filter import Filter
14
15 class PCUProtocolType(Row):
16     """
17     Representation of a row in the pcu_protocol_type table. To use,
18     instantiate with a dict of values.
19     """
20
21     table_name = 'pcu_protocol_type'
22     primary_key = 'pcu_protocol_type_id'
23     join_tables = []
24     fields = {
25         'pcu_protocol_type_id': Parameter(int, "PCU protocol type identifier"),
26         'pcu_type_id': Parameter(int, "PCU type identifier"), 
27         'port': Parameter(int, "PCU port"),
28         'protocol': Parameter(str, "Protocol"),
29         'supported': Parameter(bool, "Is the port/protocol supported by PLC") 
30         }
31
32     def validate_port(self, port):
33         # make sure port is not blank
34         
35         if not port:
36             raise PLCInvalidArgument, "Port must be specified"
37         
38         return port
39
40     def validate_protocol(self, protocol):
41         # make sure port is not blank
42         if not len(protocol):
43             raise PLCInvalidArgument, "protocol must be specified"
44
45         return protocol
46
47 class PCUProtocolTypes(Table):
48     """
49     Representation of the pcu_protocol_types table in the database.
50     """
51
52     def __init__(self, api, protocol_type_filter = None, columns = None):
53         Table.__init__(self, api, PCUProtocolType, columns)
54
55         sql = "SELECT %s FROM pcu_protocol_type WHERE True" % \
56               ", ".join(self.columns)
57         
58         if protocol_type_filter is not None:
59             if isinstance(protocol_type_filter, (list, tuple, set)):
60                 # Separate the list into integers and strings
61                 ints = filter(lambda x: isinstance(x, (int, long)), protocol_type_filter)
62                 protocol_type_filter = Filter(PCUProtocolType.fields, {'pcu_protocol_type_id': ints})
63                 sql += " AND (%s) %s" % protocol_type_filter.sql(api, "OR")
64             elif isinstance(protocol_type_filter, dict):
65                 protocol_type_filter = Filter(PCUProtocolType.fields, protocol_type_filter)
66                 sql += " AND (%s) %s" % protocol_type_filter.sql(api, "AND")
67             elif isinstance (protocol_type_filter, int):
68                 protocol_type_filter = Filter(PCUProtocolType.fields, {'pcu_protocol_type_id':[protocol_type_filter]})
69
70                 sql += " AND (%s) %s" % protocol_type_filter.sql(api, "AND")
71             else:
72                 raise PLCInvalidArgument, "Wrong pcu_protocol_type filter %r"%protocol_type_filter      
73
74
75         self.selectall(sql)