Fix vfc-vnflcm pep8 issues
[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 rest_framework import status
19 from rest_framework.response import Response
20 from rest_framework.views import APIView
21
22 from lcm.nf.vnfs.vnf_cancel.delete_vnf_identifier import DeleteVnf
23 from lcm.nf.vnfs.vnf_cancel.term_vnf import TermVnf
24 from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf
25 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
26 from lcm.nf.vnfs.vnf_query.query_vnf import QueryVnf
27 from lcm.pub.exceptions import NFLCMException
28 from lcm.pub.utils.jobutil import JobUtil
29
30 logger = logging.getLogger(__name__)
31
32
33 class CreateVnfAndQueryVnfs(APIView):
34     def get(self, request):
35         logger.debug("QueryMultiVnf--get::> %s" % request.data)
36         try:
37             resp_data = QueryVnf(request.data).query_multi_vnf()
38         except NFLCMException as e:
39             logger.error(e.message)
40             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
41         except Exception as e:
42             logger.error(e.message)
43             logger.error(traceback.format_exc())
44             return Response(data={'error': 'Failed to get Vnfs'},
45                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
46         return Response(data=resp_data, status=status.HTTP_200_OK)
47
48     def post(self, request):
49         logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
50         try:
51             nf_inst_id = CreateVnf(request.data).do_biz()
52         except NFLCMException as e:
53             logger.error(e.message)
54             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
55         except Exception as e:
56             logger.error(e.message)
57             logger.error(traceback.format_exc())
58             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
59         rsp = {
60             "vnfInstanceId": nf_inst_id
61         }
62         return Response(data=rsp, status=status.HTTP_201_CREATED)
63
64
65 class InstantiateVnf(APIView):
66     def post(self, request, instanceid):
67         logger.debug("InstantiateVnf--post::> %s" % request.data)
68         try:
69             job_id = JobUtil.create_job('NF', 'INSTANTIATE', instanceid)
70             JobUtil.add_job_status(job_id, 0, "INST_VNF_READY")
71             InstVnf(request.data, instanceid, job_id).start()
72         except NFLCMException as e:
73             logger.error(e.message)
74             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
75         except Exception as e:
76             logger.error(e.message)
77             logger.error(traceback.format_exc())
78             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
79         rsp = {
80             "jobId": job_id
81         }
82         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
83
84
85 class DeleteVnfAndQueryVnf(APIView):
86     def get(self, request, instanceid):
87         logger.debug("QuerySingleVnf--get::> %s" % request.data)
88         try:
89             resp_data = QueryVnf(request.data, instanceid).query_single_vnf()
90         except NFLCMException as e:
91             logger.error(e.message)
92             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
93         except Exception as e:
94             logger.eror(e.message)
95             logger.error(traceback.format_exc())
96             return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid},
97                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
98         return Response(data=resp_data, status=status.HTTP_200_OK)
99
100     def delete(self, request, instanceid):
101         logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
102         try:
103             DeleteVnf(request.data, instanceid).do_biz()
104         except NFLCMException as e:
105             logger.error(e.message)
106             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
107         except Exception as e:
108             logger.error(e.message)
109             logger.error(traceback.format_exc())
110             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
111         return Response(data=None, status=status.HTTP_204_NO_CONTENT)
112
113
114 class TerminateVnf(APIView):
115     def post(self, request, instanceid):
116         logger.debug("TerminateVnf--post::> %s" % request.data)
117         try:
118             job_id = JobUtil.create_job('NF', 'TERMINATE', instanceid)
119             JobUtil.add_job_status(job_id, 0, "TERM_VNF_READY")
120             TermVnf(request.data, instanceid, job_id).start()
121         except NFLCMException as e:
122             logger.error(e.message)
123             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
124         except Exception as e:
125             logger.error(e.message)
126             logger.error(traceback.format_exc())
127             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
128         rsp = {
129             "jobId": job_id
130         }
131         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)