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