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
8 from types import StringTypes
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Table import Row, Table
13 from PLC.Filter import Filter
17 Representation of a row in the pcu_types table. To use,
18 instantiate with a dict of values.
21 table_name = 'pcu_types'
22 primary_key = 'pcu_type_id'
23 join_tables = ['pcu_protocol_type']
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")
32 def validate_model(self, model):
33 # Make sure name is not blank
35 raise PLCInvalidArgument, "Model must be specified"
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"
45 class PCUTypes(Table):
47 Representation of the pcu_types table in the database.
50 def __init__(self, api, pcu_type_filter = None, columns = None):
52 # Remove pcu_protocol_types from query since its not really a field
53 # in the db. We will add it later
55 columns = PCUType.fields.keys()
56 if 'pcu_protocol_types' in columns:
57 removed_fields = ['pcu_protocol_types']
58 columns.remove('pcu_protocol_types')
62 Table.__init__(self, api, PCUType, columns)
64 sql = "SELECT %s FROM view_pcu_types WHERE True" % \
65 ", ".join(self.columns)
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")
84 raise PLCInvalidArgument, "Wrong pcu_type filter %r"%pcu_type_filter
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()
94 protocol_type_ids.update(pcu_type['pcu_protocol_type_ids'])
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')
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])