seperate sol and deprecated serializers
[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 import traceback
17
18 from drf_yasg.utils import swagger_auto_schema
19 from rest_framework import status
20 from rest_framework.response import Response
21 from rest_framework.views import APIView
22
23 from lcm.ns.serializers.sol.create_ns_serializers import CreateNsRequestSerializer
24 from lcm.ns.serializers.sol.ns_instance import NsInstanceSerializer
25 from lcm.pub.exceptions import BadRequestException, NSLCMException
26 from lcm.pub.utils.values import ignore_case_get
27 from lcm.ns.biz.ns_create import CreateNSService
28 from lcm.ns.biz.ns_get import GetNSInfoService
29
30 logger = logging.getLogger(__name__)
31
32
33 class NSInstancesView(APIView):
34     @swagger_auto_schema(
35         request_body=None,
36         responses={
37             status.HTTP_200_OK: NsInstanceSerializer(help_text="NS instances", many=True),
38             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
39         }
40     )
41     def get(self, request):
42         logger.debug(request.query_params)
43         try:
44             logger.debug("CreateNSView::get")
45             ret = GetNSInfoService().get_ns_info()  # todo
46             logger.debug("CreateNSView::get::ret=%s", ret)
47             resp_serializer = NsInstanceSerializer(data=ret, many=True)
48             if not resp_serializer.is_valid():
49                 raise NSLCMException(resp_serializer.errors)
50             return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
51         except Exception as e:
52             logger.error(traceback.format_exc())
53             logger.error("Exception in GetNS: %s", e.message)
54             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
55
56     @swagger_auto_schema(
57         request_body=CreateNsRequestSerializer(),
58         responses={
59             status.HTTP_201_CREATED: NsInstanceSerializer(),
60             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
61         }
62     )
63     def post(self, request):
64         logger.debug("Enter NSInstancesView::POST ns_instances: Header:%s, Body: %s" % (request.META, request.data))
65         try:
66             globalCustomerId = request.META.get("HTTP_GLOBALCUSTOMERID ", None)
67             if not globalCustomerId:
68                 raise BadRequestException("Not found globalCustomerId in header")
69             req_serializer = CreateNsRequestSerializer(data=request.data)
70             if not req_serializer.is_valid():
71                 raise BadRequestException(req_serializer.errors)
72
73             if ignore_case_get(request.data, 'test') == "test":
74                 return Response(data={'nsInstanceId': "test"}, status=status.HTTP_201_CREATED)
75             csar_id = ignore_case_get(request.data, 'nsdId')
76             ns_name = ignore_case_get(request.data, 'nsName')
77             description = ignore_case_get(request.data, 'description')
78             context = {
79                 "globalCustomerId": globalCustomerId,
80                 "serviceType": "NetworkService"
81             }
82             ns_inst_id = CreateNSService(csar_id, ns_name, description, context).do_biz()
83             logger.debug("CreateNSView::post::ret={'nsInstanceId':%s}", ns_inst_id)
84             ns_filter = {"ns_inst_id": ns_inst_id}
85             nsInstance = GetNSInfoService(ns_filter).get_ns_info()[0]  # todo
86             resp_serializer = NsInstanceSerializer(data=nsInstance)
87             if not resp_serializer.is_valid():
88                 raise NSLCMException(resp_serializer.errors)
89             return Response(data=resp_serializer.data, status=status.HTTP_201_CREATED)
90         except BadRequestException as e:
91             logger.error("Exception in CreateNS: %s", e.message)
92             data = {'status': status.HTTP_400_BAD_REQUEST, 'detail': e.message}
93             return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
94         except Exception as e:
95             logger.error("Exception in CreateNS: %s", e.message)
96             data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
97             return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
98
99
100 class IndividualNsInstanceView(APIView):
101     @swagger_auto_schema(
102         request_body=None,
103         responses={
104             status.HTTP_200_OK: NsInstanceSerializer(help_text="NS instances", many=True),
105             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
106         }
107     )
108     def get(self, request, ns_instance_id):
109         logger.debug("Enter IndividualNsInstanceView::get ns(%s)", ns_instance_id)
110         # todo
111         return Response(data={}, status=status.HTTP_200_OK)
112
113     @swagger_auto_schema(
114         request_body=None,
115         responses={
116             status.HTTP_204_NO_CONTENT: None
117         }
118     )
119     def delete(self, request, ns_instance_id):
120         logger.debug("Enter IndividualNsInstanceView::DELETE ns_instance(%s)", ns_instance_id)
121         # todo
122         return Response(data={}, status=status.HTTP_204_NO_CONTENT)