Implement query vim info from ESR 87/10687/1
authorying.yunlong <ying.yunlong@zte.com.cn>
Thu, 7 Sep 2017 03:53:32 +0000 (11:53 +0800)
committerying.yunlong <ying.yunlong@zte.com.cn>
Thu, 7 Sep 2017 03:53:32 +0000 (11:53 +0800)
Add get_vim_by_id and get_vims methods to get vnfm
info from esr.

Change-Id: I94f64e46afb1cdb279465c24a6ea710eb617548e
Issue-ID: VFC-305
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
lcm/pub/msapi/aai.py

index 3ae685e..6fa1eff 100644 (file)
@@ -29,7 +29,7 @@ def call_aai(resource, method, content=''):
         'X-FromAppId': 'VFC-NFVO-LCM',
         'X-TransactionId': str(uuid.uuid1())
     }
-    return restcall.call_req(base_url=AAI_BASE_URL, 
+    return restcall.call_req(base_url=AAI_BASE_URL,
         user=AAI_USER, 
         passwd=AAI_PASSWD, 
         auth_type=restcall.rest_no_auth, 
@@ -192,7 +192,8 @@ def get_vnfm_by_id(vnfm_inst_id):
         raise NSLCMException('Send get VNFM information request to extsys failed.')
 
     # convert vnfm_info_aai to internal vnfm_info
-    vnfm_info = convert_vnfm_info(json.JSONDecoder().decode(ret[1]))
+    vnfm_info_aai = json.JSONDecoder().decode(ret[1])
+    vnfm_info = convert_vnfm_info(vnfm_info_aai)
     return vnfm_info
 
 def convert_vnfm_info(vnfm_info_aai):
@@ -226,6 +227,62 @@ def select_vnfm(vnfm_type, vim_id):
         type = ignore_case_get(esr_system_info, "type")
         vimId = vnfm["vnfm-id"]
         if type == vnfm_type and vimId == vim_id:
+            # convert vnfm_info_aai to internal vnfm_info
             vnfm = convert_vnfm_info(vnfm)
             return vnfm
     raise NSLCMException('No vnfm found with %s in vim(%s)' % (vnfm_type, vim_id))
+
+
+def get_vim_by_id(vim_id):
+    cloud_owner, cloud_region = split_vim_to_owner_region(vim_id)
+    ret = call_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s" % (cloud_owner, cloud_region), "GET")
+    if ret[0] != 0:
+        logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
+        raise NSLCMException("Failed to query vim(%s) from extsys." % vim_id)
+
+    # convert vim_info_aai to internal vim_info
+    vim_info_aai = json.JSONDecoder().decode(ret[1])
+    vim_info = convert_vim_info(vim_info_aai)
+    return vim_info
+
+def split_vim_to_owner_region(vim_id):
+    split_vim = vim_id.split('_')
+    cloud_owner = split_vim[0]
+    cloud_region = "".join(split_vim[1:])
+    return cloud_owner, cloud_region
+
+def convert_vim_info(vim_info_aai):
+    vim_id = vim_info_aai["cloud-owner"] + '_' + vim_info_aai["cloud-region-id"]
+    esr_system_info = ignore_case_get(vim_info_aai, "esr-system-info")
+    tenants = ignore_case_get(vim_info_aai, "tenants")
+    vim_info = {
+        "vimId": vim_id,
+        "name": vim_id,
+        "url": ignore_case_get(esr_system_info, "service-url"),
+        "userName": ignore_case_get(esr_system_info, "service-url"),
+        "password": ignore_case_get(esr_system_info, "service-url"),
+        "tenant": ignore_case_get(tenants[0], "tenant-id"),
+        "vendor": ignore_case_get(esr_system_info, "vendor"),
+        "version": ignore_case_get(esr_system_info, "version"),
+        "description": "vim",
+        "domain": "",
+        "type": "openstack",
+        "createTime": "2016-07-18 12:22:53"
+    }
+    return vim_info
+
+
+def get_vims():
+    ret = call_aai("/cloud-infrastructure/cloud-regions", "GET")
+    if ret[0] != 0:
+        logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
+        raise NSLCMException("Failed to query vims from extsys.")
+
+    # convert vim_info_aai to internal vim_info
+    vims_aai = json.JSONDecoder().decode(ret[1])
+    vims_info = []
+    for vim in vims_aai:
+        vim = convert_vim_info(vim)
+        vims_info.append(vim)
+
+    return vims_info