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 serviceType = request.META.get("HTTP_SERVICETYPE", None)
74 serviceType = "NetworkService"
75 req_serializer = CreateNsRequestSerializer(data=request.data)
76 if not req_serializer.is_valid():
77 raise BadRequestException(req_serializer.errors)
78 if ignore_case_get(request.data, 'test') == "test":
79 return Response(data={'nsInstanceId': "test"}, status=status.HTTP_201_CREATED)
80 csar_id = ignore_case_get(request.data, 'nsdId')
81 ns_name = ignore_case_get(request.data, 'nsName')
82 description = ignore_case_get(request.data, 'nsDescription')
84 "globalCustomerId": globalCustomerId,
85 "serviceType": serviceType
87 ns_inst_id = CreateNSService(csar_id, ns_name, description, context).do_biz()
88 logger.debug("CreateNSView::post::ret={'nsInstanceId':%s}", ns_inst_id)
89 ns_filter = {"ns_inst_id": ns_inst_id}
90 nsInstance = GetNSInfoService(ns_filter).get_ns_info(is_sol=True)[0]
91 logger.debug("nsInstance: %s" % nsInstance)
92 resp_serializer = NsInstanceSerializer(data=nsInstance)
93 if not resp_serializer.is_valid():
94 raise NSLCMException(resp_serializer.errors)
95 response = Response(data=resp_serializer.data, status=status.HTTP_201_CREATED)
96 response["Location"] = NS_INSTANCE_BASE_URI % nsInstance['id']
99 except BadRequestException as e:
100 logger.error("Exception in CreateNS: %s", e.message)
101 data = {'status': status.HTTP_400_BAD_REQUEST, 'detail': e.message}
102 return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
103 except Exception as e:
104 logger.error("Exception in CreateNS: %s", e.message)
105 data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
106 return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
109 class IndividualNsInstanceView(APIView):
110 @swagger_auto_schema(
113 status.HTTP_200_OK: NsInstanceSerializer(help_text="NS instances", many=False),
114 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
117 def get(self, request, ns_instance_id):
119 logger.debug("Enter NSDetailView::get ns(%s)", ns_instance_id)
120 ns_filter = {"ns_inst_id": ns_instance_id}
121 ret = GetNSInfoService(ns_filter).get_ns_info(is_sol=True)
123 data = {'status': status.HTTP_404_NOT_FOUND, 'detail': "NS Instance ID(%s) is not founded" % ns_instance_id}
124 return Response(data=data, status=status.HTTP_404_NOT_FOUND)
125 logger.debug("Leave NSDetailView::get::ret=%s", ret)
126 resp_serializer = NsInstanceSerializer(data=ret[0])
127 if not resp_serializer.is_valid():
128 raise NSLCMException(resp_serializer.errors)
129 return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
130 except Exception as e:
131 logger.error(traceback.format_exc())
132 logger.error("Exception in GetNSDetail: %s", e.message)
133 data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
134 return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
136 @swagger_auto_schema(
139 status.HTTP_204_NO_CONTENT: None
142 def delete(self, request, ns_instance_id):
144 logger.debug("Enter NSDetailView::delete ns(%s)", ns_instance_id)
145 DeleteNsService(ns_instance_id).do_biz()
146 return Response(data={}, status=status.HTTP_204_NO_CONTENT)
147 except Exception as e:
148 logger.error(traceback.format_exc())
149 logger.error("Exception in delete NS: %s", e.message)
150 data = {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'detail': e.message}
151 return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)