1 # Copyright 2017 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.
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
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
36 logger = logging.getLogger(__name__)
39 class CreateVnfAndQueryVnfs(APIView):
42 status.HTTP_200_OK: VnfInstancesSerializer(),
43 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
46 @view_safe_call_with_log(logger=logger)
47 def get(self, request):
48 logger.debug("QueryMultiVnf--get::> %s" % request.data)
49 resp_data = QueryVnf(request.data).query_multi_vnf()
50 if len(resp_data) == 0:
51 return Response(data=[], status=status.HTTP_200_OK)
52 vnf_instances_serializer = VnfInstancesSerializer(data=resp_data)
53 if not vnf_instances_serializer.is_valid():
54 raise NFLCMException(vnf_instances_serializer.errors)
56 return Response(data=vnf_instances_serializer.data, status=status.HTTP_200_OK)
59 request_body=CreateVnfReqSerializer(),
61 status.HTTP_201_CREATED: VnfInstanceSerializer(),
62 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
65 @view_safe_call_with_log(logger=logger)
66 def post(self, request):
67 logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
68 req_serializer = CreateVnfReqSerializer(data=request.data)
69 if not req_serializer.is_valid():
70 raise NFLCMException(req_serializer.errors)
72 nf_inst = CreateVnf(request.data).do_biz()
73 create_vnf_resp_serializer = VnfInstanceSerializer(
74 data={"id": nf_inst.nfinstid,
75 "vnfProvider": nf_inst.vendor,
76 "vnfdVersion": nf_inst.version,
77 "vnfPkgId": nf_inst.package_id,
78 "instantiationState": nf_inst.status})
79 if not create_vnf_resp_serializer.is_valid():
80 raise NFLCMException(create_vnf_resp_serializer.errors)
81 return Response(data=create_vnf_resp_serializer.data, status=status.HTTP_201_CREATED)
84 class DeleteVnfAndQueryVnf(APIView):
87 status.HTTP_200_OK: VnfInstanceSerializer(),
88 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
91 @view_safe_call_with_log(logger=logger)
92 def get(self, request, instanceid):
93 logger.debug("QuerySingleVnf--get::> %s" % request.data)
94 resp_data = QueryVnf(request.data, instanceid).query_single_vnf()
96 vnfs_instance_serializer = VnfInstanceSerializer(data=resp_data)
97 if not vnfs_instance_serializer.is_valid():
98 raise NFLCMException(vnfs_instance_serializer.errors)
100 return Response(data=vnfs_instance_serializer.data, status=status.HTTP_200_OK)
102 @swagger_auto_schema(
104 status.HTTP_204_NO_CONTENT: "Successfully",
105 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
108 @view_safe_call_with_log(logger=logger)
109 def delete(self, request, instanceid):
110 logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
112 DeleteVnf(request.data, instanceid).do_biz()
114 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
116 @swagger_auto_schema(
117 request_body=VnfInfoModificationsSerializer(),
119 status.HTTP_202_ACCEPTED: "Successfully",
120 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
123 @view_safe_call_with_log(logger=logger)
124 def patch(self, request, instanceid):
125 return deal_vnf_action(
127 opt_type=OPERATION_TYPE.MODIFY_INFO,
128 opt_status=VNF_STATUS.UPDATING,
131 req_serializer=VnfInfoModificationsSerializer,