fix swagger error
[vfc/nfvo/lcm.git] / lcm / ns / views / sol / instantiate_ns_views.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 logging
16 from drf_yasg.utils import swagger_auto_schema
17 from rest_framework import status
18 from rest_framework.response import Response
19 from rest_framework.views import APIView
20
21 from lcm.ns.biz.ns_instant import InstantNSService
22 from lcm.ns.serializers.sol.inst_ns_serializers import InstantNsReqSerializer
23 from lcm.pub.exceptions import BadRequestException
24 from lcm.ns.const import NS_OCC_BASE_URI
25 from lcm.ns.serializers.sol.pub_serializers import ProblemDetailsSerializer
26
27 logger = logging.getLogger(__name__)
28
29
30 class InstantiateNsView(APIView):
31     @swagger_auto_schema(
32         request_body=InstantNsReqSerializer(),
33         responses={
34             status.HTTP_202_ACCEPTED: "HTTP_202_ACCEPTED",
35             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
36         }
37     )
38     def post(self, request, ns_instance_id):
39         logger.debug("Enter InstantiateNsView::post::ns_instance_id=%s", ns_instance_id)
40         logger.debug("request.data=%s", request.data)
41         try:
42             req_serializer = InstantNsReqSerializer(data=request.data)
43             if not req_serializer.is_valid():
44                 logger.debug("request.data is not valid,error: %s" % req_serializer.errors)
45                 raise BadRequestException(req_serializer.errors)
46
47             InstantNsReq = request.data
48             if "additionalParamsForVnf" in InstantNsReq:
49                 InstantNsReq['locationConstraints'] = []
50                 for additionalParamsForVnf in InstantNsReq["additionalParamsForVnf"]:
51                     vnf = {}
52                     vnf['vnfProfileId'] = additionalParamsForVnf['vnfProfileId']
53                     vnf['locationConstraints'] = {'vimId': additionalParamsForVnf['additionalParams']['vimId']}
54                     vnf['additionalParams'] = additionalParamsForVnf['additionalParams']
55                     InstantNsReq['locationConstraints'].append(vnf)
56
57             ack = InstantNSService(ns_instance_id, request.data).do_biz()
58             nsLcmOpOccId = ack['occ_id']
59             response = Response(data={}, status=status.HTTP_202_ACCEPTED)
60             logger.debug("Location: %s" % ack['occ_id'])
61             response["Location"] = NS_OCC_BASE_URI % nsLcmOpOccId
62             logger.debug("Leave InstantiateNsView::post::ack=%s", ack)
63             return response
64         except BadRequestException as e:
65             logger.error("Exception in InstantiateNsView: %s", e.message)
66             data = {'status': status.HTTP_400_BAD_REQUEST, 'detail': e.message}
67             return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
68         except Exception as e:
69             logger.error("Exception in InstantiateNsView: %s", e.message)
70             data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
71             return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)