Add artifact download logic of vfc-nfvo-lcm 59/10059/1
authorfujinhua <fu.jinhua@zte.com.cn>
Sat, 2 Sep 2017 03:35:18 +0000 (11:35 +0800)
committerfujinhua <fu.jinhua@zte.com.cn>
Sat, 2 Sep 2017 03:35:18 +0000 (11:35 +0800)
Change-Id: I6ee84996c09618f1faa0df93f8bf40520e3ea751
Issue-Id: VFC-242
Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
lcm/packages/sdc_nf_package.py
lcm/packages/sdc_ns_package.py
lcm/pub/msapi/sdc.py
lcm/pub/utils/restcall.py

index fe257a9..11f27b3 100644 (file)
@@ -98,7 +98,8 @@ class SdcNfDistributeThread(threading.Thread):
 
         artifact = sdc.get_artifact(sdc.ASSETTYPE_RESOURCES, self.csar_id)
         local_path = os.path.join(CATALOG_ROOT_PATH, self.csar_id)
-        local_file_name = sdc.download_artifacts(artifact["toscaModelURL"], local_path)
+        local_file_name = sdc.download_artifacts(artifact["toscaModelURL"], 
+            local_path, "%s.csar" % artifact.get("name", self.csar_id))
         
         vnfd_json = toscaparser.parse_vnfd(local_file_name)
         vnfd = json.JSONDecoder().decode(vnfd_json)
index d8960dd..9824b39 100644 (file)
@@ -100,7 +100,8 @@ class SdcNsPackage(object):
 
         artifact = sdc.get_artifact(sdc.ASSETTYPE_SERVICES, csar_id)
         local_path = os.path.join(CATALOG_ROOT_PATH, csar_id)
-        local_file_name = sdc.download_artifacts(artifact["toscaModelURL"], local_path)
+        local_file_name = sdc.download_artifacts(artifact["toscaModelURL"], 
+            local_path, "%s.csar" % artifact.get("name", csar_id))
         
         nsd_json = toscaparser.parse_nsd(local_file_name)
         nsd = json.JSONDecoder().decode(nsd_json)
index 79a48d2..76f0d0c 100644 (file)
@@ -14,6 +14,7 @@
 
 import json
 import logging
+import os
 
 from lcm.pub.exceptions import NSLCMException
 from lcm.pub.utils import restcall
@@ -26,7 +27,7 @@ ASSETTYPE_SERVICES = "services"
 
 def call_sdc(resource, method, content=''):
     additional_headers = {
-        'X-ECOMP-InstanceID': 'VFC'
+        'X-ECOMP-InstanceID': 'VFC',
     }
     return restcall.call_req(base_url=SDC_BASE_URL, 
         user=SDC_USER, 
@@ -79,14 +80,27 @@ def delete_artifact(asset_type, asset_id, artifact_id):
         raise NSLCMException("Failed to delete artifacts(%s) from sdc." % artifact_id)
     return json.JSONDecoder().decode(ret[1])
 
-def download_artifacts(download_url, local_path):
+def download_artifacts(download_url, local_path, file_name):
+    additional_headers = {
+        'X-ECOMP-InstanceID': 'VFC',
+        'accept': 'application/octet-stream'
+    }
     ret = restcall.call_req(base_url=SDC_BASE_URL, 
         user=SDC_USER, 
         passwd=SDC_PASSWD, 
         auth_type=rest_no_auth, 
         resource=download_url, 
-        method="GET")
-    # TODO:
+        method="GET",
+        additional_headers=additional_headers)
+    if ret[0] != 0:
+        logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
+        raise NSLCMException("Failed to download %s from sdc." % download_url)
+    local_file_name = os.path.join(local_path, file_name)
+    local_file = open(local_file_name, 'wb')
+    local_file.write(ret[1])
+    local_file.close()
+    return local_file_name
+
     
 
     
index d05d30b..16f89ee 100644 (file)
@@ -49,8 +49,11 @@ def call_req(base_url, user, passwd, auth_type, resource, method,
             http.follow_all_redirects = True
             try:
                 resp, resp_content = http.request(full_url, method=method.upper(), body=content, headers=headers)
-                resp_status, resp_body = resp['status'], resp_content.decode('UTF-8')
-                logger.debug("[%s][%d]status=%s,resp_body=%s)" % (callid, retry_times, resp_status, resp_body))
+                resp_status, resp_body = resp['status'], resp_content
+                logger.debug("[%s][%d]status=%s)" % (callid, retry_times, resp_status))
+                if headers['accept'] == 'application/json':
+                    resp_body = resp_content.decode('UTF-8')
+                    logger.debug("resp_body=%s", resp_body)
                 if resp_status in status_ok_list:
                     ret = [0, resp_body, resp_status]
                 else: