update swagger info, add tags for swagger_auto_schema
[modeling/etsicatalog.git] / catalog / packages / views / vnf_package_views.py
1 # Copyright 2018 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
17 from django.http import StreamingHttpResponse
18 from drf_yasg.utils import swagger_auto_schema, no_body
19 from rest_framework import status
20 from rest_framework.decorators import api_view
21 from rest_framework.response import Response
22
23 from catalog.packages.serializers.upload_vnf_pkg_from_uri_req import UploadVnfPackageFromUriRequestSerializer
24 from catalog.packages.serializers.create_vnf_pkg_info_req import CreateVnfPkgInfoRequestSerializer
25 from catalog.packages.serializers.vnf_pkg_info import VnfPkgInfoSerializer
26 from catalog.packages.serializers.vnf_pkg_infos import VnfPkgInfosSerializer
27 from catalog.packages.biz.vnf_package import VnfPackage
28 from catalog.packages.biz.vnf_package import VnfPkgUploadThread
29 from catalog.packages.biz.vnf_package import parse_vnfd_and_save
30 from catalog.packages.biz.vnf_package import handle_upload_failed
31 from .common import validate_data
32 from .common import view_safe_call_with_log
33
34 logger = logging.getLogger(__name__)
35
36
37 @swagger_auto_schema(
38     method="GET",
39     operation_description="Query multiple VNF package resource",
40     tags=["VNF Package API"],
41     request_body=no_body,
42     responses={
43         status.HTTP_200_OK: VnfPkgInfosSerializer(),
44         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
45     }
46 )
47 @swagger_auto_schema(
48     method="POST",
49     operation_description="Create an individual VNF package resource",
50     tags=["VNF Package API"],
51     request_body=CreateVnfPkgInfoRequestSerializer,
52     responses={
53         status.HTTP_201_CREATED: VnfPkgInfoSerializer(),
54         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
55     }
56 )
57 @api_view(http_method_names=["GET", "POST"])
58 @view_safe_call_with_log(logger=logger)
59 def vnf_packages_rc(request):
60     if request.method == 'GET':
61         logger.debug("Query VNF packages> %s" % request.data)
62         data = VnfPackage().query_multiple()
63         validate_data(data, VnfPkgInfosSerializer)
64         return Response(data=data, status=status.HTTP_200_OK)
65
66     if request.method == 'POST':
67         logger.debug("Create VNF package> %s" % request.data)
68         create_vnf_pkg_info_request = validate_data(request.data,
69                                                     CreateVnfPkgInfoRequestSerializer)
70         data = VnfPackage().create_vnf_pkg(create_vnf_pkg_info_request.data)
71         validate_data(data, VnfPkgInfoSerializer)
72         return Response(data=data, status=status.HTTP_201_CREATED)
73
74
75 @swagger_auto_schema(
76     method='PUT',
77     operation_description="Upload VNF package content",
78     tags=["VNF Package API"],
79     request_body=no_body,
80     responses={
81         status.HTTP_202_ACCEPTED: "Successfully",
82         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
83     }
84 )
85 @swagger_auto_schema(
86     method="GET",
87     operation_description="Fetch VNF package content",
88     tags=["VNF Package API"],
89     request_body=no_body,
90     responses={
91         status.HTTP_200_OK: VnfPkgInfosSerializer(),
92         status.HTTP_404_NOT_FOUND: "VNF package does not exist",
93         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
94     }
95 )
96 @api_view(http_method_names=["PUT", "GET"])
97 @view_safe_call_with_log(logger=logger)
98 def package_content_ru(request, **kwargs):
99     vnf_pkg_id = kwargs.get("vnfPkgId")
100     if request.method == "PUT":
101         logger.debug("Upload VNF package %s" % vnf_pkg_id)
102         files = request.FILES.getlist('file')
103         try:
104             local_file_name = VnfPackage().upload(vnf_pkg_id, files[0])
105             parse_vnfd_and_save(vnf_pkg_id, local_file_name)
106             return Response(None, status=status.HTTP_202_ACCEPTED)
107         except Exception as e:
108             handle_upload_failed(vnf_pkg_id)
109             raise e
110
111     if request.method == "GET":
112         file_range = request.META.get('HTTP_RANGE')
113         file_iterator = VnfPackage().download(vnf_pkg_id, file_range)
114         return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
115
116
117 @swagger_auto_schema(
118     method='POST',
119     operation_description="Upload VNF package content from uri",
120     tags=["VNF Package API"],
121     request_body=UploadVnfPackageFromUriRequestSerializer,
122     responses={
123         status.HTTP_202_ACCEPTED: "Successfully",
124         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
125     }
126 )
127 @api_view(http_method_names=['POST'])
128 @view_safe_call_with_log(logger=logger)
129 def upload_from_uri_c(request, **kwargs):
130     vnf_pkg_id = kwargs.get("vnfPkgId")
131     try:
132         upload_vnf_from_uri_request = validate_data(request.data,
133                                                     UploadVnfPackageFromUriRequestSerializer)
134         VnfPkgUploadThread(upload_vnf_from_uri_request.data, vnf_pkg_id).start()
135         return Response(None, status=status.HTTP_202_ACCEPTED)
136     except Exception as e:
137         handle_upload_failed(vnf_pkg_id)
138         raise e
139
140
141 @swagger_auto_schema(
142     method='GET',
143     operation_description="Query an individual VNF package resource",
144     tags=["VNF Package API"],
145     request_body=no_body,
146     responses={
147         status.HTTP_200_OK: VnfPkgInfoSerializer(),
148         status.HTTP_404_NOT_FOUND: "VNF package does not exist",
149         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
150     }
151 )
152 @swagger_auto_schema(
153     method='DELETE',
154     operation_description="Delete an individual VNF package resource",
155     tags=["VNF Package API"],
156     request_body=no_body,
157     responses={
158         status.HTTP_204_NO_CONTENT: "No content",
159         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
160     }
161 )
162 @api_view(http_method_names=['GET', 'DELETE'])
163 @view_safe_call_with_log(logger=logger)
164 def vnf_package_rd(request, **kwargs):
165     vnf_pkg_id = kwargs.get("vnfPkgId")
166     if request.method == 'GET':
167         logger.debug("Query an individual VNF package> %s" % request.data)
168         data = VnfPackage().query_single(vnf_pkg_id)
169         validate_data(data, VnfPkgInfoSerializer)
170         return Response(data=data, status=status.HTTP_200_OK)
171
172     if request.method == 'DELETE':
173         logger.debug("Delete an individual VNF package> %s" % request.data)
174         VnfPackage().delete_vnf_pkg(vnf_pkg_id)
175         return Response(data=None, status=status.HTTP_204_NO_CONTENT)