Code optimization
[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 rest_framework import status
19 from rest_framework.response import Response
20 from rest_framework.views import APIView
21
22 from lcm.nf.biz.create_vnf import CreateVnf
23 from lcm.nf.biz.query_vnf import QueryVnf
24 from lcm.nf.biz.update_vnf import UpdateVnf
25 from lcm.nf.biz.delete_vnf import DeleteVnf
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.exceptions import NFLCMException
31 from lcm.nf.const import VNF_STATUS
32 from lcm.nf.const import OPERATION_TYPE
33 from .common import view_safe_call_with_log
34 from .common import deal_vnf_action
35 from .common import deal_indivdual_query
36 from lcm.pub.exceptions import NFLCMExceptionBadRequest
37
38 logger = logging.getLogger(__name__)
39
40
41 class CreateVnfAndQueryVnfs(APIView):
42     @swagger_auto_schema(
43         responses={
44             status.HTTP_200_OK: VnfInstancesSerializer(),
45             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
46         }
47     )
48     @view_safe_call_with_log(logger=logger)
49     def get(self, request):
50         logger.debug("QueryMultiVnf--get::> %s" % request.data)
51         resp_data = QueryVnf(request.data).query_multi_vnf()
52         if len(resp_data) == 0:
53             return Response(data=[], status=status.HTTP_200_OK)
54         vnf_instances_serializer = VnfInstancesSerializer(data=resp_data)
55         if not vnf_instances_serializer.is_valid():
56             raise NFLCMException(vnf_instances_serializer.errors)
57
58         return Response(data=resp_data, status=status.HTTP_200_OK)
59
60     @swagger_auto_schema(
61         request_body=CreateVnfReqSerializer(),
62         responses={
63             status.HTTP_201_CREATED: VnfInstanceSerializer(),
64             status.HTTP_400_BAD_REQUEST: "Bad Request",
65             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
66         }
67     )
68     @view_safe_call_with_log(logger=logger)
69     def post(self, request):
70         logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
71         req_serializer = CreateVnfReqSerializer(data=request.data)
72         if not req_serializer.is_valid():
73             raise NFLCMExceptionBadRequest(req_serializer.errors)
74
75         nf_inst = CreateVnf(request.data).do_biz()
76         rsp_data = {"id": nf_inst.nfinstid,
77                     "vnfInstanceName": nf_inst.nf_name,
78                     "vnfdId": nf_inst.vnfdid,
79                     "vnfProvider": nf_inst.vendor,
80                     "vnfSoftwareVersion": nf_inst.vnfSoftwareVersion,
81                     "vnfdVersion": nf_inst.version,
82                     "vnfPkgId": nf_inst.package_id,
83                     "instantiationState": nf_inst.status,
84                     "vnfInstanceDescription": nf_inst.nf_desc
85                     }
86         create_vnf_resp_serializer = VnfInstanceSerializer(data=rsp_data)
87         if not create_vnf_resp_serializer.is_valid():
88             raise NFLCMException(create_vnf_resp_serializer.errors)
89
90         return Response(data=rsp_data, status=status.HTTP_201_CREATED)
91
92
93 class DeleteVnfAndQueryVnf(APIView):
94     @swagger_auto_schema(
95         responses={
96             status.HTTP_200_OK: VnfInstanceSerializer(),
97             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
98         }
99     )
100     @view_safe_call_with_log(logger=logger)
101     def get(self, request, instanceid):
102         logger.debug("QuerySingleVnf--get::> %s" % request.data)
103
104         return deal_indivdual_query(res_serializer=VnfInstanceSerializer,
105                                     query_fun=QueryVnf(
106                                         request.data,
107                                         instanceid).query_single_vnf)
108
109     @swagger_auto_schema(
110         responses={
111             status.HTTP_204_NO_CONTENT: "Successfully",
112             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
113         }
114     )
115     @view_safe_call_with_log(logger=logger)
116     def delete(self, request, instanceid):
117         logger.debug("DeleteVnfIdentifier--delete::> %s" % instanceid)
118
119         DeleteVnf(request.data, instanceid).do_biz()
120
121         return Response(data=None, status=status.HTTP_204_NO_CONTENT)
122
123     @swagger_auto_schema(
124         request_body=VnfInfoModificationsSerializer(),
125         responses={
126             status.HTTP_202_ACCEPTED: "Successfully",
127             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
128         }
129     )
130     @view_safe_call_with_log(logger=logger)
131     def patch(self, request, instanceid):
132         return deal_vnf_action(
133             logger=logger,
134             opt_type=OPERATION_TYPE.MODIFY_INFO,
135             opt_status=VNF_STATUS.UPDATING,
136             instid=instanceid,
137             req=request,
138             req_serializer=VnfInfoModificationsSerializer,
139             act_task=UpdateVnf
140         )