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