Add query vnfm-info and unit test
[vfc/nfvo/lcm.git] / lcm / ns / vnfs / views.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 logging
15 import traceback
16 import uuid
17
18 from rest_framework import status
19 from rest_framework.response import Response
20 from rest_framework.views import APIView
21
22 from lcm.ns.vnfs import create_vnfs
23 from lcm.ns.vnfs.create_vnfs import CreateVnfs
24 from lcm.ns.vnfs.verify_vnfs import VerifyVnfs
25 from lcm.ns.vnfs.get_vnfs import GetVnf
26 from lcm.ns.vnfs.scale_vnfs import NFManualScaleService
27 from lcm.ns.vnfs.terminate_nfs import TerminateVnfs
28 from lcm.ns.vnfs.grant_vnfs import GrantVnfs
29 from lcm.ns.vnfs.notify_lcm import NotifyLcm
30 from lcm.pub.exceptions import NSLCMException
31 from lcm.pub.msapi.extsys import get_vnfm_by_id
32 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
33 from lcm.pub.utils.values import ignore_case_get
34
35 logger = logging.getLogger(__name__)
36
37
38 class NfView(APIView):
39     def post(self, request):
40         logger.debug("VnfCreateView--post::> %s" % request.data)
41         data = {'ns_instance_id': ignore_case_get(request.data, 'nsInstanceId'),
42                 'additional_param_for_ns': ignore_case_get(request.data, 'additionalParamForVnf'),
43                 'additional_param_for_vnf': ignore_case_get(request.data, 'additionalParamForVnf'),
44                 'vnf_index': ignore_case_get(request.data, 'vnfIndex')}
45         nf_inst_id, job_id = create_vnfs.prepare_create_params()
46         CreateVnfs(data, nf_inst_id, job_id).start()
47         rsp = {
48             "vnfInstId": nf_inst_id,
49             "jobId": job_id}
50         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
51
52
53 class NfDetailView(APIView):
54     def get(self, request, vnfinstid):
55         logger.debug("VnfQueryView--get::> %s" % vnfinstid)
56         nf_inst_info = GetVnf(vnfinstid).do_biz()
57         if not nf_inst_info:
58             return Response(status=status.HTTP_404_NOT_FOUND)
59         return Response(status=status.HTTP_200_OK,
60                         data={'vnfInstId': nf_inst_info[0].nfinstid, 'vnfName': nf_inst_info[0].nf_name,
61                               'vnfStatus': nf_inst_info[0].status})
62
63     def post(self, request_paras, vnfinstid):
64         logger.debug("VnfTerminateView--post::> %s, %s", vnfinstid, request_paras.data)
65         vnf_inst_id = vnfinstid
66         terminationType = ignore_case_get(request_paras.data, 'terminationType')
67         gracefulTerminationTimeout = ignore_case_get(request_paras.data, 'gracefulTerminationTimeout')
68         job_id = JobUtil.create_job("VNF", JOB_TYPE.TERMINATE_VNF, vnf_inst_id)
69         data = {'terminationType': terminationType, 'gracefulTerminationTimeout': gracefulTerminationTimeout}
70         logger.debug("data=%s", data)
71         try:
72             TerminateVnfs(data, vnf_inst_id, job_id).start()
73         except Exception as e:
74             return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
75         rsp = {'jobId': job_id}
76         return Response(data=rsp, status=status.HTTP_201_CREATED)
77
78
79 class NfGrant(APIView):
80     def post(self, request):
81         logger.debug("NfGrant--post::> %s" % request.data)
82         try:
83             vnf_inst_id = ignore_case_get(request.data, 'vnfInstanceId')
84             job_id = JobUtil.create_job("VNF", JOB_TYPE.GRANT_VNF, vnf_inst_id)
85             rsp = GrantVnfs(request.data, job_id).send_grant_vnf_to_resMgr()
86             """
87             rsp = {
88                 "vim": {
89                     "vimid": ignore_case_get(ignore_case_get(request.data, 'additionalparam'), 'vimid'),
90                     "accessinfo": {
91                         "tenant": "admin"
92                     }
93                 }
94             }
95             """
96             return Response(data=rsp, status=status.HTTP_201_CREATED)
97         except Exception as e:
98             logger.error(traceback.format_exc())
99             return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
100
101
102 class LcmNotify(APIView):
103     def post(self, request_paras, vnfmid, vnfInstanceId):
104         logger.debug("LcmNotify--post::> %s" % request_paras.data)
105         try:
106             NotifyLcm(vnfmid, vnfInstanceId, request_paras.data).do_biz()
107             return Response(data={}, status=status.HTTP_201_CREATED)
108         except Exception as e:
109             return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
110
111
112 class NfScaleView(APIView):
113     def post(self, request_paras, vnfinstid):
114         logger.debug("NfScaleView--post::> %s" % request_paras.data)
115         try:
116             NFManualScaleService(vnfinstid, request_paras.data).start()
117             return Response(data={}, status=status.HTTP_202_ACCEPTED)
118         except Exception as e:
119             return Response(data={'error': '%s' % e.message}, status=status.HTTP_409_CONFLICT)
120
121 class NfVerifyView(APIView):
122     def post(self, request):
123         job_id = "VNFSDK_" + str(uuid.uuid4())
124         logger.debug("NfVerifyView--post::%s> %s", job_id, request.data)
125         VerifyVnfs(request.data, job_id).start()
126         return Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)
127
128 class NfVnfmInfoView(APIView):
129     def get(self, request, vnfmid):
130         logger.debug("NfVnfmInfoView--get::> %s" % vnfmid)
131         try:
132             vnfm_info = get_vnfm_by_id(vnfmid)
133         except NSLCMException as e:
134             logger.error(e.message)
135             return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
136         except:
137             logger.error(traceback.format_exc())
138             return Response(data={'error': 'Failed to get vnfm info.'},
139                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
140         return Response(data=vnfm_info, status=status.HTTP_200_OK)