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.exceptions import NSLCMException
19 from lcm.pub.utils.restcall import req_by_msb
21 logger = logging.getLogger(__name__)
24 def create_vl(req_param):
25 ret = req_by_msb("/api/resmgr/v1/vl", "POST", json.JSONEncoder().encode(req_param))
27 logger.error("Failed to create vl to resmgr. detail is %s.", ret[1])
28 # raise NSLCMException('Failed to create vl to resmgr.')
29 # return json.JSONDecoder().decode(ret[1])
32 def delete_vl(vl_inst_id):
33 ret = req_by_msb("/api/resmgr/v1/vl/%s" % vl_inst_id, "DELETE")
35 logger.error("Failed to delete vl(%s) to resmgr. detail is %s.", vl_inst_id, ret[1])
36 # raise NSLCMException("Failed to delete vl(%s) to resmgr." % vl_inst_id)
39 def delete_sfc(sfc_inst_id):
40 ret = req_by_msb("/api/resmgr/v1/sfc/%s" % sfc_inst_id, "DELETE")
42 logger.error("Failed to delete sfc(%s) to resmgr. detail is %s.", sfc_inst_id, ret[1])
43 # raise NSLCMException("Failed to delete sfc(%s) to resmgr." % sfc_inst_id)
46 def grant_vnf(req_param):
47 grant_data = json.JSONEncoder().encode(req_param)
48 ret = req_by_msb("/api/resmgr/v1/resource/grant", "PUT", grant_data)
50 logger.error("Failed to grant vnf to resmgr. detail is %s.", ret[1])
51 # raise NSLCMException('Failed to grant vnf to resmgr.')
53 if "vimId" in req_param:
54 vim_id = req_param["vimId"]
55 elif "additionalparam" in req_param and "vimid" in req_param["additionalparam"]:
56 vim_id = req_param["additionalparam"]["vimid"]
57 elif "additionalParams" in req_param and "vimid" in req_param["additionalParams"]:
58 vim_id = req_param["additionalParams"]["vimid"]
60 from lcm.pub.msapi import extsys
61 vim = extsys.get_vim_by_id(vim_id)
62 if isinstance(vim, list):
71 "tenant": vim["tenant"]
75 logger.debug("grant_rsp=%s" % grant_rsp)
78 raise NSLCMException('Failed to grant vnf to resmgr.')
79 return json.JSONDecoder().decode(ret[1])
83 uri = '/api/resmgr/v1/vnf'
84 req_param = json.JSONEncoder().encode({
85 'orchVnfInstanceId': data['nf_inst_id'],
86 'vnfInstanceId': data['vnfm_nf_inst_id'],
87 'vnfInstanceName': data['vnf_inst_name'],
88 'nsId': data['ns_inst_id'],
89 'nsName': data['ns_inst_name'],
90 'vnfmId': data['vnfm_inst_id'],
91 'vnfmName': data['vnfm_inst_name'],
92 'vnfPackageName': data['vnfd_name'],
93 'vnfDescriptorName': data['vnfd_id'],
94 'jobId': data['job_id'],
95 'vnfStatus': data['nf_inst_status'],
96 'vnfType': data['vnf_type'],
97 'onboardingId': data['nf_package_id'],
98 'onboardingName': data['vnfd_name']})
100 ret = req_by_msb(uri, "POST", req_param)
102 logger.error('Send create VNF request to resmgr failed.')
103 # raise NSLCMException('Send create VNF request to resmgr failed.')
106 def create_vnf_creation_info(data):
107 uri = '/api/resmgr/v1/vnfinfo'
108 req_param = json.JSONEncoder().encode({
109 'vnfInstanceId': data['nf_inst_id'],
110 'nsId': data['ns_inst_id'],
111 'vnfmId': data['vnfm_inst_id'],
114 ret = req_by_msb(uri, "POST", req_param)
116 logger.error('Send write vnf creation information to resmgr failed.')
117 # raise NSLCMException('Send write vnf creation information to resmgr failed.')
120 def terminate_vnf(vnf_inst_id):
121 uri = '/api/resmgr/v1/vnf/%s' % vnf_inst_id
123 ret = req_by_msb(uri, "DELETE", json.dumps(req_param))
125 logger.error('Send terminate VNF request to resmgr failed.')
126 # raise NSLCMException('Send terminate VNF request to resmgr failed.')