1 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
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
29 logger = logging.getLogger(__name__)
48 def get_problem_details_serializer(status_code, error_message):
50 "status": status_code,
51 "detail": error_message
53 problem_details_serializer = ProblemDetailsSerializer(data=problem_details)
54 problem_details_serializer.is_valid()
55 return problem_details_serializer
58 class QueryMultiNsLcmOpOccs(APIView):
61 status.HTTP_200_OK: NSLCMOpOccsSerializer(),
62 status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
63 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
66 @view_safe_call_with_log(logger=logger)
67 def get(self, request):
68 logger.debug("QueryMultiNsLcmOpOccs--get::> %s" % request.query_params)
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)
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)
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:
86 return Response(data=ns_lcm_op_occs_serializer.data, status=status.HTTP_200_OK)
89 class QuerySingleNsLcmOpOcc(APIView):
92 status.HTTP_200_OK: NSLCMOpOccSerializer(),
93 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
96 @view_safe_call_with_log(logger=logger)
97 def get(self, request, lcmopoccid):
98 logger.debug("QuerySingleNsLcmOpOcc--get::> %s" % request.query_params)
100 resp_data = QueryNsLcmOpOcc(request.query_params,
101 lcm_op_occ_id=lcmopoccid).query_single_ns_lcm_op_occ()
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)
107 return Response(data=ns_lcm_op_occ_serializer.data, status=status.HTTP_200_OK)