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 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
50 def fmt_error_rsp(error_message, status):
51 return {"errorMessage": error_message, "error": status}
54 def make_error_resp(status, detail):
64 def view_safe_call_with_log(logger):
65 def view_safe_call(func):
66 def wrapper(*args, **kwargs):
68 return func(*args, **kwargs)
69 except NsdmDuplicateSubscriptionException as e:
70 logger.error(e.args[0])
71 return make_error_resp(
73 status=status.HTTP_303_SEE_OTHER
75 except VnfPkgDuplicateSubscriptionException as e:
76 logger.error(e.args[0])
77 return make_error_resp(
79 status=status.HTTP_303_SEE_OTHER
81 except PackageNotFoundException as e:
82 logger.error(e.args[0])
83 return make_error_resp(
85 status=status.HTTP_404_NOT_FOUND
87 except ResourceNotFoundException as e:
88 logger.error(e.args[0])
89 return make_error_resp(
91 status=status.HTTP_404_NOT_FOUND
93 except ArtifactNotFoundException as e:
94 logger.error(e.args[0])
95 return make_error_resp(
97 status=status.HTTP_404_NOT_FOUND
99 except BadRequestException as e:
100 logger.error(e.args[0])
101 return make_error_resp(
103 status=status.HTTP_400_BAD_REQUEST
105 except NsdmBadRequestException as e:
106 logger.error(e.args[0])
107 return make_error_resp(
109 status=status.HTTP_400_BAD_REQUEST
111 except VnfPkgSubscriptionException as e:
112 logger.error(e.args[0])
113 return make_error_resp(
115 status=status.HTTP_500_INTERNAL_SERVER_ERROR
117 except CatalogException as e:
118 logger.error(e.args[0])
119 return make_error_resp(
121 status=status.HTTP_500_INTERNAL_SERVER_ERROR
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
131 return view_safe_call