Swagger issue fixes from the Ericsson team
[modeling/etsicatalog.git] / catalog / packages / views / common.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 traceback
16 import logging
17
18 from rest_framework import status
19 from rest_framework.response import Response
20
21 from catalog.pub.exceptions import CatalogException
22 from catalog.pub.exceptions import BadRequestException
23 from catalog.pub.exceptions import NsdmBadRequestException
24 from catalog.pub.exceptions import PackageNotFoundException
25 from catalog.pub.exceptions import ResourceNotFoundException
26 from catalog.pub.exceptions import ArtifactNotFoundException
27 from catalog.pub.exceptions import NsdmDuplicateSubscriptionException
28 from catalog.pub.exceptions import VnfPkgDuplicateSubscriptionException
29 from catalog.pub.exceptions import VnfPkgSubscriptionException
30
31 logger = logging.getLogger(__name__)
32
33
34 def validate_data(data, serializer):
35     serialized_data = serializer(data=data)
36     if not serialized_data.is_valid():
37         logger.error('Data validation failed.')
38         raise CatalogException(serialized_data.errors)
39     return serialized_data
40
41
42 def validate_req_data(data, serializer):
43     serialized_data = serializer(data=data)
44     if not serialized_data.is_valid():
45         logger.error('Data validation failed.')
46         raise BadRequestException(serialized_data.errors)
47     return serialized_data
48
49
50 def fmt_error_rsp(error_message, status):
51     return {"errorMessage": error_message, "error": status}
52
53
54 def make_error_resp(status, detail):
55     return Response(
56         data={
57             'status': status,
58             'detail': detail
59         },
60         status=status
61     )
62
63
64 def view_safe_call_with_log(logger):
65     def view_safe_call(func):
66         def wrapper(*args, **kwargs):
67             try:
68                 return func(*args, **kwargs)
69             except NsdmDuplicateSubscriptionException as e:
70                 logger.error(e.args[0])
71                 return make_error_resp(
72                     detail=e.args[0],
73                     status=status.HTTP_303_SEE_OTHER
74                 )
75             except VnfPkgDuplicateSubscriptionException as e:
76                 logger.error(e.args[0])
77                 return make_error_resp(
78                     detail=e.args[0],
79                     status=status.HTTP_303_SEE_OTHER
80                 )
81             except PackageNotFoundException as e:
82                 logger.error(e.args[0])
83                 return make_error_resp(
84                     detail=e.args[0],
85                     status=status.HTTP_404_NOT_FOUND
86                 )
87             except ResourceNotFoundException as e:
88                 logger.error(e.args[0])
89                 return make_error_resp(
90                     detail=e.args[0],
91                     status=status.HTTP_404_NOT_FOUND
92                 )
93             except ArtifactNotFoundException as e:
94                 logger.error(e.args[0])
95                 return make_error_resp(
96                     detail=e.args[0],
97                     status=status.HTTP_404_NOT_FOUND
98                 )
99             except BadRequestException as e:
100                 logger.error(e.args[0])
101                 return make_error_resp(
102                     detail=e.args[0],
103                     status=status.HTTP_400_BAD_REQUEST
104                 )
105             except NsdmBadRequestException as e:
106                 logger.error(e.args[0])
107                 return make_error_resp(
108                     detail=e.args[0],
109                     status=status.HTTP_400_BAD_REQUEST
110                 )
111             except VnfPkgSubscriptionException as e:
112                 logger.error(e.args[0])
113                 return make_error_resp(
114                     detail=e.args[0],
115                     status=status.HTTP_500_INTERNAL_SERVER_ERROR
116                 )
117             except CatalogException as e:
118                 logger.error(e.args[0])
119                 return make_error_resp(
120                     detail=e.args[0],
121                     status=status.HTTP_500_INTERNAL_SERVER_ERROR
122                 )
123             except Exception as e:
124                 logger.error(e.args[0])
125                 logger.error(traceback.format_exc())
126                 return make_error_resp(
127                     detail='Unexpected exception',
128                     status=status.HTTP_500_INTERNAL_SERVER_ERROR
129                 )
130         return wrapper
131     return view_safe_call