8b026e5c7b33796fa0d322035e46e04b0f4a18b9
[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 import json
15 import logging
16 import os
17 import traceback
18
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.vnf_cancel.delete_vnf_identifier import DeleteVnf
24 from lcm.nf.vnfs.vnf_cancel.term_vnf import TermVnf
25 from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf
26 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
27 from lcm.nf.vnfs.vnf_query.query_vnf import QueryVnf
28 from lcm.pub.exceptions import NFLCMException
29 from lcm.pub.utils.jobutil import JobUtil
30
31 logger = logging.getLogger(__name__)
32
33
34 class CreateVnfAndQueryVnfs(APIView):
35     def get(self, request):
36         logger.debug("QuerySingleVnf--get::> %s" % request.data)
37         try:
38             resp_data = QueryVnf(request.data).query_multi_vnf()
39         except NFLCMException as e:
40             logger.error(e.message)
41             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
42         except:
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:
56             logger.error(traceback.format_exc())
57             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
58         rsp = {"vnfInstanceId": nf_inst_id}
59         return Response(data=rsp, status=status.HTTP_201_CREATED)
60
61
62 class InstantiateVnf(APIView):
63     def post(self, request, instanceid):
64         logger.debug("InstantiateVnf--post::> %s" % request.data)
65         try:
66             job_id = JobUtil.create_job('NF', 'INSTANTIATE', instanceid)
67             JobUtil.add_job_status(job_id, 0, "INST_VNF_READY")
68             InstVnf(request.data, instanceid, job_id).start()
69         except NFLCMException as e:
70             logger.error(e.message)
71             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
72         except Exception:
73             logger.error(traceback.format_exc())
74             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
75         rsp = {"jobId": job_id}
76         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
77
78
79 class DeleteVnfAndQueryVnf(APIView):
80     def get(self, request, instanceid):
81         logger.debug("QuerySingleVnf--get::> %s" % request.data)
82         try:
83             resp_data = QueryVnf(request.data, instanceid).query_single_vnf()
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:
88             logger.error(traceback.format_exc())
89             return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid},
90                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
91         return Response(data=resp_data, status=status.HTTP_200_OK)
92
93     def delete(self, request, instanceid):
94         logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
95         try:
96             DeleteVnf(request.data, instanceid).do_biz()
97         except NFLCMException as e:
98             logger.error(e.message)
99             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
100         except Exception:
101             logger.error(traceback.format_exc())
102             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
103         return Response(data=None, status=status.HTTP_204_NO_CONTENT)
104
105
106 class TerminateVnf(APIView):
107     def post(self, request, instanceid):
108         logger.debug("TerminateVnf--post::> %s" % request.data)
109         try:
110             job_id = JobUtil.create_job('NF', 'TERMINATE', instanceid)
111             JobUtil.add_job_status(job_id, 0, "TERM_VNF_READY")
112             TermVnf(request.data, instanceid, job_id).start()
113         except NFLCMException as e:
114             logger.error(e.message)
115             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
116         except Exception:
117             logger.error(traceback.format_exc())
118             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
119         rsp = {"jobId": job_id}
120         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
121
122
123 class SwaggerJsonView(APIView):
124     def get(self, request):
125         json_file = os.path.join(os.path.dirname(__file__), 'swagger.json')
126         f = open(json_file)
127         json_data = json.JSONDecoder().decode(f.read())
128         f.close()
129         return Response(json_data)