Add Individual LCM Op Occs API to 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_occ import VNFLCMOpOccSerializer
26 from lcm.nf.serializers.vnf_lcm_op_occs import VNFLCMOpOccsSerializer
27 from lcm.pub.exceptions import NFLCMException
28
29 logger = logging.getLogger(__name__)
30 EXCLUDE_DEFAULT = ['operationParams', 'error', 'resourceChanges', 'changedInfo', 'changedExtConnectivity']
31 VALID_FILTERS = ["all_fields", "fields", "exclude_fields", "exclude_default",
32                  "id", "operationState", "stateEnteredTime", "startTime",
33                  "vnfInstanceId", "grantId", "operation"]
34
35
36 def get_problem_details_serializer(status_code, error_message):
37     problem_details = {
38         "status": status_code,
39         "detail": error_message
40     }
41     problem_details_serializer = ProblemDetailsSerializer(data=problem_details)
42     problem_details_serializer.is_valid()
43     return problem_details_serializer
44
45
46 class QueryMultiVnfLcmOpOccs(APIView):
47     @swagger_auto_schema(
48         responses={
49             status.HTTP_200_OK: VNFLCMOpOccsSerializer(),
50             status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
51             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
52         }
53     )
54     def get(self, request):
55         logger.debug("QueryMultiVnfLcmOpOccs--get::> %s" % request.query_params)
56         try:
57             if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
58                 problem_details_serializer = get_problem_details_serializer(status.HTTP_400_BAD_REQUEST, "Not a valid filter")
59                 return Response(data=problem_details_serializer.data, status=status.HTTP_400_BAD_REQUEST)
60             resp_data = QueryVnfLcmOpOcc(request.query_params).query_multi_vnf_lcm_op_occ()
61
62             vnf_lcm_op_occs_serializer = VNFLCMOpOccsSerializer(data=resp_data)
63             if not vnf_lcm_op_occs_serializer.is_valid():
64                 raise NFLCMException(vnf_lcm_op_occs_serializer.errors)
65
66             logger.debug("QueryMultiVnfLcmOpOccs--get::> Remove default fields if exclude_default" +
67                          " is specified")
68             # TODO(bharath): Add support for "fields", "exclude_fields" in query parameters
69             if 'exclude_default' in request.query_params.keys():
70                 for field in EXCLUDE_DEFAULT:
71                     for lcm_op in vnf_lcm_op_occs_serializer.data:
72                         del lcm_op[field]
73             return Response(data=vnf_lcm_op_occs_serializer.data, status=status.HTTP_200_OK)
74         except NFLCMException as e:
75             logger.error(e.message)
76             problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR, e.message)
77             return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
78
79         except Exception as e:
80             logger.error(e.message)
81             logger.error(traceback.format_exc())
82             problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR, e.message)
83             return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
84
85
86 class QuerySingleVnfLcmOpOcc(APIView):
87     @swagger_auto_schema(
88         responses={
89             status.HTTP_200_OK: VNFLCMOpOccSerializer(),
90             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
91         }
92     )
93     def get(self, request, lcmopoccid):
94         logger.debug("QuerySingleVnfLcmOpOcc--get::> %s" % request.query_params)
95         try:
96             resp_data = QueryVnfLcmOpOcc(request.query_params, lcm_op_occ_id=lcmopoccid).query_single_vnf_lcm_op_occ()
97
98             vnf_lcm_op_occ_serializer = VNFLCMOpOccSerializer(data=resp_data)
99             if not vnf_lcm_op_occ_serializer.is_valid():
100                 raise NFLCMException(vnf_lcm_op_occ_serializer.errors)
101
102             return Response(data=vnf_lcm_op_occ_serializer.data, status=status.HTTP_200_OK)
103         except NFLCMException as e:
104             logger.error(e.message)
105             problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR, e.message)
106             return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
107         except Exception as e:
108             logger.error(e.message)
109             logger.error(traceback.format_exc())
110             problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR, e.message)
111             return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)