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.
17 from lcm.pub.exceptions import NSLCMException
18 from lcm.pub.msapi.extsys import get_vnfm_by_id
19 from lcm.pub.utils.restcall import req_by_msb
21 logger = logging.getLogger(__name__)
24 def send_nf_init_request(vnfm_inst_id, req_param):
25 vnfm = get_vnfm_by_id(vnfm_inst_id)
26 uri = '/api/%s/v1/%s/vnfs' % (vnfm["type"], vnfm_inst_id)
27 ret = req_by_msb(uri, "POST", req_param)
29 logger.error("Failed to send nf init req:%s,%s", ret[2], ret[1])
30 raise NSLCMException('Failed to send nf init request to VNFM(%s)' % vnfm_inst_id)
31 return json.JSONDecoder().decode(ret[1])
34 def send_nf_terminate_request(vnfm_inst_id, vnf_inst_id, req_param):
35 vnfm = get_vnfm_by_id(vnfm_inst_id)
36 uri = '/api/%s/v1/%s/vnfs/%s/terminate' % (vnfm["type"], vnfm_inst_id, vnf_inst_id)
37 ret = req_by_msb(uri, "POST", req_param)
39 logger.error("Failed to send nf terminate req:%s,%s", ret[2], ret[1])
40 raise NSLCMException('Failed to send nf terminate request to VNFM(%s)' % vnfm_inst_id)
41 return json.JSONDecoder().decode(ret[1]) if ret[1] else {}
44 def query_vnfm_job(vnfm_inst_id, job_id, response_id=0):
45 vnfm = get_vnfm_by_id(vnfm_inst_id)
47 uri = '/api/%s/v1/%s/jobs/%s?responseId=%s' % (vnfm["type"], vnfm_inst_id, job_id, response_id)
49 rsp = req_by_msb(uri, "GET")
50 if str(rsp[2]) == '404':
53 logger.warning('retry_time=%s, detail message:%s' % (retry_time, rsp[1]))
59 raise NSLCMException('Failed to query job from VNFM!')
60 return True, json.JSONDecoder().decode(rsp[1])
63 def send_nf_scaling_request(vnfm_inst_id, vnf_inst_id, req_param):
64 vnfm = get_vnfm_by_id(vnfm_inst_id)
65 uri = '/api/%s/v1/%s/vnfs/%s/scale' % (vnfm["type"], vnfm_inst_id, vnf_inst_id)
66 ret = req_by_msb(uri, "POST", req_param)
68 logger.error("Failed to send nf scale req:%s,%s", ret[2], ret[1])
69 raise NSLCMException('Failed to send nf scale request to VNFM(%s)' % vnfm_inst_id)
70 return json.JSONDecoder().decode(ret[1])
73 def send_nf_heal_request(vnfm_inst_id, vnf_inst_id, req_param):
74 vnfm = get_vnfm_by_id(vnfm_inst_id)
75 uri = "/api/%s/v1/%s/vnfs/%s/heal" % (vnfm["type"], vnfm_inst_id, vnf_inst_id)
76 ret = req_by_msb(uri, "POST", req_param)
78 logger.error("Failed to send nf heal req:%s,%s", ret[2], ret[1])
79 raise NSLCMException('Failed to send nf heal request to VNFM(%s)' % vnfm_inst_id)
80 return json.JSONDecoder().decode(ret[1]) if ret[1] else {}
83 def send_nf_operate_request(vnfm_inst_id, vnf_inst_id, req_param):
84 vnfm = get_vnfm_by_id(vnfm_inst_id)
85 uri = "/api/%s/v1/%s/vnfs/%s/operate" % (vnfm["type"], vnfm_inst_id, vnf_inst_id)
86 ret = req_by_msb(uri, "POST", req_param)
88 logger.error("Failed to send nf operate req:%s,%s", ret[2], ret[1])
89 raise NSLCMException('Failed to send nf operate request to VNFM(%s)' % vnfm_inst_id)
90 return json.JSONDecoder().decode(ret[1])