update link to upper-constraints.txt
[vfc/nfvo/lcm.git] / lcm / pub / msapi / vnfmdriver.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 import json
15 import logging
16
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
20
21 logger = logging.getLogger(__name__)
22
23
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)
28     if ret[0] != 0:
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])
32
33
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)
38     if ret[0] > 0:
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 {}
42
43
44 def query_vnfm_job(vnfm_inst_id, job_id, response_id=0):
45     vnfm = get_vnfm_by_id(vnfm_inst_id)
46     retry_time = 3
47     uri = '/api/%s/v1/%s/jobs/%s?responseId=%s' % (vnfm["type"], vnfm_inst_id, job_id, response_id)
48     while retry_time > 0:
49         rsp = req_by_msb(uri, "GET")
50         if str(rsp[2]) == '404':
51             return False, ''
52         if rsp[0] != 0:
53             logger.warning('retry_time=%s, detail message:%s' % (retry_time, rsp[1]))
54             retry_time -= 1
55         else:
56             break
57     if retry_time <= 0:
58         logger.error(rsp[1])
59         raise NSLCMException('Failed to query job from VNFM!')
60     return True, json.JSONDecoder().decode(rsp[1])
61
62
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)
67     if ret[0] > 0:
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])
71
72
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)
77     if ret[0] > 0:
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 {}
81
82
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)
87     if ret[0] > 0:
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])