added create_network(), delete_network(), create_subnet(), delete_subnet(), process_t...
[plcapi.git] / PLC / PCUProtocolTypes.py
1 #
2 # Functions for interacting with the pcu_type_port 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.Storage.AlchemyObject import AlchemyObj
11
12 class PCUProtocolType(AlchemyObj):
13     """
14     Representation of a row in the pcu_protocol_type table. To use,
15     instantiate with a dict of values.
16     """
17
18     tablename = 'pcu_protocol_type'
19     join_tables = []
20     fields = {
21         'pcu_protocol_type_id': Parameter(int, "PCU protocol type identifier", primary_key=True),
22         'pcu_type_id': Parameter(int, "PCU type identifier"),
23         'port': Parameter(int, "PCU port"),
24         'protocol': Parameter(str, "Protocol"),
25         'supported': Parameter(bool, "Is the port/protocol supported by PLC")
26         }
27
28     def validate_port(self, port):
29         # make sure port is not blank
30
31         if not port:
32             raise PLCInvalidArgument, "Port must be specified"
33
34         return port
35
36     def validate_protocol(self, protocol):
37         # make sure port is not blank
38         if not len(protocol):
39             raise PLCInvalidArgument, "protocol must be specified"
40
41         return protocol
42
43     def sync(self, commit=True, validate=True):
44         AlchemyObj.sync(self, commit=commit, validate=validate)
45         if 'pcu_protocol_type_id' not in self:
46             AlchemyObj.insert(self, dict(self))
47         else:
48             filter = {'pcu_protocol_type_id', self['pcu_protocol_type_id']}
49             AlchemyObj.update(self, filter, dict(self))
50
51     def delete(self, commit=True):
52         assert 'pcu_protocol_type_id' in self
53         AlchemyObj.delete(self, dict(self))
54
55 class PCUProtocolTypes(list):
56     """
57     Representation of the pcu_protocol_types table in the database.
58     """
59
60     def __init__(self, api, protocol_type_filter = None, columns = None):
61         if not  protocol_type_filter:
62             protocol_types = PCUProtocolType().select()
63         elif isinstance(protocol_type_filter, (list, tuple, set, int, long)):
64             protocol_types = PCUProtocolType.select(filter={'pcu_protocol_type_id': protocol_type_filter})
65         elif isinstance(protocol_type_filter, dict):
66             protocol_types = PCUProtocolType.select(filter= protocol_type_filter)
67         else:
68             raise PLCInvalidArgument, "Wrong pcu_protocol_type filter %r"%protocol_type_filter
69
70         self.extend(protocol_types)