e03aee00376c12d70f2fcfcc35ed768425df686d
[vfc/nfvo/lcm.git] / lcm / ns / views / sol / ns_instances_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
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.ns.serializers.sol.create_ns_serializers import CreateNsRequestSerializer
23 from lcm.ns.serializers.sol.ns_instance import NsInstanceSerializer
24 from lcm.pub.exceptions import BadRequestException, NSLCMException
25 from lcm.pub.utils.values import ignore_case_get
26 from lcm.ns.biz.ns_create import CreateNSService
27 from lcm.ns.biz.ns_get import GetNSInfoService
28 from lcm.ns.biz.ns_delete import DeleteNsService
29 from lcm.ns.const import NS_INSTANCE_BASE_URI
30 from .common import view_safe_call_with_log
31
32 logger = logging.getLogger(__name__)
33
34
35 class NSInstancesView(APIView):
36     @swagger_auto_schema(
37         request_body=None,
38         responses={
39             status.HTTP_200_OK: NsInstanceSerializer(help_text="NS instances", many=True),
40             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
41         }
42     )
43     @view_safe_call_with_log(logger=logger)
44     def get(self, request):
45         logger.debug(request.query_params)
46
47         logger.debug("CreateNSView::get")
48         ret = GetNSInfoService().get_ns_info(is_sol=True)  # todo
49         logger.debug("CreateNSView::get::ret=%s", ret)
50         resp_serializer = NsInstanceSerializer(data=ret, many=True)
51         if not resp_serializer.is_valid():
52             raise NSLCMException(resp_serializer.errors)
53         return Response(data=ret, status=status.HTTP_200_OK)
54
55     @swagger_auto_schema(
56         request_body=CreateNsRequestSerializer(),
57         responses={
58             status.HTTP_201_CREATED: NsInstanceSerializer(),
59             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
60         }
61     )
62     @view_safe_call_with_log(logger=logger)
63     def post(self, request):
64         logger.debug("Enter NSInstancesView::POST ns_instances: Header:%s, Body: %s" % (request.META, request.data))
65
66         globalCustomerId = request.META.get("HTTP_GLOBALCUSTOMERID", None)
67         if not globalCustomerId:
68             raise BadRequestException("Not found globalCustomerId in header")
69         serviceType = request.META.get("HTTP_SERVICETYPE", None)
70         if not serviceType:
71             serviceType = "NetworkService"
72         req_serializer = CreateNsRequestSerializer(data=request.data)
73         if not req_serializer.is_valid():
74             raise BadRequestException(req_serializer.errors)
75         if ignore_case_get(request.data, 'test') == "test":
76             return Response(data={'nsInstanceId': "test"}, status=status.HTTP_201_CREATED)
77         csar_id = ignore_case_get(request.data, 'nsdId')
78         ns_name = ignore_case_get(request.data, 'nsName')
79         description = ignore_case_get(request.data, 'nsDescription')
80         context = {
81             "globalCustomerId": globalCustomerId,
82             "serviceType": serviceType
83         }
84         ns_inst_id = CreateNSService(csar_id, ns_name, description, context).do_biz()
85         logger.debug("CreateNSView::post::ret={'nsInstanceId':%s}", ns_inst_id)
86         ns_filter = {"ns_inst_id": ns_inst_id}
87         nsInstance = GetNSInfoService(ns_filter).get_ns_info(is_sol=True)[0]
88         logger.debug("nsInstance: %s" % nsInstance)
89         resp_serializer = NsInstanceSerializer(data=nsInstance)
90         if not resp_serializer.is_valid():
91             raise NSLCMException(resp_serializer.errors)
92         response = Response(data=nsInstance, status=status.HTTP_201_CREATED)
93         response["Location"] = NS_INSTANCE_BASE_URI % nsInstance['id']
94         return response
95
96
97 class IndividualNsInstanceView(APIView):
98     @swagger_auto_schema(
99         request_body=None,
100         responses={
101             status.HTTP_200_OK: NsInstanceSerializer(help_text="NS instances", many=False),
102             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
103         }
104     )
105     @view_safe_call_with_log(logger=logger)
106     def get(self, request, ns_instance_id):
107
108         logger.debug("Enter NSDetailView::get ns(%s)", ns_instance_id)
109         ns_filter = {"ns_inst_id": ns_instance_id}
110         ret = GetNSInfoService(ns_filter).get_ns_info(is_sol=True)
111         if not ret:
112             data = {'status': status.HTTP_404_NOT_FOUND, 'detail': "NS Instance ID(%s) is not founded" % ns_instance_id}
113             return Response(data=data, status=status.HTTP_404_NOT_FOUND)
114         logger.debug("Leave NSDetailView::get::ret=%s", ret)
115         resp_serializer = NsInstanceSerializer(data=ret[0])
116         if not resp_serializer.is_valid():
117             raise NSLCMException(resp_serializer.errors)
118         return Response(data=ret[0], status=status.HTTP_200_OK)
119
120     @swagger_auto_schema(
121         request_body=None,
122         responses={
123             status.HTTP_204_NO_CONTENT: "HTTP_204_NO_CONTENT"
124         }
125     )
126     @view_safe_call_with_log(logger=logger)
127     def delete(self, request, ns_instance_id):
128
129         logger.debug("Enter NSDetailView::delete ns(%s)", ns_instance_id)
130         DeleteNsService(ns_instance_id).do_biz()
131         return Response(data={}, status=status.HTTP_204_NO_CONTENT)