Add code framework for upd vnf 46/84046/1
authorfujinhua <fu.jinhua@zte.com.cn>
Wed, 3 Apr 2019 06:26:01 +0000 (14:26 +0800)
committerfujinhua <fu.jinhua@zte.com.cn>
Wed, 3 Apr 2019 06:26:01 +0000 (14:26 +0800)
Change-Id: I0526fcf19a2c350e272c78f498fb1be7bdb74176
Issue-ID: VFC-1306
Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
lcm/lcm/nf/biz/update_vnf.py [new file with mode: 0644]
lcm/lcm/nf/views/curd_vnf_views.py

diff --git a/lcm/lcm/nf/biz/update_vnf.py b/lcm/lcm/nf/biz/update_vnf.py
new file mode 100644 (file)
index 0000000..4a21140
--- /dev/null
@@ -0,0 +1,27 @@
+# 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
+from threading import Thread
+
+logger = logging.getLogger(__name__)
+
+
+class UpdateVnf(Thread):
+    def __init__(self, data, instanceid, job_id):
+        self.data = data
+        self.nf_inst_id = instanceid
+        self.job_id = job_id
+
+    def run(self):
+        logger.debug("start update for vnf %s", self.nf_inst_id)
index 6db70b7..ea5d428 100644 (file)
@@ -23,11 +23,16 @@ from rest_framework.views import APIView
 
 from lcm.nf.biz.create_vnf import CreateVnf
 from lcm.nf.biz.query_vnf import QueryVnf
+from lcm.nf.biz.update_vnf import UpdateVnf
 from lcm.nf.serializers.create_vnf_req import CreateVnfReqSerializer
 from lcm.nf.serializers.vnf_instance import VnfInstanceSerializer
 from lcm.nf.serializers.vnf_instances import VnfInstancesSerializer
+from lcm.nf.serializers.vnf_info_modifications import VnfInfoModificationsSerializer
+from lcm.pub.utils.jobutil import JobUtil
 from lcm.pub.exceptions import NFLCMException
 from lcm.pub.exceptions import NFLCMExceptionNotFound
+from lcm.pub.database.models import NfInstModel
+from lcm.nf.const import VNF_STATUS
 
 logger = logging.getLogger(__name__)
 
@@ -139,3 +144,39 @@ class DeleteVnfAndQueryVnf(APIView):
             logger.error(traceback.format_exc())
             logger.debug('Delete VNF instance[%s] failed' % instanceid)
             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+    @swagger_auto_schema(
+        request_body=VnfInfoModificationsSerializer(),
+        responses={
+            status.HTTP_202_ACCEPTED: "Successfully",
+            status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
+        }
+    )
+    def patch(self, request, instanceid):
+        logger.debug("UpdateSingleVnf--patch::> %s, %s", instanceid, request.data)
+        try:
+            upd_vnf_serializer = VnfInfoModificationsSerializer(data=request.data)
+            if not upd_vnf_serializer.is_valid():
+                raise NFLCMException(upd_vnf_serializer.errors)
+
+            job_id = JobUtil.create_job('NF', 'UPDATE', instanceid)
+            JobUtil.add_job_status(job_id, 0, "UPDATE_VNF_READY")
+
+            vnf_insts = NfInstModel.objects.filter(nfinstid=instanceid)
+            if not vnf_insts.exists():
+                raise NFLCMExceptionNotFound("VNF(%s) does not exist." % instanceid)
+            vnf_insts.update(status=VNF_STATUS.UPDATING)
+
+            JobUtil.add_job_status(job_id, 15, 'Nf updating pre-check finish')
+            UpdateVnf(request.data, instanceid, job_id).start()
+
+            return Response(data=None, status=status.HTTP_202_ACCEPTED)
+        except NFLCMException as e:
+            logger.error(e.message)
+            logger.error('Update VNF instance[%s] failed' % instanceid)
+            return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+        except Exception as e:
+            logger.error(e.message)
+            logger.error(traceback.format_exc())
+            logger.error('Update VNF instance[%s] failed' % instanceid)
+            return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)