1 # Copyright 2018 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from rest_framework import status
19 from rest_framework.response import Response
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
31 logger = logging.getLogger(__name__)
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
42 def fmt_error_rsp(error_message, status):
43 return {"errorMessage": error_message, "error": status}
46 def make_error_resp(status, detail):
56 def view_safe_call_with_log(logger):
57 def view_safe_call(func):
58 def wrapper(*args, **kwargs):
60 return func(*args, **kwargs)
61 except NsdmDuplicateSubscriptionException as e:
62 logger.error(e.args[0])
63 return make_error_resp(
65 status=status.HTTP_303_SEE_OTHER
67 except VnfPkgDuplicateSubscriptionException as e:
68 logger.error(e.args[0])
69 return make_error_resp(
71 status=status.HTTP_303_SEE_OTHER
73 except PackageNotFoundException as e:
74 logger.error(e.args[0])
75 return make_error_resp(
77 status=status.HTTP_404_NOT_FOUND
79 except ResourceNotFoundException as e:
80 logger.error(e.args[0])
81 return make_error_resp(
83 status=status.HTTP_404_NOT_FOUND
85 except ArtifactNotFoundException as e:
86 logger.error(e.args[0])
87 return make_error_resp(
89 status=status.HTTP_404_NOT_FOUND
91 except BadRequestException as e:
92 logger.error(e.args[0])
93 return make_error_resp(
95 status=status.HTTP_400_BAD_REQUEST
97 except NsdmBadRequestException as e:
98 logger.error(e.args[0])
99 return make_error_resp(
101 status=status.HTTP_400_BAD_REQUEST
103 except VnfPkgSubscriptionException as e:
104 logger.error(e.args[0])
105 return make_error_resp(
107 status=status.HTTP_500_INTERNAL_SERVER_ERROR
109 except CatalogException as e:
110 logger.error(e.args[0])
111 return make_error_resp(
113 status=status.HTTP_500_INTERNAL_SERVER_ERROR
115 except Exception as e:
116 logger.error(e.args[0])
117 logger.error(traceback.format_exc())
118 return make_error_resp(
119 detail='Unexpected exception',
120 status=status.HTTP_500_INTERNAL_SERVER_ERROR
123 return view_safe_call