various fixes, plus handle return code properly
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Thu, 9 Feb 2012 16:41:22 +0000 (17:41 +0100)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Thu, 9 Feb 2012 16:41:22 +0000 (17:41 +0100)
sfa/federica/fddriver.py

index bfd4827..cac050c 100644 (file)
@@ -1,9 +1,10 @@
 from sfa.util.sfalogging import logger
+from sfa.util.faults import SfaFault
 
 # this is probably too big to swallow but for a starting point..
 from sfa.plc.pldriver import PlDriver
 
-from sfa.fd.fdshell import FdShell
+from sfa.federica.fdshell import FdShell
 
 # hardwired for now
 # this could/should be obtained by issuing getRSpecVersion
@@ -15,6 +16,20 @@ class FdDriver (PlDriver):
         PlDriver.__init__ (self, config)
         self.shell=FdShell(config)
 
+    # the agreement with the federica driver is for them to expose results in a way
+    # compliant with the avpi v2 return code, i.e. a dict with 'code' 'value' 'output'
+    # essentially, either 'code'==0, then 'value' is set to the actual result
+    # otherwise, 'code' is set to an error code and 'output' holds an error message
+    def response (self, from_xmlrpc):
+        if isinstance (from_xmlrpc, dict) and 'code' in from_xmlrpc:
+            if from_xmlrpc['code']==0:
+                return from_xmlrpc['value']
+            else:
+                raise SfaFault(from_xmlrpc['code'],from_xmlrpc['output'])
+        else:
+            logger.warning("unexpected result from federica xmlrpc api")
+            return from_xmlrpc
+
     def aggregate_version (self):
         return { 'federica_version_string' : federica_version_string, }
 
@@ -22,7 +37,7 @@ class FdDriver (PlDriver):
         return "federica"
 
     def list_slices (self, creds, options):
-        return self.shell.listSlices()
+        return self.response(self.shell.listSlices())
 
     def sliver_status (self, slice_urn, slice_hrn):
         return "fddriver.sliver_status: undefined/todo for slice %s"%slice_hrn
@@ -36,21 +51,22 @@ class FdDriver (PlDriver):
         if not slice_hrn:
             # self.cache is initialized unless the global config has it turned off
             if cached_requested and self.cache:
+                # using federica_version_string as the key into the cache
                 rspec = self.cache.get(federica_version_string)
                 if rspec:
                     logger.debug("FdDriver.ListResources: returning cached advertisement")
-                    return rspec 
+                    return self.response(rspec)
             # otherwise, need to get it
             rspec = self.shell.listAvailableResources (federica_version_string)
             # cache it for future use
             if self.cache:
                 logger.debug("FdDriver.ListResources: stores advertisement in cache")
                 self.cache.add(federica_version_string, rspec)
-            return rspec
+            return self.response(rspec)
         # about a given slice : don't cache
         else:
 # that's what the final version would look like
-            return self.shell.listSliceResources(federica_version_string, slice_urn)
+            return self.response(self.shell.listSliceResources(federica_version_string, slice_urn))
 # # just to see how the ad shows up in sface
 # # caching it for convenience as it's the ad anyways
 #             if cached_requested and self.cache:
@@ -65,14 +81,14 @@ class FdDriver (PlDriver):
     def create_sliver (self, slice_urn, slice_hrn, creds, rspec_string, users, options):
         # right now version_string is ignored on the federica side
         # we normally derive it from options
-        return  self.shell.createSlice(creds, slice_urn, federica_version_string, rspec_string)
+        return self.response(self.shell.createSlice(creds, slice_urn, federica_version_string, rspec_string))
 
-#String deleteSlice(String credentials, String rspecVersion, String sliceUrn):
+#String deleteSlice(String credentials, String sliceUrn):
     def delete_sliver (self, slice_urn, slice_hrn, creds, options):
         # right now version_string is ignored on the federica side
         # we normally derive it from options
         # xxx not sure if that's currentl supported at all
-        return self.shell.deleteSlice(creds, federica_version_string, slice_urn)
+        return self.response(self.shell.deleteSlice(creds, slice_urn))
 
     # for the the following methods we use what is provided by the default driver class
     #def renew_sliver (self, slice_urn, slice_hrn, creds, expiration_time, options):