Refactor codes for heal vnf 15/84315/2
authorfujinhua <fu.jinhua@zte.com.cn>
Fri, 5 Apr 2019 09:17:06 +0000 (17:17 +0800)
committerfujinhua <fu.jinhua@zte.com.cn>
Fri, 5 Apr 2019 09:17:58 +0000 (17:17 +0800)
Change-Id: Ic42a459ef37e34fd742f8439dee91e6ed295072b
Issue-ID: VFC-1306
Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
lcm/lcm/nf/views/heal_vnf_view.py

index b30194d..4fe3f84 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
@@ -23,10 +22,11 @@ from rest_framework.views import APIView
 from lcm.nf.biz.heal_vnf import HealVnf\r
 from lcm.nf.serializers.heal_vnf_req import HealVnfRequestSerializer\r
 from lcm.nf.serializers.response import ProblemDetailsSerializer\r
-from lcm.pub.exceptions import NFLCMException, NFLCMExceptionNotFound, NFLCMExceptionConflict\r
+from lcm.pub.exceptions import NFLCMException, NFLCMExceptionNotFound\r
 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 HealVnfView(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("HealVnf--post::> %s" % request.data)\r
-        try:\r
-            heal_vnf_request_serializer = HealVnfRequestSerializer(data=request.data)\r
-            if not heal_vnf_request_serializer.is_valid():\r
-                raise NFLCMException(heal_vnf_request_serializer.errors)\r
 \r
-            job_id = JobUtil.create_job('NF', 'HEAL', instanceid)\r
-            JobUtil.add_job_status(job_id, 0, "HEAL_VNF_READY")\r
-            self.heal_pre_check(instanceid, job_id)\r
-            HealVnf(heal_vnf_request_serializer.data, instanceid, job_id).start()\r
-            response = Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)\r
-            # todo, heal_vnf codes uses job as the status storage, not in VNFLcmOpOccModel.\r
-            # response["Location"] = "/api/vnflcm/v1/vnf_lc_ops/%s" % lcmopoccid\r
-            return response\r
-        except NFLCMExceptionNotFound as e:\r
-            probDetail = ProblemDetailsSerializer(data={"status": status.HTTP_404_NOT_FOUND, "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": status.HTTP_409_CONFLICT, "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
+        heal_vnf_request_serializer = HealVnfRequestSerializer(data=request.data)\r
+        if not heal_vnf_request_serializer.is_valid():\r
+            raise NFLCMException(heal_vnf_request_serializer.errors)\r
+\r
+        job_id = JobUtil.create_job('NF', 'HEAL', instanceid)\r
+        JobUtil.add_job_status(job_id, 0, "HEAL_VNF_READY")\r
+        self.heal_pre_check(instanceid, job_id)\r
+        HealVnf(heal_vnf_request_serializer.data, instanceid, job_id).start()\r
+        response = Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)\r
+        # todo, heal_vnf codes uses job as the status storage, not in VNFLcmOpOccModel.\r
+        # response["Location"] = "/api/vnflcm/v1/vnf_lc_ops/%s" % lcmopoccid\r
+        return response\r
 \r
     def heal_pre_check(self, nf_inst_id, job_id):\r
         vnf_insts = NfInstModel.objects.filter(nfinstid=nf_inst_id)\r