Add Sdc rest call config
[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
18 from lcm.pub.exceptions import NSLCMException
19 from lcm.pub.utils.restcall import call_req, rest_no_auth
20 from lcm.pub.config.config import SDC_BASE_URL, SDC_USER, SDC_PASSWD
21
22 logger = logging.getLogger(__name__)
23
24 ASSETTYPE_RESOURCES = "resources" 
25 ASSETTYPE_SERVICES = "services"
26
27 def call_sdc(resource, method, content=''):
28     return call_req(base_url=SDC_BASE_URL, 
29         user=SDC_USER, 
30         passwd=SDC_PASSWD, 
31         auth_type=rest_no_auth, 
32         resource=resource, 
33         method=method, 
34         content=content)
35
36 def get_artifacts(asset_type):
37     resource = "/sdc/v1/catalog/{assetType}"
38     resource = resource.format(assetType=asset_type)
39     ret = call_sdc(resource, "GET")
40     if ret[0] != 0:
41         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
42         raise NSLCMException("Failed to query artifacts(%s) from sdc." % asset_type)
43     return json.JSONDecoder().decode(ret[1])
44
45 def get_artifact(asset_type, csar_id):
46     artifacts = get_artifacts(asset_type)
47     for artifact in artifacts:
48         if artifact["uuid"] == csar_id:
49             return artifact
50     raise NSLCMException("Failed to query artifact(%s,%s) from sdc." % (asset_type, csar_id))
51
52 def delete_artifact(asset_type, asset_id, artifact_id):
53     resource = "/sdc/v1/catalog/{assetType}/{uuid}/artifacts/{artifactUUID}"
54     resource = resource.format(assetType=asset_type, uuid=asset_id, artifactUUID=artifact_id)
55     ret = call_sdc(resource, "DELETE")
56     if ret[0] != 0:
57         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
58         raise NSLCMException("Failed to delete artifacts(%s) from sdc." % artifact_id)
59     return json.JSONDecoder().decode(ret[1])
60
61 def download_artifacts(download_url, local_path):
62     ret = call_req(base_url=download_url, 
63         user=SDC_USER, 
64         passwd=SDC_PASSWD, 
65         auth_type=rest_no_auth, 
66         resource="", 
67         method="GET")
68     # TODO:
69     
70
71     
72
73
74    
75
76
77
78