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
31 logger = logging.getLogger(__name__)
34 class NSInstancesView(APIView):
38 status.HTTP_200_OK: NsInstanceSerializer(help_text="NS instances", many=True),
39 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
42 def get(self, request):
43 logger.debug(request.query_params)
45 logger.debug("CreateNSView::get")
46 ret = GetNSInfoService().get_ns_info(is_sol=True) # todo
47 logger.debug("CreateNSView::get::ret=%s", ret)
48 resp_serializer = NsInstanceSerializer(data=ret, many=True)
49 if not resp_serializer.is_valid():
50 raise NSLCMException(resp_serializer.errors)
51 return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
52 except Exception as e:
53 logger.error(traceback.format_exc())
54 logger.error("Exception in GetNS: %s", e.message)
55 data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
56 return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
59 request_body=CreateNsRequestSerializer(),
61 status.HTTP_201_CREATED: NsInstanceSerializer(),
62 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
65 def post(self, request):
66 logger.debug("Enter NSInstancesView::POST ns_instances: Header:%s, Body: %s" % (request.META, request.data))
68 globalCustomerId = request.META.get("HTTP_GLOBALCUSTOMERID", None)
69 if not globalCustomerId:
70 raise BadRequestException("Not found globalCustomerId in header")
71 req_serializer = CreateNsRequestSerializer(data=request.data)
72 if not req_serializer.is_valid():
73 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, 'description')
81 "globalCustomerId": globalCustomerId,
82 "serviceType": "NetworkService"
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] # todo
88 resp_serializer = NsInstanceSerializer(data=nsInstance)
89 if not resp_serializer.is_valid():
90 raise NSLCMException(resp_serializer.errors)
91 return Response(data=resp_serializer.data, status=status.HTTP_201_CREATED)
92 except BadRequestException as e:
93 logger.error("Exception in CreateNS: %s", e.message)
94 data = {'status': status.HTTP_400_BAD_REQUEST, 'detail': e.message}
95 return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
96 except Exception as e:
97 logger.error("Exception in CreateNS: %s", e.message)
98 data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
99 return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
102 class IndividualNsInstanceView(APIView):
103 @swagger_auto_schema(
106 status.HTTP_200_OK: NsInstanceSerializer(help_text="NS instances", many=False),
107 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
110 def get(self, request, ns_instance_id):
112 logger.debug("Enter NSDetailView::get ns(%s)", ns_instance_id)
113 ns_filter = {"ns_inst_id": ns_instance_id}
114 ret = GetNSInfoService(ns_filter).get_ns_info(is_sol=True)
116 data = {'status': status.HTTP_404_NOT_FOUND, 'detail': "NS Instance ID(%s) is not founded" % ns_instance_id}
117 return Response(data=data, status=status.HTTP_404_NOT_FOUND)
118 logger.debug("Leave NSDetailView::get::ret=%s", ret)
119 resp_serializer = NsInstanceSerializer(data=ret[0])
120 if not resp_serializer.is_valid():
121 raise NSLCMException(resp_serializer.errors)
122 return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
123 except Exception as e:
124 logger.error(traceback.format_exc())
125 logger.error("Exception in GetNSDetail: %s", e.message)
126 data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
127 return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
129 @swagger_auto_schema(
132 status.HTTP_204_NO_CONTENT: None
135 def delete(self, request, ns_instance_id):
137 logger.debug("Enter NSDetailView::delete ns(%s)", ns_instance_id)
138 DeleteNsService(ns_instance_id).do_biz()
139 return Response(data={}, status=status.HTTP_204_NO_CONTENT)
140 except Exception as e:
141 logger.error(traceback.format_exc())
142 logger.error("Exception in delete NS: %s", e.message)
143 data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
144 return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)