Fix restcall to AAI 27/10227/1
authorBin Yang <bin.yang@windriver.com>
Mon, 4 Sep 2017 13:37:57 +0000 (21:37 +0800)
committerBin Yang <bin.yang@windriver.com>
Mon, 4 Sep 2017 13:37:57 +0000 (21:37 +0800)
fix restcall to AAI, and fix extension API response

Change-Id: I0d786135de5c4d96d447a51af99adfe04178765e
Issue-Id: MULTICLOUD-58
Signed-off-by: Bin Yang <bin.yang@windriver.com>
.gitignore
.gitreview
newton/.gitignore
newton/newton/extensions/views/extensions.py
newton/newton/pub/config/config.py
newton/newton/pub/msapi/extsys.py
newton/newton/pub/utils/fileutil.py
newton/newton/pub/utils/restcall.py
newton/newton/requests/views/image.py

index 56ab27e..e86d02b 100644 (file)
@@ -5,3 +5,7 @@
 target/
 logs/*.log
 *.pyc
+.tox
+.coverage
+htmlcov/
+
index 659d605..d595395 100644 (file)
@@ -1,4 +1,4 @@
 [gerrit]
-host=gerrit.open-o.org
+host=gerrit.onap.org
 port=29418
-project=multivimdriver-openstack.git
+project=multicloud/openstack.git
index 821f62a..e86d02b 100644 (file)
@@ -1,2 +1,11 @@
+.project
+.classpath
+.settings/
+.checkstyle
+target/
 logs/*.log
-*.pyc
\ No newline at end of file
+*.pyc
+.tox
+.coverage
+htmlcov/
+
index e930f06..a327da8 100644 (file)
@@ -48,12 +48,9 @@ class Extensions(APIView):
                         "alias": "epa-caps",
                         "description": "Multiple network support",
                         "name": "EPACapsQuery",
-                        'links': [ \
-                            {
-                                "url": "http://%s:%s/api/multicloud-newton/v0/%s/extensions/epa-caps" \
-                                       % (config.MSB_SERVICE_IP, config.MSB_SERVICE_PORT, vimid), \
-                                }
-                        ]
+                        "url": "http://%s:%s/api/multicloud-newton/v0/%s/extensions/epa-caps" \
+                                       % (config.MSB_SERVICE_IP, config.MSB_SERVICE_PORT, vimid),
+                        "spec": ""
                     }
                 ]
 
index fc7775b..9683711 100644 (file)
@@ -15,5 +15,15 @@ import os
 MSB_SERVICE_IP = '127.0.0.1'
 MSB_SERVICE_PORT = '80'
 
+# [A&AI]
+AAI_ADDR = "aai.api.simpledemo.openecomp.org"
+AAI_PORT = "8443"
+AAI_SERVICE_URL = 'https://%s:%s/aai' % (AAI_ADDR, AAI_PORT)
+AAI_SCHEMA_VERSION = "v11"
+AAI_USERNAME = 'AAI'
+AAI_PASSWORD = 'AAI'
+
+AAI_APP_ID = 'MultiCloud-Newton'
+
 # [IMAGE LOCAL PATH]
 ROOT_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
index 449ad5b..57beb0e 100644 (file)
@@ -15,7 +15,8 @@ import re
 
 from rest_framework import status
 from newton.pub.exceptions import VimDriverNewtonException
-from newton.pub.utils.restcall import req_by_msb
+from newton.pub.utils.restcall import req_by_msb,req_to_aai
+
 
 logger = logging.getLogger(__name__)
 
@@ -25,16 +26,28 @@ def get_vim_by_id(vim_id):
 
     if cloud_owner and cloud_region_id:
         retcode, content, status_code = \
-            req_by_msb("/api/aai-cloudInfrastructure/v1/cloud-infrastructure/cloud-regions/cloud-region/%s/%s"
-                       % (cloud_owner,cloud_region_id), "GET")
+            req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s"
+                       % (cloud_owner,cloud_region_id),"GET")
         if retcode != 0:
             logger.error("Status code is %s, detail is %s.", status_code, content)
             raise VimDriverNewtonException(
-                "Failed to query VIM with id (%s:%s,%s) from extsys." % (vim_id,cloud_owner,cloud_region_id),
+                "Failed to query VIM with id (%s:%s,%s)." % (vim_id,cloud_owner,cloud_region_id),
                 status_code, content)
         tmp_viminfo = json.JSONDecoder().decode(content)
+
+        #assume esr-system-info-id is composed by {cloud-owner} _ {cloud-region-id}
+        retcode2,content2,status_code2 = \
+            req_to_aai("/cloud-infrastructure/esr-system-info/%s/%s/%s_%s" \
+                       % (cloud_owner,cloud_region_id,cloud_owner,cloud_region_id),
+                       "GET")
+        if retcode2 != 0:
+            logger.error("Status code is %s, detail is %s.", status_code, content)
+            raise VimDriverNewtonException(
+                "Failed to query ESR system with id (%s:%s,%s)." % (vim_id,cloud_owner,cloud_region_id),
+                status_code, content)
+        tmp_authinfo = json.JSONDecoder().decode(content2)
+
         #convert vim information
-        #tbd
 
         if tmp_viminfo:
             viminfo = {}
@@ -47,15 +60,16 @@ def get_vim_by_id(vim_id):
             viminfo['cloud_extra_info'] = tmp_viminfo['cloud-extra-info']
             viminfo['cloud_epa_caps'] = tmp_viminfo['cloud-epa-caps']
 
-            tmp_authinfo = tmp_viminfo['auth-info-items'][0]
             if tmp_authinfo:
-                viminfo['userName'] = tmp_authinfo['username']
+                viminfo['userName'] = tmp_authinfo['user-name']
                 viminfo['password'] = tmp_authinfo['password']
                 viminfo['domain'] = tmp_authinfo['cloud-domain']
-                viminfo['url'] = tmp_authinfo['auth-url']
-                viminfo['tenant'] = tmp_authinfo['defaultTenant']['name'] if not tmp_authinfo['defaultTenant'] else None
+                viminfo['url'] = tmp_authinfo['url']
+                viminfo['tenant'] = tmp_authinfo['default-tenant']
                 viminfo['cacert'] = tmp_authinfo['ssl-cacert']
                 viminfo['insecure'] = tmp_authinfo['ssl-insecure']
+            else:
+                return None
 
             return viminfo
         else:
@@ -67,12 +81,12 @@ def delete_vim_by_id(vim_id):
     cloud_owner, cloud_region_id = decode_vim_id(vim_id)
     if cloud_owner and cloud_region_id:
         retcode, content, status_code = \
-            req_by_msb("/api/aai-cloudInfrastructure/v1/cloud-infrastructure/cloud-regions/cloud-region/%s/%s"
+            req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s"
                        % ( cloud_owner, cloud_region_id), "DELETE")
         if retcode != 0:
             logger.error("Status code is %s, detail is %s.", status_code, content)
             raise VimDriverNewtonException(
-                "Failed to delete VIM in AAI with id (%s:%s,%s) from extsys." % (vim_id,cloud_owner,cloud_region_id),
+                "Failed to delete VIM in AAI with id (%s:%s,%s)." % (vim_id,cloud_owner,cloud_region_id),
                 status_code, content)
         return 0
     # return non zero if failed to decode cloud owner and region id
index 6fbbec7..304564a 100644 (file)
@@ -36,8 +36,7 @@ def download_file_from_http(url, local_dir, file_name):
     is_download_ok = False
     try:
         make_dirs(local_dir)
-        r = urllib2.Request(url)
-        req = urllib2.urlopen(r)
+        req = urllib.request.urlopen(url)
         save_file = open(local_file_name, 'wb')
         save_file.write(req.read())
         save_file.close()
index d784f09..c2fa361 100644 (file)
@@ -16,10 +16,15 @@ from six.moves import urllib
 import uuid
 from six.moves import http_client
 import httplib2
+import uuid
 
 from rest_framework import status
-
+from newton.pub.config.config import AAI_SCHEMA_VERSION
+from newton.pub.config.config import AAI_SERVICE_URL
+from newton.pub.config.config import AAI_USERNAME
+from newton.pub.config.config import AAI_PASSWORD
 from newton.pub.config.config import MSB_SERVICE_IP, MSB_SERVICE_PORT
+from newton.pub.config.config import AAI_APP_ID
 
 rest_no_auth, rest_oneway_auth, rest_bothway_auth = 0, 1, 2
 HTTP_200_OK, HTTP_201_CREATED, HTTP_204_NO_CONTENT, HTTP_202_ACCEPTED \
@@ -72,11 +77,11 @@ def call_req(base_url, user, passwd, auth_type,
                 else:
                     ret = [1, resp_body, resp_status]
                 break
-            except httplib.ResponseNotReady:
+            except http.client.ResponseNotReady:
 #                logger.debug("retry_times=%d", retry_times)
                 ret = [1, "Unable to connect to %s" % full_url, resp_status]
                 continue
-    except urllib2.URLError as err:
+    except urllib.error.URLError as err:
         ret = [2, str(err), resp_status]
     except Exception:
         logger.error(traceback.format_exc())
@@ -100,6 +105,19 @@ def req_to_vim(base_url, resource, method, extra_headers='', content=''):
     return call_req(base_url, "", "", rest_no_auth,
                     resource, method, extra_headers, content)
 
+def req_to_aai(resource, method, content='', appid=AAI_APP_ID):
+    tmp_trasaction_id = uuid.uuid1()
+    headers = {
+        'X-FromAppId': appid,
+        'X-TransactionId': tmp_trasaction_id,
+        'content-type': 'application/json',
+        'accept': 'application/json'
+    }
+    base_url = "%s/%s" % (AAI_SERVICE_URL, AAI_SCHEMA_VERSION)
+    logger.debug("req_to_aai--%s::> %s, %s" % (tmp_trasaction_id, method, resource))
+    return call_req(base_url, AAI_USERNAME, AAI_PASSWORD, rest_no_auth,
+                    resource, method, content, headers)
+
 
 def combine_url(base_url, resource):
     full_url = None
index ff507bd..c6cfd3d 100644 (file)
@@ -158,7 +158,7 @@ class Images(APIView):
                 return Response(data={'error': 'imagePath is not specified'}, status=500)
 
             #valid image url
-            imagefd = urllib2.urlopen(imageurl)
+            imagefd = urllib.request.urlopen(imageurl)
             if not imagefd:
                 logger.debug("image is not available at %s" % imageurl)
                 return Response(data={'error': 'cannot access to specified imagePath'}, status=500)
@@ -201,7 +201,7 @@ class Images(APIView):
             return Response(data=resp_body, status=resp.status_code)
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
-        except urllib2.URLError as e:
+        except urllib.error.URLError as e:
             return Response(data={'error': 'image is not accessible:%s' % str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         except HttpError as e: