1 # Copyright 2017 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 from genericparser.pub.exceptions import GenericparserException
20 from genericparser.pub.utils import restcall
21 from genericparser.pub.utils import fileutil
22 from genericparser.pub.config.config import SDC_BASE_URL, SDC_USER, SDC_PASSWD
24 logger = logging.getLogger(__name__)
26 ASSETTYPE_RESOURCES = "resources"
27 ASSETTYPE_SERVICES = "services"
28 DISTRIBUTED = "DISTRIBUTED"
31 def call_sdc(resource, method, content=''):
32 additional_headers = {
33 'X-ECOMP-InstanceID': 'VFC',
35 return restcall.call_req(base_url=SDC_BASE_URL,
38 auth_type=restcall.rest_no_auth,
42 additional_headers=additional_headers)
46 sample of return value
49 "uuid": "c94490a0-f7ef-48be-b3f8-8d8662a37236",
50 "invariantUUID": "63eaec39-ffbe-411c-a838-448f2c73f7eb",
51 "name": "underlayvpn",
53 "toscaModelURL": "/sdc/v1/genericparser/resources/c94490a0-f7ef-48be-b3f8-8d8662a37236/toscaModel",
55 "subCategory": "VolteVF",
57 "lifecycleState": "CERTIFIED",
58 "lastUpdaterUserId": "jh0003"
64 def get_artifacts(asset_type):
65 resource = "/sdc/v1/genericparser/{assetType}"
66 resource = resource.format(assetType=asset_type)
67 ret = call_sdc(resource, "GET")
69 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
70 raise GenericparserException("Failed to query artifacts(%s) from sdc." % asset_type)
71 return json.JSONDecoder().decode(ret[1])
74 def get_artifact(asset_type, csar_id):
75 artifacts = get_artifacts(asset_type)
76 for artifact in artifacts:
77 if artifact["uuid"] == csar_id:
78 if asset_type == ASSETTYPE_SERVICES and \
79 artifact.get("distributionStatus", None) != DISTRIBUTED:
80 raise GenericparserException("The artifact (%s,%s) is not distributed from sdc." % (asset_type, csar_id))
83 raise GenericparserException("Failed to query artifact(%s,%s) from sdc." % (asset_type, csar_id))
86 def delete_artifact(asset_type, asset_id, artifact_id):
87 resource = "/sdc/v1/genericparser/{assetType}/{uuid}/artifacts/{artifactUUID}"
88 resource = resource.format(assetType=asset_type, uuid=asset_id, artifactUUID=artifact_id)
89 ret = call_sdc(resource, "DELETE")
91 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
92 raise GenericparserException("Failed to delete artifacts(%s) from sdc." % artifact_id)
93 return json.JSONDecoder().decode(ret[1])
96 def download_artifacts(download_url, local_path, file_name):
97 additional_headers = {
98 'X-ECOMP-InstanceID': 'VFC',
99 'accept': 'application/octet-stream'
101 ret = restcall.call_req(base_url=SDC_BASE_URL,
104 auth_type=restcall.rest_no_auth,
105 resource=download_url,
107 additional_headers=additional_headers)
109 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
110 raise GenericparserException("Failed to download %s from sdc." % download_url)
111 fileutil.make_dirs(local_path)
112 local_file_name = os.path.join(local_path, file_name)
113 local_file = open(local_file_name, 'wb')
114 local_file.write(ret[1])
116 return local_file_name