ad435f9bd6ac14cee8dd94061bd575c7ca615ce9
[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             nsInstanceId = request.query_params.get('nsInstanceId', None)
67             if nsInstanceId is not None:
68                 filter = {"nsInstanceId": nsInstanceId}
69                 pnfInstDataSet = GetPnf(filter).do_biz()
70             else:
71                 pnfInstDataSet = GetPnf().do_biz()
72             logger.debug("PnfView::get::ret=%s", pnfInstDataSet)
73             resp_serializer = PnfInstancesSerializer(data=[pnfInstData.__dict__ for pnfInstData in pnfInstDataSet])
74             if not resp_serializer.is_valid():
75                 raise NSLCMException(resp_serializer.errors)
76             return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
77         except Exception as e:
78             logger.error(traceback.format_exc())
79             logger.error("Exception in GetPnf: %s", e.message)
80             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
81
82
83 class IndividualPnfView(APIView):
84     @swagger_auto_schema(
85         request_body=None,
86         responses={
87             status.HTTP_204_NO_CONTENT: 'successful',
88             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
89         }
90     )
91     def delete(self, request, pnf_id):
92         try:
93             logger.debug("Enter IndividualPnfView::delete pnf(%s)", pnf_id)
94             DeletePnf(pnf_id).do_biz()
95             return Response(data={}, status=status.HTTP_204_NO_CONTENT)
96         except Exception as e:
97             logger.error(traceback.format_exc())
98             logger.error("Exception in delete pnf: %s", e.message)
99             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
100
101     @swagger_auto_schema(
102         request_body=None,
103         responses={
104             status.HTTP_200_OK: PnfInstanceSerializer(help_text="Pnf instance", many=True),
105             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error",
106             status.HTTP_404_NOT_FOUND: "Pnf instance does not exist"
107         }
108     )
109     def get(self, request, pnf_id):
110         try:
111             logger.debug("Enter IndividualPnfView::get pnf(%s)", pnf_id)
112             pnf_filter = {"pnfId": pnf_id}
113             pnfInstData = GetPnf(pnf_filter, True).do_biz()
114             if not pnfInstData:
115                 return Response(status=status.HTTP_404_NOT_FOUND)
116             logger.debug("Leave IndividualPnfView::get::ret=%s", pnfInstData)
117             resp_serializer = PnfInstanceSerializer(data=pnfInstData.__dict__)
118             if not resp_serializer.is_valid():
119                 raise NSLCMException(resp_serializer.errors)
120             return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
121         except Exception as e:
122             logger.error(traceback.format_exc())
123             logger.error("Exception in IndividualPnfView: %s", e.message)
124             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)