Implement read VNFD API
[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="GET",
77     operation_description="Read VNFD of an on-boarded VNF package",
78     tags=["VNF Package API"],
79     request_body=no_body,
80     responses={
81         status.HTTP_200_OK: VnfPkgInfosSerializer(),
82         status.HTTP_404_NOT_FOUND: "VNF package does not exist",
83         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
84     }
85 )
86 @api_view(http_method_names=["GET"])
87 @view_safe_call_with_log(logger=logger)
88 def vnfd_rd(request, **kwargs):
89     vnf_pkg_id = kwargs.get("vnfPkgId")
90     logger.debug("Read VNFD for  VNF package %s" % vnf_pkg_id)
91     try:
92         file_iterator = VnfPackage().download_vnfd(vnf_pkg_id)
93         return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
94     except Exception as e:
95         logger.error(e)
96         raise e
97
98
99 @swagger_auto_schema(
100     method='PUT',
101     operation_description="Upload VNF package content",
102     tags=["VNF Package API"],
103     request_body=no_body,
104     responses={
105         status.HTTP_202_ACCEPTED: "Successfully",
106         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
107     }
108 )
109 @swagger_auto_schema(
110     method="GET",
111     operation_description="Fetch VNF package content",
112     tags=["VNF Package API"],
113     request_body=no_body,
114     responses={
115         status.HTTP_200_OK: VnfPkgInfosSerializer(),
116         status.HTTP_404_NOT_FOUND: "VNF package does not exist",
117         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
118     }
119 )
120 @api_view(http_method_names=["PUT", "GET"])
121 @view_safe_call_with_log(logger=logger)
122 def package_content_ru(request, **kwargs):
123     vnf_pkg_id = kwargs.get("vnfPkgId")
124     if request.method == "PUT":
125         logger.debug("Upload VNF package %s" % vnf_pkg_id)
126         files = request.FILES.getlist('file')
127         try:
128             local_file_name = VnfPackage().upload(vnf_pkg_id, files[0])
129             parse_vnfd_and_save(vnf_pkg_id, local_file_name)
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     if request.method == "GET":
136         file_range = request.META.get('HTTP_RANGE')
137         file_iterator = VnfPackage().download(vnf_pkg_id, file_range)
138         return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
139
140
141 @swagger_auto_schema(
142     method='POST',
143     operation_description="Upload VNF package content from uri",
144     tags=["VNF Package API"],
145     request_body=UploadVnfPackageFromUriRequestSerializer,
146     responses={
147         status.HTTP_202_ACCEPTED: "Successfully",
148         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
149     }
150 )
151 @api_view(http_method_names=['POST'])
152 @view_safe_call_with_log(logger=logger)
153 def upload_from_uri_c(request, **kwargs):
154     vnf_pkg_id = kwargs.get("vnfPkgId")
155     try:
156         upload_vnf_from_uri_request = validate_data(request.data,
157                                                     UploadVnfPackageFromUriRequestSerializer)
158         VnfPkgUploadThread(upload_vnf_from_uri_request.data, vnf_pkg_id).start()
159         return Response(None, status=status.HTTP_202_ACCEPTED)
160     except Exception as e:
161         handle_upload_failed(vnf_pkg_id)
162         raise e
163
164
165 @swagger_auto_schema(
166     method='GET',
167     operation_description="Query an individual VNF package resource",
168     tags=["VNF Package API"],
169     request_body=no_body,
170     responses={
171         status.HTTP_200_OK: VnfPkgInfoSerializer(),
172         status.HTTP_404_NOT_FOUND: "VNF package does not exist",
173         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
174     }
175 )
176 @swagger_auto_schema(
177     method='DELETE',
178     operation_description="Delete an individual VNF package resource",
179     tags=["VNF Package API"],
180     request_body=no_body,
181     responses={
182         status.HTTP_204_NO_CONTENT: "No content",
183         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
184     }
185 )
186 @api_view(http_method_names=['GET', 'DELETE'])
187 @view_safe_call_with_log(logger=logger)
188 def vnf_package_rd(request, **kwargs):
189     vnf_pkg_id = kwargs.get("vnfPkgId")
190     if request.method == 'GET':
191         logger.debug("Query an individual VNF package> %s" % request.data)
192         data = VnfPackage().query_single(vnf_pkg_id)
193         validate_data(data, VnfPkgInfoSerializer)
194         return Response(data=data, status=status.HTTP_200_OK)
195
196     if request.method == 'DELETE':
197         logger.debug("Delete an individual VNF package> %s" % request.data)
198         VnfPackage().delete_vnf_pkg(vnf_pkg_id)
199         return Response(data=None, status=status.HTTP_204_NO_CONTENT)