Add vfc-vnflcm instantiation auto-swagger
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / 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 rest_framework import status
20 from rest_framework.response import Response
21 from rest_framework.views import APIView
22
23 from lcm.nf.vnfs.serializers import CreateVnfReqSerializer, CreateVnfRespSerializer, VnfsInfoSerializer, \
24     InstantiateVnfResponseSerializer, InstantiateVnfRequestSerializer
25 from lcm.nf.vnfs.vnf_cancel.delete_vnf_identifier import DeleteVnf
26 from lcm.nf.vnfs.vnf_cancel.term_vnf import TermVnf
27 from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf
28 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
29 from lcm.nf.vnfs.vnf_query.query_vnf import QueryVnf
30 from lcm.pub.exceptions import NFLCMException
31 from lcm.pub.utils.jobutil import JobUtil
32
33 logger = logging.getLogger(__name__)
34
35
36 class CreateVnfAndQueryVnfs(APIView):
37     @swagger_auto_schema(
38         request_body=None,
39         responses={
40             status.HTTP_200_OK: VnfsInfoSerializer(),
41             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
42         }
43     )
44     def get(self, request):
45         logger.debug("QueryMultiVnf--get::> %s" % request.data)
46         try:
47             resp_data = QueryVnf(request.data).query_multi_vnf()
48
49             vnfsInfoSerializer = VnfsInfoSerializer(data=resp_data)
50             resp_isValid = vnfsInfoSerializer.is_valid()
51             if not resp_isValid:
52                 raise NFLCMException(vnfsInfoSerializer.errors)
53
54             return Response(data=vnfsInfoSerializer.data, status=status.HTTP_200_OK)
55         except NFLCMException as e:
56             logger.error(e.message)
57             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
58         except Exception as e:
59             logger.error(e.message)
60             logger.error(traceback.format_exc())
61             return Response(data={'error': 'Failed to get Vnfs'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
62
63     @swagger_auto_schema(
64         request_body=CreateVnfReqSerializer(),
65         responses={
66             status.HTTP_201_CREATED: CreateVnfRespSerializer(),
67             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
68         }
69     )
70     def post(self, request):
71         logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
72         req_serializer = CreateVnfReqSerializer(data=request.data)
73         req_isValid = req_serializer.is_valid()
74         try:
75             if not req_isValid:
76                 raise NFLCMException(req_serializer.errors)
77
78             nf_inst_id = CreateVnf(req_serializer.data).do_biz()
79
80             resp_serializer = CreateVnfRespSerializer(data={"vnfInstanceId": nf_inst_id})
81             resp_isValid = resp_serializer.is_valid()
82             if not resp_isValid:
83                 raise NFLCMException(resp_serializer.errors)
84             return Response(data=resp_serializer.data, status=status.HTTP_201_CREATED)
85         except NFLCMException as e:
86             logger.error(e.message)
87             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
88         except Exception as e:
89             logger.error(e.message)
90             logger.error(traceback.format_exc())
91             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
92
93
94 class InstantiateVnf(APIView):
95     @swagger_auto_schema(
96         request_body=InstantiateVnfRequestSerializer(),
97         responses={
98             status.HTTP_202_ACCEPTED: InstantiateVnfResponseSerializer(),
99             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
100         }
101     )
102     def post(self, request, instanceid):
103         logger.debug("InstantiateVnf--post::> %s" % request.data)
104         instantiateVnfRequestSerializer = InstantiateVnfRequestSerializer(data=request.data)
105         req_isValid = instantiateVnfRequestSerializer.is_valid()
106         try:
107             if not req_isValid:
108                 raise NFLCMException(instantiateVnfRequestSerializer.errors)
109
110             job_id = JobUtil.create_job('NF', 'INSTANTIATE', instanceid)
111             JobUtil.add_job_status(job_id, 0, "INST_VNF_READY")
112             InstVnf(instantiateVnfRequestSerializer.data, instanceid, job_id).start()
113
114             instantiateVnfResponseSerializer = InstantiateVnfResponseSerializer(data={"jobId": job_id})
115             resp_isValid = instantiateVnfResponseSerializer.is_valid()
116             if not resp_isValid:
117                 raise NFLCMException(instantiateVnfResponseSerializer.errors)
118
119             return Response(data=instantiateVnfResponseSerializer.data, status=status.HTTP_202_ACCEPTED)
120         except NFLCMException as e:
121             logger.error(e.message)
122             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
123         except Exception as e:
124             logger.error(e.message)
125             logger.error(traceback.format_exc())
126             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
127
128
129 class DeleteVnfAndQueryVnf(APIView):
130     def get(self, request, instanceid):
131         logger.debug("QuerySingleVnf--get::> %s" % request.data)
132         try:
133             resp_data = QueryVnf(request.data, instanceid).query_single_vnf()
134         except NFLCMException as e:
135             logger.error(e.message)
136             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
137         except Exception as e:
138             logger.eror(e.message)
139             logger.error(traceback.format_exc())
140             return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid},
141                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
142         return Response(data=resp_data, status=status.HTTP_200_OK)
143
144     def delete(self, request, instanceid):
145         logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
146         try:
147             DeleteVnf(request.data, instanceid).do_biz()
148         except NFLCMException as e:
149             logger.error(e.message)
150             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
151         except Exception as e:
152             logger.error(e.message)
153             logger.error(traceback.format_exc())
154             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
155         return Response(data=None, status=status.HTTP_204_NO_CONTENT)
156
157
158 class TerminateVnf(APIView):
159     def post(self, request, instanceid):
160         logger.debug("TerminateVnf--post::> %s" % request.data)
161         try:
162             job_id = JobUtil.create_job('NF', 'TERMINATE', instanceid)
163             JobUtil.add_job_status(job_id, 0, "TERM_VNF_READY")
164             TermVnf(request.data, instanceid, job_id).start()
165         except NFLCMException as e:
166             logger.error(e.message)
167             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
168         except Exception as e:
169             logger.error(e.message)
170             logger.error(traceback.format_exc())
171             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
172         rsp = {
173             "jobId": job_id
174         }
175         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)