update link to upper-constraints.txt
[vfc/nfvo/lcm.git] / lcm / pub / msapi / sdc.py
1 # Copyright 2017 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import json
16 import logging
17 import os
18
19 from lcm.pub.exceptions import NSLCMException
20 from lcm.pub.utils import restcall
21 from lcm.pub.config.config import SDC_BASE_URL, SDC_USER, SDC_PASSWD
22
23 logger = logging.getLogger(__name__)
24
25 ASSETTYPE_RESOURCES = "resources"
26 ASSETTYPE_SERVICES = "services"
27
28
29 def call_sdc(resource, method, content=''):
30     additional_headers = {
31         'X-ECOMP-InstanceID': 'VFC',
32     }
33     return restcall.call_req(base_url=SDC_BASE_URL,
34                              user=SDC_USER,
35                              passwd=SDC_PASSWD,
36                              auth_type=restcall.rest_no_auth,
37                              resource=resource,
38                              method=method,
39                              content=content,
40                              additional_headers=additional_headers)
41
42
43 """
44 sample of return value
45 [
46     {
47         "uuid": "c94490a0-f7ef-48be-b3f8-8d8662a37236",
48         "invariantUUID": "63eaec39-ffbe-411c-a838-448f2c73f7eb",
49         "name": "underlayvpn",
50         "version": "2.0",
51         "toscaModelURL": "/sdc/v1/catalog/resources/c94490a0-f7ef-48be-b3f8-8d8662a37236/toscaModel",
52         "category": "Volte",
53         "subCategory": "VolteVF",
54         "resourceType": "VF",
55         "lifecycleState": "CERTIFIED",
56         "lastUpdaterUserId": "jh0003"
57     }
58 ]
59 """
60
61
62 def get_artifacts(asset_type):
63     resource = "/sdc/v1/catalog/{assetType}"
64     resource = resource.format(assetType=asset_type)
65     ret = call_sdc(resource, "GET")
66     if ret[0] != 0:
67         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
68         raise NSLCMException("Failed to query artifacts(%s) from sdc." % asset_type)
69     return json.JSONDecoder().decode(ret[1])
70
71
72 def get_artifact(asset_type, csar_id):
73     artifacts = get_artifacts(asset_type)
74     for artifact in artifacts:
75         if artifact["uuid"] == csar_id:
76             return artifact
77     raise NSLCMException("Failed to query artifact(%s,%s) from sdc." % (asset_type, csar_id))
78
79
80 def delete_artifact(asset_type, asset_id, artifact_id):
81     resource = "/sdc/v1/catalog/{assetType}/{uuid}/artifacts/{artifactUUID}"
82     resource = resource.format(assetType=asset_type, uuid=asset_id, artifactUUID=artifact_id)
83     ret = call_sdc(resource, "DELETE")
84     if ret[0] != 0:
85         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
86         raise NSLCMException("Failed to delete artifacts(%s) from sdc." % artifact_id)
87     return json.JSONDecoder().decode(ret[1])
88
89
90 def download_artifacts(download_url, local_path, file_name):
91     additional_headers = {
92         'X-ECOMP-InstanceID': 'VFC',
93         'accept': 'application/octet-stream'
94     }
95     ret = restcall.call_req(base_url=SDC_BASE_URL,
96                             user=SDC_USER,
97                             passwd=SDC_PASSWD,
98                             auth_type=restcall.rest_no_auth,
99                             resource=download_url,
100                             method="GET",
101                             additional_headers=additional_headers)
102     if ret[0] != 0:
103         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
104         raise NSLCMException("Failed to download %s from sdc." % download_url)
105     local_file_name = os.path.join(local_path, file_name)
106     local_file = open(local_file_name, 'wb')
107     local_file.write(ret[1])
108     local_file.close()
109     return local_file_name