Refactor vfc vnflcm code format
[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 json
16 import logging
17 import os
18 import traceback
19
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.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     def get(self, request):
37         logger.debug("QuerySingleVnf--get::> %s" % request.data)
38         try:
39             resp_data = QueryVnf(request.data).query_multi_vnf()
40         except NFLCMException as e:
41             logger.error(e.message)
42             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
43         except:
44             logger.error(traceback.format_exc())
45             return Response(data={'error': 'Failed to get Vnfs'},
46                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
47         return Response(data=resp_data, status=status.HTTP_200_OK)
48
49     def post(self, request):
50         logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
51         try:
52             nf_inst_id = CreateVnf(request.data).do_biz()
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:
57             logger.error(traceback.format_exc())
58             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
59         rsp = {"vnfInstanceId": nf_inst_id}
60         return Response(data=rsp, status=status.HTTP_201_CREATED)
61
62
63 class InstantiateVnf(APIView):
64     def post(self, request, instanceid):
65         logger.debug("InstantiateVnf--post::> %s" % request.data)
66         try:
67             job_id = JobUtil.create_job('NF', 'INSTANTIATE', instanceid)
68             JobUtil.add_job_status(job_id, 0, "INST_VNF_READY")
69             InstVnf(request.data, instanceid, job_id).start()
70         except NFLCMException as e:
71             logger.error(e.message)
72             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
73         except Exception:
74             logger.error(traceback.format_exc())
75             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
76         rsp = {"jobId": job_id}
77         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
78
79
80 class DeleteVnfAndQueryVnf(APIView):
81     def get(self, request, instanceid):
82         logger.debug("QuerySingleVnf--get::> %s" % request.data)
83         try:
84             resp_data = QueryVnf(request.data, instanceid).query_single_vnf()
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:
89             logger.error(traceback.format_exc())
90             return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid},
91                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
92         return Response(data=resp_data, status=status.HTTP_200_OK)
93
94     def delete(self, request, instanceid):
95         logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
96         try:
97             DeleteVnf(request.data, instanceid).do_biz()
98         except NFLCMException as e:
99             logger.error(e.message)
100             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
101         except Exception:
102             logger.error(traceback.format_exc())
103             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
104         return Response(data=None, status=status.HTTP_204_NO_CONTENT)
105
106
107 class TerminateVnf(APIView):
108     def post(self, request, instanceid):
109         logger.debug("TerminateVnf--post::> %s" % request.data)
110         try:
111             job_id = JobUtil.create_job('NF', 'TERMINATE', instanceid)
112             JobUtil.add_job_status(job_id, 0, "TERM_VNF_READY")
113             TermVnf(request.data, instanceid, job_id).start()
114         except NFLCMException as e:
115             logger.error(e.message)
116             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
117         except Exception:
118             logger.error(traceback.format_exc())
119             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
120         rsp = {"jobId": job_id}
121         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
122
123
124 class SwaggerJsonView(APIView):
125     def get(self, request):
126         json_file = os.path.join(os.path.dirname(__file__), 'swagger.json')
127         f = open(json_file)
128         json_data = json.JSONDecoder().decode(f.read())
129         f.close()
130         return Response(json_data)