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