Add code and testcase of query single vnf
[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 from lcm.pub.utils.values import ignore_case_get
31
32 logger = logging.getLogger(__name__)
33
34
35 class CreateVnfIdentifier(APIView):
36     def post(self, request):
37         logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
38         try:
39             nf_inst_id = CreateVnf(request.data).do_biz()
40         except NFLCMException as e:
41             logger.error(e.message)
42             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
43         except Exception:
44             logger.error(traceback.format_exc())
45             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
46         rsp = {"vnfInstanceId": nf_inst_id}
47         return Response(data=rsp, status=status.HTTP_201_CREATED)
48
49
50 class InstantiateVnf(APIView):
51     def post(self, request, instanceid):
52         try:
53             logger.debug("InstantiateVnf--post::> %s" % request.data)
54             job_id = JobUtil.create_job('NF', 'INSTANTIATE', instanceid)
55             JobUtil.add_job_status(job_id, 0, "INST_VNF_READY")
56             InstVnf(request.data, instanceid, job_id).start()
57         except NFLCMException as e:
58             logger.error(e.message)
59             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
60         except Exception:
61             logger.error(traceback.format_exc())
62             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
63         rsp = {"jobId": job_id}
64         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
65
66
67 class DetailVnf(APIView):
68     def get(self, request, instanceid):
69         logger.debug("QuerySingleVnf--get::> %s" % request.data)
70         try:
71             resp_data = QueryVnf(request.data, instanceid).query_single_vnf()
72         except NFLCMException as e:
73             logger.error(e.message)
74             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
75         except:
76             logger.error(traceback.format_exc())
77             tt = traceback.format_exc()
78             return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid},
79                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
80         return Response(data=resp_data, status=status.HTTP_200_OK)
81
82     def delete(self, request, instanceid):
83         logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
84         try:
85             DeleteVnf(request.data, instanceid).do_biz()
86         except NFLCMException as e:
87             logger.error(e.message)
88             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
89         except Exception:
90             logger.error(traceback.format_exc())
91             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
92         return Response(data=None, status=status.HTTP_204_NO_CONTENT)
93
94
95 class TerminateVnf(APIView):
96     def post(self, request, instanceid):
97         logger.debug("TerminateVnf--post::> %s" % request.data)
98         job_id = JobUtil.create_job('NF', 'TERMINATE', instanceid)
99         JobUtil.add_job_status(job_id, 0, "TERM_VNF_READY")
100         TermVnf(request.data, instanceid, job_id).start()
101         rsp = {"jobId": job_id}
102         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
103
104
105 class QueryMultipleVnf(APIView):
106     def get(self, request):
107         logger.debug("QueryMultipleVnf--get::> %s" % request.data)
108         return Response(data='', status=status.HTTP_202_ACCEPTED)
109
110 # class QuerySingleVnf(APIView):
111 #     def get(self, request, instanceid):
112 #         logger.debug("QuerySingleVnf--get::> %s" % request.data)
113 #         try:
114 #             resp_data = QueryVnf(request.data, instanceid).query_single_vnf(instanceid)
115 #         except NFLCMException as e:
116 #             logger.error(e.message)
117 #             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
118 #         except:
119 #             logger.error(traceback.format_exc())
120 #             return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid},
121 #                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
122 #         return Response(data=resp_data, status=status.HTTP_202_ACCEPTED)
123
124
125 # class GetOperationStatus(APIView):
126 #     def get(self, request):
127 #         logger.debug("GetOperationStatus--get::> %s" % request.data)
128 #         return Response(data='', status=status.HTTP_202_ACCEPTED)
129
130
131 class SwaggerJsonView(APIView):
132     def get(self, request):
133         json_file = os.path.join(os.path.dirname(__file__), 'swagger.json')
134         f = open(json_file)
135         json_data = json.JSONDecoder().decode(f.read())
136         f.close()
137         return Response(json_data)