Add swagger code for nf packages
[vfc/nfvo/catalog.git] / catalog / packages / views.py
1 # Copyright 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
15 import logging
16 import uuid
17 from catalog.pub.utils.syscomm import fun_name
18 from rest_framework.response import Response
19 from rest_framework import status
20 from rest_framework.decorators import api_view
21 from catalog.pub.utils.values import ignore_case_get
22 from catalog.packages import nf_package
23 from catalog.packages import ns_package
24
25 from drf_yasg import openapi
26 from drf_yasg.utils import no_body, swagger_auto_schema
27
28 from catalog.serializers import NsPackagesSerializer
29 from catalog.serializers import NfPackageSerializer
30 from catalog.serializers import NfPackageDistributeRequestSerializer
31 from catalog.serializers import PostJobResponseResultSerializer
32
33
34 logger = logging.getLogger(__name__)
35
36
37 @swagger_auto_schema(
38     method='POST',
39     operation_description="On distribute NS package",
40     request_body=no_body,
41     responses={
42         status.HTTP_202_ACCEPTED: openapi.Response(
43             'return code',
44             openapi.Schema(
45                 type=openapi.TYPE_STRING,
46                 pattern='CSAR(\w+) distributed successfully.')),
47         status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response(
48             'error message',
49             openapi.Schema(
50                 type=openapi.TYPE_STRING))})
51 @swagger_auto_schema(
52     method='GET',
53     operation_description="Query NS packages",
54     request_body=no_body,
55     responses={
56         status.HTTP_200_OK: NsPackagesSerializer,
57         status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response(
58             'error message',
59             openapi.Schema(
60                 type=openapi.TYPE_STRING,
61                 pattern='failed'))})
62 @api_view(http_method_names=['POST', 'GET'])
63 def nspackages_rc(request, *args, **kwargs):
64     logger.debug("Enter %s, method is %s", fun_name(), request.method)
65     ret, normal_status, validation_response = None, None, None
66
67     if request.method == 'GET':
68         # Gets ns package list
69         ret = ns_package.ns_get_csars()
70         normal_status = status.HTTP_200_OK
71         responseSerializer = NsPackagesSerializer(data=ret[1])
72
73         if not responseSerializer.is_valid():
74             validation_response = handleValidatonError(
75                 responseSerializer, False)
76     elif request.method == 'POST':
77         # Distributes the package accroding to the given csarId
78         csar_id = ignore_case_get(request.data, "csarId")
79         logger.debug("csar_id is %s", csar_id)
80         ret = ns_package.ns_on_distribute(csar_id)
81         normal_status = status.HTTP_202_ACCEPTED
82
83     if validation_response:
84         return validation_response
85
86     logger.debug("Leave %s, Return value is %s", fun_name(), ret)
87     if ret[0] != 0:
88         return Response(
89             data={
90                 'error': ret[1]},
91             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
92     return Response(data=ret[1], status=normal_status)
93
94
95 @swagger_auto_schema(
96     method='POST',
97     operation_description="On distribute Nf package",
98     request_body=NfPackageDistributeRequestSerializer(),
99     responses={
100         status.HTTP_202_ACCEPTED: PostJobResponseResultSerializer,
101         status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response(
102             'error message',
103             openapi.Schema(
104                 type=openapi.TYPE_STRING))})
105 @swagger_auto_schema(
106     method='GET',
107     operation_description="Query Nf packages",
108     request_body=no_body,
109     responses={
110         status.HTTP_200_OK: NfPackageSerializer(
111             many=True),
112         status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response(
113             'error message',
114             openapi.Schema(
115                 type=openapi.TYPE_STRING))})
116 @api_view(http_method_names=['POST', 'GET'])
117 def nfpackages_rc(request, *args, **kwargs):
118     logger.debug(
119         "Enter %s%s, method is %s",
120         fun_name(),
121         request.data,
122         request.method)
123     ret, normal_status = None, None
124     if request.method == 'GET':
125         ret = nf_package.nf_get_csars()
126         normal_status = status.HTTP_200_OK
127     elif request.method == 'POST':
128         csar_id = ignore_case_get(request.data, "csarId")
129         vim_ids = ignore_case_get(request.data, "vimIds")
130         lab_vim_id = ignore_case_get(request.data, "labVimId")
131         job_id = str(uuid.uuid4())
132         nf_package.NfDistributeThread(
133             csar_id, vim_ids, lab_vim_id, job_id).start()
134         ret = [0, {"jobId": job_id}]
135         normal_status = status.HTTP_202_ACCEPTED
136     logger.debug("Leave %s, Return value is %s", fun_name(), ret)
137     if ret[0] != 0:
138         return Response(
139             data={
140                 'error': ret[1]},
141             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
142     return Response(data=ret[1], status=normal_status)
143
144
145 @api_view(http_method_names=['DELETE', 'GET'])
146 def ns_rd_csar(request, *args, **kwargs):
147     csar_id = ignore_case_get(kwargs, "csarId")
148     logger.info("Enter %s, method is %s, csar_id is %s",
149                 fun_name(), request.method, csar_id)
150     ret, normal_status = None, None
151     if request.method == 'GET':
152         ret = ns_package.ns_get_csar(csar_id)
153         normal_status = status.HTTP_200_OK
154     elif request.method == 'DELETE':
155         ret = ns_package.ns_delete_csar(csar_id)
156         normal_status = status.HTTP_202_ACCEPTED
157     logger.info("Leave %s, Return value is %s", fun_name(), ret)
158     if ret[0] != 0:
159         return Response(
160             data={
161                 'error': ret[1]},
162             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
163     return Response(data=ret[1], status=normal_status)
164
165
166 @api_view(http_method_names=['DELETE', 'GET'])
167 def nf_rd_csar(request, *args, **kwargs):
168     csar_id = ignore_case_get(kwargs, "csarId")
169     logger.info("Enter %s, method is %s, csar_id is %s",
170                 fun_name(), request.method, csar_id)
171     ret, normal_status = None, None
172     if request.method == 'GET':
173         ret = nf_package.nf_get_csar(csar_id)
174         normal_status = status.HTTP_200_OK
175     elif request.method == 'DELETE':
176         job_id = str(uuid.uuid4())
177         nf_package.NfPkgDeleteThread(csar_id, job_id).start()
178         ret = [0, {"jobId": job_id}]
179         normal_status = status.HTTP_202_ACCEPTED
180     logger.info("Leave %s, Return value is %s", fun_name(), ret)
181     if ret[0] != 0:
182         return Response(
183             data={
184                 'error': ret[1]},
185             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
186     return Response(data=ret[1], status=normal_status)
187
188
189 @api_view(http_method_names=['POST'])
190 def ns_model_parser(request, *args, **kwargs):
191     csar_id = ignore_case_get(request.data, "csarId")
192     inputs = ignore_case_get(request.data, "inputs")
193     logger.debug(
194         "Enter %s, csar_id=%s, inputs=%s",
195         fun_name(),
196         csar_id,
197         inputs)
198     ret = ns_package.parse_nsd(csar_id, inputs)
199     logger.info("Leave %s, Return value is %s", fun_name(), ret)
200     if ret[0] != 0:
201         return Response(
202             data={
203                 'error': ret[1]},
204             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
205     return Response(data=ret[1], status=status.HTTP_202_ACCEPTED)
206
207
208 @api_view(http_method_names=['POST'])
209 def vnf_model_parser(request, *args, **kwargs):
210     csar_id = ignore_case_get(request.data, "csarId")
211     inputs = ignore_case_get(request.data, "inputs")
212     logger.debug(
213         "Enter %s, csar_id=%s, inputs=%s",
214         fun_name(),
215         csar_id,
216         inputs)
217     ret = nf_package.parse_vnfd(csar_id, inputs)
218     logger.info("Leave %s, Return value is %s", fun_name(), ret)
219     if ret[0] != 0:
220         return Response(
221             data={
222                 'error': ret[1]},
223             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
224     return Response(data=ret[1], status=status.HTTP_202_ACCEPTED)
225
226
227 def handleValidatonError(base_serializer, is_request):
228     errormessage = base_serializer.errors
229     logger.error(errormessage)
230
231     if is_request:
232         message = 'Invalid request'
233     else:
234         message = 'Invalid response'
235     logger.error(message)
236
237     return Response(data={'error': errormessage},
238                     status=status.HTTP_500_INTERNAL_SERVER_ERROR)