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