Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / PLC / PCUTypes.py
1 #
2 # Functions for interacting with the pcu_types 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 from types import StringTypes
10
11 from PLC.Faults import *
12 from PLC.Parameter import Parameter
13 from PLC.Table import Row, Table
14 from PLC.Filter import Filter
15
16 class PCUType(Row):
17     """
18     Representation of a row in the pcu_types table. To use,
19     instantiate with a dict of values.
20     """
21
22     table_name = 'pcu_types'
23     primary_key = 'pcu_type_id'
24     join_tables = ['pcu_port_type']
25     fields = {
26         'pcu_type_id': Parameter(int, "PCU Type Identifier"),
27         'model': Parameter(str, "PCU model", max = 254),
28         'name': Parameter(str, "PCU full name", max = 254),
29         'pcu_protocol_type_ids': Parameter([int], "PCU Protocol Type Identifiers"),
30         'pcu_protocol_types': Parameter([dict], "PCU Protocol Type List")
31         }
32
33     def validate_model(self, model):
34         # Make sure name is not blank
35         if not len(model):
36             raise PLCInvalidArgument, "Model must be specified"
37         
38         # Make sure boot state does not alredy exist
39         conflicts = PCUTypes(self.api, [model])
40         for pcu_type in conflicts:
41             if 'pcu_type_id' not in self or self['pcu_type_id'] != pcu_type['pcu_type_id']: 
42                 raise PLCInvalidArgument, "Model already in use"
43
44         return model
45
46 class PCUTypes(Table):
47     """
48     Representation of the pcu_types table in the database.
49     """
50
51     def __init__(self, api, pcu_type_filter = None, columns = None):
52
53         # Remove pcu_protocol_types from query since its not really a field
54         # in the db. We will add it later
55         if columns == None:
56             columns = PCUType.fields.keys()
57         if 'pcu_protocol_types' in columns:
58             removed_fields = ['pcu_protocol_types']
59             columns.remove('pcu_protocol_types')
60         else:
61             removed_fields = []
62
63         Table.__init__(self, api, PCUType, columns)
64
65         sql = "SELECT %s FROM view_pcu_types WHERE True" % \
66               ", ".join(self.columns)
67         
68         if pcu_type_filter is not None:
69             if isinstance(pcu_type_filter, (list, tuple, set)):
70                 # Separate the list into integers and strings
71                 ints = filter(lambda x: isinstance(x, (int, long)), pcu_type_filter)
72                 strs = filter(lambda x: isinstance(x, StringTypes), pcu_type_filter)
73                 pcu_type_filter = Filter(PCUType.fields, {'pcu_type_id': ints, 'model': strs})
74                 sql += " AND (%s) %s" % pcu_type_filter.sql(api, "OR")
75             elif isinstance(pcu_type_filter, dict):
76                 pcu_type_filter = Filter(PCUType.fields, pcu_type_filter)
77                 sql += " AND (%s) %s" % pcu_type_filter.sql(api, "AND")
78             elif isinstance (pcu_type_filter, StringTypes):
79                 pcu_type_filter = Filter(PCUType.fields, {'model':[pcu_type_filter]})
80                 sql += " AND (%s) %s" % pcu_type_filter.sql(api, "AND")
81             elif isinstance (pcu_type_filter, int):
82                 pcu_type_filter = Filter(PCUType.fields, {'pcu_type_id':[pcu_type_filter]})
83                 sql += " AND (%s) %s" % pcu_type_filter.sql(api, "AND")
84             else:
85                 raise PLCInvalidArgument, "Wrong pcu_type filter %r"%pcu_type_filter    
86
87
88         self.selectall(sql)
89
90          # return a list of protocol type objects for each port type
91         if 'pcu_protocol_types' in removed_fields:
92             from PLC.PCUProtocolTypes import PCUProtocolTypes
93             protocol_type_ids = set()
94             for pcu_type in self:
95                 protocol_type_ids.update(pcu_type['pcu_protocol_type_ids'])
96
97             protocol_return_fields = ['pcu_protocol_type_id', 'port', 'protocol', 'supported']
98             all_protocol_types = PCUProtocolTypes(self.api, list(protocol_type_ids), \
99                                                   protocol_return_fields).dict('pcu_protocol_type_id')
100
101             for pcu_type in self:
102                 pcu_type['pcu_protocol_types'] = []
103                 for protocol_type_id in pcu_type['pcu_protocol_type_ids']:
104                     pcu_type['pcu_protocol_types'].append(all_protocol_types[protocol_type_id])