Optimized code of gvnfm-vnflcm
[vfc/gvnfm/vnflcm.git] / lcm / lcm / pub / msapi / catalog.py
1 # Copyright 2016 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.utils.restcall import req_by_msb
19 from lcm.pub.utils.values import ignore_case_get
20 from lcm.pub.exceptions import NFLCMException
21
22 logger = logging.getLogger(__name__)
23
24 STATUS_ONBOARDED, STATUS_NON_ONBOARDED = "onBoarded", "non-onBoarded"
25
26 P_STATUS_NORMAL, P_STATUS_ONBOARDING, P_STATUS_ONBOARDFAILED = "normal", "onBoarding", "onBoardFailed"
27 P_STATUS_DELETING, P_STATUS_DELETEFAILED = "deleting", "deleteFailed"
28
29
30 def query_csar_from_catalog(csar_id, key=''):
31     ret = req_by_msb("/openoapi/catalog/v1/csars/%s" % csar_id, "GET")
32     if ret[0] != 0:
33         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
34         if ret[2] == '404':
35             raise NFLCMException("CSAR(%s) does not exist." % csar_id)
36         raise NFLCMException("Failed to query CSAR(%s) from catalog." % csar_id)
37     csar_info = json.JSONDecoder().decode(ret[1])
38     return ignore_case_get(csar_info, key) if key else csar_info
39
40
41 def query_rawdata_from_catalog(csar_id, input_parameters=[]):
42     req_param = json.JSONEncoder().encode({"csarId": csar_id, "inputParameters": input_parameters})
43     ret = req_by_msb("/openoapi/catalog/v1/servicetemplates/queryingrawdata", "POST", req_param)
44     if ret[0] != 0:
45         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
46         raise NFLCMException("Failed to query rawdata of CSAR(%s) from catalog." % csar_id)
47     return json.JSONDecoder().decode(ret[1])
48
49
50 def set_csar_state(csar_id, prop, val):
51     ret = req_by_msb("/openoapi/catalog/v1/csars/%s?%s=%s" % (csar_id, prop, val), "PUT")
52     if ret[0] != 0:
53         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
54         return [1, "Failed to set %s to %s of CSAR(%s)." % (prop, val, csar_id)]
55     return [0, "Set %s to %s of CSAR(%s) successfully." % (prop, val, csar_id)]
56
57
58 def delete_csar_from_catalog(csar_id):
59     ret = req_by_msb("/openoapi/catalog/v1/csars/%s" % csar_id, "DELETE")
60     if ret[0] != 0 and ret[2] != '404':
61         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
62         set_csar_state(csar_id, "processState", P_STATUS_DELETEFAILED)
63         return [1, "Failed to delete CSAR(%s) from catalog." % csar_id]
64     return [0, "Delete CSAR(%s) successfully." % csar_id]
65
66
67 def get_download_url_from_catalog(csar_id, relative_path):
68     ret = req_by_msb("/openoapi/catalog/v1/csars/%s/files?relativePath=%s" % (csar_id, relative_path), "GET")
69     if ret[0] != 0:
70         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
71         raise NFLCMException("Failed to get download url of CSAR(%s)." % csar_id)
72     csar_file_info = json.JSONDecoder().decode(ret[1])
73     return ignore_case_get(csar_file_info, "downloadUri"), ignore_case_get(csar_file_info, "localPath")
74
75
76 def get_process_id(name, srv_template_id):
77     ret = req_by_msb('/openoapi/catalog/v1/servicetemplates/%s/operations' % srv_template_id, 'GET')
78     if ret[0] != 0:
79         raise NFLCMException('Failed to get service[%s,%s] process id' % (name, srv_template_id))
80     items = json.JSONDecoder().decode(ret[1])
81     for item in items:
82         if name in item['name']:
83             return item['processId']
84     raise NFLCMException('service[%s,%s] process id not exist' % (name, srv_template_id))
85
86
87 def get_servicetemplate_id(nsd_id):
88     ret = req_by_msb('/openoapi/catalog/v1/servicetemplates', 'GET')
89     if ret[0] != 0:
90         raise NFLCMException('Failed to get servicetemplates info')
91     stpls = json.JSONDecoder().decode(ret[1])
92     for stpl in stpls:
93         if stpl["id"] == nsd_id:
94             return stpl["serviceTemplateId"]
95     raise NFLCMException('servicetemplate(%s) does not exist.' % nsd_id)
96
97
98 def get_servicetemplate(nsd_id):
99     ret = req_by_msb('/openoapi/catalog/v1/servicetemplates', 'GET')
100     if ret[0] != 0:
101         raise NFLCMException('Failed to get servicetemplates info')
102     stpls = json.JSONDecoder().decode(ret[1])
103     for stpl in stpls:
104         if stpl["id"] == nsd_id:
105             return stpl
106     return NFLCMException('servicetemplate(%s) does not exist.' % nsd_id)