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, 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
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 resp = Response(status=status.HTTP_303_SEE_OTHER, headers={'Location': e.args[0]})
72 # resp["Location"] = e.args[0]
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]
79 except PackageNotFoundException as e:
80 logger.error(e.args[0])
81 return make_error_resp(
83 status=status.HTTP_404_NOT_FOUND
85 except ResourceNotFoundException as e:
86 logger.error(e.args[0])
87 return make_error_resp(
89 status=status.HTTP_404_NOT_FOUND
91 except ArtifactNotFoundException as e:
92 logger.error(e.args[0])
93 return make_error_resp(
95 status=status.HTTP_404_NOT_FOUND
97 except BadRequestException as e:
98 logger.error(e.args[0])
99 return make_error_resp(
101 status=status.HTTP_400_BAD_REQUEST
103 except NsdmBadRequestException as e:
104 logger.error(e.args[0])
105 return make_error_resp(
107 status=status.HTTP_400_BAD_REQUEST
109 except SubscriptionDoesNotExistsException as e:
110 logger.error(e.args[0])
111 return make_error_resp(
113 status=status.HTTP_404_NOT_FOUND
115 except VnfPkgSubscriptionException as e:
116 logger.error(e.args[0])
117 return make_error_resp(
119 status=status.HTTP_500_INTERNAL_SERVER_ERROR
121 except CatalogException as e:
122 logger.error(e.args[0])
123 return make_error_resp(
125 status=status.HTTP_500_INTERNAL_SERVER_ERROR
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
137 return view_safe_call