Refactor codes for lcm op occs get
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / views / lcm_op_occs_view.py
1 # Copyright (C) 2018 Verizon. All Rights Reserved
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
15 import logging
16
17 from drf_yasg.utils import swagger_auto_schema
18 from rest_framework import status
19 from rest_framework.response import Response
20 from rest_framework.views import APIView
21
22 from lcm.nf.biz.query_vnf_lcm_op_occ import QueryVnfLcmOpOcc
23 from lcm.nf.serializers.response import ProblemDetailsSerializer
24 from lcm.nf.serializers.vnf_lcm_op_occ import VNFLCMOpOccSerializer
25 from lcm.nf.serializers.vnf_lcm_op_occs import VNFLCMOpOccsSerializer
26 from lcm.pub.exceptions import NFLCMException
27 from .common import view_safe_call_with_log
28 from .common import deal_indivdual_query
29
30 logger = logging.getLogger(__name__)
31 EXCLUDE_DEFAULT = ['operationParams', 'error', 'resourceChanges', 'changedInfo', 'changedExtConnectivity']
32 VALID_FILTERS = ["all_fields", "fields", "exclude_fields", "exclude_default",
33                  "id", "operationState", "stateEnteredTime", "startTime",
34                  "vnfInstanceId", "grantId", "operation"]
35
36
37 def get_problem_details_serializer(status_code, error_message):
38     problem_details = {
39         "status": status_code,
40         "detail": error_message
41     }
42     problem_details_serializer = ProblemDetailsSerializer(data=problem_details)
43     problem_details_serializer.is_valid()
44     return problem_details_serializer
45
46
47 class QueryMultiVnfLcmOpOccs(APIView):
48     @swagger_auto_schema(
49         responses={
50             status.HTTP_200_OK: VNFLCMOpOccsSerializer(),
51             status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
52             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
53         }
54     )
55     @view_safe_call_with_log(logger=logger)
56     def get(self, request):
57         logger.debug("QueryMultiVnfLcmOpOccs--get::> %s" % request.query_params)
58         if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
59             problem_details_serializer = get_problem_details_serializer(status.HTTP_400_BAD_REQUEST, "Not a valid filter")
60             return Response(data=problem_details_serializer.data, status=status.HTTP_400_BAD_REQUEST)
61         resp_data = QueryVnfLcmOpOcc(request.query_params).query_multi_vnf_lcm_op_occ()
62
63         vnf_lcm_op_occs_serializer = VNFLCMOpOccsSerializer(data=resp_data)
64         if not vnf_lcm_op_occs_serializer.is_valid():
65             raise NFLCMException(vnf_lcm_op_occs_serializer.errors)
66
67         logger.debug("QueryMultiVnfLcmOpOccs--get::> Remove default fields if exclude_default" +
68                      " is specified")
69         # TODO(bharath): Add support for "fields", "exclude_fields" in query parameters
70         if 'exclude_default' in request.query_params.keys():
71             for field in EXCLUDE_DEFAULT:
72                 for lcm_op in vnf_lcm_op_occs_serializer.data:
73                     del lcm_op[field]
74         return Response(data=vnf_lcm_op_occs_serializer.data, status=status.HTTP_200_OK)
75
76
77 class QuerySingleVnfLcmOpOcc(APIView):
78     @swagger_auto_schema(
79         responses={
80             status.HTTP_200_OK: VNFLCMOpOccSerializer(),
81             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
82         }
83     )
84     @view_safe_call_with_log(logger=logger)
85     def get(self, request, lcmopoccid):
86         logger.debug("QuerySingleVnfLcmOpOcc--get::> %s" % request.query_params)
87
88         return deal_indivdual_query(res_serializer=VNFLCMOpOccSerializer,
89                                     query_fun=QueryVnfLcmOpOcc(
90                                         data=request.data,
91                                         lcm_op_occ_id=lcmopoccid).query_single_vnf_lcm_op_occ)