From: ying.yunlong Date: Wed, 15 Feb 2017 07:18:33 +0000 (+0800) Subject: Get inst resource before grant resource X-Git-Tag: release/mercury~76 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=vfc%2Fgvnfm%2Fvnflcm.git;a=commitdiff_plain;h=9a8f9cd744b44514cf94e15c84f315751f493c3c Get inst resource before grant resource Change-Id: I046b066d92b08deeb5bb6740482bfccacccf7d78 Issue-Id: GVNFM-14 Signed-off-by: ying.yunlong --- diff --git a/lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py b/lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py index c4023c81..e39dfe75 100644 --- a/lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py +++ b/lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py @@ -18,7 +18,8 @@ from django.test import TestCase, Client from rest_framework import status from lcm.nf.vnfs.vnf_cancel.term_vnf import TermVnf -from lcm.pub.database.models import NfInstModel, JobStatusModel +from lcm.pub.database.models import NfInstModel, JobStatusModel, VmInstModel, NetworkInstModel, SubNetworkInstModel, \ + PortInstModel from lcm.pub.utils.jobutil import JobUtil from lcm.pub.utils.timeutil import now_time @@ -26,9 +27,22 @@ from lcm.pub.utils.timeutil import now_time class TestNFTerminate(TestCase): def setUp(self): self.client = Client() + VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1111", vmname="test_01", + is_predefined=1, operationalstate=1) + VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="1111", + is_predefined=1, vmname="test_02", operationalstate=1) + NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network', + is_predefined=1, tenant='admin', insttype=0, instid='1111') + SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1', + is_predefined=1, name='sub_pnet', tenant='admin', insttype=0, instid='1111') + PortInstModel.objects.create(portid='1', networkid='1', subnetworkid='1', vimid='1', resouceid='1', + is_predefined=1, name='aaa_pnet_cp', tenant='admin', insttype=0, instid='1111') def tearDown(self): - pass + VmInstModel.objects.all().delete() + NetworkInstModel.objects.all().delete() + SubNetworkInstModel.objects.all().delete() + PortInstModel.objects.all().delete() def assert_job_result(self, job_id, job_progress, job_detail): jobs = JobStatusModel.objects.filter( diff --git a/lcm/lcm/nf/vnfs/vnf_cancel/term_vnf.py b/lcm/lcm/nf/vnfs/vnf_cancel/term_vnf.py index ccf2c327..4b51ae3e 100644 --- a/lcm/lcm/nf/vnfs/vnf_cancel/term_vnf.py +++ b/lcm/lcm/nf/vnfs/vnf_cancel/term_vnf.py @@ -15,7 +15,9 @@ import logging import traceback from threading import Thread -from lcm.pub.database.models import JobStatusModel, NfInstModel +from lcm.pub.config.enum import VNF_STATUS +from lcm.pub.database.models import JobStatusModel, NfInstModel, VmInstModel, NetworkInstModel, StorageInstModel, \ + FlavourInstModel, PortInstModel, SubNetworkInstModel from lcm.pub.exceptions import NFLCMException from lcm.pub.utils.jobutil import JobUtil from lcm.pub.utils.timeutil import now_time @@ -32,10 +34,19 @@ class TermVnf(Thread): self.job_id = job_id self.terminationType = ignore_case_get(self.data, "terminationType") self.gracefulTerminationTimeout = ignore_case_get(self.data, "gracefulTerminationTimeout") + self.inst_resource = {'volumn': [], # [{"vim_id": ignore_case_get(ret, "vim_id")},{}] + 'network': [], + 'subnet': [], + 'port': [], + 'flavor': [], + 'vm': [], + } def run(self): try: self.term_pre() + self.grant_resource() + self.query_inst_resource(self.nf_inst_id) JobUtil.add_job_status(self.job_id, 100, "Terminate Vnf success.") is_exist = JobStatusModel.objects.filter(jobid=self.job_id).exists() logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist) @@ -58,4 +69,82 @@ class TermVnf(Thread): if self.terminationType == 'GRACEFUL' and not self.gracefulTerminationTimeout: raise NFLCMException("Graceful termination must set timeout") JobUtil.add_job_status(self.job_id, 10, 'Nf terminating pre-check finish') - logger.info("Nf terminating pre-check finish") \ No newline at end of file + logger.info("Nf terminating pre-check finish") + NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status=VNF_STATUS.TERMINATING) + + def query_inst_resource(self, inst_id): + logger.info('[query_resource begin]:inst_id=%s' % inst_id) + # query_volumn_resource + vol_list = StorageInstModel.objects.filter(instid=inst_id, + is_predefined=1) + for vol in vol_list: + vol_info = {} + if not vol.resouceid: + continue + vol_info["res_id"] = vol.resouceid + vol_info["vim_id"] = vol.vimid + self.inst_resource['volumn'].append(vol_info) + logger.info('[query_volumn_resource]:ret_volumns=%s' % self.inst_resource['volumn']) + + # query_network_resource + network_list = NetworkInstModel.objects.filter(instid=inst_id, + is_predefined=1) + for network in network_list: + network_info = {} + if not network.resouceid: + continue + network_info["res_id"] = network.resouceid + network_info["vim_id"] = network.vimid + self.inst_resource['network'].append(network_info) + logger.info('[query_network_resource]:ret_networks=%s' % self.inst_resource['network']) + + # query_subnetwork_resource + subnetwork_list = SubNetworkInstModel.objects.filter(instid=inst_id, + is_predefined=1) + for subnetwork in subnetwork_list: + subnetwork_info = {} + if not subnetwork.resouceid: + continue + subnetwork_info["res_id"] = subnetwork.resouceid + subnetwork_info["vim_id"] = subnetwork.vimid + self.inst_resource['subnet'].append(subnetwork_info) + logger.info('[query_subnetwork_resource]:ret_networks=%s' % self.inst_resource['subnet']) + + # query_port_resource + port_list = PortInstModel.objects.filter(instid=inst_id, + is_predefined=1) + for port in port_list: + port_info = {} + if not port.resouceid: + continue + port_info["res_id"] = port.resouceid + port_info["vim_id"] = port.vimid + self.inst_resource['port'].append(port_info) + logger.info('[query_port_resource]:ret_networks=%s' % self.inst_resource['port']) + + # query_flavor_resource + flavor_list = FlavourInstModel.objects.filter(instid=inst_id, + is_predefined=1) + for flavor in flavor_list: + flavor_info = {} + if not flavor.resouceid: + continue + flavor_info["res_id"] = flavor.resouceid + flavor_info["vim_id"] = flavor.vimid + self.inst_resource['flavor'].append(flavor_info) + logger.info('[query_flavor_resource]:ret_networks=%s' % self.inst_resource['flavor']) + + # query_vm_resource + vm_list = VmInstModel.objects.filter(instid=inst_id, + is_predefined=1) + for vm in vm_list: + vm_info = {} + if not vm.resouceid: + continue + vm_info["res_id"] = vm.resouceid + vm_info["vim_id"] = vm.vimid + self.inst_resource['vm'].append(vm_info) + logger.info('[query_vm_resource]:ret_vms=%s' % self.inst_resource['vm']) + + def grant_resource(self): + pass diff --git a/lcm/lcm/nf/vnfs/vnf_create/inst_vnf.py b/lcm/lcm/nf/vnfs/vnf_create/inst_vnf.py index 17b45baf..8fa01329 100644 --- a/lcm/lcm/nf/vnfs/vnf_create/inst_vnf.py +++ b/lcm/lcm/nf/vnfs/vnf_create/inst_vnf.py @@ -387,6 +387,7 @@ class InstVnf(Thread): name='40G', tenant='admin', insttype=0, + is_predefined=ret["returnCode"], instid=self.nf_inst_id) elif res_type == adaptor.OPT_CREATE_NETWORK: logger.info('Create networks!') @@ -402,6 +403,7 @@ class InstVnf(Thread): name='pnet_network', tenant='admin', insttype=0, + is_predefined=ret["returnCode"], instid=self.nf_inst_id) elif res_type == adaptor.OPT_CREATE_SUBNET: logger.info('Create subnets!') @@ -418,6 +420,7 @@ class InstVnf(Thread): name='sub_pnet', tenant='admin', insttype=0, + is_predefined=ret["returnCode"], instid=self.nf_inst_id) elif res_type == adaptor.OPT_CREATE_PORT: logger.info('Create ports!') @@ -435,6 +438,7 @@ class InstVnf(Thread): name='aaa_pnet_cp', tenant='admin', insttype=0, + is_predefined=ret["returnCode"], instid=self.nf_inst_id) elif res_type == adaptor.OPT_CREATE_FLAVOR: logger.info('Create flavors!') @@ -448,6 +452,7 @@ class InstVnf(Thread): name='1', vcpu='1', extraspecs='1', + is_predefined=ret["returnCode"], instid=self.nf_inst_id) elif res_type == adaptor.OPT_CREATE_VM: logger.info('Create vms!') @@ -463,6 +468,7 @@ class InstVnf(Thread): insttype=0, instid=self.nf_inst_id, vmname="test_01", + is_predefined=ret["returnCode"], operationalstate=1) def do_rollback(self, args_=None): diff --git a/lcm/lcm/pub/config/enum.py b/lcm/lcm/pub/config/enum.py new file mode 100644 index 00000000..e72aab4f --- /dev/null +++ b/lcm/lcm/pub/config/enum.py @@ -0,0 +1,20 @@ +# Copyright 2017 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. +def enum(**enums): + return type('Enum', (), enums) + +# [VNF_STATUS] +VNF_STATUS = enum(NULL='null', INSTANTIATING="instantiating", INACTIVE='inactive', ACTIVE="active", + FAILED="failed", TERMINATING="terminating", SCALING="scaling", OPERATING="operating", + UPDATING="updating", HEALING="healing") \ No newline at end of file diff --git a/lcm/lcm/pub/database/models.py b/lcm/lcm/pub/database/models.py index 03be360e..ff7f290d 100644 --- a/lcm/lcm/pub/database/models.py +++ b/lcm/lcm/pub/database/models.py @@ -171,6 +171,7 @@ class FlavourInstModel(models.Model): memory = models.CharField(db_column='MEMORY', max_length=255) extraspecs = models.CharField(db_column='EXTRASPECS', max_length=255) instid = models.CharField(db_column='INSTID', max_length=255) + is_predefined = models.IntegerField(db_column='ISPREDEFINED', default=0, null=True) class NetworkInstModel(models.Model): class Meta: @@ -256,6 +257,7 @@ class PortInstModel(models.Model): tenant = models.CharField(db_column='TENANT', max_length=255, null=True) interfacename = models.CharField(db_column='INTERFACENAME', max_length=255, blank=True, null=True) vmid = models.CharField(db_column='VMID', max_length=255, blank=True, null=True) + is_predefined = models.IntegerField(db_column='ISPREDEFINED', default=0, null=True) class CPInstModel(models.Model): class Meta: