2 # Functions for interacting with the pcu_types table in the database
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
9 from types import StringTypes
11 from PLC.Faults import *
12 from PLC.Parameter import Parameter
13 from PLC.Table import Row, Table
14 from PLC.Filter import Filter
18 Representation of a row in the pcu_types table. To use,
19 instantiate with a dict of values.
22 table_name = 'pcu_types'
23 primary_key = 'pcu_type_id'
24 join_tables = ['pcu_protocol_type']
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")
33 def validate_model(self, model):
34 # Make sure name is not blank
36 raise PLCInvalidArgument, "Model must be specified"
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"
46 class PCUTypes(Table):
48 Representation of the pcu_types table in the database.
51 def __init__(self, api, pcu_type_filter = None, columns = None):
53 # Remove pcu_protocol_types from query since its not really a field
54 # in the db. We will add it later
56 columns = PCUType.fields.keys()
57 if 'pcu_protocol_types' in columns:
58 removed_fields = ['pcu_protocol_types']
59 columns.remove('pcu_protocol_types')
63 Table.__init__(self, api, PCUType, columns)
65 sql = "SELECT %s FROM view_pcu_types WHERE True" % \
66 ", ".join(self.columns)
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")
85 raise PLCInvalidArgument, "Wrong pcu_type filter %r"%pcu_type_filter
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()
95 protocol_type_ids.update(pcu_type['pcu_protocol_type_ids'])
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')
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])