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