From: fujinhua Date: Fri, 19 Apr 2019 03:44:28 +0000 (+0800) Subject: refactor codes for vnflcm X-Git-Tag: 1.3.0~45 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=vfc%2Fgvnfm%2Fvnflcm.git;a=commitdiff_plain;h=1306f9c091fadb8cdd5a6b34aef046524872cfda refactor codes for vnflcm Change-Id: Ic9fbaa0cc72ebb59eefe53aea370f86e81442b2d Issue-ID: VFC-1307 Signed-off-by: fujinhua --- diff --git a/lcm/lcm/nf/views/common.py b/lcm/lcm/nf/views/common.py index 1be6a5de..1f6b6b01 100644 --- a/lcm/lcm/nf/views/common.py +++ b/lcm/lcm/nf/views/common.py @@ -24,6 +24,9 @@ from lcm.pub.exceptions import NFLCMExceptionBadRequest from lcm.pub.exceptions import NFLCMExceptionNotFound from lcm.pub.exceptions import NFLCMExceptionConflict from lcm.pub.exceptions import NFLCMExceptionSeeOther +from lcm.pub.database.models import NfInstModel +from lcm.pub.utils.jobutil import JobUtil +from lcm.nf.const import OPERATION_TYPE logger = logging.getLogger(__name__) @@ -88,3 +91,31 @@ def view_safe_call_with_log(logger): ) return wrapper return view_safe_call + + +def deal_vnf_action(logger, opt_type, opt_status, instid, req, req_serializer, act_task): + logger.debug("%s--post::> %s, %s", opt_type, instid, req.data) + + act_vnf_req_serializer = req_serializer(data=req.data) + if not act_vnf_req_serializer.is_valid(): + raise NFLCMException(act_vnf_req_serializer.errors) + + vnf_insts = NfInstModel.objects.filter(nfinstid=instid) + if not vnf_insts.exists(): + raise NFLCMExceptionNotFound("VNF(%s) does not exist." % instid) + + if opt_type == OPERATION_TYPE.INSTANTIATE: + if vnf_insts[0].status == 'INSTANTIATED': + raise NFLCMExceptionConflict("VNF(%s) is already INSTANTIATED." % instid) + else: + if vnf_insts[0].status != 'INSTANTIATED': + raise NFLCMExceptionConflict("VNF(%s) is not INSTANTIATED." % instid) + + job_id = JobUtil.create_job('NF', opt_type, instid) + JobUtil.add_job_status(job_id, 0, "VNF_%s_READY" % opt_type) + + vnf_insts.update(status=opt_status) + act_task(req.data, instid, job_id).start() + + resp = Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED) + return resp