refactor codes for update vnf
[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 from lcm.pub.database.models import NfInstModel
28 from lcm.pub.utils.jobutil import JobUtil
29 from lcm.nf.const import OPERATION_TYPE
30
31 logger = logging.getLogger(__name__)
32
33
34 def make_error_resp(status, detail):
35     return Response(
36         data={
37             'status': status,
38             'detail': detail
39         },
40         status=status
41     )
42
43
44 def view_safe_call_with_log(logger):
45     def view_safe_call(func):
46         def wrapper(*args, **kwargs):
47             try:
48                 return func(*args, **kwargs)
49             except NFLCMExceptionSeeOther as e:
50                 logger.error(e.message)
51                 resp = Response(status=status.HTTP_303_SEE_OTHER)
52                 resp["Location"] = ""
53                 # resp["Location"] = "subscriptions/%s" % e.id
54                 return resp
55             except NFLCMExceptionNotFound as e:
56                 logger.error(e.message)
57                 return make_error_resp(
58                     detail=e.message,
59                     status=status.HTTP_404_NOT_FOUND
60                 )
61             except NFLCMExceptionBadRequest as e:
62                 logger.error(e.message)
63                 return make_error_resp(
64                     detail=e.message,
65                     status=status.HTTP_400_BAD_REQUEST
66                 )
67             except NFLCMExceptionConflict as e:
68                 logger.error(e.message)
69                 return make_error_resp(
70                     detail=e.message,
71                     status=status.HTTP_409_CONFLICT
72                 )
73             except NFLCMExceptionPreconditionFailed as e:
74                 logger.error(e.message)
75                 return make_error_resp(
76                     detail=e.message,
77                     status=status.HTTP_412_PRECONDITION_FAILED
78                 )
79             except NFLCMException as e:
80                 logger.error(e.message)
81                 return make_error_resp(
82                     detail=e.message,
83                     status=status.HTTP_500_INTERNAL_SERVER_ERROR
84                 )
85             except Exception as e:
86                 logger.error(e.message)
87                 logger.error(traceback.format_exc())
88                 return make_error_resp(
89                     detail='Unexpected exception',
90                     status=status.HTTP_500_INTERNAL_SERVER_ERROR
91                 )
92         return wrapper
93     return view_safe_call
94
95
96 def deal_vnf_action(logger, opt_type, opt_status, instid, req, req_serializer, act_task):
97     logger.debug("%s--post::> %s, %s", opt_type, instid, req.data)
98
99     act_vnf_req_serializer = req_serializer(data=req.data)
100     if not act_vnf_req_serializer.is_valid():
101         raise NFLCMException(act_vnf_req_serializer.errors)
102
103     vnf_insts = NfInstModel.objects.filter(nfinstid=instid)
104     if not vnf_insts.exists():
105         raise NFLCMExceptionNotFound("VNF(%s) does not exist." % instid)
106
107     if opt_type == OPERATION_TYPE.INSTANTIATE:
108         if vnf_insts[0].status == 'INSTANTIATED':
109             raise NFLCMExceptionConflict("VNF(%s) is already INSTANTIATED." % instid)
110     elif opt_type != OPERATION_TYPE.MODIFY_INFO:
111         if vnf_insts[0].status != 'INSTANTIATED':
112             raise NFLCMExceptionConflict("VNF(%s) is not INSTANTIATED." % instid)
113
114     job_id = JobUtil.create_job('NF', opt_type, instid)
115     JobUtil.add_job_status(job_id, 0, "VNF_%s_READY" % opt_type)
116
117     vnf_insts.update(status=opt_status)
118     act_task(req.data, instid, job_id).start()
119
120     return Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)