summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py29
-rw-r--r--lcm/lcm/nf/vnfs/views.py9
-rw-r--r--lcm/lcm/nf/vnfs/vnf_cancel/term_vnf.py61
3 files changed, 96 insertions, 3 deletions
diff --git a/lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py b/lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py
index cc23d301..252d8790 100644
--- a/lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py
+++ b/lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py
@@ -11,10 +11,15 @@
# 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 uuid
+
+import mock
from django.test import TestCase, Client
from rest_framework import status
-from lcm.pub.database.models import NfInstModel
+from lcm.nf.vnfs.vnf_cancel.term_vnf import TermVnf
+from lcm.pub.database.models import NfInstModel, JobStatusModel
+from lcm.pub.utils.jobutil import JobUtil
from lcm.pub.utils.timeutil import now_time
@@ -25,6 +30,13 @@ class TestNFTerminate(TestCase):
def tearDown(self):
pass
+ def assert_job_result(self, job_id, job_progress, job_detail):
+ jobs = JobStatusModel.objects.filter(
+ jobid=job_id,
+ progress=job_progress,
+ descp=job_detail)
+ self.assertEqual(1, len(jobs))
+
def test_delete_vnf_identifier(self):
NfInstModel.objects.create(nfinstid='1111', mnfinstid='1111', nf_name='2222',
package_id='todo', vnfm_inst_id='todo', version='', vendor='',
@@ -51,3 +63,18 @@ class TestNFTerminate(TestCase):
response = self.client.delete("/openoapi/vnflcm/v1/vnf_instances/1111")
self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
self.assertEqual("No instantiated vnf", response.data["error"])
+
+ @mock.patch.object(TermVnf, 'run')
+ def test_terminate_vnf(self, mock_run):
+ mock_run.re.return_value = None
+ response = self.client.post("/openoapi/vnflcm/v1/vnf_instances/12/terminate", data={}, format='json')
+ self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
+
+ def test_terminate_vnf_when_inst_id_not_exist(self):
+ data = {"terminationType": "GRACEFUL",
+ "gracefulTerminationTimeout": 120}
+ self.nf_inst_id = str(uuid.uuid4())
+ self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
+ JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
+ TermVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
+ self.assert_job_result(self.job_id, 255, "VnfInst(%s) does not exist" % self.nf_inst_id)
diff --git a/lcm/lcm/nf/vnfs/views.py b/lcm/lcm/nf/vnfs/views.py
index f17ced43..6d5213b8 100644
--- a/lcm/lcm/nf/vnfs/views.py
+++ b/lcm/lcm/nf/vnfs/views.py
@@ -21,6 +21,7 @@ from rest_framework.response import Response
from rest_framework.views import APIView
from lcm.nf.vnfs.vnf_cancel.delete_vnf_identifier import DeleteVnf
+from lcm.nf.vnfs.vnf_cancel.term_vnf import TermVnf
from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf
from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
from lcm.pub.exceptions import NFLCMException
@@ -69,9 +70,13 @@ class DeleteVnfIdentifier(APIView):
class TerminateVnf(APIView):
- def post(self, request):
+ def post(self, request, instanceid):
logger.debug("TerminateVnf--post::> %s" % request.data)
- return Response(data='', status=status.HTTP_202_ACCEPTED)
+ job_id = JobUtil.create_job('NF', 'TERMINATE', instanceid)
+ JobUtil.add_job_status(job_id, 0, "TERM_VNF_READY")
+ TermVnf(request.data, instanceid, job_id).start()
+ rsp = {"jobId": job_id}
+ return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
class QueryMultipleVnf(APIView):
diff --git a/lcm/lcm/nf/vnfs/vnf_cancel/term_vnf.py b/lcm/lcm/nf/vnfs/vnf_cancel/term_vnf.py
new file mode 100644
index 00000000..ccf2c327
--- /dev/null
+++ b/lcm/lcm/nf/vnfs/vnf_cancel/term_vnf.py
@@ -0,0 +1,61 @@
+# 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.
+import logging
+import traceback
+from threading import Thread
+
+from lcm.pub.database.models import JobStatusModel, NfInstModel
+from lcm.pub.exceptions import NFLCMException
+from lcm.pub.utils.jobutil import JobUtil
+from lcm.pub.utils.timeutil import now_time
+from lcm.pub.utils.values import ignore_case_get
+
+logger = logging.getLogger(__name__)
+
+
+class TermVnf(Thread):
+ def __init__(self, data, nf_inst_id, job_id):
+ super(TermVnf, self).__init__()
+ self.data = data
+ self.nf_inst_id = nf_inst_id
+ self.job_id = job_id
+ self.terminationType = ignore_case_get(self.data, "terminationType")
+ self.gracefulTerminationTimeout = ignore_case_get(self.data, "gracefulTerminationTimeout")
+
+ def run(self):
+ try:
+ self.term_pre()
+ 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)
+ except NFLCMException as e:
+ logger.error('VNF instantiation failed, detail message: %s' % e.message)
+ # NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status='failed', lastuptime=now_time())
+ JobUtil.add_job_status(self.job_id, 255, e.message)
+ # self.vnf_term_failed_handle(e.message)
+ except:
+ # self.vnf_term_failed_handle('unexpected exception')
+ logger.error(traceback.format_exc())
+
+ def term_pre(self):
+ vnf_insts = NfInstModel.objects.filter(pk=self.nf_inst_id)
+ if not vnf_insts.exists():
+ raise NFLCMException('VnfInst(%s) does not exist' % self.nf_inst_id)
+ sel_vnf = vnf_insts[0]
+ if sel_vnf.instantiationState != 'VNF_INSTANTIATED':
+ raise NFLCMException("No instantiated vnf")
+ 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