64fe002929989f7e136607816fee5d423bafd2b9
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / views / curd_vnf_views.py
1 # Copyright 2017 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 logging
16
17 from drf_yasg.utils import swagger_auto_schema
18 from lcm.nf.biz.delete_vnf import DeleteVnf
19 from rest_framework import status
20 from rest_framework.response import Response
21 from rest_framework.views import APIView
22
23 from lcm.nf.biz.create_vnf import CreateVnf
24 from lcm.nf.biz.query_vnf import QueryVnf
25 from lcm.nf.biz.update_vnf import UpdateVnf
26 from lcm.nf.serializers.create_vnf_req import CreateVnfReqSerializer
27 from lcm.nf.serializers.vnf_instance import VnfInstanceSerializer
28 from lcm.nf.serializers.vnf_instances import VnfInstancesSerializer
29 from lcm.nf.serializers.vnf_info_modifications import VnfInfoModificationsSerializer
30 from lcm.pub.utils.jobutil import JobUtil
31 from lcm.pub.exceptions import NFLCMException
32 from lcm.pub.exceptions import NFLCMExceptionNotFound
33 from lcm.pub.database.models import NfInstModel
34 from lcm.nf.const import VNF_STATUS
35 from .common import view_safe_call_with_log
36
37 logger = logging.getLogger(__name__)
38
39
40 class CreateVnfAndQueryVnfs(APIView):
41     @swagger_auto_schema(
42         responses={
43             status.HTTP_200_OK: VnfInstancesSerializer(),
44             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
45         }
46     )
47     @view_safe_call_with_log(logger=logger)
48     def get(self, request):
49         logger.debug("QueryMultiVnf--get::> %s" % request.data)
50         resp_data = QueryVnf(request.data).query_multi_vnf()
51         if len(resp_data) == 0:
52             return Response(data=[], status=status.HTTP_200_OK)
53         vnf_instances_serializer = VnfInstancesSerializer(data=resp_data)
54         if not vnf_instances_serializer.is_valid():
55             raise NFLCMException(vnf_instances_serializer.errors)
56
57         return Response(data=vnf_instances_serializer.data, status=status.HTTP_200_OK)
58
59     @swagger_auto_schema(
60         request_body=CreateVnfReqSerializer(),
61         responses={
62             status.HTTP_201_CREATED: VnfInstanceSerializer(),
63             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
64         }
65     )
66     @view_safe_call_with_log(logger=logger)
67     def post(self, request):
68         logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
69         req_serializer = CreateVnfReqSerializer(data=request.data)
70         if not req_serializer.is_valid():
71             raise NFLCMException(req_serializer.errors)
72
73         nf_inst = CreateVnf(request.data).do_biz()
74         create_vnf_resp_serializer = VnfInstanceSerializer(
75             data={"id": nf_inst.nfinstid,
76                   "vnfProvider": nf_inst.vendor,
77                   "vnfdVersion": nf_inst.version,
78                   "vnfPkgId": nf_inst.package_id,
79                   "instantiationState": nf_inst.status})
80         if not create_vnf_resp_serializer.is_valid():
81             raise NFLCMException(create_vnf_resp_serializer.errors)
82         return Response(data=create_vnf_resp_serializer.data, status=status.HTTP_201_CREATED)
83
84
85 class DeleteVnfAndQueryVnf(APIView):
86     @swagger_auto_schema(
87         responses={
88             status.HTTP_200_OK: VnfInstanceSerializer(),
89             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
90         }
91     )
92     @view_safe_call_with_log(logger=logger)
93     def get(self, request, instanceid):
94         logger.debug("QuerySingleVnf--get::> %s" % request.data)
95         resp_data = QueryVnf(request.data, instanceid).query_single_vnf()
96
97         vnfs_instance_serializer = VnfInstanceSerializer(data=resp_data)
98         if not vnfs_instance_serializer.is_valid():
99             raise NFLCMException(vnfs_instance_serializer.errors)
100
101         return Response(data=vnfs_instance_serializer.data, status=status.HTTP_200_OK)
102
103     @swagger_auto_schema(
104         responses={
105             status.HTTP_204_NO_CONTENT: "Successfully",
106             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
107         }
108     )
109     @view_safe_call_with_log(logger=logger)
110     def delete(self, request, instanceid):
111         logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
112
113         DeleteVnf(request.data, instanceid).do_biz()
114
115         return Response(data=None, status=status.HTTP_204_NO_CONTENT)
116
117     @swagger_auto_schema(
118         request_body=VnfInfoModificationsSerializer(),
119         responses={
120             status.HTTP_202_ACCEPTED: "Successfully",
121             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
122         }
123     )
124     @view_safe_call_with_log(logger=logger)
125     def patch(self, request, instanceid):
126         logger.debug("UpdateSingleVnf--patch::> %s, %s", instanceid, request.data)
127
128         upd_vnf_serializer = VnfInfoModificationsSerializer(data=request.data)
129         if not upd_vnf_serializer.is_valid():
130             raise NFLCMException(upd_vnf_serializer.errors)
131
132         job_id = JobUtil.create_job('NF', 'UPDATE', instanceid)
133         JobUtil.add_job_status(job_id, 0, "UPDATE_VNF_READY")
134
135         vnf_insts = NfInstModel.objects.filter(nfinstid=instanceid)
136         if not vnf_insts.exists():
137             raise NFLCMExceptionNotFound("VNF(%s) does not exist." % instanceid)
138         vnf_insts.update(status=VNF_STATUS.UPDATING)
139
140         JobUtil.add_job_status(job_id, 15, 'Nf updating pre-check finish')
141         UpdateVnf(request.data, instanceid, job_id).start()
142
143         return Response(data=None, status=status.HTTP_202_ACCEPTED)