Methods for managing PCU model types and protocols
authorTony Mack <tmack@cs.princeton.edu>
Mon, 12 Nov 2007 22:54:33 +0000 (22:54 +0000)
committerTony Mack <tmack@cs.princeton.edu>
Mon, 12 Nov 2007 22:54:33 +0000 (22:54 +0000)
PLC/Methods/AddPCUProtocolType.py [new file with mode: 0644]
PLC/Methods/AddPCUType.py [new file with mode: 0644]
PLC/Methods/DeletePCUProtocolType.py [new file with mode: 0644]
PLC/Methods/DeletePCUType.py [new file with mode: 0644]
PLC/Methods/GetPCUProtocolTypes.py [new file with mode: 0644]
PLC/Methods/GetPCUTypes.py [new file with mode: 0644]
PLC/Methods/UpdatePCUProtocolType.py [new file with mode: 0644]
PLC/Methods/UpdatePCUType.py [new file with mode: 0644]

diff --git a/PLC/Methods/AddPCUProtocolType.py b/PLC/Methods/AddPCUProtocolType.py
new file mode 100644 (file)
index 0000000..76dad3b
--- /dev/null
@@ -0,0 +1,55 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.PCUProtocolTypes import PCUProtocolType, PCUProtocolTypes
+from PLC.PCUTypes import PCUType, PCUTypes
+from PLC.Auth import Auth
+
+can_update = lambda (field, value): field in \
+            ['pcu_type_id', 'port', 'protocol', 'supported']   
+
+class AddPCUProtocolType(Method):
+    """
+    Adds a new pcu protocol type.
+
+    Returns the new pcu_protocol_type_id (> 0) if successful, faults otherwise.
+    """
+
+    roles = ['admin']
+
+    protocol_type_fields = dict(filter(can_update, PCUProtocolType.fields.items()))
+
+    accepts = [
+        Auth(),
+       Mixed(PCUType.fields['pcu_type_id'],
+              PCUType.fields['model']),
+        protocol_type_fields
+       ]
+
+    returns = Parameter(int, 'New pcu_protocol_type_id (> 0) if successful')
+
+    def call(self, auth, pcu_type_id_or_model, protocol_type_fields):
+
+       # Check if pcu type exists
+       pcu_types = PCUTypes(self.api, [pcu_type_id_or_model])
+       if not pcu_types:
+           raise PLCInvalidArgument, "No such pcu type"
+       pcu_type = pcu_types[0]
+
+       
+       # Check if this port is already used
+       if 'port' not in protocol_type_fields:
+           raise PLCInvalidArgument, "Must specify a port"
+       else:
+           protocol_types = PCUProtocolTypes(self.api, {'pcu_type_id': pcu_type['pcu_type_id']})
+           for protocol_type in protocol_types:
+               if protocol_type['port'] == protocol_type_fields['port']:
+                   raise PLCInvalidArgument, "Port alreay in use" 
+
+       protocol_type_fields = dict(filter(can_update, protocol_type_fields.items()))
+        protocol_type = PCUProtocolType(self.api, protocol_type_fields)
+       protocol_type['pcu_type_id'] = pcu_type['pcu_type_id']
+       protocol_type.sync()
+       self.event_object = {'PCUProtocolType': [protocol_type['pcu_protocol_type_id']]}        
+
+        return protocol_type['pcu_protocol_type_id']
diff --git a/PLC/Methods/AddPCUType.py b/PLC/Methods/AddPCUType.py
new file mode 100644 (file)
index 0000000..106791a
--- /dev/null
@@ -0,0 +1,35 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.PCUTypes import PCUType, PCUTypes
+from PLC.Auth import Auth
+
+can_update = lambda (field, value): field in \
+            ['model', 'name']  
+
+class AddPCUType(Method):
+    """
+    Adds a new pcu type.
+
+    Returns the new pcu_type_id (> 0) if successful, faults otherwise.
+    """
+
+    roles = ['admin']
+
+    pcu_type_fields = dict(filter(can_update, PCUType.fields.items()))
+
+    accepts = [
+        Auth(),
+        pcu_type_fields
+       ]
+
+    returns = Parameter(int, 'New pcu_type_id (> 0) if successful')
+
+    
+    def call(self, auth, pcu_type_fields):
+       pcu_type_fields = dict(filter(can_update, pcu_type_fields.items()))
+        pcu_type = PCUType(self.api, pcu_type_fields)
+       pcu_type.sync()
+       self.event_object = {'PCUType': [pcu_type['pcu_type_id']]}      
+
+        return pcu_type['pcu_type_id']
diff --git a/PLC/Methods/DeletePCUProtocolType.py b/PLC/Methods/DeletePCUProtocolType.py
new file mode 100644 (file)
index 0000000..ab66520
--- /dev/null
@@ -0,0 +1,33 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.PCUProtocolTypes import PCUProtocolType, PCUProtocolTypes
+from PLC.Auth import Auth
+
+class DeletePCUProtocolType(Method):
+    """
+    Deletes a PCU protocol type.
+
+    Returns 1 if successful, faults otherwise. 
+    """
+
+    roles = ['admin']
+
+    accepts = [
+        Auth(),
+        PCUProtocolType.fields['pcu_protocol_type_id']
+        ]
+
+    returns = Parameter(int, '1 if successful')
+    
+
+    def call(self, auth, protocol_type_id):
+        protocol_types = PCUProtocolTypes(self.api, [protocol_type_id])
+        if not protocol_types:
+            raise PLCInvalidArgument, "No such pcu protocol type"
+
+        protocol_type = protocol_types[0]
+        protocol_type.delete()
+       self.event_objects = {'PCUProtocolType': [protocol_type['pcu_protocol_type_id']]}
+
+        return 1
diff --git a/PLC/Methods/DeletePCUType.py b/PLC/Methods/DeletePCUType.py
new file mode 100644 (file)
index 0000000..d73c204
--- /dev/null
@@ -0,0 +1,33 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.PCUTypes import PCUType, PCUTypes
+from PLC.Auth import Auth
+
+class DeletePCUType(Method):
+    """
+    Deletes a PCU type.
+
+    Returns 1 if successful, faults otherwise. 
+    """
+
+    roles = ['admin']
+
+    accepts = [
+        Auth(),
+        PCUType.fields['pcu_type_id']
+        ]
+
+    returns = Parameter(int, '1 if successful')
+    
+
+    def call(self, auth, pcu_type_id):
+        pcu_types = PCUTypes(self.api, [pcu_type_id])
+        if not pcu_types:
+            raise PLCInvalidArgument, "No such pcu type"
+
+        pcu_type = pcu_types[0]
+        pcu_type.delete()
+       self.event_objects = {'PCUType': [pcu_type['pcu_type_id']]}
+
+        return 1
diff --git a/PLC/Methods/GetPCUProtocolTypes.py b/PLC/Methods/GetPCUProtocolTypes.py
new file mode 100644 (file)
index 0000000..44f9380
--- /dev/null
@@ -0,0 +1,40 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.PCUProtocolTypes import PCUProtocolType, PCUProtocolTypes
+from PLC.Auth import Auth
+from PLC.Filter import Filter
+
+class GetPCUProtocolTypes(Method):
+    """
+    Returns an array of PCU Types.
+    """
+
+    roles = ['admin', 'pi', 'user', 'tech', 'node']
+
+    accepts = [
+        Auth(),
+       Mixed([PCUProtocolType.fields['pcu_type_id']],
+               Filter(PCUProtocolType.fields)),
+        Parameter([str], "List of fields to return", nullok = True)
+        ]
+
+    returns = [PCUProtocolType.fields]
+    
+
+    def call(self, auth, protocol_type_filter = None, return_fields = None):
+
+       #Must query at least pcu_type_id
+       if return_fields is not None and 'pcu_protocol_type_id' not in return_fields:
+           return_fields.append('pcu_protocol_type_id')
+           added_fields = ['pcu_protocol_type_id']
+       else:
+           added_fields = []
+
+       protocol_types = PCUProtocolTypes(self.api, protocol_type_filter, return_fields)
+
+       for added_field in added_fields:
+           for protocol_type in protocol_types:
+               del protocol_type[added_field]
+               
+        return protocol_types 
diff --git a/PLC/Methods/GetPCUTypes.py b/PLC/Methods/GetPCUTypes.py
new file mode 100644 (file)
index 0000000..cf0d689
--- /dev/null
@@ -0,0 +1,48 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.PCUTypes import PCUType, PCUTypes
+from PLC.Auth import Auth
+from PLC.Filter import Filter
+
+class GetPCUTypes(Method):
+    """
+    Returns an array of PCU Types.
+    """
+
+    roles = ['admin', 'pi', 'user', 'tech', 'node']
+
+    accepts = [
+        Auth(),
+       Mixed([PCUType.fields['pcu_type_id'],
+               PCUType.fields['model']],
+               Filter(PCUType.fields)),
+        Parameter([str], "List of fields to return", nullok = True)
+        ]
+
+    returns = [PCUType.fields]
+    
+
+    def call(self, auth, pcu_type_filter = None, return_fields = None):
+
+       #Must query at least pcu_type_id
+       if return_fields is not None:
+           added_fields = []
+           if 'pcu_type_id' not in return_fields:
+               return_fields.append('pcu_type_id')
+               added_fields.append('pcu_type_id')
+           if 'pcu_protocol_types' in return_fields and \
+              'pcu_protocol_type_ids' not in return_fields:
+               return_fields.append('pcu_protocol_type_ids')
+               added_fields.append('pcu_protocol_type_ids') 
+       else:
+           added_fields = []
+
+       pcu_types = PCUTypes(self.api, pcu_type_filter, return_fields)
+
+       # remove added fields and protocol_types
+       for added_field in added_fields:
+           for pcu_type in pcu_types:
+               del pcu_type[added_field]
+               
+        return pcu_types 
diff --git a/PLC/Methods/UpdatePCUProtocolType.py b/PLC/Methods/UpdatePCUProtocolType.py
new file mode 100644 (file)
index 0000000..b1a30bc
--- /dev/null
@@ -0,0 +1,41 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.PCUProtocolTypes import PCUProtocolType, PCUProtocolTypes
+from PLC.Auth import Auth
+
+can_update = lambda (field, value): field in \
+             ['pcu_type_id', 'port', 'protocol', 'supported']
+
+class UpdatePCUProtocolType(Method):
+    """
+    Updates a pcu protocol type. Only the fields specified in
+    port_typee_fields are updated, all other fields are left untouched.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin']
+
+    protocol_type_fields = dict(filter(can_update, PCUProtocolType.fields.items()))
+
+    accepts = [
+        Auth(),
+        PCUProtocolType.fields['pcu_protocol_type_id'],
+        protocol_type_fields
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, protocol_type_id, protocol_type_fields):
+        protocol_type_fields = dict(filter(can_update, protocol_type_fields.items()))
+
+        protocol_types = PCUProtocolTypes(self.api, [protocol_type_id])
+        if not protocol_types:
+            raise PLCInvalidArgument, "No such pcu protocol type"
+
+        protocol_type = protocol_types[0]
+        protocol_type.update(protocol_type_fields)
+        protocol_type.sync()
+       self.event_objects = {'PCUProtocolType': [protocol_type['pcu_protocol_type_id']]}       
+        return 1
diff --git a/PLC/Methods/UpdatePCUType.py b/PLC/Methods/UpdatePCUType.py
new file mode 100644 (file)
index 0000000..fc4e886
--- /dev/null
@@ -0,0 +1,42 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.PCUTypes import PCUType, PCUTypes
+from PLC.Auth import Auth
+
+can_update = lambda (field, value): field in \
+             ['model', 'name']
+
+class UpdatePCUType(Method):
+    """
+    Updates a PCU type. Only the fields specified in
+    pcu_typee_fields are updated, all other fields are left untouched.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin']
+
+    pcu_type_fields = dict(filter(can_update, PCUType.fields.items()))
+
+    accepts = [
+        Auth(),
+        PCUType.fields['pcu_type_id'],
+        pcu_type_fields
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, pcu_type_id, pcu_type_fields):
+        pcu_type_fields = dict(filter(can_update, pcu_type_fields.items()))
+
+        pcu_types = PCUTypes(self.api, [pcu_type_id])
+        if not pcu_types:
+            raise PLCInvalidArgument, "No such pcu type"
+
+        pcu_type = pcu_types[0]
+        pcu_type.update(pcu_type_fields)
+        pcu_type.sync()
+       self.event_objects = {'PCUType': [pcu_type['pcu_type_id']]}     
+
+        return 1