1 # Copyright 2016 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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 NSLCMException
22 logger = logging.getLogger(__name__)
24 STATUS_ONBOARDED, STATUS_NON_ONBOARDED = "onBoarded", "non-onBoarded"
25 P_STATUS_ENABLED, P_STATUS_DISABLED = "Enabled", "Disabled"
26 P_STATUS_NORMAL, P_STATUS_ONBOARDING, P_STATUS_ONBOARDFAILED = "normal", "onBoarding", "onBoardFailed"
27 P_STATUS_DELETING, P_STATUS_DELETEFAILED = "deleting", "deleteFailed"
30 def query_csar_from_catalog(csar_id, key=''):
31 ret = req_by_msb("/api/catalog/v1/csars/%s" % csar_id, "GET")
33 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
35 raise NSLCMException("CSAR(%s) does not exist." % csar_id)
36 raise NSLCMException("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
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("/api/catalog/v1/servicetemplates/queryingrawdata", "POST", req_param)
45 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
46 raise NSLCMException("Failed to query rawdata of CSAR(%s) from catalog." % csar_id)
47 return json.JSONDecoder().decode(ret[1])
50 def set_csar_state(csar_id, prop, val):
51 ret = req_by_msb("/api/catalog/v1/csars/%s?%s=%s" % (csar_id, prop, val), "PUT")
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)]
58 def delete_csar_from_catalog(csar_id):
59 ret = req_by_msb("/api/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]
67 def get_download_url_from_catalog(csar_id, relative_path):
68 ret = req_by_msb("/api/catalog/v1/csars/%s/files?relativePath=%s" % (csar_id, relative_path), "GET")
70 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
71 raise NSLCMException("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")
76 def get_process_id(name, srv_template_id):
77 ret = req_by_msb('/api/catalog/v1/servicetemplates/%s/operations' % srv_template_id, 'GET')
79 raise NSLCMException('Failed to get service[%s,%s] process id' % (name, srv_template_id))
80 items = json.JSONDecoder().decode(ret[1])
82 if name in item['name']:
83 return item['processId']
84 raise NSLCMException('service[%s,%s] process id not exist' % (name, srv_template_id))
87 def get_servicetemplate_id(nsd_id):
88 ret = req_by_msb('/api/catalog/v1/servicetemplates', 'GET')
90 raise NSLCMException('Failed to get servicetemplates info')
91 stpls = json.JSONDecoder().decode(ret[1])
93 if stpl.get("id", "") == nsd_id:
94 return stpl["serviceTemplateId"]
95 raise NSLCMException('servicetemplate(%s) does not exist.' % nsd_id)
98 def get_servicetemplate(nsd_id):
99 ret = req_by_msb('/api/catalog/v1/servicetemplates', 'GET')
101 raise NSLCMException('Failed to get servicetemplates info')
102 stpls = json.JSONDecoder().decode(ret[1])
104 if stpl.get("id", "") == nsd_id:
106 return NSLCMException('servicetemplate(%s) does not exist.' % nsd_id)