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