Adding more error check in OARrestapi.
[sfa.git] / sfa / iotlab / OARrestapi.py
index 1867908..5731cd1 100644 (file)
@@ -213,21 +213,31 @@ class OARrestapi:
             conn = HTTPConnection(self.oarserver['ip'],
                                   self.oarserver['port'])
             conn.request("GET", self.oarserver['uri'], data, headers)
-            resp = (conn.getresponse()).read()
+            resp = conn.getresponse()
+            body = resp.read()
+        except Exception as error:
+            logger.log_exc("GET_OAR_SRVR : Connection error: %s "
+                % (error))
+            raise Exception ("GET_OAR_SRVR : Connection error %s " %(error))
+
+        finally:
             conn.close()
 
-        except HTTPException, error:
-            logger.log_exc("GET_OAR_SRVR : Problem with OAR server : %s "
-                           % (error))
+        except HTTPException, error:
+            logger.log_exc("GET_OAR_SRVR : Problem with OAR server : %s "
+                           % (error))
             #raise ServerError("GET_OAR_SRVR : Could not reach OARserver")
+        if resp.status >= 400:
+            raise ValueError ("Response Error %s, %s" %(resp.status,
+                resp.reason))
         try:
-            js_dict = json.loads(resp)
+            js_dict = json.loads(body)
             #print "\r\n \t\t\t js_dict keys" , js_dict.keys(), " \r\n", js_dict
             return js_dict
 
         except ValueError, error:
             logger.log_exc("Failed to parse Server Response: %s ERROR %s"
-                           % (js_dict, error))
+                           % (body, error))
             #raise ServerError("Failed to parse Server Response:" + js)
 
 
@@ -260,17 +270,28 @@ class OARrestapi:
             conn = HTTPConnection(self.oarserver['ip'], \
                                         self.oarserver['port'])
             conn.request("POST", self.oarserver['uri'], data, headers)
-            resp = (conn.getresponse()).read()
-            conn.close()
+            resp = conn.getresponse()
+            body = resp.read()
+
         except NotConnected:
             logger.log_exc("POSTRequestToOARRestAPI NotConnected ERROR: \
                             data %s \r\n \t\n \t\t headers %s uri %s" \
                             %(data,headers,self.oarserver['uri']))
+        except Exception as error:
+            logger.log_exc("POST_OAR_SERVER : Connection error: %s "
+                % (error))
+            raise Exception ("POST_OAR_SERVER : Connection error %s " %(error))
+
+        finally:
+            conn.close()
+
+        if resp.status >= 400:
+            raise ValueError ("Response Error %s, %s" %(resp.status,
+                resp.reason))
 
-            #raise ServerError("POST_OAR_SRVR : error")
 
         try:
-            answer = json.loads(resp)
+            answer = json.loads(body)
             logger.debug("POSTRequestToOARRestAPI : answer %s" % (answer))
             return answer
 
@@ -307,6 +328,7 @@ class ParsingResourcesFull():
         'archi': self.AddHardwareType,
         'state': self.AddBootState,
         'id': self.AddOarNodeId,
+        'mobility_type': self.AddMobilityType,
         }
 
 
@@ -380,6 +402,21 @@ class ParsingResourcesFull():
     #     """
     #     tuplelist.append(('radio', str(value)))
 
+    def AddMobilityType(self, tuplelist, value):
+        """Adds  which kind of mobility it is, train or roomba robot.
+
+        :param tuplelist: tuple list on which to add the node's mobility status.
+            The tuplelist is the value associated with the node's id in the
+            OARGETParser's dictionary node_dictlist.
+        :param value: tells if a node is a mobile node or not. The value is
+            found in the json.
+
+        :type tuplelist: list
+        :type value: integer
+
+        """
+        tuplelist.append(('mobility_type', str(value)))
+
 
     def AddMobility(self, tuplelist, value):
         """Add if the node is a mobile node or not to the tuple list.
@@ -387,8 +424,8 @@ class ParsingResourcesFull():
         :param tuplelist: tuple list on which to add the node's mobility status.
             The tuplelist is the value associated with the node's id in the
             OARGETParser's dictionary node_dictlist.
-        :param value: tells if a node is a mobile node or not. The value is found
-            in the json.
+        :param value: tells if a node is a mobile node or not. The value is
+            found in the json.
 
         :type tuplelist: list
         :type value: integer
@@ -396,6 +433,8 @@ class ParsingResourcesFull():
         .. seealso:: AddNodeNetworkAddr
 
         """
+        # future support of mobility type
+
         if value is 0:
             tuplelist.append(('mobile', 'False'))
         else:
@@ -416,7 +455,7 @@ class ParsingResourcesFull():
          .. seealso:: AddNodeNetworkAddr
 
         """
-        tuplelist.append(('posx', value ))
+        tuplelist.append(('posx', value))