Add identity-url to region data in AAI

This change automates the process of updating
region data with identity-url

Change-Id: Ibda9ac8d1bbe1a489d496a5f293aafdebb73d416
Signed-off-by: Bartek Grzybowski <b.grzybowski@partner.samsung.com>
Issue-ID: SO-2308
diff --git a/test/vcpe/preload.py b/test/vcpe/preload.py
index f99d8de..5d86487 100755
--- a/test/vcpe/preload.py
+++ b/test/vcpe/preload.py
@@ -180,6 +180,86 @@
                 return None
         return common_dict
 
+    def aai_region_query(self, req_method, json=None, verify=False):
+        """
+        Perform actual AAI API request for region
+        :param req_method: request method ({'get','put'})
+        :param json: Json payload
+        :param verify: SSL verify mode
+        :return:
+        """
+        url, headers, auth = (self.vcpecommon.aai_region_query_url,
+                              self.vcpecommon.aai_headers,
+                              self.vcpecommon.aai_userpass)
+        try:
+            if req_method == 'get':
+                request = requests.get(url, headers=headers, auth=auth,
+                                       verify=verify)
+            elif req_method == 'put':
+                request = requests.put(url, headers=headers, auth=auth,
+                                        verify=verify, json=json)
+            else:
+                raise requests.exceptions.RequestException
+        except requests.exceptions.RequestException as e:
+            self.logger.error("Error connecting to AAI API. Error details: " + str(e.message))
+            return False
+        try:
+            assert request.status_code == 200
+        except AssertionError:
+            self.logger.error('AAI request failed. API returned http code ' + str(request.status_code))
+            return False
+        try:
+            return request.json()
+        except ValueError as e:
+            if req_method == 'get':
+                self.logger.error('Unable to parse AAI response: ' + e.message)
+                return False
+            elif req_method == 'put':
+                return request.ok
+            else:
+                return False
+
+    def preload_aai_data(self, template_aai_region_data):
+        """
+        Update aai region data with identity-url
+        :param template_aai_region_data: path to region data template
+        :return:
+        """
+        request = self.aai_region_query('get')
+        if request:
+            # Check if identity-url already updated (for idempotency)
+            self.logger.debug("Regiond data acquired from AAI:\n" + json.dumps(request,indent=4))
+            try:
+                assert request['identity-url']
+            except KeyError:
+                pass
+            else:
+                self.logger.info('Identity-url already present in {0} data, not updating'.format(self.vcpecommon.cloud['--os-region-name']))
+                return
+
+            # Get resource_version and relationship_list from region data
+            resource_version = request['resource-version']
+            relationship_list = request['relationship-list']
+
+            replace_dict = {'${identity-url}': self.vcpecommon.cloud['--os-auth-url'],
+                            '${identity_api_version}': self.vcpecommon.cloud['--os-identity-api-version'],
+                            '${region_name}': self.vcpecommon.cloud['--os-region-name'],
+                            '${resource_version}': resource_version
+                           }
+            json_data = self.generate_json(template_aai_region_data, replace_dict)
+            json_data['relationship-list'] = relationship_list
+            self.logger.debug('Region update payload:\n' + json.dumps(json_data,indent=4))
+        else:
+            sys.exit(1)
+
+        # Update region data
+        request = self.aai_region_query('put', json_data)
+        if request:
+            self.logger.info('Successully updated identity-url in {0} '
+                    'region'.format(self.vcpecommon.cloud['--os-region-name']))
+        else:
+            sys.exit(1)
+
     def test(self):
         # this is for testing purpose
         name_suffix = datetime.now().strftime('%Y%m%d%H%M')
diff --git a/test/vcpe/preload_templates/template_aai_region_data.json b/test/vcpe/preload_templates/template_aai_region_data.json
new file mode 100644
index 0000000..c574630
--- /dev/null
+++ b/test/vcpe/preload_templates/template_aai_region_data.json
@@ -0,0 +1,11 @@
+{
+  "cloud-owner": "CloudOwner",
+  "cloud-region-id": "${region_name}",
+  "cloud-type": "SharedNode",
+  "owner-defined-type": "OwnerType",
+  "cloud-region-version": "v1",
+  "identity-url": "${identity-url}/v${identity_api_version}",
+  "cloud-zone": "CloudZone",
+  "resource-version": "${resource_version}",
+  "relationship-list": ""
+}
diff --git a/test/vcpe/vcpe.py b/test/vcpe/vcpe.py
index bf6fcaf..48190fe 100755
--- a/test/vcpe/vcpe.py
+++ b/test/vcpe/vcpe.py
@@ -218,6 +218,9 @@
     vcpecommon = VcpeCommon()
     init_sdc(vcpecommon)
     download_vcpe_service_templates(vcpecommon)
+    preloader = preload.Preload(vcpecommon)
+    template_aai_region_data = vcpecommon.find_file('aai_region_data', 'json', 'preload_templates')
+    preloader.preload_aai_data(template_aai_region_data)
 
 
 def init_sdc(vcpecommon):
diff --git a/test/vcpe/vcpecommon.py b/test/vcpe/vcpecommon.py
index 545524e..ddbce4b 100755
--- a/test/vcpe/vcpecommon.py
+++ b/test/vcpe/vcpecommon.py
@@ -223,6 +223,16 @@
         self.mariadb_galera_endpoint_ip = self.get_k8s_service_endpoint_info('mariadb-galera','ip')
         self.mariadb_galera_endpoint_port = self.get_k8s_service_endpoint_info('mariadb-galera','port')
 
+        #############################################################################################
+        # AAI urls
+        self.aai_region_query_url = 'https://' + self.oom_so_sdnc_aai_ip + ':' +\
+                                    self.aai_query_port +\
+                                    '/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/' +\
+                                    self.cloud['--os-region-name']
+        self.aai_headers = {'Accept': 'application/json',
+                            'Content-Type': 'application/json',
+                            'X-FromAppId': 'postman', 'X-TransactionId': '9999'}
+
     def heatbridge(self, openstack_stack_name, svc_instance_uuid):
         """
         Add vserver information to AAI