fix bug for ETSI Catalog Manager needs to raise 303 exception for the same callback_u...
[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, SubscriptionDoesNotExistsException
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                 resp = Response(status=status.HTTP_303_SEE_OTHER, headers={'Location': e.args[0]})
72                 # resp["Location"] = e.args[0]
73                 return resp
74             except VnfPkgDuplicateSubscriptionException as e:
75                 logger.error(e.args[0])
76                 resp = Response(status=status.HTTP_303_SEE_OTHER, headers={'Location': e.args[0]})
77                 # resp["Location"] = e.args[0]
78                 return resp
79             except PackageNotFoundException as e:
80                 logger.error(e.args[0])
81                 return make_error_resp(
82                     detail=e.args[0],
83                     status=status.HTTP_404_NOT_FOUND
84                 )
85             except ResourceNotFoundException as e:
86                 logger.error(e.args[0])
87                 return make_error_resp(
88                     detail=e.args[0],
89                     status=status.HTTP_404_NOT_FOUND
90                 )
91             except ArtifactNotFoundException as e:
92                 logger.error(e.args[0])
93                 return make_error_resp(
94                     detail=e.args[0],
95                     status=status.HTTP_404_NOT_FOUND
96                 )
97             except BadRequestException as e:
98                 logger.error(e.args[0])
99                 return make_error_resp(
100                     detail=e.args[0],
101                     status=status.HTTP_400_BAD_REQUEST
102                 )
103             except NsdmBadRequestException as e:
104                 logger.error(e.args[0])
105                 return make_error_resp(
106                     detail=e.args[0],
107                     status=status.HTTP_400_BAD_REQUEST
108                 )
109             except SubscriptionDoesNotExistsException as e:
110                 logger.error(e.args[0])
111                 return make_error_resp(
112                     detail=e.args[0],
113                     status=status.HTTP_404_NOT_FOUND
114                 )
115             except VnfPkgSubscriptionException as e:
116                 logger.error(e.args[0])
117                 return make_error_resp(
118                     detail=e.args[0],
119                     status=status.HTTP_500_INTERNAL_SERVER_ERROR
120                 )
121             except CatalogException as e:
122                 logger.error(e.args[0])
123                 return make_error_resp(
124                     detail=e.args[0],
125                     status=status.HTTP_500_INTERNAL_SERVER_ERROR
126                 )
127             except Exception as e:
128                 logger.error(e.args[0])
129                 logger.error(traceback.format_exc())
130                 return make_error_resp(
131                     detail='Unexpected exception',
132                     status=status.HTTP_500_INTERNAL_SERVER_ERROR
133                 )
134
135         return wrapper
136
137     return view_safe_call