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,
73 return Response(data=problem_details_serializer.data, status=status.HTTP_400_BAD_REQUEST)
74 resp_data = QueryNsLcmOpOcc(request.query_params).query_multi_ns_lcm_op_occ()
75 if len(resp_data) == 0:
76 return Response(data=[], status=status.HTTP_200_OK)
78 ns_lcm_op_occs_serializer = NSLCMOpOccsSerializer(data=resp_data)
79 if not ns_lcm_op_occs_serializer.is_valid():
80 raise NSLCMException(ns_lcm_op_occs_serializer.errors)
82 logger.debug("QueryMultiNsLcmOpOccs--get::> Remove default fields if exclude_default is specified")
83 if 'exclude_default' in list(request.query_params.keys()):
84 for field in EXCLUDE_DEFAULT:
85 for lcm_op in resp_data:
87 return Response(data=resp_data, status=status.HTTP_200_OK)
90 class QuerySingleNsLcmOpOcc(APIView):
92 This resource represents NS lifecycle management operation occurrences.
93 The client can use this resource to query status information about multiple NS lifecycle management operation occurrences.
98 status.HTTP_200_OK: NSLCMOpOccSerializer(),
99 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
102 @view_safe_call_with_log(logger=logger)
103 def get(self, request, lcmopoccid):
105 The client can use this method to query status information about multiple NS lifecycle management operation occurrences.
110 logger.debug("QuerySingleNsLcmOpOcc--get::> %s" % request.query_params)
112 resp_data = QueryNsLcmOpOcc(request.query_params,
113 lcm_op_occ_id=lcmopoccid).query_single_ns_lcm_op_occ()
115 ns_lcm_op_occ_serializer = NSLCMOpOccSerializer(data=resp_data)
116 if not ns_lcm_op_occ_serializer.is_valid():
117 raise NSLCMException(ns_lcm_op_occ_serializer.errors)
119 return Response(data=resp_data, status=status.HTTP_200_OK)