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 rest_framework import status
20 from rest_framework.response import Response
21 from rest_framework.views import APIView
23 from lcm.ns.ns_create import CreateNSService
24 from lcm.ns.ns_get import GetNSInfoService
25 from lcm.ns.ns_instant import InstantNSService
26 from lcm.ns.ns_manual_scale import NSManualScaleService
27 from lcm.ns.ns_terminate import TerminateNsService, DeleteNsService
28 from lcm.pub.database.models import NSInstModel, ServiceBaseInfoModel
29 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
30 from lcm.pub.utils.values import ignore_case_get
31 from lcm.pub.utils.restcall import req_by_msb
32 from lcm.pub.exceptions import NSLCMException
34 logger = logging.getLogger(__name__)
37 class CreateNSView(APIView):
38 def get(self, request):
39 logger.debug("CreateNSView::get")
40 ret = GetNSInfoService().get_ns_info()
41 logger.debug("CreateNSView::get::ret=%s", ret)
42 return Response(data=ret, status=status.HTTP_200_OK)
44 def post(self, request):
45 logger.debug("Enter CreateNS: %s", request.data)
46 nsd_id = ignore_case_get(request.data, 'nsdId')
47 ns_name = ignore_case_get(request.data, 'nsName')
48 description = ignore_case_get(request.data, 'description')
50 ns_inst_id = CreateNSService(nsd_id, ns_name, description).do_biz()
51 except Exception as e:
52 logger.error("Exception in CreateNS: %s", e.message)
53 return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
54 logger.debug("CreateNSView::post::ret={'nsInstanceId':%s}", ns_inst_id)
55 return Response(data={'nsInstanceId': ns_inst_id}, status=status.HTTP_201_CREATED)
58 class NSInstView(APIView):
59 def post(self, request, ns_instance_id):
60 ack = InstantNSService(ns_instance_id, request.data).do_biz()
61 logger.debug("Leave NSInstView::post::ack=%s", ack)
62 return Response(data=ack['data'], status=ack['status'])
65 class TerminateNSView(APIView):
66 def post(self, request, ns_instance_id):
67 logger.debug("Enter TerminateNSView::post %s", request.data)
68 termination_type = ignore_case_get(request.data, 'terminationType')
69 graceful_termination_timeout = ignore_case_get(request.data, 'gracefulTerminationTimeout')
70 job_id = JobUtil.create_job("VNF", JOB_TYPE.TERMINATE_VNF, ns_instance_id)
72 TerminateNsService(ns_instance_id, termination_type, graceful_termination_timeout, job_id).start()
73 except Exception as e:
74 logger.error("Exception in CreateNS: %s", e.message)
75 return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
76 ret = {'jobId': job_id}
77 logger.debug("Leave TerminateNSView::post ret=%s", ret)
78 return Response(data=ret, status=status.HTTP_202_ACCEPTED)
81 class NSDetailView(APIView):
82 def get(self, request, ns_instance_id):
83 logger.debug("Enter NSDetailView::get ns(%s)", ns_instance_id)
84 ret = GetNSInfoService(ns_instance_id).get_ns_info()
86 return Response(status=status.HTTP_404_NOT_FOUND)
87 logger.debug("Leave NSDetailView::get::ret=%s", ret)
88 return Response(data=ret, status=status.HTTP_200_OK)
90 def delete(self, request, ns_instance_id):
91 logger.debug("Enter NSDetailView::delete ns(%s)", ns_instance_id)
92 DeleteNsService(ns_instance_id).do_biz()
93 return Response(data={}, status=status.HTTP_204_NO_CONTENT)
96 class SwaggerJsonView(APIView):
97 def get(self, request):
98 json_file = os.path.join(os.path.dirname(__file__), 'swagger.json')
100 json_data = json.JSONDecoder().decode(f.read())
102 return Response(json_data)
105 class NSInstPostDealView(APIView):
108 def post(self, request, ns_instance_id):
109 logger.debug("Enter NSInstPostDealView::post %s, %s", request.data, ns_instance_id)
110 ns_post_status = ignore_case_get(request.data, 'status')
111 ns_status = 'ACTIVE' if ns_post_status == 'true' else 'FAILED'
112 ns_opr_status = 'success' if ns_post_status == 'true' else 'failed'
114 NSInstModel.objects.filter(id=ns_instance_id).update(status=ns_status)
115 ServiceBaseInfoModel.objects.filter(service_id=ns_instance_id).update(
116 active_status=ns_status, status=ns_opr_status)
117 nsd_info = NSInstModel.objects.filter(id=ns_instance_id)
118 nsd_id = nsd_info[0].nsd_id
119 nsd_model = json.loads(nsd_info[0].nsd_model)
120 if "policies" in nsd_model and nsd_model["policies"]:
121 policy = nsd_model["policies"][0]
122 if "properties" in policy and policy["properties"]:
123 file_url = ignore_case_get(policy["properties"][0], "drl_file_url")
126 self.send_policy_request(ns_instance_id, nsd_id, file_url)
128 logger.error(traceback.format_exc())
129 return Response(data={'error': 'Failed to update status of NS(%s)' % ns_instance_id},
130 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
131 logger.debug("*****NS INST %s, %s******", ns_status, ns_opr_status)
132 return Response(data={'success': 'Update status of NS(%s) to %s' % (ns_instance_id, ns_status)},
133 status=status.HTTP_202_ACCEPTED)
135 def send_policy_request(self,ns_instance_id, nsd_id, file_url):
137 "nsid": ns_instance_id,
141 req_param = json.JSONEncoder().encode(input_data)
142 policy_engine_url = 'openoapi/polengine/v1/policyinfo'
143 ret = req_by_msb(policy_engine_url, "POST", req_param)
145 logger.error("Failed to send ns policy req")
146 #raise NSLCMException('Failed to send ns policy req)')
149 class NSManualScaleView(APIView):
150 def post(self, request, ns_instance_id):
151 logger.debug("Enter NSManualScaleView::post %s, %s", request.data, ns_instance_id)
152 job_id = JobUtil.create_job("NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_instance_id)
154 NSManualScaleService(ns_instance_id, request.data, job_id).start()
155 except Exception as e:
156 logger.error(traceback.format_exc())
157 JobUtil.add_job_status(job_id, 255, 'NS scale failed: %s' % e.message)
158 return Response(data={'error': 'NS scale failed: %s' % ns_instance_id},
159 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
160 return Response(data={'jobId': job_id}, status=status.HTTP_202_ACCEPTED)