add comments
[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     """
33     This task resource represents the "Instantiate NS" operation. The client can use this resource to instantiate a NS
34     instance.
35     """
36     @swagger_auto_schema(
37         request_body=InstantNsReqSerializer(),
38         responses={
39             status.HTTP_202_ACCEPTED: "HTTP_202_ACCEPTED",
40             status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
41         }
42     )
43     @view_safe_call_with_log(logger=logger)
44     def post(self, request, ns_instance_id):
45         """
46         The POST method requests to instantiate a NS instance resource.
47         :param request:
48         :param ns_instance_id:
49         :return:
50         """
51         logger.debug("Enter InstantiateNsView::post::ns_instance_id=%s", ns_instance_id)
52         logger.debug("request.data=%s", request.data)
53
54         req_serializer = InstantNsReqSerializer(data=request.data)
55         if not req_serializer.is_valid():
56             logger.debug("request.data is not valid,error: %s" % req_serializer.errors)
57             raise BadRequestException(req_serializer.errors)
58
59         InstantNsReq = request.data
60         if "additionalParamsForVnf" in InstantNsReq:
61             InstantNsReq['locationConstraints'] = []
62             for additionalParamsForVnf in InstantNsReq["additionalParamsForVnf"]:
63                 vnf = {}
64                 vnf['vnfProfileId'] = additionalParamsForVnf['vnfProfileId']
65                 vnf['locationConstraints'] = {'vimId': additionalParamsForVnf['additionalParams']['vimId']}
66                 vnf['additionalParams'] = additionalParamsForVnf['additionalParams']
67                 InstantNsReq['locationConstraints'].append(vnf)
68
69         ack = InstantNSService(ns_instance_id, request.data).do_biz()
70         nsLcmOpOccId = ack.get('occ_id')
71         if not nsLcmOpOccId:
72             return Response(data=ack['data'], status=ack['status'])
73         response = Response(data=ack['data'], status=status.HTTP_202_ACCEPTED)
74         logger.debug("Location: %s" % ack['occ_id'])
75         response["Location"] = NS_OCC_BASE_URI % nsLcmOpOccId
76         logger.debug("Leave InstantiateNsView::post::ack=%s", ack)
77         return response