0e1ae6fec9520af7b1aab3ec12252a943a15531b
[vfc/nfvo/lcm.git] / lcm / ns_pnfs / views / pnf_view.py
1 # Copyright 2018 ZTE Corporation.
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 import traceback
17
18 from drf_yasg.utils import swagger_auto_schema
19 from rest_framework import status
20 from rest_framework.response import Response
21 from rest_framework.views import APIView
22 from lcm.ns_pnfs.serializers.pnf_serializer import PnfInstanceSerializer, PnfInstancesSerializer
23 from lcm.ns_pnfs.biz.create_pnf import CreatePnf
24 from lcm.ns_pnfs.biz.delete_pnf import DeletePnf
25 from lcm.ns_pnfs.biz.get_pnf import GetPnf
26 from lcm.pub.exceptions import NSLCMException
27
28 logger = logging.getLogger(__name__)
29
30
31 class PnfView(APIView):
32     @swagger_auto_schema(
33         request_body=PnfInstanceSerializer(),
34         responses={
35             status.HTTP_201_CREATED: PnfInstanceSerializer()
36         }
37     )
38     def post(self, request):
39         logger.debug("PnfView--post::> %s" % request.data)
40
41         req_serializer = PnfInstanceSerializer(data=request.data)
42         if not req_serializer.is_valid():
43             logger.error(req_serializer.errors)
44             resp = {"result": 1, "detail": req_serializer.errors, "pnfId": ""}
45             return Response(data=resp, status=status.HTTP_201_CREATED)
46
47         pnfInstData = CreatePnf(request.data).do_biz()
48         resp_serializer = PnfInstanceSerializer(data=pnfInstData.__dict__)
49         if not resp_serializer.is_valid():
50             logger.error(resp_serializer.errors)
51             resp = {"result": 1, "detail": resp_serializer.errors, "pnfId": ""}
52             return Response(data=resp, status=status.HTTP_201_CREATED)
53
54         return Response(data=resp_serializer.data, status=status.HTTP_201_CREATED)
55
56     @swagger_auto_schema(
57         request_body="None",
58         responses={
59             status.HTTP_200_OK: PnfInstancesSerializer(help_text="Pnf instances", many=True),
60             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
61         }
62     )
63     def get(self, request):
64         try:
65             logger.debug("PnfView::get")
66             pnfInstDataSet = GetPnf().do_biz()
67             logger.debug("PnfView::get::ret=%s", pnfInstDataSet)
68             resp_serializer = PnfInstancesSerializer(data=[pnfInstData.__dict__ for pnfInstData in pnfInstDataSet])
69             if not resp_serializer.is_valid():
70                 raise NSLCMException(resp_serializer.errors)
71             return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
72         except Exception as e:
73             logger.error(traceback.format_exc())
74             logger.error("Exception in GetPnf: %s", e.message)
75             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
76
77
78 class IndividualPnfView(APIView):
79     @swagger_auto_schema(
80         request_body="None",
81         responses={
82             status.HTTP_204_NO_CONTENT: 'successful',
83             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
84         }
85     )
86     def delete(self, request, pnf_id):
87         try:
88             logger.debug("Enter IndividualPnfView::delete pnf(%s)", pnf_id)
89             DeletePnf(pnf_id).do_biz()
90             return Response(data={}, status=status.HTTP_204_NO_CONTENT)
91         except Exception as e:
92             logger.error(traceback.format_exc())
93             logger.error("Exception in delete pnf: %s", e.message)
94             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
95
96     @swagger_auto_schema(
97         request_body="None",
98         responses={
99             status.HTTP_200_OK: PnfInstanceSerializer(help_text="Pnf instance", many=True),
100             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error",
101             status.HTTP_404_NOT_FOUND: "Pnf instance does not exist"
102         }
103     )
104     def get(self, request, pnf_id):
105         try:
106             logger.debug("Enter IndividualPnfView::get pnf(%s)", pnf_id)
107             pnf_filter = {"pnfId": pnf_id}
108             pnfInstData = GetPnf(pnf_filter, True).do_biz()
109             if not pnfInstData:
110                 return Response(status=status.HTTP_404_NOT_FOUND)
111             logger.debug("Leave IndividualPnfView::get::ret=%s", pnfInstData)
112             resp_serializer = PnfInstanceSerializer(data=pnfInstData.__dict__)
113             if not resp_serializer.is_valid():
114                 raise NSLCMException(resp_serializer.errors)
115             return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
116         except Exception as e:
117             logger.error(traceback.format_exc())
118             logger.error("Exception in IndividualPnfView: %s", e.message)
119             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)