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