1 # Copyright 2018 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 logger = logging.getLogger(__name__)
31 class PnfView(APIView):
33 request_body=PnfInstanceSerializer(),
35 status.HTTP_201_CREATED: PnfInstanceSerializer()
38 def post(self, request):
39 logger.debug("PnfView--post::> %s" % request.data)
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)
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)
54 return Response(data=resp_serializer.data, status=status.HTTP_201_CREATED)
59 status.HTTP_200_OK: PnfInstancesSerializer(help_text="Pnf instances", many=True),
60 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
63 def get(self, request):
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)
78 class IndividualPnfView(APIView):
82 status.HTTP_204_NO_CONTENT: 'successful',
83 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
86 def delete(self, request, pnf_id):
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)
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"
104 def get(self, request, pnf_id):
106 logger.debug("Enter IndividualPnfView::get pnf(%s)", pnf_id)
107 pnf_filter = {"pnfId": pnf_id}
108 pnfInstData = GetPnf(pnf_filter, True).do_biz()
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)