1 # Copyright 2016-2017 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.
19 from drf_yasg.utils import swagger_auto_schema
20 from rest_framework import status
21 from rest_framework.response import Response
22 from rest_framework.views import APIView
24 from lcm.ns.serializers.deprecated.ns_serializers import _InstNsPostDealReqSerializer
25 from lcm.pub.database.models import NSInstModel, ServiceBaseInfoModel
26 from lcm.pub.exceptions import BadRequestException
27 from lcm.pub.utils.restcall import req_by_msb
28 from lcm.pub.utils.values import ignore_case_get
30 logger = logging.getLogger(__name__)
33 class NSInstPostDealView(APIView):
35 request_body=_InstNsPostDealReqSerializer(help_text="NS instant post deal"),
37 status.HTTP_202_ACCEPTED: "NS instant post deal success",
38 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
41 def post(self, request, ns_instance_id):
42 logger.debug("Enter NSInstPostDealView::post %s, %s", request.data, ns_instance_id)
43 ns_post_status = ignore_case_get(request.data, 'status')
44 ns_status = 'ACTIVE' if ns_post_status == 'true' else 'FAILED'
45 ns_opr_status = 'success' if ns_post_status == 'true' else 'failed'
47 req_serializer = _InstNsPostDealReqSerializer(data=request.data)
48 if not req_serializer.is_valid():
49 raise BadRequestException(req_serializer.errors)
50 NSInstModel.objects.filter(id=ns_instance_id).update(status=ns_status)
51 ServiceBaseInfoModel.objects.filter(service_id=ns_instance_id).update(
52 active_status=ns_status, status=ns_opr_status)
53 nsd_info = NSInstModel.objects.filter(id=ns_instance_id)
54 nsd_id = nsd_info[0].nsd_id
55 nsd_model = json.loads(nsd_info[0].nsd_model)
56 if "policies" in nsd_model and nsd_model["policies"]:
57 policy = nsd_model["policies"][0]
58 if "properties" in policy and policy["properties"]:
59 file_url = ignore_case_get(policy["properties"][0], "drl_file_url")
62 self.send_policy_request(ns_instance_id, nsd_id, file_url)
64 logger.error(traceback.format_exc())
65 return Response(data={'error': 'Failed to update status of NS(%s)' % ns_instance_id},
66 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
67 logger.debug("*****NS INST %s, %s******", ns_status, ns_opr_status)
68 return Response(data={'success': 'Update status of NS(%s) to %s' % (ns_instance_id, ns_status)},
69 status=status.HTTP_202_ACCEPTED)
71 def send_policy_request(self, ns_instance_id, nsd_id, file_url):
73 "nsid": ns_instance_id,
77 req_param = json.JSONEncoder().encode(input_data)
78 policy_engine_url = 'api/polengine/v1/policyinfo'
79 ret = req_by_msb(policy_engine_url, "POST", req_param)
81 logger.error("Failed to send ns policy req")