263c6cbf96cfe2e6c9385b318bf4972f2257793f
[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
20
21 logger = logging.getLogger(__name__)
22
23 ASSETTYPE_RESOURCES = "resources" 
24 ASSETTYPE_SERVICES = "services"
25
26 def call_sdc(resource, method, content=''):
27     base_url = "TODO: SDC Base URL"
28     sdc_user = "TODO: sdc user"
29     sdc_passwd = "TODO: sdc passwd"
30     sdc_auth_type = "TODO: sdc auth type"
31     return call_req(base_url, sdc_user, sdc_passwd, sdc_auth_type, resource, method, content)
32
33 def get_artifacts(asset_type):
34     resource = "/sdc/v1/catalog/{assetType}"
35     resource = resource.format(assetType=asset_type)
36     ret = call_sdc(resource, "GET")
37     if ret[0] != 0:
38         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
39         raise NSLCMException("Failed to query artifacts(%s) from sdc." % asset_type)
40     return json.JSONDecoder().decode(ret[1])
41
42 def get_artifact(asset_type, csar_id):
43     artifacts = get_artifacts(asset_type)
44     for artifact in artifacts:
45         if artifact["uuid"] == csar_id:
46             return artifact
47     raise NSLCMException("Failed to query artifact(%s,%s) from sdc." % (asset_type, csar_id))
48
49 def delete_artifact(asset_type, asset_id, artifact_id):
50     resource = "/sdc/v1/catalog/{assetType}/{uuid}/artifacts/{artifactUUID}"
51     resource = resource.format(assetType=asset_type, uuid=asset_id, artifactUUID=artifact_id)
52     ret = call_sdc(resource, "DELETE")
53     if ret[0] != 0:
54         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
55         raise NSLCMException("Failed to delete artifacts(%s) from sdc." % artifact_id)
56     return json.JSONDecoder().decode(ret[1])
57
58 def download_artifacts(download_url, local_path):
59     sdc_user = "TODO: sdc user"
60     sdc_passwd = "TODO: sdc passwd"
61     sdc_auth_type = "TODO: sdc auth type"
62     ret = call_req(download_url, sdc_user, sdc_passwd, sdc_auth_type, "", "GET")
63     
64
65     
66
67
68    
69
70
71
72