Refactor codes for op vnf 14/84414/1
authorfujinhua <fu.jinhua@zte.com.cn>
Sat, 6 Apr 2019 00:45:12 +0000 (08:45 +0800)
committerfujinhua <fu.jinhua@zte.com.cn>
Sat, 6 Apr 2019 00:45:12 +0000 (08:45 +0800)
Change-Id: I5a0e3345f5bf1ee026476eb97ca9be2821dfe03a
Issue-ID: VFC-1306
Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
lcm/lcm/nf/views/operate_vnf_view.py

index ab8f905..3221806 100644 (file)
@@ -13,7 +13,6 @@
 # limitations under the License.\r
 \r
 import logging\r
-import traceback\r
 \r
 from drf_yasg.utils import swagger_auto_schema\r
 from rest_framework import status\r
@@ -27,6 +26,7 @@ from lcm.pub.exceptions import NFLCMException, NFLCMExceptionNotFound, NFLCMExce
 from lcm.pub.utils.jobutil import JobUtil\r
 from lcm.pub.database.models import NfInstModel\r
 from lcm.nf.const import VNF_STATUS\r
+from .common import view_safe_call_with_log\r
 \r
 logger = logging.getLogger(__name__)\r
 \r
@@ -41,40 +41,22 @@ class OperateVnfView(APIView):
             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"\r
         }\r
     )\r
+    @view_safe_call_with_log(logger=logger)\r
     def post(self, request, instanceid):\r
         logger.debug("OperateVnf--post::> %s" % request.data)\r
-        try:\r
-            operate_vnf_request_serializer = OperateVnfRequestSerializer(data=request.data)\r
-            if not operate_vnf_request_serializer.is_valid():\r
-                raise NFLCMException(operate_vnf_request_serializer.errors)\r
 \r
-            job_id = JobUtil.create_job('NF', 'OPERATE', instanceid)\r
-            JobUtil.add_job_status(job_id, 0, "OPERATE_VNF_READY")\r
-            self.operate_pre_check(instanceid, job_id)\r
-            OperateVnf(operate_vnf_request_serializer.data, instanceid, job_id).start()\r
-            response = Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)\r
-            # Location todo, it use job as the status storage\r
-            # response["Location"] = "/api/vnflcm/v1/vnf_lcm_op_occs/%s" % lcmopoccid\r
-            return response\r
-        except NFLCMExceptionNotFound as e:\r
-            probDetail = ProblemDetailsSerializer(data={"status": 404, "detail": "VNF Instance not found"})\r
-            resp_isvalid = probDetail.is_valid()\r
-            if not resp_isvalid:\r
-                raise NFLCMException(probDetail.errors)\r
-            return Response(data=probDetail.data, status=status.HTTP_404_NOT_FOUND)\r
-        except NFLCMExceptionConflict as e:\r
-            probDetail = ProblemDetailsSerializer(data={"status": 409, "detail": "VNF Instance not in Instantiated State"})\r
-            resp_isvalid = probDetail.is_valid()\r
-            if not resp_isvalid:\r
-                raise NFLCMException(probDetail.errors)\r
-            return Response(data=probDetail.data, status=status.HTTP_409_CONFLICT)\r
-        except NFLCMException as e:\r
-            logger.error(e.message)\r
-            return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)\r
-        except Exception as e:\r
-            logger.error(e.message)\r
-            logger.error(traceback.format_exc())\r
-            return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)\r
+        operate_vnf_request_serializer = OperateVnfRequestSerializer(data=request.data)\r
+        if not operate_vnf_request_serializer.is_valid():\r
+            raise NFLCMException(operate_vnf_request_serializer.errors)\r
+\r
+        job_id = JobUtil.create_job('NF', 'OPERATE', instanceid)\r
+        JobUtil.add_job_status(job_id, 0, "OPERATE_VNF_READY")\r
+        self.operate_pre_check(instanceid, job_id)\r
+        OperateVnf(operate_vnf_request_serializer.data, instanceid, job_id).start()\r
+        response = Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)\r
+        # Location todo, it use job as the status storage\r
+        # response["Location"] = "/api/vnflcm/v1/vnf_lcm_op_occs/%s" % lcmopoccid\r
+        return response\r
 \r
     def operate_pre_check(self, nfInstId, jobId):\r
         vnf_insts = NfInstModel.objects.filter(nfinstid=nfInstId)\r