add comments
[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,
72                                                                         "Not a valid filter")
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)
77
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)
81
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:
86                     del lcm_op[field]
87         return Response(data=resp_data, status=status.HTTP_200_OK)
88
89
90 class QuerySingleNsLcmOpOcc(APIView):
91     """
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.
94     """
95
96     @swagger_auto_schema(
97         responses={
98             status.HTTP_200_OK: NSLCMOpOccSerializer(),
99             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
100         }
101     )
102     @view_safe_call_with_log(logger=logger)
103     def get(self, request, lcmopoccid):
104         """
105         The client can use this method to query status information about multiple NS lifecycle management operation occurrences.
106         :param request:
107         :param lcmopoccid:
108         :return:
109         """
110         logger.debug("QuerySingleNsLcmOpOcc--get::> %s" % request.query_params)
111
112         resp_data = QueryNsLcmOpOcc(request.query_params,
113                                     lcm_op_occ_id=lcmopoccid).query_single_ns_lcm_op_occ()
114
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)
118
119         return Response(data=resp_data, status=status.HTTP_200_OK)