Add serializer check for job query interface
[vfc/nfvo/lcm.git] / lcm / jobs / views.py
1 # Copyright 2016 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 import logging
15 import traceback
16
17 from drf_yasg import openapi
18 from rest_framework.response import Response
19 from rest_framework.views import APIView
20 from rest_framework import status
21 from drf_yasg.utils import swagger_auto_schema
22
23 from lcm.jobs.job_get import GetJobInfoService
24 from lcm.pub.utils.jobutil import JobUtil
25 from lcm.jobs.serializers import JobUpdReqSerializer, JobUpdRespSerializer
26 from lcm.jobs.serializers import JobQueryRespSerializer
27 from lcm.pub.exceptions import NSLCMException
28
29 logger = logging.getLogger(__name__)
30
31
32 class JobView(APIView):
33     @swagger_auto_schema(
34         manual_parameters=[
35             openapi.Parameter('responseId',
36                               openapi.IN_QUERY,
37                               "responseId",
38                               type=openapi.TYPE_INTEGER
39                               ),
40         ],
41         responses={
42             status.HTTP_200_OK: JobQueryRespSerializer(),
43             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
44         }
45     )
46     def get(self, request, job_id):
47         try:
48             response_id = int(request.GET.get('responseId', 0))
49             ret = GetJobInfoService(job_id, response_id).do_biz()
50             resp_serializer = JobQueryRespSerializer(data=ret)
51             if not resp_serializer.is_valid():
52                 raise NSLCMException(resp_serializer.errors)
53             return Response(data=ret, status=status.HTTP_200_OK)
54         except Exception as e:
55             logger.error(traceback.format_exc())
56             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
57
58     @swagger_auto_schema(
59         request_body=JobUpdReqSerializer(),
60         responses={
61             status.HTTP_202_ACCEPTED: JobUpdRespSerializer()
62         }
63     )
64     def post(self, request, job_id):
65         try:
66             logger.debug("Enter JobView:post, %s, %s ", job_id, request.data)
67
68             req_serializer = JobUpdReqSerializer(data=request.data)
69             if not req_serializer.is_valid():
70                 raise NSLCMException(req_serializer.errors)
71
72             jobs = JobUtil.query_job_status(job_id)
73             if not jobs:
74                 raise NSLCMException("Job(%s) does not exist.")
75
76             if jobs[-1].errcode != '255':
77                 progress = request.data.get('progress')
78                 desc = request.data.get('desc', '%s' % progress)
79                 errcode = '0' if request.data.get('errcode') in ('true', 'active') else '255'
80                 logger.debug("errcode=%s", errcode)
81                 JobUtil.add_job_status(job_id, progress, desc, error_code=errcode)
82
83             resp_serializer = JobUpdRespSerializer(data={'result': 'ok'})
84             if not resp_serializer.is_valid():
85                 raise NSLCMException(req_serializer.errors)
86
87             return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED)
88         except Exception as e:
89             resp_serializer = JobUpdRespSerializer(data={
90                 'result': 'error',
91                 'msg': e.message})
92             if not resp_serializer.is_valid():
93                 logger.error(resp_serializer.errors)
94                 return Response(data={
95                     'result': 'error',
96                     'msg': resp_serializer.errors}, status=status.HTTP_202_ACCEPTED)
97             return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED)