2330fa2cbc00b4c049cb62ede3718426390bf49e
[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("QuerySingleVnf--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:
42             logger.error(traceback.format_exc())
43             return Response(data={'error': 'Failed to get Vnfs'},
44                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
45         return Response(data=resp_data, status=status.HTTP_200_OK)
46
47     def post(self, request):
48         logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
49         try:
50             nf_inst_id = CreateVnf(request.data).do_biz()
51         except NFLCMException as e:
52             logger.error(e.message)
53             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
54         except Exception:
55             logger.error(traceback.format_exc())
56             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
57         rsp = {"vnfInstanceId": nf_inst_id}
58         return Response(data=rsp, status=status.HTTP_201_CREATED)
59
60
61 class InstantiateVnf(APIView):
62     def post(self, request, instanceid):
63         logger.debug("InstantiateVnf--post::> %s" % request.data)
64         try:
65             job_id = JobUtil.create_job('NF', 'INSTANTIATE', instanceid)
66             JobUtil.add_job_status(job_id, 0, "INST_VNF_READY")
67             InstVnf(request.data, instanceid, job_id).start()
68         except NFLCMException as e:
69             logger.error(e.message)
70             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
71         except Exception:
72             logger.error(traceback.format_exc())
73             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
74         rsp = {"jobId": job_id}
75         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
76
77
78 class DeleteVnfAndQueryVnf(APIView):
79     def get(self, request, instanceid):
80         logger.debug("QuerySingleVnf--get::> %s" % request.data)
81         try:
82             resp_data = QueryVnf(request.data, instanceid).query_single_vnf()
83         except NFLCMException as e:
84             logger.error(e.message)
85             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
86         except:
87             logger.error(traceback.format_exc())
88             return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid},
89                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
90         return Response(data=resp_data, status=status.HTTP_200_OK)
91
92     def delete(self, request, instanceid):
93         logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
94         try:
95             DeleteVnf(request.data, instanceid).do_biz()
96         except NFLCMException as e:
97             logger.error(e.message)
98             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
99         except Exception:
100             logger.error(traceback.format_exc())
101             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
102         return Response(data=None, status=status.HTTP_204_NO_CONTENT)
103
104
105 class TerminateVnf(APIView):
106     def post(self, request, instanceid):
107         logger.debug("TerminateVnf--post::> %s" % request.data)
108         try:
109             job_id = JobUtil.create_job('NF', 'TERMINATE', instanceid)
110             JobUtil.add_job_status(job_id, 0, "TERM_VNF_READY")
111             TermVnf(request.data, instanceid, job_id).start()
112         except NFLCMException as e:
113             logger.error(e.message)
114             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
115         except Exception:
116             logger.error(traceback.format_exc())
117             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
118         rsp = {"jobId": job_id}
119         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)