fixing flake8 tests
[osm/RO.git] / RO-VIM-opennebula / osm_rovim_opennebula / vimconn_opennebula.py
index d788dcb..a84b665 100644 (file)
@@ -28,16 +28,17 @@ vimconnector implements all the methods to interact with OpenNebula using the XM
 __author__ = "Jose Maria Carmona Perez,Juan Antonio Hernando Labajo, Emilio Abraham Garrido Garcia,Alberto Florez " \
              "Pages, Andres Pozo Munoz, Santiago Perez Marin, Onlife Networks Telefonica I+D Product Innovation "
 __date__ = "$13-dec-2017 11:09:29$"
-from osm_ro import vimconn
+from osm_ro_plugin import vimconn
 import requests
-import logging
+import logging
 import oca
-import untangle
+import untangle
 import math
 import random
 import pyone
 
-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={}):
 
@@ -59,7 +60,7 @@ class vimconnector(vimconn.vimconnector):
             check against the VIM
         """
 
-        vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level,
+        vimconn.VimConnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level,
                                       config)
 
     def _new_one_connection(self):
@@ -95,7 +96,7 @@ class vimconnector(vimconn.vimconnector):
                                 return str(group.id)
         except Exception as e:
             self.logger.error("Create new tenant error: " + str(e))
-            raise vimconn.vimconnException(e)
+            raise vimconn.VimConnException(e)
 
     def delete_tenant(self, tenant_id):
         """Delete a tenant from VIM. Returns the old tenant identifier"""
@@ -112,10 +113,10 @@ class vimconnector(vimconn.vimconnector):
                             self._delete_secondarygroup(user.id, group.id)
                             group.delete(client)
                     return None
-            raise vimconn.vimconnNotFoundException("Group {} not found".format(tenant_id))
+            raise vimconn.VimConnNotFoundException("Group {} not found".format(tenant_id))
         except Exception as e:
             self.logger.error("Delete tenant " + str(tenant_id) + " error: " + str(e))
-            raise vimconn.vimconnException(e)
+            raise vimconn.VimConnException(e)
 
     def _add_secondarygroup(self, id_user, id_group):
         # change secondary_group to primary_group
@@ -154,7 +155,7 @@ class vimconnector(vimconn.vimconnector):
                    </methodCall>'.format(self.user, self.passwd, (str(id_user)), (str(id_group)))
         requests.post(self.url, params)
 
-    def new_network(self, net_name, net_type, ip_profile=None, shared=False, provider_network_profile=None):  # , **vim_specific):
+    def new_network(self, net_name, net_type, ip_profile=None, shared=False, provider_network_profile=None):
         """Adds a tenant network to VIM
         Params:
             'net_name': name of the network
@@ -200,47 +201,47 @@ class vimconnector(vimconn.vimconnector):
                     size = int(math.pow(2, 32 - prefix))
                 if "dhcp_start_address" in ip_profile and ip_profile["dhcp_start_address"] is not None:
                     ip_start = str(ip_profile["dhcp_start_address"])
-                if ip_profile["ip_version"] == "IPv6":
-                    ip_prefix_type = "GLOBAL_PREFIX"
+                if ip_profile["ip_version"] == "IPv6":
+                    ip_prefix_type = "GLOBAL_PREFIX"
 
             if vlan is not None:
                 vlan_id = vlan
             else:
                 vlan_id = str(random.randint(100, 4095))
-            #if "internal" in net_name:
+            # if "internal" in net_name:
             # OpenNebula not support two networks with same name
             random_net_name = str(random.randint(1, 1000000))
             net_name = net_name + random_net_name
             net_id = one.vn.allocate({
-                        'NAME': net_name,
-                        'VN_MAD': '802.1Q',
-                        'PHYDEV': self.config["network"]["phydev"],
-                        'VLAN_ID': vlan_id
-                    }, self.config["cluster"]["id"])
-            arpool = {'AR_POOL': {
-                        'AR': {
-                            'TYPE': 'IP4',
-                            'IP': ip_start,
-                            'SIZE': size
-                        }
+                'NAME': net_name,
+                'VN_MAD': '802.1Q',
+                'PHYDEV': self.config["network"]["phydev"],
+                'VLAN_ID': vlan_id
+            }, self.config["cluster"]["id"])
+            arpool = {
+                'AR_POOL': {
+                    'AR': {
+                        'TYPE': 'IP4',
+                        'IP': ip_start,
+                        'SIZE': size
                     }
+                }
             }
             one.vn.add_ar(net_id, arpool)
             return net_id, created_items
         except Exception as e:
             self.logger.error("Create new network error: " + str(e))
-            raise vimconn.vimconnException(e)
+            raise vimconn.VimConnException(e)
 
     def get_network_list(self, filter_dict={}):
         """Obtain tenant networks of VIM
-        Params:
-            'filter_dict' (optional) contains entries to return only networks that matches ALL entries:
-                name: string  => returns only networks with this name
-                id:   string  => returns networks with this VIM id, this imply returns one network at most
-                shared: boolean >= returns only networks that are (or are not) shared
-                tenant_id: sting => returns only networks that belong to this tenant/project
-                ,#(not used yet) admin_state_up: boolean => returns only networks that are (or are not) in admin state active
-                #(not used yet) status: 'ACTIVE','ERROR',... => filter networks that are on this status
+        :params filter_dict: (optional) contains entries to return only networks that matches ALL entries:
+            name: string  => returns only networks with this name
+            id:   string  => returns networks with this VIM id, this imply returns one network at most
+            shared: boolean >= returns only networks that are (or are not) shared
+            tenant_id: sting => returns only networks that belong to this tenant/project
+            (not used yet) admin_state_up: boolean => returns only networks that are (or are not) in admin state active
+            (not used yet) status: 'ACTIVE','ERROR',... => filter networks that are on this status
         Returns the network list of dictionaries. each dictionary contains:
             'id': (mandatory) VIM network id
             'name': (mandatory) VIM network name
@@ -272,7 +273,7 @@ class vimconnector(vimconn.vimconnector):
             return response
         except Exception as e:
             self.logger.error("Get network list error: " + str(e))
-            raise vimconn.vimconnException(e)
+            raise vimconn.VimConnException(e)
 
     def get_network(self, net_id):
         """Obtain network details from the 'net_id' VIM network
@@ -297,10 +298,10 @@ class vimconnector(vimconn.vimconnector):
             if net:
                 return net
             else:
-                raise vimconn.vimconnNotFoundException("Network {} not found".format(net_id))
+                raise vimconn.VimConnNotFoundException("Network {} not found".format(net_id))
         except Exception as e:
             self.logger.error("Get network " + str(net_id) + " error): " + str(e))
-            raise vimconn.vimconnException(e)
+            raise vimconn.VimConnException(e)
 
     def delete_network(self, net_id, created_items=None):
         """
@@ -316,7 +317,7 @@ class vimconnector(vimconn.vimconnector):
             return net_id
         except Exception as e:
             self.logger.error("Delete network " + str(net_id) + "error: network not found" + str(e))
-            raise vimconn.vimconnException(e)
+            raise vimconn.VimConnException(e)
 
     def refresh_nets_status(self, net_list):
         """Get the status of the networks
@@ -343,17 +344,17 @@ class vimconnector(vimconn.vimconnector):
                     net_vim = self.get_network(net_id)
                     net["status"] = net_vim["status"]
                     net["vim_info"] = None
-                except vimconn.vimconnNotFoundException as e:
+                except vimconn.VimConnNotFoundException as e:
                     self.logger.error("Exception getting net status: {}".format(str(e)))
                     net['status'] = "DELETED"
                     net['error_msg'] = str(e)
-                except vimconn.vimconnException as e:
+                except vimconn.VimConnException as e:
                     self.logger.error(e)
                     net["status"] = "VIM_ERROR"
                     net["error_msg"] = str(e)
                 net_dict[net_id] = net
             return net_dict
-        except vimconn.vimconnException as e:
+        except vimconn.VimConnException as e:
             self.logger.error(e)
             for k in net_dict:
                 net_dict[k]["status"] = "VIM_ERROR"
@@ -371,10 +372,10 @@ class vimconnector(vimconn.vimconnector):
             template = one.template.info(int(flavor_id))
             if template is not None:
                 return {'id': template.ID, 'name': template.NAME}
-            raise vimconn.vimconnNotFoundException("Flavor {} not found".format(flavor_id))
+            raise vimconn.VimConnNotFoundException("Flavor {} not found".format(flavor_id))
         except Exception as e:
             self.logger.error("get flavor " + str(flavor_id) + " error: " + str(e))
-            raise vimconn.vimconnException(e)
+            raise vimconn.VimConnException(e)
 
     def new_flavor(self, flavor_data):
         """Adds a tenant flavor to VIM
@@ -384,9 +385,9 @@ class vimconnector(vimconn.vimconnector):
                 vpcus: cpus (cloud type)
                 extended: EPA parameters
                   - numas: #items requested in same NUMA
-                        memory: number of 1G huge pages memory
-                        paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual threads
-                        interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa
+                      memory: number of 1G huge pages memory
+                      paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual threads
+                      interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa
                           - name: interface name
                             dedicated: yes|no|yes:sriov;  for PT, SRIOV or only one SRIOV for the physical NIC
                             bandwidth: X Gbps; requested guarantee bandwidth
@@ -424,7 +425,7 @@ class vimconnector(vimconn.vimconnector):
 
         except Exception as e:
             self.logger.error("Create new flavor error: " + str(e))
-            raise vimconn.vimconnException(e)
+            raise vimconn.VimConnException(e)
 
     def delete_flavor(self, flavor_id):
         """ Deletes a tenant flavor from VIM
@@ -436,7 +437,7 @@ class vimconnector(vimconn.vimconnector):
             return flavor_id
         except Exception as e:
             self.logger.error("Error deleting flavor " + str(flavor_id) + ". Flavor not found")
-            raise vimconn.vimconnException(e)
+            raise vimconn.VimConnException(e)
 
     def get_image_list(self, filter_dict={}):
         """Obtain tenant images from VIM
@@ -468,64 +469,69 @@ class vimconnector(vimconn.vimconnector):
             return images
         except Exception as e:
             self.logger.error("Get image list error: " + str(e))
-            raise vimconn.vimconnException(e)
+            raise vimconn.VimConnException(e)
 
     def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None,
                        availability_zone_index=None, availability_zone_list=None):
-
-        """Adds a VM instance to VIM
-            Params:
-                'start': (boolean) indicates if VM must start or created in pause mode.
-                'image_id','flavor_id': image and flavor VIM id to use for the VM
-                'net_list': list of interfaces, each one is a dictionary with:
-                    'name': (optional) name for the interface.
-                    'net_id': VIM network id where this interface must be connect to. Mandatory for type==virtual
-                    'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM capabilities
-                    'model': (optional and only have sense for type==virtual) interface model: virtio, e1000, ...
-                    'mac_address': (optional) mac address to assign to this interface
-                    'ip_address': (optional) IP address to assign to this interface
-                    #TODO: CHECK if an optional 'vlan' parameter is needed for VIMs when type if VF and net_id is not provided,
-                        the VLAN tag to be used. In case net_id is provided, the internal network vlan is used for tagging VF
-                    'type': (mandatory) can be one of:
-                        'virtual', in this case always connected to a network of type 'net_type=bridge'
-                        'PCI-PASSTHROUGH' or 'PF' (passthrough): depending on VIM capabilities it can be connected to a data/ptp network ot it
-                            can created unconnected
-                        'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity.
-                        'VFnotShared'(SRIOV without VLAN tag) same as PF for network connectivity. VF where no other VFs
-                                are allocated on the same physical NIC
-                    'bw': (optional) only for PF/VF/VFnotShared. Minimal Bandwidth required for the interface in GBPS
-                    'port_security': (optional) If False it must avoid any traffic filtering at this interface. If missing
-                                    or True, it must apply the default VIM behaviour
-                    After execution the method will add the key:
-                    'vim_id': must be filled/added by this method with the VIM identifier generated by the VIM for this
-                            interface. 'net_list' is modified
-                'cloud_config': (optional) dictionary with:
-                    'key-pairs': (optional) list of strings with the public key to be inserted to the default user
-                    'users': (optional) list of users to be inserted, each item is a dict with:
-                        'name': (mandatory) user name,
-                        'key-pairs': (optional) list of strings with the public key to be inserted to the user
-                    'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
-                        or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
-                    'config-files': (optional). List of files to be transferred. Each item is a dict with:
-                        'dest': (mandatory) string with the destination absolute path
-                        'encoding': (optional, by default text). Can be one of:
-                            'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
-                        'content' (mandatory): string with the content of the file
-                        'permissions': (optional) string with file permissions, typically octal notation '0644'
-                        'owner': (optional) file owner, string with the format 'owner:group'
-                    'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
-                'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
-                    'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
-                    'size': (mandatory) string with the size of the disk in GB
-                availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
-                availability_zone_list: list of availability zones given by user in the VNFD descriptor.  Ignore if
+        """
+        Adds a VM instance to VIM
+        :param name:
+        :param description:
+        :param start: (boolean) indicates if VM must start or created in pause mode.
+        :param image_id: image VIM id to use for the VM
+        :param flavor_id: flavor VIM id to use for the VM
+        :param net_list:  list of interfaces, each one is a dictionary with:
+            'name': (optional) name for the interface.
+            'net_id': VIM network id where this interface must be connect to. Mandatory for type==virtual
+            'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM
+                 capabilities
+            'model': (optional and only have sense for type==virtual) interface model: virtio, e1000, ...
+            'mac_address': (optional) mac address to assign to this interface
+            'ip_address': (optional) IP address to assign to this interface
+            #TODO: CHECK if an optional 'vlan' parameter is needed for VIMs when type if VF and net_id is not
+                 provided, the VLAN tag to be used. In case net_id is provided, the internal network vlan is
+                  used for tagging VF
+            'type': (mandatory) can be one of:
+                'virtual', in this case always connected to a network of type 'net_type=bridge'
+                'PCI-PASSTHROUGH' or 'PF' (passthrough): depending on VIM capabilities it can be connected to
+                    a data/ptp network ot itcan created unconnected
+                'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity.
+                'VFnotShared'(SRIOV without VLAN tag) same as PF for network connectivity. VF where no other VFs
+                        are allocated on the same physical NIC
+            'bw': (optional) only for PF/VF/VFnotShared. Minimal Bandwidth required for the interface in GBPS
+            'port_security': (optional) If False it must avoid any traffic filtering at this interface. If missing
+                            or True, it must apply the default VIM behaviour
+            After execution the method will add the key:
+            'vim_id': must be filled/added by this method with the VIM identifier generated by the VIM for this
+                    interface. 'net_list' is modified
+        :param cloud_config: (optional) dictionary with:
+            'key-pairs': (optional) list of strings with the public key to be inserted to the default user
+            'users': (optional) list of users to be inserted, each item is a dict with:
+                'name': (mandatory) user name,
+                'key-pairs': (optional) list of strings with the public key to be inserted to the user
+            'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
+                or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
+            'config-files': (optional). List of files to be transferred. Each item is a dict with:
+                'dest': (mandatory) string with the destination absolute path
+                'encoding': (optional, by default text). Can be one of:
+                    'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
+                'content' (mandatory): string with the content of the file
+                'permissions': (optional) string with file permissions, typically octal notation '0644'
+                'owner': (optional) file owner, string with the format 'owner:group'
+            'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
+        :param disk_list: (optional) list with additional disks to the VM. Each item is a dict with:
+            'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
+            'size': (mandatory) string with the size of the disk in GB
+        :param availability_zone_index:  Index of availability_zone_list to use for this this VM. None if not AV
+            required
+        :param availability_zone_list: list of availability zones given by user in the VNFD descriptor.  Ignore if
                     availability_zone_index is None
-            Returns a tuple with the instance 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_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
-                Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
-                as not present.
-            """
+        :return: a tuple with the instance 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_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
+            Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
+            as not present.
+        """
         self.logger.debug(
             "new_vminstance input: image='{}' flavor='{}' nics='{}'".format(image_id, flavor_id, str(net_list)))
         try:
@@ -560,10 +566,10 @@ class vimconnector(vimconn.vimconnector):
             return str(vm_instance_id), None
         except pyone.OneNoExistsException as e:
             self.logger.error("Network with id " + str(e) + " not found: " + str(e))
-            raise vimconn.vimconnNotFoundException(e)
+            raise vimconn.VimConnNotFoundException(e)
         except Exception as e:
             self.logger.error("Create new vm instance error: " + str(e))
-            raise vimconn.vimconnException(e)
+            raise vimconn.VimConnException(e)
 
     def get_vminstance(self, vm_id):
         """Returns the VM instance information from VIM"""
@@ -573,7 +579,7 @@ class vimconnector(vimconn.vimconnector):
             return vm
         except Exception as e:
             self.logger.error("Getting vm instance error: " + str(e) + ": VM Instance not found")
-            raise vimconn.vimconnException(e)
+            raise vimconn.VimConnException(e)
 
     def delete_vminstance(self, vm_id, created_items=None):
         """
@@ -593,12 +599,12 @@ class vimconnector(vimconn.vimconnector):
                 else:
                     vm = one.vm.info(int(vm_id))
 
-        except pyone.OneNoExistsException as e:
+        except pyone.OneNoExistsException:
             self.logger.info("The vm " + str(vm_id) + " does not exist or is already deleted")
-            raise vimconn.vimconnNotFoundException("The vm {} does not exist or is already deleted".format(vm_id))
+            raise vimconn.VimConnNotFoundException("The vm {} does not exist or is already deleted".format(vm_id))
         except Exception as e:
             self.logger.error("Delete vm instance " + str(vm_id) + " error: " + str(e))
-            raise vimconn.vimconnException(e)
+            raise vimconn.VimConnException(e)
 
     def refresh_vms_status(self, vm_list):
         """Get the status of the virtual machines and their interfaces/ports
@@ -667,9 +673,9 @@ class vimconnector(vimconn.vimconnector):
                     interface = {'vim_info': None, "mac_address": str(net["MAC"]), "vim_net_id": str(net["NETWORK_ID"]),
                                  "vim_interface_id": str(net["NETWORK_ID"])}
                     # maybe it should be 2 different keys for ip_address if an interface has ipv4 and ipv6
-                    if u'IP' in net:
+                    if 'IP' in net:
                         interface["ip_address"] = str(net["IP"])
-                    if u'IP6_GLOBAL' in net:
+                    if 'IP6_GLOBAL' in net:
                         interface["ip_address"] = str(net["IP6_GLOBAL"])
                     interfaces.append(interface)
             else:
@@ -677,11 +683,11 @@ class vimconnector(vimconn.vimconnector):
                 interface = {'vim_info': None, "mac_address": str(net["MAC"]), "vim_net_id": str(net["NETWORK_ID"]),
                              "vim_interface_id": str(net["NETWORK_ID"])}
                 # maybe it should be 2 different keys for ip_address if an interface has ipv4 and ipv6
-                if u'IP' in net:
+                if 'IP' in net:
                     interface["ip_address"] = str(net["IP"])
-                if u'IP6_GLOBAL' in net:
+                if 'IP6_GLOBAL' in net:
                     interface["ip_address"] = str(net["IP6_GLOBAL"])
                 interfaces.append(interface)
             return interfaces
-        except Exception as e:
+        except Exception:
             self.logger.error("Error getting vm interface_information of vm_id: " + str(vm_element.ID))