1be6a5de9bfb49697a44d09bb9b92500d8e7e8f0
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / views / common.py
1 # Copyright 2019 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 lcm.pub.exceptions import NFLCMException
22 from lcm.pub.exceptions import NFLCMExceptionPreconditionFailed
23 from lcm.pub.exceptions import NFLCMExceptionBadRequest
24 from lcm.pub.exceptions import NFLCMExceptionNotFound
25 from lcm.pub.exceptions import NFLCMExceptionConflict
26 from lcm.pub.exceptions import NFLCMExceptionSeeOther
27
28 logger = logging.getLogger(__name__)
29
30
31 def make_error_resp(status, detail):
32     return Response(
33         data={
34             'status': status,
35             'detail': detail
36         },
37         status=status
38     )
39
40
41 def view_safe_call_with_log(logger):
42     def view_safe_call(func):
43         def wrapper(*args, **kwargs):
44             try:
45                 return func(*args, **kwargs)
46             except NFLCMExceptionSeeOther as e:
47                 logger.error(e.message)
48                 resp = Response(status=status.HTTP_303_SEE_OTHER)
49                 resp["Location"] = ""
50                 # resp["Location"] = "subscriptions/%s" % e.id
51                 return resp
52             except NFLCMExceptionNotFound as e:
53                 logger.error(e.message)
54                 return make_error_resp(
55                     detail=e.message,
56                     status=status.HTTP_404_NOT_FOUND
57                 )
58             except NFLCMExceptionBadRequest as e:
59                 logger.error(e.message)
60                 return make_error_resp(
61                     detail=e.message,
62                     status=status.HTTP_400_BAD_REQUEST
63                 )
64             except NFLCMExceptionConflict as e:
65                 logger.error(e.message)
66                 return make_error_resp(
67                     detail=e.message,
68                     status=status.HTTP_409_CONFLICT
69                 )
70             except NFLCMExceptionPreconditionFailed as e:
71                 logger.error(e.message)
72                 return make_error_resp(
73                     detail=e.message,
74                     status=status.HTTP_412_PRECONDITION_FAILED
75                 )
76             except NFLCMException as e:
77                 logger.error(e.message)
78                 return make_error_resp(
79                     detail=e.message,
80                     status=status.HTTP_500_INTERNAL_SERVER_ERROR
81                 )
82             except Exception as e:
83                 logger.error(e.message)
84                 logger.error(traceback.format_exc())
85                 return make_error_resp(
86                     detail='Unexpected exception',
87                     status=status.HTTP_500_INTERNAL_SERVER_ERROR
88                 )
89         return wrapper
90     return view_safe_call