diff options
-rw-r--r-- | zte/vmanager/driver/interfaces/serializers.py | 70 | ||||
-rw-r--r-- | zte/vmanager/driver/interfaces/urls.py | 2 | ||||
-rw-r--r-- | zte/vmanager/driver/interfaces/views.py | 171 |
3 files changed, 167 insertions, 76 deletions
diff --git a/zte/vmanager/driver/interfaces/serializers.py b/zte/vmanager/driver/interfaces/serializers.py index bb6a9141..a19613ca 100644 --- a/zte/vmanager/driver/interfaces/serializers.py +++ b/zte/vmanager/driver/interfaces/serializers.py @@ -15,6 +15,76 @@ from rest_framework import serializers +class additionalParamSerializer(serializers.Serializer): + sdncontroller = serializers.CharField( + help_text="sdncontroller", + required=True, + max_length=255, + allow_null=True) + NatIpRange = serializers.CharField( + help_text="NatIpRange", + required=True, + max_length=255, + allow_null=True) + m6000_mng_ip = serializers.CharField( + help_text="m6000_mng_ip", + required=True, + max_length=255, + allow_null=True) + externalPluginManageNetworkName = serializers.CharField( + help_text="externalPluginManageNetworkName", + required=True, + max_length=255, + allow_null=True) + location = serializers.CharField( + help_text="location", + required=True, + max_length=255, + allow_null=True) + externalManageNetworkName = serializers.CharField( + help_text="externalManageNetworkName", + required=True, + max_length=255, + allow_null=True) + sfc_data_network = serializers.CharField( + help_text="sfc_data_network", + required=True, + max_length=255, + allow_null=True) + externalDataNetworkName = serializers.CharField( + help_text="externalDataNetworkName", + required=True, + max_length=255, + allow_null=True) + inputs = serializers.DictField( + help_text='inputs', + child=serializers.CharField(allow_blank=True), + required=False, + allow_null=True) + + +class InstantiateVnfRequestSerializer(serializers.Serializer): + vnfInstanceName = serializers.CharField( + help_text="vnfInstanceName", + required=True, + max_length=255, + allow_null=True) + vnfPackageId = serializers.CharField( + help_text="vnfPackageId", + required=True, + max_length=255, + allow_null=True) + vnfDescriptorId = serializers.CharField( + help_text="vnfDescriptorId", + required=True, + max_length=255, + allow_null=True) + additionalParam = additionalParamSerializer( + help_text="additionalParam", + required=True, + allow_null=True) + + class TerminateVnfRequestSerializer(serializers.Serializer): terminationType = serializers.CharField( help_text="terminationType", 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..1a1976ed 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,102 @@ 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) + instantiateVnfRequestSerializer = InstantiateVnfRequestSerializer(data=request.data) + if not instantiateVnfRequestSerializer.is_valid(): + raise Exception(instantiateVnfRequestSerializer.errors) - 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)) + ret = get_vnfminfo_from_nslcm(vnfmid) + if ret[0] != 0: + raise Exception(ret[1]) - 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]) + vnfm_info = json.JSONDecoder().decode(ret[1]) + logger.debug("[%s] vnfm_info=%s", fun_name(), vnfm_info) + vnf_package_id = ignorcase_get(instantiateVnfRequestSerializer.data, "vnfPackageId") + ret = vnfd_get(vnf_package_id) + if ret[0] != 0: + raise Exception(ret[1]) + + 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: + raise Exception(ret[1]) + + vnf_package_info = json.JSONDecoder().decode(ret[1]) + packageInfo = ignorcase_get(vnf_package_info, "packageInfo") + logger.debug("[%s] packageInfo=%s", fun_name(), packageInfo) + data = { + "NFVOID": 1, + "VNFMID": vnfmid, + "extension": {}, + } + 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"] + + for name, value in ignorcase_get(ignorcase_get(instantiateVnfRequestSerializer.data, "additionalParam"), "inputs").items(): + inputs.append({"name": name, "value": value}) + + data["extension"]["inputs"] = json.dumps(inputs) + additionalParam = ignorcase_get(instantiateVnfRequestSerializer.data, "additionalParam") + data["extension"]["extVirtualLinks"] = ignorcase_get(additionalParam, "extVirtualLinks") + data["extension"]["vnfinstancename"] = ignorcase_get(instantiateVnfRequestSerializer.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: + raise Exception(ret[1]) + + 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) + instRespSerializer = InstScaleHealRespSerializer(data=resp_data) + if not instRespSerializer.is_valid(): + raise Exception(instRespSerializer.errors) + + return Response(data=instRespSerializer.data, status=status.HTTP_200_OK) + except Exception as e: + logger.error("Error occurred when instantiating VNF,error:%s", e.message) + logger.error(traceback.format_exc()) + return Response(data={'error': 'InstantiateVnf expection'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) class TerminateVnf(APIView): |