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