update link to upper-constraints.txt
[integration.git] / test / vcpe / vcpecommon.py
index 9c5e626..0e02987 100755 (executable)
@@ -10,95 +10,28 @@ import sys
 import ipaddress
 import mysql.connector
 import requests
-import commands
+import subprocess
 import time
 import yaml
 from novaclient import client as openstackclient
+from openstack.config import loader
 from kubernetes import client, config
 from netaddr import IPAddress, IPNetwork
 
-######################################################################
-# Parts which must be updated / cross-checked during each deployment #
-# are marked as CHANGEME                                             #
-######################################################################
-
 class VcpeCommon:
-    #############################################################################################
-    # Set network prefix of k8s host external address; it's used for pod public IP autodetection
-    # but can be overriden from user in case of autodetection failure
-    external_net_addr = '10.12.0.0'
-    external_net_prefix_len = 16
-
-    #############################################################################################
-    # set the openstack cloud access credentials here
-    oom_mode = True
-
-    #############################################################################################
-    # set the gra_api flag
-    # Mustn't be set to True until Frankfurt DGs are updated for GRA-API infrastructure
-    gra_api_flag= False
-
-    ###########################
-    # set Openstack credentials
-    # CHANGEME part
-    cloud = {
-        '--os-auth-url': 'http://10.12.25.2:5000',
-        '--os-username': 'kxi',
-        '--os-user-domain-id': 'default',
-        '--os-project-domain-id': 'default',
-        '--os-tenant-id': '712b6016580e410b9abfec9ca34953ce' if oom_mode else '1e097c6713e74fd7ac8e4295e605ee1e',
-        '--os-region-name': 'RegionOne',
-        '--os-password': 'n3JhGMGuDzD8',
-        '--os-project-domain-name': 'Integration-Release-Daily' if oom_mode else 'Integration-SB-07',
-        '--os-identity-api-version': '3'
-    }
-
-    ############################################################################
-    # set oam and public network which must exist in openstack before deployment
-    # CHANGEME part
-    common_preload_config = {
-        'oam_onap_net': 'oam_network_exxC' if oom_mode else 'oam_onap_lAky',
-        'oam_onap_subnet': 'oam_network_exxC' if oom_mode else 'oam_onap_lAky',
-        'public_net': 'external',
-        'public_net_id': '971040b2-7059-49dc-b220-4fab50cb2ad4'
-    }
-
-    #############################################################################
-    # Set name of Onap's k8s namespace and sdnc controller pod
-    # CHANGEME part
-    onap_namespace = 'onap'
-    onap_environment = 'dev'
-    sdnc_controller_pod = '-'.join([onap_environment, 'sdnc-sdnc-0'])
-
-    template_variable_symbol = '${'
-    cpe_vm_prefix = 'zdcpe'
-
-    #############################################################################################
-    # preloading network config
-    #  key=network role
-    #  value = [subnet_start_ip, subnet_gateway_ip]
-    preload_network_config = {
-        'cpe_public': ['10.2.0.2', '10.2.0.1'],
-        'cpe_signal': ['10.4.0.2', '10.4.0.1'],
-        'brg_bng': ['10.3.0.2', '10.3.0.1'],
-        'bng_mux': ['10.1.0.10', '10.1.0.1'],
-        'mux_gw': ['10.5.0.10', '10.5.0.1']
-    }
-
-    dcae_ves_collector_name = 'dcae-bootstrap'
-    global_subscriber_id = 'SDN-ETHERNET-INTERNET'
-    project_name = 'Project-Demonstration'
-    owning_entity_id = '520cc603-a3c4-4ec2-9ef4-ca70facd79c0'
-    owning_entity_name = 'OE-Demonstration1'
-
-    def __init__(self, extra_host_names=None):
+
+    def __init__(self, extra_host_names=None, cfg_file=None):
         self.logger = logging.getLogger(__name__)
         self.logger.setLevel(logging.DEBUG)
         self.logger.info('Initializing configuration')
+        self.default_config = 'vcpeconfig.yaml'
 
         # Read configuration from config file
-        self._load_config()
+        self._load_config(cfg_file)
+        # Load OpenStack settings
+        self._load_os_config()
 
+        self.sdnc_controller_pod = '-'.join([self.onap_environment, 'sdnc-sdnc-0'])
         # OOM: this is the address that the brg and bng will nat for sdnc access - 10.0.0.x address of k8 host for sdnc-0 container
         self.sdnc_oam_ip = self.get_pod_node_oam_ip(self.sdnc_controller_pod)
         # OOM: this is a k8s host external IP, e.g. oom-k8s-01 IP
@@ -134,12 +67,6 @@ class VcpeCommon:
             'vfmodule': 'vcpe_vfmodule'
         }
         self.aai_userpass = 'AAI', 'AAI'
-
-        ############################################################################################################
-        # following key is overriding public key from vCPE heat templates, it's important to use correct one in here
-        # CHANGEME part
-        self.pub_key = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDKXDgoo3+WOqcUG8/5uUbk81+yczgwC4Y8ywTmuQqbNxlY1oQ0YxdMUqUnhitSXs5S/yRuAVOYHwGg2mCs20oAINrP+mxBI544AMIb9itPjCtgqtE2EWo6MmnFGbHB4Sx3XioE7F4VPsh7japsIwzOjbrQe+Mua1TGQ5d4nfEOQaaglXLLPFfuc7WbhbJbK6Q7rHqZfRcOwAMXgDoBqlyqKeiKwnumddo2RyNT8ljYmvB6buz7KnMinzo7qB0uktVT05FH9Rg0CTWH5norlG5qXgP2aukL0gk1ph8iAt7uYLf1ktp+LJI2gaF6L0/qli9EmVCSLr1uJ38Q8CBflhkh'
-
         self.os_tenant_id = self.cloud['--os-tenant-id']
         self.os_region_name = self.cloud['--os-region-name']
         self.common_preload_config['pub_key'] = self.pub_key
@@ -231,12 +158,15 @@ class VcpeCommon:
                             'Content-Type': 'application/json',
                             'X-FromAppId': 'postman', 'X-TransactionId': '9999'}
 
-    def _load_config(self, cfg_file='vcpeconfig.yaml'):
+    def _load_config(self, cfg_file):
         """
         Reads vcpe config file and injects settings as object's attributes
         :param cfg_file: Configuration file path
         """
 
+        if cfg_file is None:
+            cfg_file = self.default_config
+
         try:
             with open(cfg_file, 'r') as cfg:
                 cfg_yml = yaml.full_load(cfg)
@@ -257,6 +187,34 @@ class VcpeCommon:
             self.logger.error('Unable to parse config file: ' + str(e))
             sys.exit(1)
 
+    def _load_os_config(self):
+        """
+        Reads cloud settings and sets them as object's 'cloud' attribute
+        """
+        # Create OpenStackConfig config instance
+        os_config = loader.OpenStackConfig()
+        # Try reading cloud settings for self.cloud_name
+        try:
+            os_cloud = os_config.cloud_config['clouds'][self.cloud_name]
+        except KeyError:
+            self.logger.error('Error fetching cloud settings for cloud "{0}"'
+                              .format(self.cloud_name))
+            sys.exit(1)
+        self.logger.debug('Cloud config:\n {0}'.format(json.dumps(
+                          os_cloud,indent=4)))
+
+        # Extract all OS settings keys and alter their names
+        # to conform to openstack cli client
+        self.cloud = {}
+        for k in os_cloud:
+            if isinstance(os_cloud[k],dict):
+                for sub_k in os_cloud[k]:
+                    os_setting_name = '--os-' + sub_k.replace('_','-')
+                    self.cloud[os_setting_name] = os_cloud[k][sub_k]
+            else:
+                os_setting_name = '--os-' + k.replace('_','-')
+                self.cloud[os_setting_name] = os_cloud[k]
+
     def heatbridge(self, openstack_stack_name, svc_instance_uuid):
         """
         Add vserver information to AAI
@@ -264,7 +222,7 @@ class VcpeCommon:
         self.logger.info('Adding vServer information to AAI for {0}'.format(openstack_stack_name))
         if not self.oom_mode:
             cmd = '/opt/demo.sh heatbridge {0} {1} vCPE'.format(openstack_stack_name, svc_instance_uuid)
-            ret = commands.getstatusoutput("ssh -i onap_dev root@{0} '{1}'".format(self.hosts['robot'], cmd))
+            ret = subprocess.getstatusoutput("ssh -i onap_dev root@{0} '{1}'".format(self.hosts['robot'], cmd))
             self.logger.debug('%s', ret)
         else:
             print('To add vGMUX vserver info to AAI, do the following:')
@@ -500,17 +458,17 @@ class VcpeCommon:
         :param sz: a string
         :return: the first IP address matching the network, e.g. 10.5.12.3
         """
-        network = ipaddress.ip_network(unicode('{0}/{1}'.format(net_addr, net_addr_len)), strict=False)
+        network = ipaddress.ip_network(unicode('{0}/{1}'.format(net_addr, net_addr_len)), strict=False) # pylint: disable=E0602
         ip_list = re.findall(r'[0-9]+(?:\.[0-9]+){3}', sz)
         for ip in ip_list:
-            this_net = ipaddress.ip_network(unicode('{0}/{1}'.format(ip, net_addr_len)), strict=False)
+            this_net = ipaddress.ip_network(unicode('{0}/{1}'.format(ip, net_addr_len)), strict=False) # pylint: disable=E0602
             if this_net == network:
                 return str(ip)
         return None
 
     def get_pod_node_oam_ip(self, pod):
         """
-        :Assuming kubectl is available and configured by default config (~/.kube/config) 
+        :Assuming kubectl is available and configured by default config (~/.kube/config)
         :param pod: pod name substring, e.g. 'sdnc-sdnc-0'
         :return pod's cluster node oam ip (10.0.0.0/16)
         """
@@ -527,12 +485,12 @@ class VcpeCommon:
                 break
 
         if ret is None:
-            ret = raw_input("Enter " + self.sdnc_controller_pod + " pod cluster node OAM IP address(10.0.0.0/16): ")
+            ret = raw_input("Enter " + self.sdnc_controller_pod + " pod cluster node OAM IP address(10.0.0.0/16): ") # pylint: disable=E0602
         return ret
 
     def get_pod_node_public_ip(self, pod):
         """
-        :Assuming kubectl is available and configured by default config (~/.kube/config) 
+        :Assuming kubectl is available and configured by default config (~/.kube/config)
         :param pod: pod name substring, e.g. 'sdnc-sdnc-0'
         :return pod's cluster node public ip (i.e. 10.12.0.0/16)
         """
@@ -549,7 +507,7 @@ class VcpeCommon:
                 break
 
         if ret is None:
-            ret = raw_input("Enter " + self.sdnc_controller_pod + " pod cluster node public IP address(i.e. " + self.external_net_addr + "): ")
+            ret = raw_input("Enter " + self.sdnc_controller_pod + " pod cluster node public IP address(i.e. " + self.external_net_addr + "): ") # pylint: disable=E0602
         return ret
 
     def get_vm_public_ip_by_nova(self, vm):
@@ -559,10 +517,10 @@ class VcpeCommon:
         :return vm public ip
         """
         subnet = IPNetwork('{0}/{1}'.format(self.external_net_addr, self.external_net_prefix_len))
-        nova = openstackclient.Client(2, self.cloud['--os-username'], self.cloud['--os-password'], self.cloud['--os-tenant-id'], self.cloud['--os-auth-url']) 
+        nova = openstackclient.Client(2, self.cloud['--os-username'], self.cloud['--os-password'], self.cloud['--os-tenant-id'], self.cloud['--os-auth-url'])
         for i in nova.servers.list():
             if i.name == vm:
-                for k, v in i.networks.items():
+                for k, v in i.networks.items(): # pylint: disable=W0612
                     for ip in v:
                         if IPAddress(ip) in subnet:
                             return ip
@@ -759,7 +717,7 @@ class VcpeCommon:
                 url = self.vpp_inf_url.format(ip) + '/interface/' + inf
                 requests.delete(url, headers=self.vpp_api_headers, auth=self.vpp_api_userpass)
 
-            if len(self.get_vxlan_interfaces(ip)) > 0:
+            if self.get_vxlan_interfaces(ip):
                 self.logger.error("Error deleting VxLAN from {0}, try to restart the VM, IP is {1}.".format(host, ip))
                 return False
 
@@ -808,4 +766,3 @@ class VcpeCommon:
 
     def load_vgmux_vnf_name(self):
         return self.load_object(self.vgmux_vnf_name_file)
-