8211be174a3546f4a7565cc0806248fa84039eae
[plcapi.git] / PLC / Methods / AddPCUProtocolType.py
1 # $Id$
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.PCUProtocolTypes import PCUProtocolType, PCUProtocolTypes
6 from PLC.PCUTypes import PCUType, PCUTypes
7 from PLC.Auth import Auth
8
9 can_update = lambda (field, value): field in \
10              ['pcu_type_id', 'port', 'protocol', 'supported']   
11
12 class AddPCUProtocolType(Method):
13     """
14     Adds a new pcu protocol type.
15
16     Returns the new pcu_protocol_type_id (> 0) if successful, faults otherwise.
17     """
18
19     roles = ['admin']
20
21     protocol_type_fields = dict(filter(can_update, PCUProtocolType.fields.items()))
22
23     accepts = [
24         Auth(),
25         Mixed(PCUType.fields['pcu_type_id'],
26               PCUType.fields['model']),
27         protocol_type_fields
28         ]
29
30     returns = Parameter(int, 'New pcu_protocol_type_id (> 0) if successful')
31
32     def call(self, auth, pcu_type_id_or_model, protocol_type_fields):
33
34         # Check if pcu type exists
35         pcu_types = PCUTypes(self.api, [pcu_type_id_or_model])
36         if not pcu_types:
37             raise PLCInvalidArgument, "No such pcu type"
38         pcu_type = pcu_types[0]
39
40         
41         # Check if this port is already used
42         if 'port' not in protocol_type_fields:
43             raise PLCInvalidArgument, "Must specify a port"
44         else:
45             protocol_types = PCUProtocolTypes(self.api, {'pcu_type_id': pcu_type['pcu_type_id']})
46             for protocol_type in protocol_types:
47                 if protocol_type['port'] == protocol_type_fields['port']:
48                     raise PLCInvalidArgument, "Port alreay in use" 
49
50         protocol_type_fields = dict(filter(can_update, protocol_type_fields.items()))
51         protocol_type = PCUProtocolType(self.api, protocol_type_fields)
52         protocol_type['pcu_type_id'] = pcu_type['pcu_type_id']
53         protocol_type.sync()
54         self.event_object = {'PCUProtocolType': [protocol_type['pcu_protocol_type_id']]}        
55
56         return protocol_type['pcu_protocol_type_id']