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