added create_network(), delete_network(), create_subnet(), delete_subnet(), process_t...
[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.Storage.AlchemyObject import AlchemyObj
13
14 class PCUType(AlchemyObj):
15     """
16     Representation of a row in the pcu_types table. To use,
17     instantiate with a dict of values.
18     """
19
20     tablename = 'pcu_types'
21     join_tables = ['pcu_protocol_type']
22     fields = {
23         'pcu_type_id': Parameter(int, "PCU Type Identifier", primary_key=True),
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", joined=True),
27         'pcu_protocol_types': Parameter([dict], "PCU Protocol Type List", joined=True)
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(list):
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 not pcu_type_filter:
53             pcu_types = PCUType().select()
54         elif isinstance(pcu_type_filter, (list, tuple, set)):
55             # Separate the list into integers and strings
56             ints = filter(lambda x: isinstance(x, (int, long)), pcu_type_filter)
57             strs = filter(lambda x: isinstance(x, StringTypes), pcu_type_filter)
58             pcu_types = PCUType().select(filter = {'pcu_type_id': ints, 'model': strs})
59         elif isinstance(pcu_type_filter, dict):
60             pcu_types = PCUType().select(filter = pcu_type_filter)
61         elif isinstance (pcu_type_filter, StringTypes):
62             pcu_types = PCUType().select(filter = {'model': pcu_type_filter})
63         elif isinstance (pcu_type_filter, int):
64             pcu_types = PCUType().select(filter = {'pcu_type_id': pcu_filter})
65         else:
66             raise PLCInvalidArgument, "Wrong pcu_type filter %r"%pcu_type_filter
67
68         
69         self.extend(pcu_types)
70         
71          # return a list of protocol type objects for each port type
72         if 'pcu_protocol_types' in removed_fields:
73             from PLC.PCUProtocolTypes import PCUProtocolTypes
74             protocol_type_ids = set()
75             for pcu_type in self:
76                 protocol_type_ids.update(pcu_type['pcu_protocol_type_ids'])
77
78             protocol_return_fields = ['pcu_protocol_type_id', 'port', 'protocol', 'supported']
79             all_protocol_types = PCUProtocolTypes(self.api, list(protocol_type_ids), \
80                                                   protocol_return_fields).dict('pcu_protocol_type_id')
81
82             for pcu_type in self:
83                 pcu_type['pcu_protocol_types'] = []
84                 for protocol_type_id in pcu_type['pcu_protocol_type_ids']:
85                     pcu_type['pcu_protocol_types'].append(all_protocol_types[protocol_type_id])