Update python2 to python3
[vfc/nfvo/lcm.git] / lcm / ns / views / sol / lcm_op_occs_view.py
1 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
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 lcm.ns.serializers.sol.ns_lcm_op_occ import NSLCMOpOccSerializer
19 from rest_framework import status
20 from rest_framework.response import Response
21 from rest_framework.views import APIView
22
23 from lcm.ns.biz.query_ns_lcm_op_occ import QueryNsLcmOpOcc
24 from lcm.ns.serializers.sol.ns_lcm_op_occ import NSLCMOpOccsSerializer
25 from lcm.ns.serializers.sol.pub_serializers import ProblemDetailsSerializer
26 from lcm.pub.exceptions import NSLCMException
27 from .common import view_safe_call_with_log
28
29 logger = logging.getLogger(__name__)
30 EXCLUDE_DEFAULT = [
31     'operationParams',
32     'error',
33     'resourceChanges'
34 ]
35 VALID_FILTERS = [
36     "fields",
37     "exclude_fields",
38     "exclude_default",
39     "id",
40     "operationState",
41     "stateEnteredTime",
42     "startTime",
43     "nsInstanceId",
44     "operation"
45 ]
46
47
48 def get_problem_details_serializer(status_code, error_message):
49     problem_details = {
50         "status": status_code,
51         "detail": error_message
52     }
53     problem_details_serializer = ProblemDetailsSerializer(data=problem_details)
54     problem_details_serializer.is_valid()
55     return problem_details_serializer
56
57
58 class QueryMultiNsLcmOpOccs(APIView):
59     @swagger_auto_schema(
60         responses={
61             status.HTTP_200_OK: NSLCMOpOccsSerializer(),
62             status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
63             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
64         }
65     )
66     @view_safe_call_with_log(logger=logger)
67     def get(self, request):
68         logger.debug("QueryMultiNsLcmOpOccs--get::> %s" % request.query_params)
69
70         if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
71             problem_details_serializer = get_problem_details_serializer(status.HTTP_400_BAD_REQUEST, "Not a valid filter")
72             return Response(data=problem_details_serializer.data, status=status.HTTP_400_BAD_REQUEST)
73         resp_data = QueryNsLcmOpOcc(request.query_params).query_multi_ns_lcm_op_occ()
74         if len(resp_data) == 0:
75             return Response(data=[], status=status.HTTP_200_OK)
76
77         ns_lcm_op_occs_serializer = NSLCMOpOccsSerializer(data=resp_data)
78         if not ns_lcm_op_occs_serializer.is_valid():
79             raise NSLCMException(ns_lcm_op_occs_serializer.errors)
80
81         logger.debug("QueryMultiNsLcmOpOccs--get::> Remove default fields if exclude_default is specified")
82         if 'exclude_default' in list(request.query_params.keys()):
83             for field in EXCLUDE_DEFAULT:
84                 for lcm_op in ns_lcm_op_occs_serializer.data:
85                     del lcm_op[field]
86         return Response(data=ns_lcm_op_occs_serializer.data, status=status.HTTP_200_OK)
87
88
89 class QuerySingleNsLcmOpOcc(APIView):
90     @swagger_auto_schema(
91         responses={
92             status.HTTP_200_OK: NSLCMOpOccSerializer(),
93             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
94         }
95     )
96     @view_safe_call_with_log(logger=logger)
97     def get(self, request, lcmopoccid):
98         logger.debug("QuerySingleNsLcmOpOcc--get::> %s" % request.query_params)
99
100         resp_data = QueryNsLcmOpOcc(request.query_params,
101                                     lcm_op_occ_id=lcmopoccid).query_single_ns_lcm_op_occ()
102
103         ns_lcm_op_occ_serializer = NSLCMOpOccSerializer(data=resp_data)
104         if not ns_lcm_op_occ_serializer.is_valid():
105             raise NSLCMException(ns_lcm_op_occ_serializer.errors)
106
107         return Response(data=ns_lcm_op_occ_serializer.data, status=status.HTTP_200_OK)