fix the test and serializer
[vfc/nfvo/lcm.git] / lcm / ns / views / inst_ns_post_deal_view.py
1 # Copyright 2016-2017 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 import json
15 import logging
16 import traceback
17
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
22
23 from lcm.ns.serializers.inst_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
28
29 logger = logging.getLogger(__name__)
30
31
32 class NSInstPostDealView(APIView):
33     @swagger_auto_schema(
34         request_body=InstNsPostDealReqSerializer(help_text="NS instant post deal"),
35         responses={
36             status.HTTP_202_ACCEPTED: "NS instant post deal success",
37             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
38         }
39     )
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'
45         try:
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")
59                 else:
60                     file_url = ""
61                 self.send_policy_request(ns_instance_id, nsd_id, file_url)
62         except:
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)
69
70     def send_policy_request(self, ns_instance_id, nsd_id, file_url):
71         input_data = {
72             "nsid": ns_instance_id,
73             "nsdid": nsd_id,
74             "fileUri": file_url
75         }
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)
79         if ret[0] != 0:
80             logger.error("Failed to send ns policy req")