08066be197163edeb630601c76fb1344cb834d2f
[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 from .common import view_safe_call_with_log
27
28 logger = logging.getLogger(__name__)
29
30
31 class InstantiateNsView(APIView):
32     @swagger_auto_schema(
33         request_body=InstantNsReqSerializer(),
34         responses={
35             status.HTTP_202_ACCEPTED: "HTTP_202_ACCEPTED",
36             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
37         }
38     )
39     @view_safe_call_with_log(logger=logger)
40     def post(self, request, ns_instance_id):
41         logger.debug("Enter InstantiateNsView::post::ns_instance_id=%s", ns_instance_id)
42         logger.debug("request.data=%s", request.data)
43
44         req_serializer = InstantNsReqSerializer(data=request.data)
45         if not req_serializer.is_valid():
46             logger.debug("request.data is not valid,error: %s" % req_serializer.errors)
47             raise BadRequestException(req_serializer.errors)
48
49         InstantNsReq = request.data
50         if "additionalParamsForVnf" in InstantNsReq:
51             InstantNsReq['locationConstraints'] = []
52             for additionalParamsForVnf in InstantNsReq["additionalParamsForVnf"]:
53                 vnf = {}
54                 vnf['vnfProfileId'] = additionalParamsForVnf['vnfProfileId']
55                 vnf['locationConstraints'] = {'vimId': additionalParamsForVnf['additionalParams']['vimId']}
56                 vnf['additionalParams'] = additionalParamsForVnf['additionalParams']
57                 InstantNsReq['locationConstraints'].append(vnf)
58
59         ack = InstantNSService(ns_instance_id, request.data).do_biz()
60         nsLcmOpOccId = ack.get('occ_id')
61         if not nsLcmOpOccId:
62             return Response(data=ack['data'], status=ack['status'])
63         response = Response(data=ack['data'], status=status.HTTP_202_ACCEPTED)
64         logger.debug("Location: %s" % ack['occ_id'])
65         response["Location"] = NS_OCC_BASE_URI % nsLcmOpOccId
66         logger.debug("Leave InstantiateNsView::post::ack=%s", ack)
67         return response