Fix vfc-vnflcm unittests
[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:
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 = {
58             "vnfInstanceId": nf_inst_id
59         }
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 = {
77             "jobId": job_id
78         }
79         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
80
81
82 class DeleteVnfAndQueryVnf(APIView):
83     def get(self, request, instanceid):
84         logger.debug("QuerySingleVnf--get::> %s" % request.data)
85         try:
86             resp_data = QueryVnf(request.data, instanceid).query_single_vnf()
87         except NFLCMException as e:
88             logger.error(e.message)
89             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
90         except:
91             logger.error(traceback.format_exc())
92             return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid},
93                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
94         return Response(data=resp_data, status=status.HTTP_200_OK)
95
96     def delete(self, request, instanceid):
97         logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
98         try:
99             DeleteVnf(request.data, instanceid).do_biz()
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:
104             logger.error(traceback.format_exc())
105             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
106         return Response(data=None, status=status.HTTP_204_NO_CONTENT)
107
108
109 class TerminateVnf(APIView):
110     def post(self, request, instanceid):
111         logger.debug("TerminateVnf--post::> %s" % request.data)
112         try:
113             job_id = JobUtil.create_job('NF', 'TERMINATE', instanceid)
114             JobUtil.add_job_status(job_id, 0, "TERM_VNF_READY")
115             TermVnf(request.data, instanceid, job_id).start()
116         except NFLCMException as e:
117             logger.error(e.message)
118             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
119         except Exception:
120             logger.error(traceback.format_exc())
121             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
122         rsp = {
123             "jobId": job_id
124         }
125         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)