6db70b7c298856c1b55eb574eebeeee1bb2e3ec4
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / views / curd_vnf_views.py
1 # Copyright 2017 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 lcm.nf.biz.delete_vnf import DeleteVnf
20 from rest_framework import status
21 from rest_framework.response import Response
22 from rest_framework.views import APIView
23
24 from lcm.nf.biz.create_vnf import CreateVnf
25 from lcm.nf.biz.query_vnf import QueryVnf
26 from lcm.nf.serializers.create_vnf_req import CreateVnfReqSerializer
27 from lcm.nf.serializers.vnf_instance import VnfInstanceSerializer
28 from lcm.nf.serializers.vnf_instances import VnfInstancesSerializer
29 from lcm.pub.exceptions import NFLCMException
30 from lcm.pub.exceptions import NFLCMExceptionNotFound
31
32 logger = logging.getLogger(__name__)
33
34
35 class CreateVnfAndQueryVnfs(APIView):
36     @swagger_auto_schema(
37         responses={
38             status.HTTP_200_OK: VnfInstancesSerializer(),
39             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
40         }
41     )
42     def get(self, request):
43         logger.debug("QueryMultiVnf--get::> %s" % request.data)
44         try:
45             resp_data = QueryVnf(request.data).query_multi_vnf()
46             if len(resp_data) == 0:
47                 return Response(data=[], status=status.HTTP_200_OK)
48             vnf_instances_serializer = VnfInstancesSerializer(data=resp_data)
49             if not vnf_instances_serializer.is_valid():
50                 raise NFLCMException(vnf_instances_serializer.errors)
51
52             return Response(data=vnf_instances_serializer.data, status=status.HTTP_200_OK)
53         except NFLCMException as e:
54             logger.error(e.message)
55             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
56         except Exception as e:
57             logger.error(e.message)
58             logger.error(traceback.format_exc())
59             return Response(data={'error': 'Failed to get Vnfs'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
60
61     @swagger_auto_schema(
62         request_body=CreateVnfReqSerializer(),
63         responses={
64             status.HTTP_201_CREATED: VnfInstanceSerializer(),
65             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
66         }
67     )
68     def post(self, request):
69         logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
70         try:
71             req_serializer = CreateVnfReqSerializer(data=request.data)
72             if not req_serializer.is_valid():
73                 raise NFLCMException(req_serializer.errors)
74
75             nf_inst = CreateVnf(request.data).do_biz()
76             create_vnf_resp_serializer = VnfInstanceSerializer(data={"id": nf_inst.nfinstid,
77                                                                      "vnfProvider": nf_inst.vendor,
78                                                                      "vnfdVersion": nf_inst.version,
79                                                                      "vnfPkgId": nf_inst.package_id,
80                                                                      "instantiationState": nf_inst.status})
81             if not create_vnf_resp_serializer.is_valid():
82                 raise NFLCMException(create_vnf_resp_serializer.errors)
83             return Response(data=create_vnf_resp_serializer.data, status=status.HTTP_201_CREATED)
84         except NFLCMException as e:
85             logger.error(e.message)
86             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
87         except Exception as e:
88             logger.error(e.message)
89             logger.error(traceback.format_exc())
90             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
91
92
93 class DeleteVnfAndQueryVnf(APIView):
94     @swagger_auto_schema(
95         responses={
96             status.HTTP_200_OK: VnfInstanceSerializer(),
97             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
98         }
99     )
100     def get(self, request, instanceid):
101         logger.debug("QuerySingleVnf--get::> %s" % request.data)
102         try:
103             resp_data = QueryVnf(request.data, instanceid).query_single_vnf()
104
105             vnfs_instance_serializer = VnfInstanceSerializer(data=resp_data)
106             if not vnfs_instance_serializer.is_valid():
107                 raise NFLCMException(vnfs_instance_serializer.errors)
108
109             return Response(data=vnfs_instance_serializer.data, status=status.HTTP_200_OK)
110         except NFLCMException as e:
111             logger.error(e.message)
112             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
113         except NFLCMExceptionNotFound as e:
114             return Response(data={'error': '%s' % e.message}, status=status.HTTP_404_NOT_FOUND)
115         except Exception as e:
116             logger.error(e.message)
117             logger.error(traceback.format_exc())
118             return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid},
119                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
120
121     @swagger_auto_schema(
122         responses={
123             status.HTTP_204_NO_CONTENT: "Successfully",
124             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
125         }
126     )
127     def delete(self, request, instanceid):
128         logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
129         try:
130             DeleteVnf(request.data, instanceid).do_biz()
131
132             return Response(data=None, status=status.HTTP_204_NO_CONTENT)
133         except NFLCMException as e:
134             logger.error(e.message)
135             logger.debug('Delete VNF instance[%s] failed' % instanceid)
136             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
137         except Exception as e:
138             logger.error(e.message)
139             logger.error(traceback.format_exc())
140             logger.debug('Delete VNF instance[%s] failed' % instanceid)
141             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)