Update create vl logic
[vfc/nfvo/lcm.git] / lcm / ns / vnfs / views.py
index 2a94c8c..6a35eb6 100644 (file)
@@ -18,6 +18,7 @@ import uuid
 from rest_framework import status
 from rest_framework.response import Response
 from rest_framework.views import APIView
+from drf_yasg.utils import swagger_auto_schema
 
 from lcm.ns.vnfs import create_vnfs
 from lcm.ns.vnfs.create_vnfs import CreateVnfs
@@ -31,13 +32,38 @@ from lcm.pub.exceptions import NSLCMException
 from lcm.pub.msapi.extsys import get_vnfm_by_id, get_vim_by_id
 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
 from lcm.pub.utils.values import ignore_case_get
+from lcm.ns.vnfs.serializers import InstVnfReqSerializer
+from lcm.ns.vnfs.serializers import InstVnfRespSerializer
+from lcm.ns.vnfs.serializers import GetVnfRespSerializer
+from lcm.ns.vnfs.serializers import TerminateVnfReqSerializer
+from lcm.ns.vnfs.serializers import TerminateVnfRespSerializer
+from lcm.ns.vnfs.serializers import GrantVnfReqSerializer
+from lcm.ns.vnfs.serializers import GrantVnfRespSerializer
+from lcm.ns.vnfs.serializers import NotifyLcmReqSerializer
+from lcm.ns.vnfs.serializers import ScaleVnfReqSerializer
+from lcm.ns.vnfs.serializers import ScaleVnfRespSerializer
+from lcm.ns.vnfs.serializers import VerifyVnfReqSerializer
+from lcm.ns.vnfs.serializers import VerifyVnfRespSerializer
+from lcm.ns.vnfs.serializers import VnfmInfoRespSerializer
+from lcm.ns.vnfs.serializers import VimInfoRespSerializer
 
 logger = logging.getLogger(__name__)
 
 
 class NfView(APIView):
+    @swagger_auto_schema(
+        request_body=InstVnfReqSerializer(),
+        responses={
+            status.HTTP_202_ACCEPTED: InstVnfRespSerializer()
+        }
+    )
     def post(self, request):
         logger.debug("VnfCreateView--post::> %s" % request.data)
+
+        # req_serializer = InstVnfReqSerializer(data=request.data)
+        # if not req_serializer.is_valid():
+        # logger.error(req_serializer.errors)
+
         data = {'ns_instance_id': ignore_case_get(request.data, 'nsInstanceId'),
                 'additional_param_for_ns': ignore_case_get(request.data, 'additionalParamForVnf'),
                 'additional_param_for_vnf': ignore_case_get(request.data, 'additionalParamForVnf'),
@@ -47,21 +73,53 @@ class NfView(APIView):
         rsp = {
             "vnfInstId": nf_inst_id,
             "jobId": job_id}
+
+        resp_serializer = InstVnfRespSerializer(data=rsp)
+        if not resp_serializer.is_valid():
+            logger.error(resp_serializer.errors)
+
         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
 
 
 class NfDetailView(APIView):
+    @swagger_auto_schema(
+        request_body=None,
+        responses={
+            status.HTTP_200_OK: GetVnfRespSerializer(),
+            status.HTTP_404_NOT_FOUND: "VNF not found"
+        }
+    )
     def get(self, request, vnfinstid):
         logger.debug("VnfQueryView--get::> %s" % vnfinstid)
         nf_inst_info = GetVnf(vnfinstid).do_biz()
         if not nf_inst_info:
             return Response(status=status.HTTP_404_NOT_FOUND)
-        return Response(status=status.HTTP_200_OK,
-                        data={'vnfInstId': nf_inst_info[0].nfinstid, 'vnfName': nf_inst_info[0].nf_name,
-                              'vnfStatus': nf_inst_info[0].status})
 
+        rsp = {
+            'vnfInstId': nf_inst_info[0].nfinstid,
+            'vnfName': nf_inst_info[0].nf_name,
+            'vnfStatus': nf_inst_info[0].status
+        }
+        resp_serializer = GetVnfRespSerializer(data=rsp)
+        if not resp_serializer.is_valid():
+            logger.error(resp_serializer.errors)
+
+        return Response(status=status.HTTP_200_OK, data=rsp)
+
+    @swagger_auto_schema(
+        request_body=TerminateVnfReqSerializer(),
+        responses={
+            status.HTTP_200_OK: TerminateVnfRespSerializer(),
+            status.HTTP_409_CONFLICT: "Inner error"
+        }
+    )
     def post(self, request_paras, vnfinstid):
         logger.debug("VnfTerminateView--post::> %s, %s", vnfinstid, request_paras.data)
+
+        req_serializer = TerminateVnfReqSerializer(data=request_paras.data)
+        if not req_serializer.is_valid():
+            logger.error(req_serializer.errors)
+
         vnf_inst_id = vnfinstid
         terminationType = ignore_case_get(request_paras.data, 'terminationType')
         gracefulTerminationTimeout = ignore_case_get(request_paras.data, 'gracefulTerminationTimeout')
@@ -71,15 +129,32 @@ class NfDetailView(APIView):
         try:
             TerminateVnfs(data, vnf_inst_id, job_id).start()
         except Exception as e:
+            logger.error(e.message)
             return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
         rsp = {'jobId': job_id}
+
+        resp_serializer = TerminateVnfRespSerializer(data=rsp)
+        if not resp_serializer.is_valid():
+            logger.error(resp_serializer.errors)
+
         return Response(data=rsp, status=status.HTTP_201_CREATED)
 
 
 class NfGrant(APIView):
+    @swagger_auto_schema(
+        request_body=GrantVnfReqSerializer(),
+        responses={
+            status.HTTP_201_CREATED: GrantVnfRespSerializer(),
+            status.HTTP_409_CONFLICT: "Inner error"
+        }
+    )
     def post(self, request):
         logger.debug("NfGrant--post::> %s" % request.data)
         try:
+            req_serializer = GrantVnfReqSerializer(data=request.data)
+            if not req_serializer.is_valid():
+                raise Exception(req_serializer.errors)
+
             vnf_inst_id = ignore_case_get(request.data, 'vnfInstanceId')
             job_id = JobUtil.create_job("VNF", JOB_TYPE.GRANT_VNF, vnf_inst_id)
             rsp = GrantVnfs(request.data, job_id).send_grant_vnf_to_resMgr()
@@ -93,49 +168,110 @@ class NfGrant(APIView):
                 }
             }
             """
+            # resp_serializer = GrantVnfRespSerializer(data=rsp)
+            # if not resp_serializer.is_valid():
+            # raise Exception(resp_serializer.errors)
+
             return Response(data=rsp, status=status.HTTP_201_CREATED)
         except Exception as e:
+            logger.error(e.message)
             logger.error(traceback.format_exc())
             return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
 
 
 class LcmNotify(APIView):
-    def post(self, request_paras, vnfmid, vnfInstanceId):
-        logger.debug("LcmNotify--post::> %s" % request_paras.data)
+    @swagger_auto_schema(
+        request_body=NotifyLcmReqSerializer(),
+        responses={
+            status.HTTP_201_CREATED: 'successful',
+            status.HTTP_409_CONFLICT: "Inner error"
+        }
+    )
+    def post(self, request, vnfmid, vnfInstanceId):
+        logger.debug("LcmNotify--post::> %s" % request.data)
         try:
-            NotifyLcm(vnfmid, vnfInstanceId, request_paras.data).do_biz()
+            # req_serializer = NotifyLcmReqSerializer(data=request.data)
+            # if not req_serializer.is_valid():
+            # raise Exception(req_serializer.errors)
+            NotifyLcm(vnfmid, vnfInstanceId, request.data).do_biz()
             return Response(data={}, status=status.HTTP_201_CREATED)
         except Exception as e:
+            logger.error(e.message)
             return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
 
 
 class NfScaleView(APIView):
-    def post(self, request_paras, vnfinstid):
-        logger.debug("NfScaleView--post::> %s" % request_paras.data)
+    @swagger_auto_schema(
+        request_body=ScaleVnfReqSerializer(),
+        responses={
+            status.HTTP_202_ACCEPTED: ScaleVnfRespSerializer(),
+            status.HTTP_409_CONFLICT: "Inner error"
+        }
+    )
+    def post(self, request, vnfinstid):
+        logger.debug("NfScaleView--post::> %s" % request.data)
         try:
-            NFManualScaleService(vnfinstid, request_paras.data).start()
+            req_serializer = ScaleVnfReqSerializer(data=request.data)
+            if not req_serializer.is_valid():
+                raise Exception(req_serializer.errors)
+            NFManualScaleService(vnfinstid, request.data).start()
             return Response(data={}, status=status.HTTP_202_ACCEPTED)
         except Exception as e:
+            logger.error(e.message)
             return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
 
 
 class NfVerifyView(APIView):
+    @swagger_auto_schema(
+        request_body=VerifyVnfReqSerializer(),
+        responses={
+            status.HTTP_202_ACCEPTED: VerifyVnfRespSerializer(),
+            status.HTTP_409_CONFLICT: "Inner error"
+        }
+    )
     def post(self, request):
         job_id = "VNFSDK_" + str(uuid.uuid4())
         logger.debug("NfVerifyView--post::%s> %s", job_id, request.data)
-        VerifyVnfs(request.data, job_id).start()
-        return Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)
+        try:
+            req_serializer = VerifyVnfReqSerializer(data=request.data)
+            if not req_serializer.is_valid():
+                raise Exception(req_serializer.errors)
+
+            VerifyVnfs(request.data, job_id).start()
+
+            rsp = {"jobId": job_id}
+            resp_serializer = VerifyVnfRespSerializer(data=rsp)
+            if not resp_serializer.is_valid():
+                raise Exception(resp_serializer.errors)
+
+            return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
+        except Exception as e:
+            logger.error(e.message)
+            return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
 
 
 class NfVnfmInfoView(APIView):
+    @swagger_auto_schema(
+        request_body=None,
+        responses={
+            status.HTTP_200_OK: VnfmInfoRespSerializer(),
+            status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
+        }
+    )
     def get(self, request, vnfmid):
         logger.debug("NfVnfmInfoView--get::> %s" % vnfmid)
         try:
             vnfm_info = get_vnfm_by_id(vnfmid)
+
+            resp_serializer = VnfmInfoRespSerializer(data=vnfm_info)
+            if not resp_serializer.is_valid():
+                raise Exception(resp_serializer.errors)
+
         except NSLCMException as e:
             logger.error(e.message)
             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
-        except:
+        except Exception as e:
+            logger.error(e.message)
             logger.error(traceback.format_exc())
             return Response(data={'error': 'Failed to get vnfm info.'},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@@ -143,14 +279,27 @@ class NfVnfmInfoView(APIView):
 
 
 class NfVimInfoView(APIView):
+    @swagger_auto_schema(
+        request_body=None,
+        responses={
+            status.HTTP_200_OK: VimInfoRespSerializer(),
+            status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
+        }
+    )
     def get(self, request, vimid):
         logger.debug("NfVimInfoView--get::> %s" % vimid)
         try:
             vim_info = get_vim_by_id(vimid)
+
+            resp_serializer = VimInfoRespSerializer(data=vim_info)
+            if not resp_serializer.is_valid():
+                raise Exception(resp_serializer.errors)
+
         except NSLCMException as e:
             logger.error(e.message)
             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
-        except:
+        except Exception as e:
+            logger.error(e.message)
             logger.error(traceback.format_exc())
             return Response(data={'error': 'Failed to get vim info.'},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)