Add GET /vnf_lcm_op_occs API in GVNFM
[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 import traceback
17
18 from drf_yasg.utils import swagger_auto_schema
19 from rest_framework import status
20 from rest_framework.response import Response
21 from rest_framework.views import APIView
22
23 from lcm.nf.biz.query_vnf_lcm_op_occ import QueryVnfLcmOpOcc
24 from lcm.nf.serializers.response import ProblemDetailsSerializer
25 from lcm.nf.serializers.vnf_lcm_op_occs import VNFLCMOpOccsSerializer
26 from lcm.pub.exceptions import NFLCMException
27
28 logger = logging.getLogger(__name__)
29 EXCLUDE_DEFAULT = ['operationParams', 'error', 'resourceChanges', 'changedInfo', 'changedExtConnectivity']
30 VALID_FILTERS = ["all_fields", "fields", "exclude_fields", "exclude_default",
31                  "id", "operationState", "stateEnteredTime", "startTime",
32                  "vnfInstanceId", "grantId", "operation"]
33
34
35 def get_problem_details_serializer(status_code, error_message):
36     problem_details = {
37         "status": status_code,
38         "detail": error_message
39     }
40     problem_details_serializer = ProblemDetailsSerializer(data=problem_details)
41     problem_details_serializer.is_valid()
42     return problem_details_serializer
43
44
45 class QueryMultiVnfLcmOpOccs(APIView):
46     @swagger_auto_schema(
47         responses={
48             status.HTTP_200_OK: VNFLCMOpOccsSerializer(),
49             status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
50             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
51         }
52     )
53     def get(self, request):
54         logger.debug("QueryMultiVnfLcmOpOccs--get::> %s" % request.query_params)
55         try:
56             if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
57                 problem_details_serializer = get_problem_details_serializer(status.HTTP_400_BAD_REQUEST, "Not a valid filter")
58                 return Response(data=problem_details_serializer.data, status=status.HTTP_400_BAD_REQUEST)
59             resp_data = QueryVnfLcmOpOcc(request.query_params).query_multi_vnf_lcm_op_occ()
60
61             vnf_lcm_op_occs_serializer = VNFLCMOpOccsSerializer(data=resp_data)
62             if not vnf_lcm_op_occs_serializer.is_valid():
63                 raise NFLCMException(vnf_lcm_op_occs_serializer.errors)
64
65             logger.debug("QueryMultiVnfLcmOpOccs--get::> Remove default fields if exclude_default" +
66                          " is specified")
67             # TODO(bharath): Add support for "fields", "exclude_fields" in query parameters
68             if 'exclude_default' in request.query_params.keys():
69                 for field in EXCLUDE_DEFAULT:
70                     for lcm_op in vnf_lcm_op_occs_serializer.data:
71                         del lcm_op[field]
72             return Response(data=vnf_lcm_op_occs_serializer.data, status=status.HTTP_200_OK)
73         except NFLCMException as e:
74             logger.error(e.message)
75             problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR, e.message)
76             return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
77
78         except Exception as e:
79             logger.error(e.message)
80             logger.error(traceback.format_exc())
81             problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR, e.message)
82             return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)