Merge "Add get vnf swagger generate logic"
[vfc/nfvo/lcm.git] / lcm / ns / vnfs / views.py
1 # Copyright 2016-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 logging
15 import traceback
16 import uuid
17
18 from rest_framework import status
19 from rest_framework.response import Response
20 from rest_framework.views import APIView
21 from drf_yasg.utils import swagger_auto_schema
22
23 from lcm.ns.vnfs import create_vnfs
24 from lcm.ns.vnfs.create_vnfs import CreateVnfs
25 from lcm.ns.vnfs.verify_vnfs import VerifyVnfs
26 from lcm.ns.vnfs.get_vnfs import GetVnf
27 from lcm.ns.vnfs.scale_vnfs import NFManualScaleService
28 from lcm.ns.vnfs.terminate_nfs import TerminateVnfs
29 from lcm.ns.vnfs.grant_vnfs import GrantVnfs
30 from lcm.ns.vnfs.notify_lcm import NotifyLcm
31 from lcm.pub.exceptions import NSLCMException
32 from lcm.pub.msapi.extsys import get_vnfm_by_id, get_vim_by_id
33 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
34 from lcm.pub.utils.values import ignore_case_get
35 from lcm.ns.vnfs.serializers import InstVnfReqSerializer
36 from lcm.ns.vnfs.serializers import InstVnfRespSerializer
37 from lcm.ns.vnfs.serializers import GetVnfRespSerializer
38
39 logger = logging.getLogger(__name__)
40
41
42 class NfView(APIView):
43     @swagger_auto_schema(
44         request_body=InstVnfReqSerializer(),
45         responses={
46             status.HTTP_202_ACCEPTED: InstVnfRespSerializer()
47         }
48     )
49     def post(self, request):
50         logger.debug("VnfCreateView--post::> %s" % request.data)
51
52         req_serializer = InstVnfReqSerializer(data=request.data)
53         if not req_serializer.is_valid():
54             logger.error(req_serializer.errors)
55
56         data = {'ns_instance_id': ignore_case_get(request.data, 'nsInstanceId'),
57                 'additional_param_for_ns': ignore_case_get(request.data, 'additionalParamForVnf'),
58                 'additional_param_for_vnf': ignore_case_get(request.data, 'additionalParamForVnf'),
59                 'vnf_index': ignore_case_get(request.data, 'vnfIndex')}
60         nf_inst_id, job_id = create_vnfs.prepare_create_params()
61         CreateVnfs(data, nf_inst_id, job_id).start()
62         rsp = {
63             "vnfInstId": nf_inst_id,
64             "jobId": job_id}
65
66         resp_serializer = InstVnfRespSerializer(data=rsp)
67         if not resp_serializer.is_valid():
68             logger.error(resp_serializer.errors)
69
70         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
71
72
73 class NfDetailView(APIView):
74     @swagger_auto_schema(
75         request_body=None,
76         responses={
77             status.HTTP_200_OK: GetVnfRespSerializer(),
78             status.HTTP_404_NOT_FOUND: "VNF not found"
79         }
80     )
81     def get(self, request, vnfinstid):
82         logger.debug("VnfQueryView--get::> %s" % vnfinstid)
83         nf_inst_info = GetVnf(vnfinstid).do_biz()
84         if not nf_inst_info:
85             return Response(status=status.HTTP_404_NOT_FOUND)
86
87         rsp = {
88             'vnfInstId': nf_inst_info[0].nfinstid,
89             'vnfName': nf_inst_info[0].nf_name,
90             'vnfStatus': nf_inst_info[0].status
91         }
92         resp_serializer = GetVnfRespSerializer(data=rsp)
93         if not resp_serializer.is_valid():
94             logger.error(resp_serializer.errors)
95
96         return Response(status=status.HTTP_200_OK, data=rsp)
97
98     def post(self, request_paras, vnfinstid):
99         logger.debug("VnfTerminateView--post::> %s, %s", vnfinstid, request_paras.data)
100         vnf_inst_id = vnfinstid
101         terminationType = ignore_case_get(request_paras.data, 'terminationType')
102         gracefulTerminationTimeout = ignore_case_get(request_paras.data, 'gracefulTerminationTimeout')
103         job_id = JobUtil.create_job("VNF", JOB_TYPE.TERMINATE_VNF, vnf_inst_id)
104         data = {'terminationType': terminationType, 'gracefulTerminationTimeout': gracefulTerminationTimeout}
105         logger.debug("data=%s", data)
106         try:
107             TerminateVnfs(data, vnf_inst_id, job_id).start()
108         except Exception as e:
109             logger.error(e.message)
110             return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
111         rsp = {'jobId': job_id}
112         return Response(data=rsp, status=status.HTTP_201_CREATED)
113
114
115 class NfGrant(APIView):
116     def post(self, request):
117         logger.debug("NfGrant--post::> %s" % request.data)
118         try:
119             vnf_inst_id = ignore_case_get(request.data, 'vnfInstanceId')
120             job_id = JobUtil.create_job("VNF", JOB_TYPE.GRANT_VNF, vnf_inst_id)
121             rsp = GrantVnfs(request.data, job_id).send_grant_vnf_to_resMgr()
122             """
123             rsp = {
124                 "vim": {
125                     "vimid": ignore_case_get(ignore_case_get(request.data, 'additionalparam'), 'vimid'),
126                     "accessinfo": {
127                         "tenant": "admin"
128                     }
129                 }
130             }
131             """
132             return Response(data=rsp, status=status.HTTP_201_CREATED)
133         except Exception as e:
134             logger.error(e.message)
135             logger.error(traceback.format_exc())
136             return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
137
138
139 class LcmNotify(APIView):
140     def post(self, request_paras, vnfmid, vnfInstanceId):
141         logger.debug("LcmNotify--post::> %s" % request_paras.data)
142         try:
143             NotifyLcm(vnfmid, vnfInstanceId, request_paras.data).do_biz()
144             return Response(data={}, status=status.HTTP_201_CREATED)
145         except Exception as e:
146             logger.error(e.message)
147             return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
148
149
150 class NfScaleView(APIView):
151     def post(self, request_paras, vnfinstid):
152         logger.debug("NfScaleView--post::> %s" % request_paras.data)
153         try:
154             NFManualScaleService(vnfinstid, request_paras.data).start()
155             return Response(data={}, status=status.HTTP_202_ACCEPTED)
156         except Exception as e:
157             logger.error(e.message)
158             return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
159
160
161 class NfVerifyView(APIView):
162     def post(self, request):
163         job_id = "VNFSDK_" + str(uuid.uuid4())
164         logger.debug("NfVerifyView--post::%s> %s", job_id, request.data)
165         VerifyVnfs(request.data, job_id).start()
166         return Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)
167
168
169 class NfVnfmInfoView(APIView):
170     def get(self, request, vnfmid):
171         logger.debug("NfVnfmInfoView--get::> %s" % vnfmid)
172         try:
173             vnfm_info = get_vnfm_by_id(vnfmid)
174         except NSLCMException as e:
175             logger.error(e.message)
176             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
177         except Exception as e:
178             logger.error(e.message)
179             logger.error(traceback.format_exc())
180             return Response(data={'error': 'Failed to get vnfm info.'},
181                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
182         return Response(data=vnfm_info, status=status.HTTP_200_OK)
183
184
185 class NfVimInfoView(APIView):
186     def get(self, request, vimid):
187         logger.debug("NfVimInfoView--get::> %s" % vimid)
188         try:
189             vim_info = get_vim_by_id(vimid)
190         except NSLCMException as e:
191             logger.error(e.message)
192             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
193         except Exception as e:
194             logger.error(e.message)
195             logger.error(traceback.format_exc())
196             return Response(data={'error': 'Failed to get vim info.'},
197                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
198         return Response(data=vim_info, status=status.HTTP_200_OK)