Bug 2202: Adding support for cinder V3 API with V2 API for persistent volumes.
[osm/RO.git] / RO-VIM-openstack / osm_rovim_openstack / vimconn_openstack.py
index d70e2ec..7659649 100644 (file)
@@ -355,12 +355,21 @@ class vimconnector(vimconn.VimConnector):
                 endpoint_type=self.endpoint_type,
                 region_name=region_name,
             )
-            self.cinder = self.session["cinder"] = cClient.Client(
-                2,
-                session=sess,
-                endpoint_type=self.endpoint_type,
-                region_name=region_name,
-            )
+
+            if sess.get_all_version_data(service_type="volumev2"):
+                self.cinder = self.session["cinder"] = cClient.Client(
+                    2,
+                    session=sess,
+                    endpoint_type=self.endpoint_type,
+                    region_name=region_name,
+                )
+            else:
+                self.cinder = self.session["cinder"] = cClient.Client(
+                    3,
+                    session=sess,
+                    endpoint_type=self.endpoint_type,
+                    region_name=region_name,
+                )
 
             try:
                 self.my_tenant_id = self.session["my_tenant_id"] = sess.get_project_id()
@@ -1287,16 +1296,17 @@ class vimconnector(vimconn.VimConnector):
             extra_specs (dict):         To be filled.
 
         Returns:
-            vcpus       (int)           Number of virtual cpus
+            threads       (int)           Number of virtual cpus
 
         """
         if not numa.get("paired-threads"):
             return
+
         # cpu_thread_policy "require" implies that compute node must have an STM architecture
-        vcpus = numa["paired-threads"] * 2
+        threads = numa["paired-threads"] * 2
         extra_specs["hw:cpu_thread_policy"] = "require"
         extra_specs["hw:cpu_policy"] = "dedicated"
-        return vcpus
+        return threads
 
     @staticmethod
     def process_numa_cores(numa: dict, extra_specs: dict) -> Optional[int]:
@@ -1306,17 +1316,17 @@ class vimconnector(vimconn.VimConnector):
             extra_specs (dict):         To be filled.
 
         Returns:
-            vcpus       (int)           Number of virtual cpus
+            cores       (int)           Number of virtual cpus
 
         """
         # cpu_thread_policy "isolate" implies that the host must not have an SMT
         # architecture, or a non-SMT architecture will be emulated
         if not numa.get("cores"):
             return
-        vcpus = numa["cores"]
+        cores = numa["cores"]
         extra_specs["hw:cpu_thread_policy"] = "isolate"
         extra_specs["hw:cpu_policy"] = "dedicated"
-        return vcpus
+        return cores
 
     @staticmethod
     def process_numa_threads(numa: dict, extra_specs: dict) -> Optional[int]:
@@ -1326,37 +1336,33 @@ class vimconnector(vimconn.VimConnector):
             extra_specs (dict):         To be filled.
 
         Returns:
-            vcpus       (int)           Number of virtual cpus
+            threads       (int)           Number of virtual cpus
 
         """
         # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture
         if not numa.get("threads"):
             return
-        vcpus = numa["threads"]
+        threads = numa["threads"]
         extra_specs["hw:cpu_thread_policy"] = "prefer"
         extra_specs["hw:cpu_policy"] = "dedicated"
-        return vcpus
+        return threads
 
     def _process_numa_parameters_of_flavor(
-        self, numas: List, extra_specs: Dict, vcpus: Optional[int]
-    ) -> int:
+        self, numas: List, extra_specs: Dict
+    ) -> None:
         """Process numa parameters and fill up extra_specs.
 
         Args:
             numas   (list):             List of dictionary which includes numa information
             extra_specs (dict):         To be filled.
-            vcpus       (int)      Number of virtual cpus
-
-        Returns:
-            vcpus       (int)           Number of virtual cpus
 
         """
         numa_nodes = len(numas)
         extra_specs["hw:numa_nodes"] = str(numa_nodes)
+        cpu_cores, cpu_threads = 0, 0
 
         if self.vim_type == "VIO":
-            extra_specs["vmware:extra_config"] = '{"numa.nodeAffinity":"0"}'
-            extra_specs["vmware:latency_sensitivity_level"] = "high"
+            self.process_vio_numa_nodes(numa_nodes, extra_specs)
 
         for numa in numas:
             if "id" in numa:
@@ -1370,15 +1376,34 @@ class vimconnector(vimconn.VimConnector):
             extra_specs["hw:cpu_sockets"] = str(numa_nodes)
 
             if "paired-threads" in numa:
-                vcpus = self.process_numa_paired_threads(numa, extra_specs)
+                threads = self.process_numa_paired_threads(numa, extra_specs)
+                cpu_threads += threads
 
             elif "cores" in numa:
-                vcpus = self.process_numa_cores(numa, extra_specs)
+                cores = self.process_numa_cores(numa, extra_specs)
+                cpu_cores += cores
 
             elif "threads" in numa:
-                vcpus = self.process_numa_threads(numa, extra_specs)
+                threads = self.process_numa_threads(numa, extra_specs)
+                cpu_threads += threads
+
+        if cpu_cores:
+            extra_specs["hw:cpu_cores"] = str(cpu_cores)
+        if cpu_threads:
+            extra_specs["hw:cpu_threads"] = str(cpu_threads)
 
-        return vcpus
+    @staticmethod
+    def process_vio_numa_nodes(numa_nodes: int, extra_specs: Dict) -> None:
+        """According to number of numa nodes, updates the extra_specs for VIO.
+
+        Args:
+
+            numa_nodes      (int):         List keeps the numa node numbers
+            extra_specs     (dict):        Extra specs dict to be updated
+
+        """
+        # If there are several numas, we do not define specific affinity.
+        extra_specs["vmware:latency_sensitivity_level"] = "high"
 
     def _change_flavor_name(
         self, name: str, name_suffix: int, flavor_data: dict
@@ -1405,17 +1430,13 @@ class vimconnector(vimconn.VimConnector):
         return name
 
     def _process_extended_config_of_flavor(
-        self, extended: dict, extra_specs: dict, vcpus: Optional[int]
-    ) -> int:
+        self, extended: dict, extra_specs: dict
+    ) -> None:
         """Process the extended dict to fill up extra_specs.
         Args:
 
-            extended    (dict):         Keeping the extra specification of flavor
-            extra_specs (dict)          Dict to be filled to be used during flavor creation
-            vcpus       (int)           Number of virtual cpus
-
-        Returns:
-            vcpus       (int)           Number of virtual cpus
+            extended                    (dict):         Keeping the extra specification of flavor
+            extra_specs                 (dict)          Dict to be filled to be used during flavor creation
 
         """
         quotas = {
@@ -1441,7 +1462,7 @@ class vimconnector(vimconn.VimConnector):
 
         numas = extended.get("numas")
         if numas:
-            vcpus = self._process_numa_parameters_of_flavor(numas, extra_specs, vcpus)
+            self._process_numa_parameters_of_flavor(numas, extra_specs)
 
         for quota, item in quotas.items():
             if quota in extended.keys():
@@ -1462,8 +1483,6 @@ class vimconnector(vimconn.VimConnector):
             if extended.get(policy):
                 extra_specs[hw_policy] = extended[policy].lower()
 
-        return vcpus
-
     @staticmethod
     def _get_flavor_details(flavor_data: dict) -> Tuple:
         """Returns the details of flavor
@@ -1513,9 +1532,7 @@ class vimconnector(vimconn.VimConnector):
                         flavor_data
                     )
                     if extended:
-                        vcpus = self._process_extended_config_of_flavor(
-                            extended, extra_specs, vcpus
-                        )
+                        self._process_extended_config_of_flavor(extended, extra_specs)
 
                     # Create flavor
 
@@ -4492,3 +4509,19 @@ class vimconnector(vimconn.VimConnector):
             self.__wait_for_vm(vm_id, "ACTIVE")
         instance_status = self.get_vdu_state(vm_id)[0]
         return instance_status
+
+    def get_monitoring_data(self):
+        try:
+            self.logger.debug("Getting servers and ports data from Openstack VIMs.")
+            self._reload_connection()
+            all_servers = self.nova.servers.list(detailed=True)
+            all_ports = self.neutron.list_ports()
+            return all_servers, all_ports
+        except (
+            vimconn.VimConnException,
+            vimconn.VimConnNotFoundException,
+            vimconn.VimConnConnectionException,
+        ) as e:
+            raise vimconn.VimConnException(
+                f"Exception in monitoring while getting VMs and ports status: {str(e)}"
+            )