aboutsummaryrefslogtreecommitdiffstats
path: root/lcm/ns/views/sol/lcm_op_occs_view.py
blob: 88d0470d9bb20de27a8aefaf2901e9dd1b349a83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Copyright (c) 2019, CMCC Technologies Co., Ltd.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from drf_yasg.utils import swagger_auto_schema
from lcm.ns.serializers.sol.ns_lcm_op_occ import NSLCMOpOccSerializer
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from lcm.ns.biz.query_ns_lcm_op_occ import QueryNsLcmOpOcc
from lcm.ns.serializers.sol.ns_lcm_op_occ import NSLCMOpOccsSerializer
from lcm.ns.serializers.sol.pub_serializers import ProblemDetailsSerializer
from lcm.pub.exceptions import NSLCMException
from .common import view_safe_call_with_log

logger = logging.getLogger(__name__)
EXCLUDE_DEFAULT = [
    'operationParams',
    'error',
    'resourceChanges'
]
VALID_FILTERS = [
    "fields",
    "exclude_fields",
    "exclude_default",
    "id",
    "operationState",
    "stateEnteredTime",
    "startTime",
    "nsInstanceId",
    "operation"
]


def get_problem_details_serializer(status_code, error_message):
    problem_details = {
        "status": status_code,
        "detail": error_message
    }
    problem_details_serializer = ProblemDetailsSerializer(data=problem_details)
    problem_details_serializer.is_valid()
    return problem_details_serializer


class QueryMultiNsLcmOpOccs(APIView):
    @swagger_auto_schema(
        responses={
            status.HTTP_200_OK: NSLCMOpOccsSerializer(),
            status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(),
            status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
        }
    )
    @view_safe_call_with_log(logger=logger)
    def get(self, request):
        logger.debug("QueryMultiNsLcmOpOccs--get::> %s" % request.query_params)

        if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
            problem_details_serializer = get_problem_details_serializer(status.HTTP_400_BAD_REQUEST,
                                                                        "Not a valid filter")
            return Response(data=problem_details_serializer.data, status=status.HTTP_400_BAD_REQUEST)
        resp_data = QueryNsLcmOpOcc(request.query_params).query_multi_ns_lcm_op_occ()
        if len(resp_data) == 0:
            return Response(data=[], status=status.HTTP_200_OK)

        ns_lcm_op_occs_serializer = NSLCMOpOccsSerializer(data=resp_data)
        if not ns_lcm_op_occs_serializer.is_valid():
            raise NSLCMException(ns_lcm_op_occs_serializer.errors)

        logger.debug("QueryMultiNsLcmOpOccs--get::> Remove default fields if exclude_default is specified")
        if 'exclude_default' in list(request.query_params.keys()):
            for field in EXCLUDE_DEFAULT:
                for lcm_op in resp_data:
                    del lcm_op[field]
        return Response(data=resp_data, status=status.HTTP_200_OK)


class QuerySingleNsLcmOpOcc(APIView):
    """
    This resource represents NS lifecycle management operation occurrences.
    The client can use this resource to query status information about multiple NS lifecycle management operation occurrences.
    """

    @swagger_auto_schema(
        responses={
            status.HTTP_200_OK: NSLCMOpOccSerializer(),
            status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
        }
    )
    @view_safe_call_with_log(logger=logger)
    def get(self, request, lcmopoccid):
        """
        The client can use this method to query status information about multiple NS lifecycle management operation occurrences.
        :param request:
        :param lcmopoccid:
        :return:
        """
        logger.debug("QuerySingleNsLcmOpOcc--get::> %s" % request.query_params)

        resp_data = QueryNsLcmOpOcc(request.query_params,
                                    lcm_op_occ_id=lcmopoccid).query_single_ns_lcm_op_occ()

        ns_lcm_op_occ_serializer = NSLCMOpOccSerializer(data=resp_data)
        if not ns_lcm_op_occ_serializer.is_valid():
            raise NSLCMException(ns_lcm_op_occ_serializer.errors)

        return Response(data=resp_data, status=status.HTTP_200_OK)