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 nsInstanceId = request.query_params.get('nsInstanceId', None)
67 if nsInstanceId is not None:
68 filter = {"nsInstanceId": nsInstanceId}
69 pnfInstDataSet = GetPnf(filter).do_biz()
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)
83 class IndividualPnfView(APIView):
87 status.HTTP_204_NO_CONTENT: 'successful',
88 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
91 def delete(self, request, pnf_id):
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)
101 @swagger_auto_schema(
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"
109 def get(self, request, pnf_id):
111 logger.debug("Enter IndividualPnfView::get pnf(%s)", pnf_id)
112 pnf_filter = {"pnfId": pnf_id}
113 pnfInstData = GetPnf(pnf_filter, True).do_biz()
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)