aboutsummaryrefslogtreecommitdiffstats
path: root/lcm/ns_pnfs/views/pnf_view.py
blob: c6f2694d6d0c5865bc98bf9b2add0f17cd86486d (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 2018 ZTE Corporation.
#
# 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
import traceback

from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from lcm.ns_pnfs.serializers.pnf_serializer import PnfInstanceSerializer, PnfInstancesSerializer
from lcm.ns_pnfs.biz.create_pnf import CreatePnf
from lcm.ns_pnfs.biz.delete_pnf import DeletePnf
from lcm.ns_pnfs.biz.get_pnf import GetPnf
from lcm.pub.exceptions import NSLCMException

logger = logging.getLogger(__name__)


class PnfView(APIView):
    @swagger_auto_schema(
        request_body=PnfInstanceSerializer(),
        responses={
            status.HTTP_201_CREATED: PnfInstanceSerializer()
        }
    )
    def post(self, request):
        logger.debug("PnfView--post::> %s" % request.data)

        req_serializer = PnfInstanceSerializer(data=request.data)
        if not req_serializer.is_valid():
            logger.error(req_serializer.errors)
            resp = {"result": 1, "detail": req_serializer.errors, "pnfId": ""}
            return Response(data=resp, status=status.HTTP_201_CREATED)

        pnfInstData = CreatePnf(request.data).do_biz()
        resp_serializer = PnfInstanceSerializer(data=pnfInstData.__dict__)
        if not resp_serializer.is_valid():
            logger.error(resp_serializer.errors)
            resp = {"result": 1, "detail": resp_serializer.errors, "pnfId": ""}
            return Response(data=resp, status=status.HTTP_201_CREATED)

        return Response(data=resp_serializer.data, status=status.HTTP_201_CREATED)

    @swagger_auto_schema(
        request_body=None,
        responses={
            status.HTTP_200_OK: PnfInstancesSerializer(help_text="Pnf instances", many=True),
            status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
        }
    )
    def get(self, request):
        try:
            logger.debug("PnfView::get")
            pnfInstDataSet = GetPnf().do_biz()
            logger.debug("PnfView::get::ret=%s", pnfInstDataSet)
            resp_serializer = PnfInstancesSerializer(data=[pnfInstData.__dict__ for pnfInstData in pnfInstDataSet])
            if not resp_serializer.is_valid():
                raise NSLCMException(resp_serializer.errors)
            return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
        except Exception as e:
            logger.error(traceback.format_exc())
            logger.error("Exception in GetPnf: %s", e.message)
            return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)


class IndividualPnfView(APIView):
    @swagger_auto_schema(
        request_body=None,
        responses={
            status.HTTP_204_NO_CONTENT: 'successful',
            status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
        }
    )
    def delete(self, request, pnf_id):
        try:
            logger.debug("Enter IndividualPnfView::delete pnf(%s)", pnf_id)
            DeletePnf(pnf_id).do_biz()
            return Response(data={}, status=status.HTTP_204_NO_CONTENT)
        except Exception as e:
            logger.error(traceback.format_exc())
            logger.error("Exception in delete pnf: %s", e.message)
            return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

    @swagger_auto_schema(
        request_body=None,
        responses={
            status.HTTP_200_OK: PnfInstanceSerializer(help_text="Pnf instance", many=True),
            status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error",
            status.HTTP_404_NOT_FOUND: "Pnf instance does not exist"
        }
    )
    def get(self, request, pnf_id):
        try:
            logger.debug("Enter IndividualPnfView::get pnf(%s)", pnf_id)
            pnf_filter = {"pnfId": pnf_id}
            pnfInstData = GetPnf(pnf_filter, True).do_biz()
            if not pnfInstData:
                return Response(status=status.HTTP_404_NOT_FOUND)
            logger.debug("Leave IndividualPnfView::get::ret=%s", pnfInstData)
            resp_serializer = PnfInstanceSerializer(data=pnfInstData.__dict__)
            if not resp_serializer.is_valid():
                raise NSLCMException(resp_serializer.errors)
            return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
        except Exception as e:
            logger.error(traceback.format_exc())
            logger.error("Exception in IndividualPnfView: %s", e.message)
            return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)