1 # Copyright 2019 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
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 from lcm.ns.biz.ns_delete import DeleteNsService
30 from lcm.ns.const import NS_INSTANCE_BASE_URI
32 logger = logging.getLogger(__name__)
35 class NSInstancesView(APIView):
39 status.HTTP_200_OK: NsInstanceSerializer(help_text="NS instances", many=True),
40 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
43 def get(self, request):
44 logger.debug(request.query_params)
46 logger.debug("CreateNSView::get")
47 ret = GetNSInfoService().get_ns_info(is_sol=True) # todo
48 logger.debug("CreateNSView::get::ret=%s", ret)
49 resp_serializer = NsInstanceSerializer(data=ret, many=True)
50 if not resp_serializer.is_valid():
51 raise NSLCMException(resp_serializer.errors)
52 return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
53 except Exception as e:
54 logger.error(traceback.format_exc())
55 logger.error("Exception in GetNS: %s", e.message)
56 data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
57 return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
60 request_body=CreateNsRequestSerializer(),
62 status.HTTP_201_CREATED: NsInstanceSerializer(),
63 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
66 def post(self, request):
67 logger.debug("Enter NSInstancesView::POST ns_instances: Header:%s, Body: %s" % (request.META, request.data))
69 globalCustomerId = request.META.get("HTTP_GLOBALCUSTOMERID", None)
70 if not globalCustomerId:
71 raise BadRequestException("Not found globalCustomerId in header")
72 req_serializer = CreateNsRequestSerializer(data=request.data)
73 if not req_serializer.is_valid():
74 raise BadRequestException(req_serializer.errors)
76 if ignore_case_get(request.data, 'test') == "test":
77 return Response(data={'nsInstanceId': "test"}, status=status.HTTP_201_CREATED)
78 csar_id = ignore_case_get(request.data, 'nsdId')
79 ns_name = ignore_case_get(request.data, 'nsName')
80 description = ignore_case_get(request.data, 'description')
82 "globalCustomerId": globalCustomerId,
83 "serviceType": "NetworkService"
85 ns_inst_id = CreateNSService(csar_id, ns_name, description, context).do_biz()
86 logger.debug("CreateNSView::post::ret={'nsInstanceId':%s}", ns_inst_id)
87 ns_filter = {"ns_inst_id": ns_inst_id}
88 nsInstance = GetNSInfoService(ns_filter).get_ns_info(is_sol=True)[0]
89 resp_serializer = NsInstanceSerializer(data=nsInstance)
90 if not resp_serializer.is_valid():
91 raise NSLCMException(resp_serializer.errors)
92 response = Response(data=resp_serializer.data, status=status.HTTP_201_CREATED)
93 response["Location"] = NS_INSTANCE_BASE_URI % nsInstance['id']
96 except BadRequestException as e:
97 logger.error("Exception in CreateNS: %s", e.message)
98 data = {'status': status.HTTP_400_BAD_REQUEST, 'detail': e.message}
99 return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
100 except Exception as e:
101 logger.error("Exception in CreateNS: %s", e.message)
102 data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
103 return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
106 class IndividualNsInstanceView(APIView):
107 @swagger_auto_schema(
110 status.HTTP_200_OK: NsInstanceSerializer(help_text="NS instances", many=False),
111 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
114 def get(self, request, ns_instance_id):
116 logger.debug("Enter NSDetailView::get ns(%s)", ns_instance_id)
117 ns_filter = {"ns_inst_id": ns_instance_id}
118 ret = GetNSInfoService(ns_filter).get_ns_info(is_sol=True)
120 data = {'status': status.HTTP_404_NOT_FOUND, 'detail': "NS Instance ID(%s) is not founded" % ns_instance_id}
121 return Response(data=data, status=status.HTTP_404_NOT_FOUND)
122 logger.debug("Leave NSDetailView::get::ret=%s", ret)
123 resp_serializer = NsInstanceSerializer(data=ret[0])
124 if not resp_serializer.is_valid():
125 raise NSLCMException(resp_serializer.errors)
126 return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
127 except Exception as e:
128 logger.error(traceback.format_exc())
129 logger.error("Exception in GetNSDetail: %s", e.message)
130 data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
131 return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
133 @swagger_auto_schema(
136 status.HTTP_204_NO_CONTENT: None
139 def delete(self, request, ns_instance_id):
141 logger.debug("Enter NSDetailView::delete ns(%s)", ns_instance_id)
142 DeleteNsService(ns_instance_id).do_biz()
143 return Response(data={}, status=status.HTTP_204_NO_CONTENT)
144 except Exception as e:
145 logger.error(traceback.format_exc())
146 logger.error("Exception in delete NS: %s", e.message)
147 data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
148 return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)