Switched creation of SGs, FIPs to Neutron instead of Nova

* Since there were "novaclient.v2.security_group_rules module is deprecated"
  warnings, switched creation of SG to the Neutron instead of Nova.
* Since there were "novaclient.v2.floating_ips module is deprecated"
  warning, switched creating of FIP to the Neutron instead of Nova.
* Added special methods for creating/deleting FIPs. Added the exception
  in case FIP is already absent.
* Added check whether the iperf tool is installed at the VMs and raising
  and exception in case iperf tool is not installed in the VM (due to
  Internet access issue or transfering & installing offline packages).

Related-PROD: PROD-36943
Change-Id: I165e1358754f3c98adbf8e4cdbfb0b10d2ac117f
diff --git a/utils/os_client.py b/utils/os_client.py
index 63f9151..03462dc 100644
--- a/utils/os_client.py
+++ b/utils/os_client.py
@@ -10,6 +10,7 @@
 from keystoneauth1 import session as keystone_session
 from keystoneclient.v3 import client as keystone_client
 from neutronclient.v2_0 import client as neutron_client
+from neutronclient import common as neutron_common
 from novaclient import client as novaclient
 
 import utils
@@ -297,42 +298,49 @@
             rulesets = [
                 {
                     # ssh
-                    'ip_protocol': 'tcp',
-                    'from_port': 22,
-                    'to_port': 22,
-                    'cidr': '0.0.0.0/0',
+                    'protocol': 'tcp',
+                    'port_range_max': 22,
+                    'port_range_min': 22,
+                    'remote_ip_prefix': '0.0.0.0/0',
+                    'direction': 'ingress'
                 },
                 {
                     # iperf3
-                    'ip_protocol': 'tcp',
-                    'from_port': 5201,
-                    'to_port': 5201,
-                    'cidr': '0.0.0.0/0',
+                    'protocol': 'tcp',
+                    'port_range_max': 5201,
+                    'port_range_min': 5201,
+                    'remote_ip_prefix': '0.0.0.0/0',
+                    'direction': 'ingress'
                 },
                 {
                     # iperf
-                    'ip_protocol': 'tcp',
-                    'from_port': 5001,
-                    'to_port': 5001,
-                    'cidr': '0.0.0.0/0',
+                    'protocol': 'tcp',
+                    'port_range_max': 5001,
+                    'port_range_min': 5001,
+                    'remote_ip_prefix': '0.0.0.0/0',
+                    'direction': 'ingress'
                 },
                 {
                     # ping
-                    'ip_protocol': 'icmp',
-                    'from_port': -1,
-                    'to_port': -1,
-                    'cidr': '0.0.0.0/0',
+                    'protocol': 'icmp',
+                    'remote_ip_prefix': '0.0.0.0/0',
+                    'direction': 'ingress'
                 }
             ]
         sg_name = "spt-test-secgroup-{}".format(random.randrange(100, 999))
         sg_desc = sg_name + " SPT"
-        secgroup = self.os_clients.compute.security_groups.create(
-            sg_name, sg_desc)
+        body = {"security_group": {"name": sg_name, "description": sg_desc}}
+        secgroup = self.os_clients.network.create_security_group(body=body)
+
+        rule_body_teplate = {"security_group_rule": {}}
         for ruleset in rulesets:
-            self.os_clients.compute.security_group_rules.create(
-                secgroup.id, **ruleset)
+            rule_body_teplate["security_group_rule"] = ruleset
+            rule_body_teplate["security_group_rule"]["security_group_id"] = \
+                secgroup['security_group']['id']
+            self.os_clients.network.create_security_group_rule(
+                body=rule_body_teplate)
         logger.info("Created a security group {}".format(sg_name))
-        return secgroup
+        return secgroup['security_group']
 
     def create_basic_server(self, image=None, flavor=None, net=None,
                             availability_zone=None, sec_groups=(),
@@ -355,7 +363,7 @@
             vm = os_conn.compute.servers.find(id=vm_id)
         except Exception as e:
             raise Exception(
-                "{}. Could not get the VM \"{}\": {}".format(
+                "Could not get the VM \"{}\": {}".format(
                     vm_id, e))
         return vm
 
@@ -444,3 +452,16 @@
         nova_services = self.os_clients.compute.hosts.list()
         computes_list = [h for h in nova_services if h.service == "compute"]
         return computes_list
+
+    def create_floating_ip(self, floating_net_id):
+        fip = self.os_clients.network.create_floatingip({"floatingip": {
+            "floating_network_id": floating_net_id}})
+        return fip['floatingip']
+
+    def delete_floating_ip(self, floatingip_id):
+        try:
+            return self.os_clients.network.delete_floatingip(floatingip_id)
+        except neutron_common.exceptions.NotFound as e:
+            msg = "Could not delete a Floating IP, UUID {}. Error: {}" \
+                  "".format(floatingip_id, e)
+            logger.info(msg)
diff --git a/utils/ssh.py b/utils/ssh.py
index fb684bb..abbd89f 100644
--- a/utils/ssh.py
+++ b/utils/ssh.py
@@ -197,10 +197,10 @@
 
         # Install iperf, iperf3 using apt or downloaded deb packages
         internet_at_vms = utils.get_configuration().get("internet_at_vms")
+        path_to_iperf_deb = (config.get('iperf_deb_package_dir_path') or
+                             "/opt/packages/")
         if internet_at_vms.lower() == 'false':
-            logger.info("Copying offline iperf* deb packages, installing...")
-            path_to_iperf_deb = (config.get('iperf_deb_package_dir_path') or
-                                 "/opt/packages/")
+            logger.info("Copying offline iperf3 deb packages, installing...")
             home_ubuntu = "/home/ubuntu/"
             transport.put_iperf3_deb_packages_at_vms(path_to_iperf_deb,
                                                      home_ubuntu)
@@ -213,9 +213,16 @@
                                    'sudo apt-get install -y iperf3 iperf')
 
         # Log whether iperf is installed with version
-        check = transport.exec_command('dpkg -l | grep iperf')
+        check = transport.exec_command('dpkg -l | grep ii | grep iperf3')
         logger.info(check.decode('utf-8'))
-
+        if not check:
+            if internet_at_vms.lower() == 'true':
+                info = "Please check the Internet access at VM."
+            else:
+                info = "Could not put offline iperf packages from {} to the " \
+                       "VM.".format(path_to_iperf_deb)
+            raise BaseException("iperf3 is not installed at VM with FIP {}. {}"
+                                "".format(fip, info))
         # Staring iperf server
         transport.exec_command('nohup iperf3 -s > file 2>&1 &')
         transport.exec_command('nohup iperf -s > file 2>&1 &')