1980fd4801fff1be15621f8970f102326e5f977a
[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 drf_yasg.utils import swagger_auto_schema
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.serializers import CreateVnfReqSerializer, CreateVnfRespSerializer
24 from lcm.nf.vnfs.vnf_cancel.delete_vnf_identifier import DeleteVnf
25 from lcm.nf.vnfs.vnf_cancel.term_vnf import TermVnf
26 from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf
27 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
28 from lcm.nf.vnfs.vnf_query.query_vnf import QueryVnf
29 from lcm.pub.exceptions import NFLCMException
30 from lcm.pub.utils.jobutil import JobUtil
31
32 logger = logging.getLogger(__name__)
33
34
35 class CreateVnfAndQueryVnfs(APIView):
36     def get(self, request):
37         logger.debug("QueryMultiVnf--get::> %s" % request.data)
38         try:
39             resp_data = QueryVnf(request.data).query_multi_vnf()
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 as e:
44             logger.error(e.message)
45             logger.error(traceback.format_exc())
46             return Response(data={'error': 'Failed to get Vnfs'},
47                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
48         return Response(data=resp_data, status=status.HTTP_200_OK)
49
50     @swagger_auto_schema(
51         request_body=CreateVnfReqSerializer(),
52         responses={
53             status.HTTP_201_CREATED: CreateVnfRespSerializer(),
54             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
55         }
56     )
57     def post(self, request):
58         logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
59         req_serializer = CreateVnfReqSerializer(data=request.data)
60         req_isValid = req_serializer.is_valid()
61         try:
62             if not req_isValid:
63                 raise NFLCMException(req_serializer.errors)
64
65             nf_inst_id = CreateVnf(req_serializer.data).do_biz()
66             rsp = {
67                 "vnfInstanceId": nf_inst_id
68             }
69             resp_serializer = CreateVnfRespSerializer(data=rsp)
70             resp_isValid = resp_serializer.is_valid()
71             if not resp_isValid:
72                 raise NFLCMException(resp_serializer.errors)
73             return Response(data=resp_serializer.data, status=status.HTTP_201_CREATED)
74         except NFLCMException as e:
75             logger.error(e.message)
76             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
77         except Exception as e:
78             logger.error(e.message)
79             logger.error(traceback.format_exc())
80             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
81
82
83 class InstantiateVnf(APIView):
84     def post(self, request, instanceid):
85         logger.debug("InstantiateVnf--post::> %s" % request.data)
86         try:
87             job_id = JobUtil.create_job('NF', 'INSTANTIATE', instanceid)
88             JobUtil.add_job_status(job_id, 0, "INST_VNF_READY")
89             InstVnf(request.data, instanceid, job_id).start()
90         except NFLCMException as e:
91             logger.error(e.message)
92             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
93         except Exception as e:
94             logger.error(e.message)
95             logger.error(traceback.format_exc())
96             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
97         rsp = {
98             "jobId": job_id
99         }
100         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
101
102
103 class DeleteVnfAndQueryVnf(APIView):
104     def get(self, request, instanceid):
105         logger.debug("QuerySingleVnf--get::> %s" % request.data)
106         try:
107             resp_data = QueryVnf(request.data, instanceid).query_single_vnf()
108         except NFLCMException as e:
109             logger.error(e.message)
110             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
111         except Exception as e:
112             logger.eror(e.message)
113             logger.error(traceback.format_exc())
114             return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid},
115                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
116         return Response(data=resp_data, status=status.HTTP_200_OK)
117
118     def delete(self, request, instanceid):
119         logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
120         try:
121             DeleteVnf(request.data, instanceid).do_biz()
122         except NFLCMException as e:
123             logger.error(e.message)
124             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
125         except Exception as e:
126             logger.error(e.message)
127             logger.error(traceback.format_exc())
128             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
129         return Response(data=None, status=status.HTTP_204_NO_CONTENT)
130
131
132 class TerminateVnf(APIView):
133     def post(self, request, instanceid):
134         logger.debug("TerminateVnf--post::> %s" % request.data)
135         try:
136             job_id = JobUtil.create_job('NF', 'TERMINATE', instanceid)
137             JobUtil.add_job_status(job_id, 0, "TERM_VNF_READY")
138             TermVnf(request.data, instanceid, job_id).start()
139         except NFLCMException as e:
140             logger.error(e.message)
141             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
142         except Exception as e:
143             logger.error(e.message)
144             logger.error(traceback.format_exc())
145             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
146         rsp = {
147             "jobId": job_id
148         }
149         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)