2to3 conversion for osm_ro
[osm/RO.git] / osm_ro / vimconn_vmware.py
index 0434900..12581d6 100644 (file)
@@ -27,7 +27,7 @@ mbayramov@vmware.com
 """
 from progressbar import Percentage, Bar, ETA, FileTransferSpeed, ProgressBar
 
-import vimconn
+from . import vimconn
 import os
 import traceback
 import itertools
@@ -58,7 +58,7 @@ import logging
 import json
 import time
 import uuid
-import httplib
+import http.client
 import hashlib
 import socket
 import struct
@@ -456,7 +456,7 @@ class vimconnector(vimconn.vimconnector):
                     raise vimconn.vimconnNotFoundException("Fail to get tenant {}".format(tenant_id))
 
                 lxmlroot_respond = lxmlElementTree.fromstring(response.content)
-                namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
+                namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
                 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
                 vdc_remove_href = lxmlroot_respond.find("xmlns:Link[@rel='remove']",namespaces).attrib['href']
                 vdc_remove_href = vdc_remove_href + '?recursive=true&force=true'
@@ -871,7 +871,7 @@ class vimconnector(vimconn.vimconnector):
             raise vimconn.vimconnNotFoundException("Fail to get image {}".format(image_id))
 
         lxmlroot_respond = lxmlElementTree.fromstring(response.content)
-        namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
+        namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
         namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
 
         catalogItems_section = lxmlroot_respond.find("xmlns:CatalogItems",namespaces)
@@ -894,7 +894,7 @@ class vimconnector(vimconn.vimconnector):
                                                                                     image_id))
 
             lxmlroot_respond = lxmlElementTree.fromstring(response.content)
-            namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
+            namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
             namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
             catalogitem_remove_href = lxmlroot_respond.find("xmlns:Link[@rel='remove']",namespaces).attrib['href']
 
@@ -982,8 +982,8 @@ class vimconnector(vimconn.vimconnector):
             for catalog in vca.get_catalogs():
                 if catalog_name != catalog.name:
                     continue
-                link = filter(lambda link: link.get_type() == "application/vnd.vmware.vcloud.media+xml" and
-                                           link.get_rel() == 'add', catalog.get_Link())
+                link = [link for link in catalog.get_Link() if link.get_type() == "application/vnd.vmware.vcloud.media+xml" and
+                                           link.get_rel() == 'add']
                 assert len(link) == 1
                 data = """
                 <UploadVAppTemplateParams name="%s" xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"><Description>%s vApp Template</Description></UploadVAppTemplateParams>
@@ -1031,9 +1031,8 @@ class vimconnector(vimconn.vimconnector):
                     if response.status_code == requests.codes.ok:
                         media = mediaType.parseString(response.content, True)
                         number_of_files = len(media.get_Files().get_File())
-                        for index in xrange(0, number_of_files):
-                            links_list = filter(lambda link: link.get_rel() == 'upload:default',
-                                                media.get_Files().get_File()[index].get_Link())
+                        for index in range(0, number_of_files):
+                            links_list = [link for link in media.get_Files().get_File()[index].get_Link() if link.get_rel() == 'upload:default']
                             for link in links_list:
                                 # we skip ovf since it already uploaded.
                                 if 'ovf' in link.get_href():
@@ -1049,7 +1048,7 @@ class vimconnector(vimconn.vimconnector):
                                 hrefvmdk = link.get_href()
 
                                 if progress:
-                                    print("Uploading file: {}".format(file_vmdk))
+                                    print(("Uploading file: {}".format(file_vmdk)))
                                 if progress:
                                     widgets = ['Uploading file: ', Percentage(), ' ', Bar(), ' ', ETA(), ' ',
                                                FileTransferSpeed()]
@@ -1303,8 +1302,7 @@ class vimconnector(vimconn.vimconnector):
             return None
         # UUID has following format https://host/api/vApp/vapp-30da58a3-e7c7-4d09-8f68-d4c8201169cf
         try:
-            refs = filter(lambda ref: ref.name == vapp_name and ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml',
-                          vdc.ResourceEntities.ResourceEntity)
+            refs = [ref for ref in vdc.ResourceEntities.ResourceEntity if ref.name == vapp_name and ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml']
             if len(refs) == 1:
                 return refs[0].href.split("vapp")[1][1:]
         except Exception as e:
@@ -1326,9 +1324,7 @@ class vimconnector(vimconn.vimconnector):
                 :param vapp_uuid:
         """
         try:
-            refs = filter(lambda ref:
-                          ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml',
-                          vdc.ResourceEntities.ResourceEntity)
+            refs = [ref for ref in vdc.ResourceEntities.ResourceEntity if ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml']
             for ref in refs:
                 vappid = ref.href.split("vapp")[1][1:]
                 # find vapp with respected vapp uuid
@@ -1352,8 +1348,7 @@ class vimconnector(vimconn.vimconnector):
         """
 
         try:
-            refs = filter(lambda ref: ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml',
-                          vdc.ResourceEntities.ResourceEntity)
+            refs = [ref for ref in vdc.ResourceEntities.ResourceEntity if ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml']
             for ref in refs:
                 # we care only about UUID the rest doesn't matter
                 vappid = ref.href.split("vapp")[1][1:]
@@ -1372,30 +1367,56 @@ class vimconnector(vimconn.vimconnector):
             return None
         return None
 
-    def new_vminstance(self, name=None, description="", start=False, image_id=None, flavor_id=None, net_list={},
+    def new_vminstance(self, name=None, description="", start=False, image_id=None, flavor_id=None, net_list=[],
                        cloud_config=None, disk_list=None, availability_zone_index=None, availability_zone_list=None):
         """Adds a VM instance to VIM
         Params:
-            start: indicates if VM must start or boot in pause mode. Ignored
-            image_id,flavor_id: image and flavor uuid
-            net_list: list of interfaces, each one is a dictionary with:
-                name:
-                net_id: network uuid to connect
-                vpci: virtual vcpi to assign
-                model: interface model, virtio, e2000, ...
-                mac_address:
-                use: 'data', 'bridge',  'mgmt'
-                type: 'virtual', 'PF', 'VF', 'VFnotShared'
-                vim_id: filled/added by this function
-                cloud_config: can be a text script to be passed directly to cloud-init,
-                    or an object to inject users and ssh keys with format:
-                        key-pairs: [] list of keys to install to the default user
-                        users: [{ name, key-pairs: []}] list of users to add with their key-pair
-                #TODO ip, security groups
-        Returns >=0, the instance identifier
-                <0, error_text
+            '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, e2000, ...
+                'mac_address': (optional) mac 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'
+                     'PF' (passthrough): depending on VIM capabilities it can be connected to a data/ptp network ot it
+                           can created unconnected
+                     '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
+                availability_zone_index is None
+        Returns the instance identifier or raises an exception on error
         """
-
         self.logger.info("Creating new instance for entry {}".format(name))
         self.logger.debug("desc {} boot {} image_id: {} flavor_id: {} net_list: {} cloud_config {} disk_list {}".format(
                                     description, start, image_id, flavor_id, net_list, cloud_config, disk_list))
@@ -1469,7 +1490,7 @@ class vimconnector(vimconn.vimconnector):
         network_mode = 'bridged'
         if net_list is not None and len(net_list) > 0:
             for net in net_list:
-                if 'use' in net and net['use'] == 'mgmt':
+                if 'use' in net and net['use'] == 'mgmt' and not primary_net:
                     primary_net = net
             if primary_net is None:
                 primary_net = net_list[0]
@@ -1613,6 +1634,10 @@ class vimconnector(vimconn.vimconnector):
                 if 'net_id' not in net:
                     continue
 
+                #Using net_id as a vim_id i.e. vim interface id, as do not have saperate vim interface id
+                #Same will be returned in refresh_vms_status() as vim_interface_id
+                net['vim_id'] = net['net_id']  # Provide the same VIM identifier as the VIM network
+
                 interface_net_id = net['net_id']
                 interface_net_name = self.get_network_name_by_id(network_uuid=interface_net_id)
                 interface_network_mode = net['use']
@@ -1626,7 +1651,7 @@ class vimconnector(vimconn.vimconnector):
                                   - NONE (No IP addressing mode specified.)"""
 
                 if primary_netname is not None:
-                    nets = filter(lambda n: n.name == interface_net_name, self.vca.get_networks(self.tenant_name))
+                    nets = [n for n in self.vca.get_networks(self.tenant_name) if n.name == interface_net_name]
                     if len(nets) == 1:
                         self.logger.info("new_vminstance(): Found requested network: {}".format(nets[0].name))
 
@@ -1696,11 +1721,11 @@ class vimconnector(vimconn.vimconnector):
                 task = vm_obj.ReconfigVM_Task(spec=spec)
                 if task:
                     result = self.wait_for_vcenter_task(task, vcenter_conect)
-                    self.logger.info("Reserved memmoery {} MB for "\
-                                     "VM VM status: {}".format(str(memReserve),result))
+                    self.logger.info("Reserved memory {} MB for "
+                                     "VM VM status: {}".format(str(memReserve), result))
                 else:
-                    self.logger.info("Fail to reserved memmoery {} to VM {}".format(
-                                                                str(memReserve),str(vm_obj)))
+                    self.logger.info("Fail to reserved memory {} to VM {}".format(
+                                                                str(memReserve), str(vm_obj)))
 
             self.logger.debug("new_vminstance(): power on vApp {} ".format(name))
 
@@ -2145,7 +2170,7 @@ class vimconnector(vimconn.vimconnector):
                 result = self.vca.block_until_completed(power_task)
                 self.instance_actions_result("resume", result, vapp_name)
             elif "shutoff" in action_dict or "shutdown" in action_dict:
-                action_name , value = action_dict.items()[0]
+                action_name , value = list(action_dict.items())[0]
                 self.logger.info("action_vminstance: {} vApp: {}".format(action_name, vapp_name))
                 power_off_task = the_vapp.undeploy(action='powerOff')
                 result = self.vca.block_until_completed(power_off_task)
@@ -2279,7 +2304,7 @@ class vimconnector(vimconn.vimconnector):
             org_dict = self.get_org(self.org_uuid)
             if org_dict and 'networks' in org_dict:
                 org_network_dict = org_dict['networks']
-                for net_uuid,net_name in org_network_dict.iteritems():
+                for net_uuid,net_name in org_network_dict.items():
                     if net_name == network_name:
                         return net_uuid
 
@@ -2433,8 +2458,7 @@ class vimconnector(vimconn.vimconnector):
         vm_list_rest_call = ''.join(url_list)
 
         if not (not vca.vcloud_session or not vca.vcloud_session.organization):
-            refs = filter(lambda ref: ref.name == vdc_name and ref.type_ == 'application/vnd.vmware.vcloud.vdc+xml',
-                          vca.vcloud_session.organization.Link)
+            refs = [ref for ref in vca.vcloud_session.organization.Link if ref.name == vdc_name and ref.type_ == 'application/vnd.vmware.vcloud.vdc+xml']
             if len(refs) == 1:
                 response = Http.get(url=vm_list_rest_call,
                                     headers=vca.vcloud_session.get_vcloud_headers(),
@@ -3369,7 +3393,7 @@ class vimconnector(vimconn.vimconnector):
             return None
         try:
             lxmlroot_respond = lxmlElementTree.fromstring(response.content)
-            namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
+            namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
             namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
 
             for item in lxmlroot_respond.iterfind('xmlns:Item',namespaces):
@@ -4198,7 +4222,7 @@ class vimconnector(vimconn.vimconnector):
         try:
             #Find but type & max of instance IDs assigned to disks
             lxmlroot_respond = lxmlElementTree.fromstring(response.content)
-            namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
+            namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
             namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
             instance_id = 0
             for item in lxmlroot_respond.iterfind('xmlns:Item',namespaces):
@@ -4474,7 +4498,7 @@ class vimconnector(vimconn.vimconnector):
             catalog = self.get_catalog_obj(image_id, catalogs)
             if catalog:
                 template_name = self.get_catalogbyid(image_id, catalogs)
-                catalog_items = filter(lambda catalogItemRef: catalogItemRef.get_name() == template_name, catalog.get_CatalogItems().get_CatalogItem())
+                catalog_items = [catalogItemRef for catalogItemRef in catalog.get_CatalogItems().get_CatalogItem() if catalogItemRef.get_name() == template_name]
                 if len(catalog_items) == 1:
                     response = Http.get(catalog_items[0].get_href(),
                                         headers=vca.vcloud_session.get_vcloud_headers(),
@@ -4932,7 +4956,7 @@ class vimconnector(vimconn.vimconnector):
         if "used_vlanIDs" not in self.persistent_info:
                 self.persistent_info["used_vlanIDs"] = {}
         else:
-            used_ids = self.persistent_info["used_vlanIDs"].values()
+            used_ids = list(self.persistent_info["used_vlanIDs"].values())
 
         for vlanID_range in self.config.get('vlanID_range'):
             start_vlanid , end_vlanid = vlanID_range.split("-")
@@ -4940,7 +4964,7 @@ class vimconnector(vimconn.vimconnector):
                 raise vimconn.vimconnConflictException("Invalid vlan ID range {}".format(
                                                                         vlanID_range))
 
-            for id in xrange(int(start_vlanid), int(end_vlanid) + 1):
+            for id in range(int(start_vlanid), int(end_vlanid) + 1):
                 if id not in used_ids:
                     vlan_id = id
                     self.persistent_info["used_vlanIDs"][network_name] = vlan_id