6d5213b89ba3ed2afabc5fecd6f8acc1e5203ac7
[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.pub.exceptions import NFLCMException
28 from lcm.pub.utils.jobutil import JobUtil
29
30 logger = logging.getLogger(__name__)
31
32
33 class CreateVnfIdentifier(APIView):
34     def post(self, request):
35         logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
36         try:
37             nf_inst_id = CreateVnf(request.data).do_biz()
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:
42             logger.error(traceback.format_exc())
43             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
44         rsp = {"vnfInstanceId": nf_inst_id}
45         return Response(data=rsp, status=status.HTTP_201_CREATED)
46
47
48 class InstantiateVnf(APIView):
49     def post(self, request, instanceid):
50         logger.debug("InstantiateVnf--post::> %s" % request.data)
51         job_id = JobUtil.create_job('NF', 'INSTANTIATE', instanceid)
52         JobUtil.add_job_status(job_id, 0, "INST_VNF_READY")
53         InstVnf(request.data, instanceid, job_id).start()
54         rsp = {"jobId": job_id}
55         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
56
57
58 class DeleteVnfIdentifier(APIView):
59     def delete(self, request, instanceid):
60         logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
61         try:
62             DeleteVnf(request.data, instanceid).do_biz()
63         except NFLCMException as e:
64             logger.error(e.message)
65             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
66         except Exception:
67             logger.error(traceback.format_exc())
68             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
69         return Response(data=None, status=status.HTTP_204_NO_CONTENT)
70
71
72 class TerminateVnf(APIView):
73     def post(self, request, instanceid):
74         logger.debug("TerminateVnf--post::> %s" % request.data)
75         job_id = JobUtil.create_job('NF', 'TERMINATE', instanceid)
76         JobUtil.add_job_status(job_id, 0, "TERM_VNF_READY")
77         TermVnf(request.data, instanceid, job_id).start()
78         rsp = {"jobId": job_id}
79         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
80
81
82 class QueryMultipleVnf(APIView):
83     def get(self, request):
84         logger.debug("QueryMultipleVnf--get::> %s" % request.data)
85         return Response(data='', status=status.HTTP_202_ACCEPTED)
86
87
88 class QuerySingleVnf(APIView):
89     def get(self, request):
90         logger.debug("QuerySingleVnf--get::> %s" % request.data)
91         return Response(data='', status=status.HTTP_202_ACCEPTED)
92
93
94 class GetOperationStatus(APIView):
95     def get(self, request):
96         logger.debug("GetOperationStatus--get::> %s" % request.data)
97         return Response(data='', status=status.HTTP_202_ACCEPTED)
98
99
100 class SwaggerJsonView(APIView):
101     def get(self, request):
102         json_file = os.path.join(os.path.dirname(__file__), 'swagger.json')
103         f = open(json_file)
104         json_data = json.JSONDecoder().decode(f.read())
105         f.close()
106         return Response(json_data)