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