From 4bd2ff38b30e2113bd9e59ea474e33139b4d1ed5 Mon Sep 17 00:00:00 2001 From: fujinhua Date: Mon, 8 Apr 2019 16:00:11 +0800 Subject: [PATCH] Add change vnf flavour biz Change-Id: If962e57f90e20d34ef5aacd9952bedec3c89de28 Issue-ID: VFC-1306 Signed-off-by: fujinhua --- lcm/lcm/nf/biz/change_vnf_flavour.py | 94 +++++++++++++++++++++++++++++ lcm/lcm/nf/const.py | 41 +++++++++++-- lcm/lcm/nf/views/change_vnf_flavour_view.py | 3 +- 3 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 lcm/lcm/nf/biz/change_vnf_flavour.py diff --git a/lcm/lcm/nf/biz/change_vnf_flavour.py b/lcm/lcm/nf/biz/change_vnf_flavour.py new file mode 100644 index 00000000..f4871688 --- /dev/null +++ b/lcm/lcm/nf/biz/change_vnf_flavour.py @@ -0,0 +1,94 @@ +# Copyright 2019 ZTE Corporation. +# +# 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 logging +import traceback +import json +from threading import Thread + +from lcm.nf.biz.grant_vnf import grant_resource +from lcm.nf.const import GRANT_TYPE, CHANGE_TYPE +from lcm.nf.const import VNF_STATUS +from lcm.pub.utils.notificationsutil import NotificationsUtil, prepare_notification_data +from lcm.pub.utils.values import ignore_case_get +from lcm.pub.utils.timeutil import now_time +from lcm.pub.utils.jobutil import JobUtil +from lcm.pub.exceptions import NFLCMException +from lcm.pub.database.models import NfInstModel + +logger = logging.getLogger(__name__) + + +class ChangeVnfFlavour(Thread): + def __init__(self, data, nf_inst_id, job_id): + super(ChangeVnfFlavour, self).__init__() + self.data = data + self.nf_inst_id = nf_inst_id + self.job_id = job_id + self.vnf_insts = NfInstModel.objects.filter(nfinstid=self.nf_inst_id) + + def run(self): + try: + JobUtil.add_job_status(self.job_id, + 10, + "Start to apply grant.") + self.apply_grant() + JobUtil.add_job_status(self.job_id, + 50, + "Start to change vnf flavour.") + self.do_operation() + self.vnf_insts.update( + status='INSTANTIATED', + lastuptime=now_time() + ) + self.send_notification() + JobUtil.add_job_status(self.job_id, + 100, + "Change vnf flavour success.") + except NFLCMException as e: + logger.error(e.message) + self.change_vnf_flavour_failed_handle(e.message) + except Exception as e: + logger.error(e.message) + logger.error(traceback.format_exc()) + self.change_vnf_flavour_failed_handle(e.message) + + def apply_grant(self): + logger.debug("Start change flavour apply grant") + vdus = ignore_case_get(self.vnfd_info, "vdus") + grant_result = grant_resource(data=self.data, + nf_inst_id=self.nf_inst_id, + job_id=self.job_id, + grant_type=GRANT_TYPE.CHANGE_FLAVOUR, + vdus=vdus) + logger.debug("Change flavour Grant result: %s", grant_result) + + def do_operation(self): + logger.info('Operation resource begin') + self.vnfd_info = json.loads(self.vnf_insts[0].vnfd_model) + # TODO: Add operation logic + + def send_notification(self): + data = prepare_notification_data(nfinstid=self.nf_inst_id, + jobid=self.job_id, + changetype=CHANGE_TYPE.MODIFIED, + operation=self.op_type) + logger.debug('Notify request data = %s' % data) + NotificationsUtil().send_notification(data) + + def change_vnf_flavour_failed_handle(self, error_msg): + logger.error('Chnage vnf flavour failed, detail message: %s', error_msg) + self.vnf_insts.update(status=VNF_STATUS.FAILED, + lastuptime=now_time()) + JobUtil.add_job_status(self.job_id, 255, error_msg) diff --git a/lcm/lcm/nf/const.py b/lcm/lcm/nf/const.py index f76a390f..e61c5a27 100644 --- a/lcm/lcm/nf/const.py +++ b/lcm/lcm/nf/const.py @@ -16,12 +16,41 @@ import json from lcm.pub.config import config from lcm.pub.utils.jobutil import enum -HEAL_ACTION_TYPE = enum(START="vmCreate", RESTART="vmReset") -ACTION_TYPE = enum(START=1, STOP=2, REBOOT=3) -GRANT_TYPE = enum(INSTANTIATE="INSTANTIATE", TERMINATE="TERMINATE", HEAL_CREATE="Heal Create", HEAL_RESTART="Heal Restart", OPERATE="OPERATE") -VNF_STATUS = enum(NULL='null', INSTANTIATING="instantiating", INACTIVE='inactive', ACTIVE="active", - FAILED="failed", TERMINATING="terminating", SCALING="scaling", OPERATING="operating", - UPDATING="updating", HEALING="healing") +HEAL_ACTION_TYPE = enum( + START="vmCreate", + RESTART="vmReset" +) + +ACTION_TYPE = enum( + START=1, + STOP=2, + REBOOT=3 +) + +GRANT_TYPE = enum( + INSTANTIATE="INSTANTIATE", + TERMINATE="TERMINATE", + HEAL_CREATE="Heal Create", + HEAL_RESTART="Heal Restart", + SCALE_IN="SCALE_IN", + SCALE_OUT="SCALE_OUT", + CHANGE_FLAVOUR="CHANGE_FLAVOUR", + OPERATE="OPERATE", + CHANGE_CONNECTIVITY="CHANGE_CONNECTIVITY", +) + +VNF_STATUS = enum( + NULL='null', + INSTANTIATING="instantiating", + INACTIVE='inactive', + ACTIVE="active", + FAILED="failed", + TERMINATING="terminating", + SCALING="scaling", + OPERATING="operating", + UPDATING="updating", + HEALING="healing" +) OPERATION_TYPE = enum( INSTANTIATE="INSTANTIATE", diff --git a/lcm/lcm/nf/views/change_vnf_flavour_view.py b/lcm/lcm/nf/views/change_vnf_flavour_view.py index 732a42ac..f426fab3 100644 --- a/lcm/lcm/nf/views/change_vnf_flavour_view.py +++ b/lcm/lcm/nf/views/change_vnf_flavour_view.py @@ -27,6 +27,7 @@ from lcm.pub.exceptions import NFLCMExceptionConflict from lcm.pub.utils.jobutil import JobUtil from lcm.pub.database.models import NfInstModel from lcm.nf.const import VNF_STATUS +from lcm.nf.biz.change_vnf_flavour import ChangeVnfFlavour from .common import view_safe_call_with_log logger = logging.getLogger(__name__) @@ -54,7 +55,7 @@ class ChangeVnfFlavourView(APIView): JobUtil.add_job_status(job_id, 0, "CHG_VNF_FLAVOUR_READY") self.chg_flavour_pre_check(instanceid, job_id) - # TODO: call biz logic + ChangeVnfFlavour(chg_flavour_serializer.data, instanceid, job_id).start() response = Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED) -- 2.16.6