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.
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
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
32 logger = logging.getLogger(__name__)
35 class NSInstancesView(APIView):
37 This resource represents NS instances.
43 status.HTTP_200_OK: NsInstanceSerializer(help_text="NS instances", many=True),
44 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
47 @view_safe_call_with_log(logger=logger)
48 def get(self, request):
50 Query multiple NS instances
54 logger.debug(request.query_params)
56 logger.debug("CreateNSView::get")
57 ret = GetNSInfoService().get_ns_info(is_sol=True) # todo
58 logger.debug("CreateNSView::get::ret=%s", ret)
59 resp_serializer = NsInstanceSerializer(data=ret, many=True)
60 if not resp_serializer.is_valid():
61 raise NSLCMException(resp_serializer.errors)
62 return Response(data=ret, status=status.HTTP_200_OK)
65 request_body=CreateNsRequestSerializer(),
67 status.HTTP_201_CREATED: NsInstanceSerializer(),
68 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
71 @view_safe_call_with_log(logger=logger)
72 def post(self, request):
74 Create a NS instance resource
78 logger.debug("Enter NSInstancesView::POST ns_instances: Header:%s, Body: %s" % (request.META, request.data))
80 globalCustomerId = request.META.get("HTTP_GLOBALCUSTOMERID", None)
81 if not globalCustomerId:
82 raise BadRequestException("Not found globalCustomerId in header")
83 serviceType = request.META.get("HTTP_SERVICETYPE", None)
85 serviceType = "NetworkService"
86 req_serializer = CreateNsRequestSerializer(data=request.data)
87 if not req_serializer.is_valid():
88 raise BadRequestException(req_serializer.errors)
89 if ignore_case_get(request.data, 'test') == "test":
90 return Response(data={'nsInstanceId': "test"}, status=status.HTTP_201_CREATED)
91 csar_id = ignore_case_get(request.data, 'nsdId')
92 ns_name = ignore_case_get(request.data, 'nsName')
93 description = ignore_case_get(request.data, 'nsDescription')
95 "globalCustomerId": globalCustomerId,
96 "serviceType": serviceType
98 ns_inst_id = CreateNSService(csar_id, ns_name, description, context).do_biz()
99 logger.debug("CreateNSView::post::ret={'nsInstanceId':%s}", ns_inst_id)
100 ns_filter = {"ns_inst_id": ns_inst_id}
101 nsInstance = GetNSInfoService(ns_filter).get_ns_info(is_sol=True)[0]
102 logger.debug("nsInstance: %s" % nsInstance)
103 resp_serializer = NsInstanceSerializer(data=nsInstance)
104 if not resp_serializer.is_valid():
105 raise NSLCMException(resp_serializer.errors)
106 response = Response(data=nsInstance, status=status.HTTP_201_CREATED)
107 response["Location"] = NS_INSTANCE_BASE_URI % nsInstance['id']
111 class IndividualNsInstanceView(APIView):
113 This resource represents an individual NS instance.
116 @swagger_auto_schema(
119 status.HTTP_200_OK: NsInstanceSerializer(help_text="NS instances", many=False),
120 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
123 @view_safe_call_with_log(logger=logger)
124 def get(self, request, ns_instance_id):
126 The GET method retrieves information about a NS instance by reading an individual NS instance resource.
128 :param ns_instance_id:
131 logger.debug("Enter NSDetailView::get ns(%s)", ns_instance_id)
132 ns_filter = {"ns_inst_id": ns_instance_id}
133 ret = GetNSInfoService(ns_filter).get_ns_info(is_sol=True)
135 data = {'status': status.HTTP_404_NOT_FOUND, 'detail': "NS Instance ID(%s) is not founded" % ns_instance_id}
136 return Response(data=data, status=status.HTTP_404_NOT_FOUND)
137 logger.debug("Leave NSDetailView::get::ret=%s", ret)
138 resp_serializer = NsInstanceSerializer(data=ret[0])
139 if not resp_serializer.is_valid():
140 raise NSLCMException(resp_serializer.errors)
141 return Response(data=ret[0], status=status.HTTP_200_OK)
143 @swagger_auto_schema(
146 status.HTTP_204_NO_CONTENT: "HTTP_204_NO_CONTENT"
149 @view_safe_call_with_log(logger=logger)
150 def delete(self, request, ns_instance_id):
152 This method deletes an individual NS instance resource.
154 :param ns_instance_id:
157 logger.debug("Enter NSDetailView::delete ns(%s)", ns_instance_id)
158 DeleteNsService(ns_instance_id).do_biz()
159 return Response(data={}, status=status.HTTP_204_NO_CONTENT)