systematic use of context managers for dealing with files instead of open()/close...
[nepi.git] / src / nepi / util / sfaapi.py
index fafb99d..136ae70 100644 (file)
@@ -3,9 +3,8 @@
 #    Copyright (C) 2013 INRIA
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU General Public License version 2 as
+#    published by the Free Software Foundation;
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -21,6 +20,7 @@ import threading
 import hashlib
 import re
 import os
+import time
 
 from nepi.util.logger import Logger
 
@@ -219,10 +219,9 @@ class SFAAPI(object):
 
             resources_urn = self._get_resources_urn(resources_hrn_new)
             rspec = self.rspec_proc.build_sfa_rspec(slicename, resources_urn, None, leases)
-            f = open("/tmp/rspec_input.rspec", "w")
-            f.truncate(0)
-            f.write(rspec)
-            f.close()
+            with open("/tmp/rspec_input.rspec", "w") as f:
+                f.truncate(0)
+                f.write(rspec)
             
             if not os.path.getsize("/tmp/rspec_input.rspec") > 0:
                 raise RuntimeError("Fail to create rspec file to allocate resource in slice %s" % slicename)
@@ -254,19 +253,24 @@ class SFAAPI(object):
         self._slice_resources_batch.append(resource_hrn)
         resources_hrn_new = list()
         if self._count == len(self._total):
+            check_all_inslice = self._check_all_inslice(self._slice_resources_batch, slicename)
+            if check_all_inslice == True:
+                return True
             for resource_hrn in self._slice_resources_batch:
                 resource_parts = resource_hrn.split('.')
                 resource_hrn = '.'.join(resource_parts[:2]) + '.' + '\\.'.join(resource_parts[2:])
                 resources_hrn_new.append(resource_hrn)
             with self.lock_slice:
-                self._sfi_exec_method('delete', slicename)
+                if check_all_inslice != 0:
+                    self._sfi_exec_method('delete', slicename)
+                    time.sleep(480)
+                
                 # Re implementing urn from hrn because the library sfa-common doesn't work for wilabt
                 resources_urn = self._get_urn(resources_hrn_new)
                 rspec = self.rspec_proc.build_sfa_rspec(slicename, resources_urn, properties, leases)
-                f = open("/tmp/rspec_input.rspec", "w")
-                f.truncate(0)
-                f.write(rspec)
-                f.close()
+                with open("/tmp/rspec_input.rspec", "w") as f:
+                    f.truncate(0)
+                    f.write(rspec)
 
                 if not os.path.getsize("/tmp/rspec_input.rspec") > 0:
                     raise RuntimeError("Fail to create rspec file to allocate resources in slice %s" % slicename)
@@ -293,6 +297,24 @@ class SFAAPI(object):
         else:
             self._log.debug(" Waiting for more nodes to add the batch to the slice ")
 
+    def _check_all_inslice(self, resources_hrn, slicename):
+        slice_res = self.get_slice_resources(slicename)['resource']
+        if slice_res:
+            if len(slice_res[0]['services']) != 0:
+                slice_res_hrn = self.get_resources_hrn(slice_res).values()
+                if self._compare_lists(slice_res_hrn, resources_hrn):
+                    return True
+                else: return len(slice_res_hrn)
+        return 0
+
+    def _compare_lists(self, list1, list2):
+        if len(list1) != len(list2):
+            return False
+        for item in list1:
+            if item not in list2:
+                return False
+        return True
+
     def _get_urn(self, resources_hrn):
         """
         Get urn from hrn.