BugFix in LinuxNode unit test
[nepi.git] / src / nepi / execution / resource.py
index 9637fea..5efc2c3 100644 (file)
@@ -1,28 +1,28 @@
-"""
-    NEPI, a framework to manage network experiments
-    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.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-"""
+#
+#    NEPI, a framework to manage network experiments
+#    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.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+# Author: Alina Quereilhac <alina.quereilhac@inria.fr>
 
 from nepi.util.timefuncs import strfnow, strfdiff, strfvalid
+from nepi.util.logger import Logger
 from nepi.execution.trace import TraceAttr
 
 import copy
 import functools
-import inspect
 import logging
 import os
 import pkgutil
@@ -47,15 +47,15 @@ class ResourceState:
     RELEASED = 8
 
 ResourceState2str = dict({
-    NEW = "NEW",
-    DISCOVERED = "DISCOVERED",
-    PROVISIONED = "PROVISIONED",
-    READY = "READY",
-    STARTED = "STARTED",
-    STOPPED = "STOPPED",
-    FINISHED = "FINISHED",
-    FAILED = "FAILED",
-    RELEASED = "RELEASED",
+    ResourceState.NEW : "NEW",
+    ResourceState.DISCOVERED : "DISCOVERED",
+    ResourceState.PROVISIONED : "PROVISIONED",
+    ResourceState.READY : "READY",
+    ResourceState.STARTED : "STARTED",
+    ResourceState.STOPPED : "STOPPED",
+    ResourceState.FINISHED : "FINISHED",
+    ResourceState.FAILED : "FAILED",
+    ResourceState.RELEASED : "RELEASED",
     })
 
 def clsinit(cls):
@@ -75,7 +75,7 @@ def clsinit_copy(cls):
 
 # Decorator to invoke class initialization method
 @clsinit
-class ResourceManager(object):
+class ResourceManager(Logger):
     _rtype = "Resource"
     _attributes = None
     _traces = None
@@ -178,6 +178,8 @@ class ResourceManager(object):
         return copy.deepcopy(cls._traces.values())
 
     def __init__(self, ec, guid):
+        super(ResourceManager, self).__init__(self.rtype())
+        
         self._guid = guid
         self._ec = weakref.ref(ec)
         self._connections = set()
@@ -198,39 +200,6 @@ class ResourceManager(object):
         self._ready_time = None
         self._release_time = None
 
-        # Logging
-        self._logger = logging.getLogger("Resource")
-
-    def debug(self, msg, out = None, err = None):
-        self.log(msg, logging.DEBUG, out, err)
-
-    def error(self, msg, out = None, err = None):
-        self.log(msg, logging.ERROR, out, err)
-
-    def warn(self, msg, out = None, err = None):
-        self.log(msg, logging.WARNING, out, err)
-
-    def info(self, msg, out = None, err = None):
-        self.log(msg, logging.INFO, out, err)
-
-    def log(self, msg, level, out = None, err = None):
-        if out:
-            msg += " - OUT: %s " % out
-
-        if err:
-            msg += " - ERROR: %s " % err
-
-        msg = self.log_message(msg)
-
-        self.logger.log(level, msg)
-
-    def log_message(self, msg):
-        return " %s guid: %d - %s " % (self._rtype, self.guid, msg)
-
-    @property
-    def logger(self):
-        return self._logger
-
     @property
     def guid(self):
         return self._guid
@@ -281,6 +250,9 @@ class ResourceManager(object):
     def state(self):
         return self._state
 
+    def log_message(self, msg):
+        return " %s guid: %d - %s " % (self._rtype, self.guid, msg)
+
     def connect(self, guid):
         if self.valid_connection(guid):
             self._connections.add(guid)
@@ -559,7 +531,7 @@ class ResourceManager(object):
             callback = functools.partial(self.stop_with_conditions)
             self.ec.schedule(delay, callback)
         else:
-            self.logger.debug(" ----- STOPPING ---- ") 
+            self.debug(" ----- STOPPING ---- ") 
             self.stop()
 
     def deploy(self):
@@ -570,7 +542,7 @@ class ResourceManager(object):
             self.error("Wrong state %s for deploy" % self.state)
             return
 
-        self.debug("----- DEPLOYING ---- ")
+        self.debug("----- READY ---- ")
         self._ready_time = strfnow()
         self._state = ResourceState.READY
 
@@ -616,6 +588,7 @@ def find_types():
     search_path = os.environ.get("NEPI_SEARCH_PATH", "")
     search_path = set(search_path.split(" "))
    
+    import inspect
     import nepi.resources 
     path = os.path.dirname(nepi.resources.__file__)
     search_path.add(path)
@@ -642,6 +615,7 @@ def find_types():
                     types.append(attr)
         except:
             import traceback
+            import logging
             err = traceback.format_exc()
             logger = logging.getLogger("Resource.find_types()")
             logger.error("Error while lading Resource Managers %s" % err)