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
29 from .common import view_safe_call_with_log
31 logger = logging.getLogger(__name__)
34 class NSInstPostDealView(APIView):
36 request_body=_InstNsPostDealReqSerializer(help_text="NS instant post deal"),
38 status.HTTP_202_ACCEPTED: "NS instant post deal success",
39 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
42 @view_safe_call_with_log(logger=logger)
43 def post(self, request, ns_instance_id):
44 logger.debug("Enter NSInstPostDealView::post %s, %s", request.data, ns_instance_id)
45 ns_post_status = ignore_case_get(request.data, 'status')
46 ns_status = 'ACTIVE' if ns_post_status == 'true' else 'FAILED'
47 ns_opr_status = 'success' if ns_post_status == 'true' else 'failed'
49 req_serializer = _InstNsPostDealReqSerializer(data=request.data)
50 if not req_serializer.is_valid():
51 raise BadRequestException(req_serializer.errors)
52 NSInstModel.objects.filter(id=ns_instance_id).update(status=ns_status)
53 ServiceBaseInfoModel.objects.filter(service_id=ns_instance_id).update(
54 active_status=ns_status, status=ns_opr_status)
55 nsd_info = NSInstModel.objects.filter(id=ns_instance_id)
56 nsd_id = nsd_info[0].nsd_id
57 nsd_model = json.loads(nsd_info[0].nsd_model)
58 if "policies" in nsd_model and nsd_model["policies"]:
59 policy = nsd_model["policies"][0]
60 if "properties" in policy and policy["properties"]:
61 file_url = ignore_case_get(policy["properties"][0], "drl_file_url")
64 self.send_policy_request(ns_instance_id, nsd_id, file_url)
66 logger.error(traceback.format_exc())
67 return Response(data={'error': 'Failed to update status of NS(%s)' % ns_instance_id},
68 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
69 logger.debug("*****NS INST %s, %s******", ns_status, ns_opr_status)
70 return Response(data={'success': 'Update status of NS(%s) to %s' % (ns_instance_id, ns_status)},
71 status=status.HTTP_202_ACCEPTED)
73 def send_policy_request(self, ns_instance_id, nsd_id, file_url):
75 "nsid": ns_instance_id,
79 req_param = json.JSONEncoder().encode(input_data)
80 policy_engine_url = 'api/polengine/v1/policyinfo'
81 ret = req_by_msb(policy_engine_url, "POST", req_param)
83 logger.error("Failed to send ns policy req")