diff options
author | ying.yunlong <ying.yunlong@zte.com.cn> | 2018-02-12 15:33:01 +0800 |
---|---|---|
committer | yunlong ying <ying.yunlong@zte.com.cn> | 2018-02-12 07:57:49 +0000 |
commit | 3a158da6e82a9e8c5fb9cd4ef1f426f6607c61f2 (patch) | |
tree | 39f5f07e464d2e6350210169a98c8f275f65d44f /zte/vmanager | |
parent | fc4d2ca170880a94ecd50a0b876361dfe61a3038 (diff) |
Add vfc-ztevnfmdriver instantiate swagger
Change-Id: Iae8c3f1202111bde879b59d15e3c509229699dce
Issue-ID: VFC-736
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
Diffstat (limited to 'zte/vmanager')
-rw-r--r-- | zte/vmanager/driver/interfaces/urls.py | 2 | ||||
-rw-r--r-- | zte/vmanager/driver/interfaces/views.py | 157 |
2 files changed, 83 insertions, 76 deletions
diff --git a/zte/vmanager/driver/interfaces/urls.py b/zte/vmanager/driver/interfaces/urls.py index 1c700ef9..182b7478 100644 --- a/zte/vmanager/driver/interfaces/urls.py +++ b/zte/vmanager/driver/interfaces/urls.py @@ -17,7 +17,7 @@ from django.conf.urls import url from driver.interfaces import views urlpatterns = [ - url(r'^api/ztevnfmdriver/v1/(?P<vnfmid>[0-9a-zA-Z\-\_]+)/vnfs$', views.instantiate_vnf, name='instantiate_vnf'), + url(r'^api/ztevnfmdriver/v1/(?P<vnfmid>[0-9a-zA-Z\-\_]+)/vnfs$', views.InstamtiateVnf.as_view(), name='instantiate_vnf'), url(r'^api/ztevnfmdriver/v1/(?P<vnfmid>[0-9a-zA-Z\-\_]+)/vnfs/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/terminate$', views.TerminateVnf.as_view(), name='terminate_vnf'), url(r'^api/ztevnfmdriver/v1/(?P<vnfmid>[0-9a-zA-Z\-\_]+)/vnfs/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)$', diff --git a/zte/vmanager/driver/interfaces/views.py b/zte/vmanager/driver/interfaces/views.py index f19f55a6..93089670 100644 --- a/zte/vmanager/driver/interfaces/views.py +++ b/zte/vmanager/driver/interfaces/views.py @@ -26,7 +26,8 @@ from rest_framework.response import Response from rest_framework.views import APIView from driver.interfaces.serializers import HealReqSerializer, InstScaleHealRespSerializer, ScaleReqSerializer, \ - NotifyReqSerializer, GrantRespSerializer, GrantReqSerializer, JobQueryRespSerializer, TerminateVnfRequestSerializer + NotifyReqSerializer, GrantRespSerializer, GrantReqSerializer, JobQueryRespSerializer, TerminateVnfRequestSerializer, \ + InstantiateVnfRequestSerializer from driver.pub.config.config import VNF_FTP from driver.pub.utils import restcall from driver.pub.utils.restcall import req_by_msb @@ -85,82 +86,88 @@ def vnfpackage_get(csarid): return ret -@api_view(http_method_names=['POST']) -def instantiate_vnf(request, *args, **kwargs): - try: - logger.debug("[%s] request.data=%s", fun_name(), request.data) - vnfm_id = ignorcase_get(kwargs, "vnfmid") - ret = get_vnfminfo_from_nslcm(vnfm_id) - if ret[0] != 0: - return Response(data={'error': ret[1]}, status=ret[2]) - vnfm_info = json.JSONDecoder().decode(ret[1]) - logger.debug("[%s] vnfm_info=%s", fun_name(), vnfm_info) - vnf_package_id = ignorcase_get(request.data, "vnfPackageId") - ret = vnfd_get(vnf_package_id) - if ret[0] != 0: - return Response(data={'error': ret[1]}, status=ret[2]) - vnfd_info = json.JSONDecoder().decode(ret[1]) - logger.debug("[%s] vnfd_info=%s", fun_name(), vnfd_info) - csar_id = ignorcase_get(vnfd_info, "csarId") - ret = vnfpackage_get(csar_id) - if ret[0] != 0: - return Response(data={'error': ret[1]}, status=ret[2]) - vnf_package_info = json.JSONDecoder().decode(ret[1]) - packageInfo = ignorcase_get(vnf_package_info, "packageInfo") - logger.debug("[%s] packageInfo=%s", fun_name(), packageInfo) - data = {} - data["NFVOID"] = 1 - data["VNFMID"] = vnfm_id - vnfdModel = json.loads(ignorcase_get(packageInfo, "vnfdModel")) - metadata = ignorcase_get(vnfdModel, "metadata") - vnfd_name = ignorcase_get(metadata, "name") - # TODO convert sdc vnf package to vnf vender package - inputs = [] - if "SPGW" in vnfd_name.upper(): - data["VNFD"] = VNF_FTP + "SPGW" - inputs = load_json_file("SPGW" + "_inputs.json") - elif "MME" in vnfd_name.upper(): - data["VNFD"] = VNF_FTP + "MME" - inputs = load_json_file("MME" + "_inputs.json") - else: - data["VNFD"] = ignorcase_get(packageInfo, "downloadUri") - - data["VNFURL"] = data["VNFD"] - - data["extension"] = {} - for name, value in ignorcase_get(ignorcase_get(request.data, "additionalParam"), "inputs").items(): - inputs.append({"name": name, "value": value}) - - data["extension"]["inputs"] = json.dumps(inputs) - additionalParam = ignorcase_get(request.data, "additionalParam") - data["extension"]["extVirtualLinks"] = ignorcase_get(additionalParam, "extVirtualLinks") - data["extension"]["vnfinstancename"] = ignorcase_get(request.data, "vnfInstanceName") - data["extension"]["vnfid"] = data["VNFD"] - data["extension"]["multivim"] = 0 - logger.debug("[%s] call_req data=%s", fun_name(), data) +class InstamtiateVnf(APIView): + @swagger_auto_schema( + request_body=InstantiateVnfRequestSerializer(), + responses={ + status.HTTP_200_OK: InstScaleHealRespSerializer(), + status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" + } + ) + def post(self, request, vnfmid): + try: + logger.debug("[%s] request.data=%s", fun_name(), request.data) + ret = get_vnfminfo_from_nslcm(vnfmid) + if ret[0] != 0: + return Response(data={'error': ret[1]}, status=ret[2]) + vnfm_info = json.JSONDecoder().decode(ret[1]) + logger.debug("[%s] vnfm_info=%s", fun_name(), vnfm_info) + vnf_package_id = ignorcase_get(request.data, "vnfPackageId") + ret = vnfd_get(vnf_package_id) + if ret[0] != 0: + return Response(data={'error': ret[1]}, status=ret[2]) + vnfd_info = json.JSONDecoder().decode(ret[1]) + logger.debug("[%s] vnfd_info=%s", fun_name(), vnfd_info) + csar_id = ignorcase_get(vnfd_info, "csarId") + ret = vnfpackage_get(csar_id) + if ret[0] != 0: + return Response(data={'error': ret[1]}, status=ret[2]) + vnf_package_info = json.JSONDecoder().decode(ret[1]) + packageInfo = ignorcase_get(vnf_package_info, "packageInfo") + logger.debug("[%s] packageInfo=%s", fun_name(), packageInfo) + data = {} + data["NFVOID"] = 1 + data["VNFMID"] = vnfmid + vnfdModel = json.loads(ignorcase_get(packageInfo, "vnfdModel")) + metadata = ignorcase_get(vnfdModel, "metadata") + vnfd_name = ignorcase_get(metadata, "name") + # TODO convert sdc vnf package to vnf vender package + inputs = [] + if "SPGW" in vnfd_name.upper(): + data["VNFD"] = VNF_FTP + "SPGW" + inputs = load_json_file("SPGW" + "_inputs.json") + elif "MME" in vnfd_name.upper(): + data["VNFD"] = VNF_FTP + "MME" + inputs = load_json_file("MME" + "_inputs.json") + else: + data["VNFD"] = ignorcase_get(packageInfo, "downloadUri") - ret = restcall.call_req( - base_url=ignorcase_get(vnfm_info, "url"), - user=ignorcase_get(vnfm_info, "userName"), - passwd=ignorcase_get(vnfm_info, "password"), - auth_type=restcall.rest_no_auth, - resource="v1/vnfs", - method='post', - content=json.JSONEncoder().encode(data)) + data["VNFURL"] = data["VNFD"] - logger.debug("[%s] call_req ret=%s", fun_name(), ret) - if ret[0] != 0: - return Response(data={'error': ret[1]}, status=ret[2]) - resp = json.JSONDecoder().decode(ret[1]) - resp_data = { - "vnfInstanceId": ignorcase_get(resp, "VNFInstanceID"), - "jobId": ignorcase_get(resp, "JobId") - } - logger.debug("[%s]resp_data=%s", fun_name(), resp_data) - except Exception as e: - logger.error("Error occurred when instantiating VNF") - raise e - return Response(data=resp_data, status=ret[2]) + data["extension"] = {} + for name, value in ignorcase_get(ignorcase_get(request.data, "additionalParam"), "inputs").items(): + inputs.append({"name": name, "value": value}) + + data["extension"]["inputs"] = json.dumps(inputs) + additionalParam = ignorcase_get(request.data, "additionalParam") + data["extension"]["extVirtualLinks"] = ignorcase_get(additionalParam, "extVirtualLinks") + data["extension"]["vnfinstancename"] = ignorcase_get(request.data, "vnfInstanceName") + data["extension"]["vnfid"] = data["VNFD"] + data["extension"]["multivim"] = 0 + logger.debug("[%s] call_req data=%s", fun_name(), data) + + ret = restcall.call_req( + base_url=ignorcase_get(vnfm_info, "url"), + user=ignorcase_get(vnfm_info, "userName"), + passwd=ignorcase_get(vnfm_info, "password"), + auth_type=restcall.rest_no_auth, + resource="v1/vnfs", + method='post', + content=json.JSONEncoder().encode(data)) + + logger.debug("[%s] call_req ret=%s", fun_name(), ret) + if ret[0] != 0: + return Response(data={'error': ret[1]}, status=ret[2]) + resp = json.JSONDecoder().decode(ret[1]) + resp_data = { + "vnfInstanceId": ignorcase_get(resp, "VNFInstanceID"), + "jobId": ignorcase_get(resp, "JobId") + } + logger.debug("[%s]resp_data=%s", fun_name(), resp_data) + except Exception as e: + logger.error("Error occurred when instantiating VNF") + raise e + return Response(data=resp_data, status=ret[2]) class TerminateVnf(APIView): |