Fixing bug 1437
[osm/RO.git] / RO-VIM-openvim / osm_rovim_openvim / vimconn_openvim.py
index c89a303..8fb02cc 100644 (file)
@@ -27,7 +27,7 @@ vimconnector implements all the methods to interact with openvim using the openv
 __author__="Alfonso Tierno, Gerardo Garcia"
 __date__ ="$26-aug-2014 11:09:29$"
 
-from osm_ro import vimconn
+from osm_ro_plugin import vimconn
 import requests
 import json
 import yaml
@@ -323,13 +323,14 @@ get_processor_rankings_response_schema = {
     }
 }
 
-class vimconnector(vimconn.vimconnector):
+
+class vimconnector(vimconn.VimConnector):
     def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None,
                  log_level="DEBUG", config={}, persistent_info={}):
-        vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level, config)
+        vimconn.VimConnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level, config)
         self.tenant = None
         self.headers_req = {'content-type': 'application/json'}
-        self.logger = logging.getLogger('openmano.vim.openvim')
+        self.logger = logging.getLogger('ro.vim.openvim')
         self.persistent_info = persistent_info
         if tenant_id:
             self.tenant = tenant_id
@@ -342,7 +343,7 @@ class vimconnector(vimconn.vimconnector):
             self.tenant = value
         elif index=='tenant_name':
             self.tenant = None
-        vimconn.vimconnector.__setitem__(self,index, value)    
+        vimconn.VimConnector.__setitem__(self,index, value)    
 
     def _get_my_tenant(self):
         '''Obtain uuid of my tenant from name
@@ -357,13 +358,13 @@ class vimconnector(vimconn.vimconnector):
         try:
             tenant_list = vim_response.json()["tenants"]
             if len(tenant_list) == 0:
-                raise vimconn.vimconnNotFoundException("No tenant found for name '{}'".format(self.tenant_name))
+                raise vimconn.VimConnNotFoundException("No tenant found for name '{}'".format(self.tenant_name))
             elif len(tenant_list) > 1:
-                raise vimconn.vimconnConflictException ("More that one tenant found for name '{}'".format(self.tenant_name))
+                raise vimconn.VimConnConflictException ("More that one tenant found for name '{}'".format(self.tenant_name))
             self.tenant = tenant_list[0]["id"]
             return self.tenant
         except Exception as e:
-            raise vimconn.vimconnUnexpectedResponse("Get VIM tenant {} '{}'".format(type(e).__name__, str(e)))
+            raise vimconn.VimConnUnexpectedResponse("Get VIM tenant {} '{}'".format(type(e).__name__, str(e)))
 
     def _format_jsonerror(self,http_response):
         #DEPRECATED, to delete in the future
@@ -391,13 +392,16 @@ class vimconnector(vimconn.vimconnector):
                 a= self._remove_extra_items(d, schema['items'])
                 if a is not None: deleted.append(a)
         elif type(data) is dict:
+            to_delete = []
             for k in data.keys():
                 if 'properties' not in schema or k not in schema['properties'].keys():
-                    del data[k]
+                    to_delete.append(k)
                     deleted.append(k)
                 else:
                     a = self._remove_extra_items(data[k], schema['properties'][k])
                     if a is not None:  deleted.append({k:a})
+            for k in to_delete:
+                del data[k]
         if len(deleted) == 0: return None
         elif len(deleted) == 1: return deleted[0]
         else: return deleted
@@ -405,24 +409,24 @@ class vimconnector(vimconn.vimconnector):
     def _format_request_exception(self, request_exception):
         '''Transform a request exception into a vimconn exception'''
         if isinstance(request_exception, js_e.ValidationError):
-            raise vimconn.vimconnUnexpectedResponse("jsonschema exception '{}' at '{}'".format(request_exception.message, request_exception.path))            
+            raise vimconn.VimConnUnexpectedResponse("jsonschema exception '{}' at '{}'".format(request_exception.message, request_exception.path))            
         elif isinstance(request_exception, requests.exceptions.HTTPError):
-            raise vimconn.vimconnUnexpectedResponse(type(request_exception).__name__ + ": " + str(request_exception))
+            raise vimconn.VimConnUnexpectedResponse(type(request_exception).__name__ + ": " + str(request_exception))
         else:
-            raise vimconn.vimconnConnectionException(type(request_exception).__name__ + ": " + str(request_exception))
+            raise vimconn.VimConnConnectionException(type(request_exception).__name__ + ": " + str(request_exception))
 
     def _check_http_request_response(self, request_response):
         '''Raise a vimconn exception if the response is not Ok'''
         if request_response.status_code >= 200 and  request_response.status_code < 300:
             return
         if request_response.status_code == vimconn.HTTP_Unauthorized:
-            raise vimconn.vimconnAuthException(request_response.text)
+            raise vimconn.VimConnAuthException(request_response.text)
         elif request_response.status_code == vimconn.HTTP_Not_Found:
-            raise vimconn.vimconnNotFoundException(request_response.text)
+            raise vimconn.VimConnNotFoundException(request_response.text)
         elif request_response.status_code == vimconn.HTTP_Conflict:
-            raise vimconn.vimconnConflictException(request_response.text)
+            raise vimconn.VimConnConflictException(request_response.text)
         else: 
-            raise vimconn.vimconnUnexpectedResponse("VIM HTTP_response {}, {}".format(request_response.status_code, str(request_response.text)))
+            raise vimconn.VimConnUnexpectedResponse("VIM HTTP_response {}, {}".format(request_response.status_code, str(request_response.text)))
 
     def new_tenant(self,tenant_name,tenant_description):
         '''Adds a new tenant to VIM with this name and description, returns the tenant identifier'''
@@ -484,7 +488,7 @@ class vimconnector(vimconn.vimconnector):
         except requests.exceptions.RequestException as e:
             self._format_request_exception(e)
 
-    def new_network(self,net_name, net_type, ip_profile=None, shared=False, vlan=None): #, **vim_specific):
+    def new_network(self,net_name, net_type, ip_profile=None, shared=False, provider_network_profile=None): #, **vim_specific):
         """Adds a tenant network to VIM
         Params:
             'net_name': name of the network
@@ -501,7 +505,7 @@ class vimconnector(vimconn.vimconnector):
                 'dhcp_start_address': ip_schema, first IP to grant
                 'dhcp_count': number of IPs to grant.
             'shared': if this network can be seen/use by other tenants/organization
-            'vlan': in case of a data or ptp net_type, the intended vlan tag to be used for the network
+            'provider_network_profile': (optional) contains {segmentation-id: vlan, provider-network: vim_netowrk}
         Returns a tuple with the network identifier and created_items, or raises an exception on error
             created_items can be None or a dictionary where this method can include key-values that will be passed to
             the method delete_network. Can be used to store created segments, created l2gw connections, etc.
@@ -509,6 +513,9 @@ class vimconnector(vimconn.vimconnector):
             as not present.
         """
         try:
+            vlan = None
+            if provider_network_profile:
+                vlan = provider_network_profile.get("segmentation-id")
             created_items = {}
             self._get_my_tenant()
             if net_type=="bridge":
@@ -752,9 +759,9 @@ class vimconnector(vimconn.vimconnector):
             #if r is not None: 
             #    self.logger.warn("Warning: remove extra items %s", str(r))
             if len(response['images'])==0:
-                raise vimconn.vimconnNotFoundException("Image not found at VIM with path '{}'".format(path))
+                raise vimconn.VimConnNotFoundException("Image not found at VIM with path '{}'".format(path))
             elif len(response['images'])>1:
-                raise vimconn.vimconnConflictException("More than one image found at VIM with path '{}'".format(path))
+                raise vimconn.VimConnConflictException("More than one image found at VIM with path '{}'".format(path))
             return response['images'][0]['id']
         except (requests.exceptions.RequestException, js_e.ValidationError) as e:
             self._format_request_exception(e)
@@ -1026,11 +1033,11 @@ class vimconnector(vimconn.vimconnector):
                 if vm['status'] == "ACTIVE" and not management_ip:
                     vm['status'] = "ACTIVE:NoMgmtIP"
                     
-            except vimconn.vimconnNotFoundException as e:
+            except vimconn.VimConnNotFoundException as e:
                 self.logger.error("Exception getting vm status: %s", str(e))
                 vm['status'] = "DELETED"
                 vm['error_msg'] = str(e)
-            except (requests.exceptions.RequestException, js_e.ValidationError, vimconn.vimconnException) as e:
+            except (requests.exceptions.RequestException, js_e.ValidationError, vimconn.VimConnException) as e:
                 self.logger.error("Exception getting vm status: %s", str(e))
                 vm['status'] = "VIM_ERROR"
                 vm['error_msg'] = str(e)
@@ -1076,11 +1083,11 @@ class vimconnector(vimconn.vimconnector):
                 if net_vim.get('last_error'):
                     net['error_msg'] = net_vim['last_error']
                 net["vim_info"] = yaml.safe_dump(net_vim)
-            except vimconn.vimconnNotFoundException as e:
+            except vimconn.VimConnNotFoundException as e:
                 self.logger.error("Exception getting net status: %s", str(e))
                 net['status'] = "DELETED"
                 net['error_msg'] = str(e)
-            except (requests.exceptions.RequestException, js_e.ValidationError, vimconn.vimconnException) as e:
+            except (requests.exceptions.RequestException, js_e.ValidationError, vimconn.VimConnException) as e:
                 self.logger.error("Exception getting net status: %s", str(e))
                 net['status'] = "VIM_ERROR"
                 net['error_msg'] = str(e)
@@ -1093,7 +1100,7 @@ class vimconnector(vimconn.vimconnector):
         try:
             self._get_my_tenant()
             if "console" in action_dict:
-                raise vimconn.vimconnException("getting console is not available at openvim", http_code=vimconn.HTTP_Service_Unavailable)
+                raise vimconn.VimConnException("getting console is not available at openvim", http_code=vimconn.HTTP_Service_Unavailable)
             url = self.url+'/'+self.tenant+'/servers/'+vm_id+"/action"
             self.logger.info("Action over VM instance POST %s", url)
             vim_response = requests.post(url, headers = self.headers_req, data=json.dumps(action_dict) )