From cdad4f8c567947e62278017c18bc4219f0e6a76c Mon Sep 17 00:00:00 2001 From: tianxing <15210838572@139.com> Date: Thu, 20 Sep 2018 17:49:18 +0800 Subject: [PATCH] add vnf operation service Change-Id: Ic3b288dfd6b3e384bb4ef4e45f2806c5b7032223 Issue-ID: VFC-1138 Signed-off-by: tianxing <15210838572@139.com> --- lcm/ns_vnfs/biz/update_vnfs.py | 88 ++++++++++++++++++++++++++++++++++++++++++ lcm/ns_vnfs/const.py | 2 +- lcm/pub/msapi/vnfmdriver.py | 10 +++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 lcm/ns_vnfs/biz/update_vnfs.py diff --git a/lcm/ns_vnfs/biz/update_vnfs.py b/lcm/ns_vnfs/biz/update_vnfs.py new file mode 100644 index 00000000..11b182ba --- /dev/null +++ b/lcm/ns_vnfs/biz/update_vnfs.py @@ -0,0 +1,88 @@ +# Copyright (c) 2018, CMCC Technologies Co., Ltd. + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +import logging +import threading +import traceback + +from lcm.pub.database.models import NfInstModel +from lcm.pub.exceptions import NSLCMException +from lcm.pub.msapi.vnfmdriver import send_nf_heal_request +from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE, JOB_MODEL_STATUS +from lcm.pub.utils.values import ignore_case_get +from lcm.ns_vnfs.const import VNF_STATUS +from lcm.ns_vnfs.biz.wait_job import wait_job_finish + +JOB_ERROR = 255 + +logger = logging.getLogger(__name__) + + +class NFOperateService(threading.Thread): + def __init__(self, vnf_instance_id, data): + super(NFOperateService, self).__init__() + self.vnf_instance_id = vnf_instance_id + self.data = data + self.job_id = JobUtil.create_job("NF", JOB_TYPE.HEAL_VNF, vnf_instance_id) + + self.nf_model = {} + self.nf_additional_params = {} + self.nf_operate_params = {} + self.m_nf_inst_id = '' + self.vnfm_inst_id = '' + + def run(self): + try: + self.do_biz() + except NSLCMException as e: + JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message) + except: + logger.error(traceback.format_exc()) + JobUtil.add_job_status(self.job_id, JOB_ERROR, 'nf update fail') + + def do_biz(self): + self.update_job(1, desc='nf update start') + self.get_and_check_params() + self.update_nf_status(VNF_STATUS.UPDATING) + self.send_nf_operating_request() + self.update_nf_status(VNF_STATUS.ACTIVE) + self.update_job(100, desc='nf update success') + + def get_and_check_params(self): + nf_info = NfInstModel.objects.filter(nfinstid=self.vnf_instance_id) + if not nf_info: + logger.error('NF instance[id=%s] does not exist' % self.vnf_instance_id) + raise NSLCMException('NF instance[id=%s] does not exist' % self.vnf_instance_id) + logger.debug('vnfd_model = %s, vnf_instance_id = %s' % (nf_info[0].vnfd_model, self.vnf_instance_id)) + self.nf_model = nf_info[0].vnfd_model + self.m_nf_inst_id = nf_info[0].mnfinstid + self.vnfm_inst_id = nf_info[0].vnfm_inst_id + self.nf_additional_params = ignore_case_get(self.data, 'additionalParams') + + def send_nf_operating_request(self): + req_param = json.JSONEncoder().encode(self.nf_operate_params) + rsp = send_nf_heal_request(self.vnfm_inst_id, self.m_nf_inst_id, req_param) + vnfm_job_id = ignore_case_get(rsp, 'jobId') + ret = wait_job_finish(self.vnfm_inst_id, self.job_id, vnfm_job_id, progress_range=None, timeout=1200, + mode='1') + if ret != JOB_MODEL_STATUS.FINISHED: + logger.error('[NF update] nf update failed') + raise NSLCMException("nf update failed") + + def update_job(self, progress, desc=''): + JobUtil.add_job_status(self.job_id, progress, desc) + + def update_nf_status(self, status): + NfInstModel.objects.filter(nfinstid=self.vnf_instance_id).update(status=status) diff --git a/lcm/ns_vnfs/const.py b/lcm/ns_vnfs/const.py index bc0047f3..0c831dca 100644 --- a/lcm/ns_vnfs/const.py +++ b/lcm/ns_vnfs/const.py @@ -14,7 +14,7 @@ from lcm.pub.utils.enumutil import enum VNF_STATUS = enum(NULL='null', INSTANTIATING="instantiating", INACTIVE='inactive', ACTIVE="active", FAILED="failed", - TERMINATING="terminating", SCALING="scaling", HEALING="healing") + TERMINATING="terminating", SCALING="scaling", HEALING="healing", UPDATING="updating") INST_TYPE = enum(VNF=0, VNFM=1) INST_TYPE_NAME = enum(VNF='VNF', VNFM='VNFM') PACKAGE_TYPE = enum(VNFD='VNFD', NSD='NSD') diff --git a/lcm/pub/msapi/vnfmdriver.py b/lcm/pub/msapi/vnfmdriver.py index 096462be..7ed1b771 100644 --- a/lcm/pub/msapi/vnfmdriver.py +++ b/lcm/pub/msapi/vnfmdriver.py @@ -78,3 +78,13 @@ def send_nf_heal_request(vnfm_inst_id, vnf_inst_id, req_param): logger.error("Failed to send nf heal req:%s,%s", ret[2], ret[1]) raise NSLCMException('Failed to send nf heal request to VNFM(%s)' % vnfm_inst_id) return json.JSONDecoder().decode(ret[1]) + + +def send_nf_operate_request(vnfm_inst_id, vnf_inst_id, req_param): + vnfm = get_vnfm_by_id(vnfm_inst_id) + uri = "/api/%s/v1/%s/vnfs/%s/operate" % (vnfm["type"], vnfm_inst_id, vnf_inst_id) + ret = req_by_msb(uri, "POST", req_param) + if ret[0] > 0: + logger.error("Failed to send nf operate req:%s,%s", ret[2], ret[1]) + raise NSLCMException('Failed to send nf operate request to VNFM(%s)' % vnfm_inst_id) + return json.JSONDecoder().decode(ret[1]) -- 2.16.6