9e88e2de75d9d6827c4580b4a981162806af4a2b
[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.utils.restcall import req_by_msb
19 from lcm.pub.utils.values import ignore_case_get
20 from lcm.pub.msapi.extsys import get_vnfm_by_id
21
22 logger = logging.getLogger(__name__)
23
24
25 def send_nf_init_request(vnfm_inst_id, req_param):
26     vnfm = get_vnfm_by_id(vnfm_inst_id)
27     uri = '/api/%s/v1/%s/vnfs' % (vnfm["type"], vnfm_inst_id)
28     ret = req_by_msb(uri, "POST", req_param)
29     if ret[0] != 0:
30         logger.error("Failed to send nf init req:%s,%s", ret[2], ret[1])
31         raise NSLCMException('Failed to send nf init request to VNFM(%s)' % vnfm_inst_id)
32     return json.JSONDecoder().decode(ret[1])
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 def query_vnfm_job(vnfm_inst_id, job_id, response_id=0):
44     vnfm = get_vnfm_by_id(vnfm_inst_id)
45     retry_time = 3
46     uri = '/api/%s/v1/%s/jobs/%s?responseId=%s' % (vnfm["type"], 
47         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 def send_nf_scaling_request(vnfm_inst_id, vnf_inst_id, req_param):
63     vnfm = get_vnfm_by_id(vnfm_inst_id)
64     uri = '/api/%s/v1/%s/vnfs/%s/scale' % (vnfm["type"], vnfm_inst_id, vnf_inst_id)
65     ret = req_by_msb(uri, "POST", req_param)
66     if ret[0] > 0:
67         logger.error("Failed to send nf scale req:%s,%s", ret[2], ret[1])
68         raise NSLCMException('Failed to send nf scale request to VNFM(%s)' % vnfm_inst_id)
69     return json.JSONDecoder().decode(ret[1])
70
71
72 def send_nf_heal_request(vnfm_inst_id, vnf_inst_id, req_param):
73     vnfm = get_vnfm_by_id(vnfm_inst_id)
74     uri = "/api/%s/v1/%s/vnfs/%s/heal" % (vnfm["type"], vnfm_inst_id, vnf_inst_id)
75     ret = req_by_msb(uri, "POST", req_param)
76     if ret[0] > 0:
77         logger.error("Failed to send nf heal req:%s,%s", ret[2], ret[1])
78         raise NSLCMException('Failed to send nf heal request to VNFM(%s)' % vnfm_inst_id)
79     return json.JSONDecoder().decode(ret[1])