From bfbbc7e0b73a39573749dfe29724ccd2cfe6d736 Mon Sep 17 00:00:00 2001 From: maopengzhang Date: Wed, 3 Apr 2019 18:04:56 +0800 Subject: [PATCH] Fix NS Scale serializers error Fix NS Scale serializers error Change-Id: I387f6f0605dd825c878b78329484395e6ee677b3 Issue-ID: VFC-1214 Signed-off-by: maopengzhang --- lcm/ns/biz/scaleaspect.py | 2 +- lcm/ns/serializers/sol/pub_serializers.py | 5 +- lcm/ns/serializers/sol/scale_ns_serializers.py | 231 ++++++++++++++----------- lcm/ns/tests/test_ns_manual_scale.py | 22 ++- lcm/ns/tests/test_sol_ns_scale_api.py | 16 +- lcm/ns/views/deprecated/scale_ns_views.py | 9 +- lcm/ns/views/sol/scale_ns_views.py | 6 +- 7 files changed, 166 insertions(+), 125 deletions(-) diff --git a/lcm/ns/biz/scaleaspect.py b/lcm/ns/biz/scaleaspect.py index 0d6a0338..9c941817 100644 --- a/lcm/ns/biz/scaleaspect.py +++ b/lcm/ns/biz/scaleaspect.py @@ -124,7 +124,7 @@ def check_and_set_params(scaleNsData, ns_InstanceId): if scaleNsData is None: raise Exception("Error! scaleNsData in the request is Empty!") - scaleNsByStepsData = scaleNsData[0]["scaleNsByStepsData"][0] + scaleNsByStepsData = scaleNsData["scaleNsByStepsData"] # scaleNsData[0]["scaleNsByStepsData"][0] if scaleNsByStepsData is None: raise Exception("Error! scaleNsByStepsData in the request is Empty!") diff --git a/lcm/ns/serializers/sol/pub_serializers.py b/lcm/ns/serializers/sol/pub_serializers.py index 4825f63c..fab3c656 100644 --- a/lcm/ns/serializers/sol/pub_serializers.py +++ b/lcm/ns/serializers/sol/pub_serializers.py @@ -51,7 +51,10 @@ class ProblemDetailsSerializer(serializers.Serializer): class LinkSerializer(serializers.Serializer): href = serializers.CharField( - help_text="URI of the referenced resource.", required=True, allow_null=False, allow_blank=False) + help_text="URI of the referenced resource.", + required=True, + allow_null=False, + allow_blank=False) class AffinityOrAntiAffinityRuleSerializer(serializers.Serializer): diff --git a/lcm/ns/serializers/sol/scale_ns_serializers.py b/lcm/ns/serializers/sol/scale_ns_serializers.py index a15d1a59..b993ed2d 100644 --- a/lcm/ns/serializers/sol/scale_ns_serializers.py +++ b/lcm/ns/serializers/sol/scale_ns_serializers.py @@ -28,25 +28,33 @@ from lcm.ns.serializers.sol.ns_instance import NsScaleInfoSerializer, VnfScaleIn class ScaleNsByStepsDataSerializer(serializers.Serializer): - scalingDirection = serializers.ChoiceField(help_text="The scaling direction", - choices=["SCALE_IN", "SCALE_OUT"], required=True) - aspectId = serializers.CharField(help_text="The aspect of the NS that is requested to be scaled, as " - "declared in the NSD. ", required=True) - numberOfSteps = serializers.CharField(help_text="The number of scaling steps to be performed. Defaults " - "to 1. ", required=False, allow_null=True) + scalingDirection = serializers.ChoiceField( + help_text="The scaling direction", + choices=["SCALE_IN", "SCALE_OUT"], + required=True) + aspectId = serializers.CharField( + help_text="The aspect of the NS that is requested to be scaled, as declared in the NSD. ", + required=True) + numberOfSteps = serializers.CharField( + help_text="The number of scaling steps to be performed. Defaults to 1. ", + required=False, + allow_null=True) class ScaleNsToLevelDataSerializer(serializers.Serializer): - nsInstantiationLevel = serializers.CharField(help_text="Identifier of the target NS instantiation level " - "of the current DF to which the NS instance is " - "requested to be scaled.", - required=False, allow_null=True) - nsScaleInfo = serializers.ListField(help_text="For each NS scaling aspect of the current DF", - child=NsScaleInfoSerializer( - help_text="This type represents the target NS Scale level for " - "each NS scaling aspect of the current deployment " - "flavour.", required=True), - required=False, allow_null=True) + nsInstantiationLevel = serializers.CharField( + help_text="Identifier of the target NS instantiation level " + "of the current DF to which the NS instance is requested to be scaled.", + required=False, + allow_null=True) + nsScaleInfo = serializers.ListField( + help_text="For each NS scaling aspect of the current DF", + child=NsScaleInfoSerializer( + help_text="This type represents the target NS Scale level for " + "each NS scaling aspect of the current deployment flavour.", + required=True), + required=False, + allow_null=True) # class ParamsForVnfSerializer(serializers.Serializer): @@ -78,102 +86,125 @@ class ScaleNsToLevelDataSerializer(serializers.Serializer): class ScaleNsDataSerializer(serializers.Serializer): - vnfInstanceToBeAdded = serializers.ListField(help_text="An existing VNF instance to be added to the NS " - "instance as part of the scaling operation. ", - child=VnfInstanceDataSerializer( - help_text="This type specifies an existing VNF instance " - "to be used in the NS instance and if needed", - required=True), required=False, allow_null=True) - vnfInstanceToBeRemoved = serializers.ListField(help_text="The VNF instance to be removed from the NS " - "instance as part of the scaling operation", - required=False, allow_null=True) - scaleNsByStepsData = ScaleNsByStepsDataSerializer(help_text="The information used to scale an NS " - "instance by one or more scaling steps", - required=False, allow_null=True) - scaleNsToLevelData = ScaleNsToLevelDataSerializer(help_text="The information used to scale an NS instance" - " to a target size. ", - required=False, allow_null=True) - additionalParamsForNs = serializers.DictField(help_text="Allows the OSS/BSS to provide additional " - "parameter(s) at the NS level necessary for the " - "NS scaling ", - child=serializers.CharField(help_text="KeyValue Pairs", - allow_blank=True), - required=False, allow_null=True) - additionalParamsForVnf = serializers.ListField(help_text="Allows the OSS/BSS to provide additional " - "parameter(s) per VNF instance", - child=ParamsForVnfSerializer( - help_text="This type defines the additional parameters" - " for the VNF instance to be created " - "associated with an NS instance.", - required=True), required=False, allow_null=True) - locationConstraints = serializers.ListField(help_text="The location constraints for the VNF to be " - "instantiated as part of the NS scaling.", - child=VnfLocationConstraintSerializer( - help_text="This type represents the association of " - "location constraints to a VNF instance to" - "be created according to a specific VNF " - "profile", required=True), - required=False, allow_null=True) + vnfInstanceToBeAdded = serializers.ListField( + help_text="An existing VNF instance to be added to the NS instance as part of the scaling operation.", + child=VnfInstanceDataSerializer( + help_text="This type specifies an existing VNF instance to be used in the NS instance and if needed", + required=True), + required=False, + allow_null=True) + vnfInstanceToBeRemoved = serializers.ListField( + help_text="The VNF instance to be removed from the NS instance as part of the scaling operation", + required=False, + allow_null=True) + scaleNsByStepsData = ScaleNsByStepsDataSerializer( + help_text="The information used to scale an NS instance by one or more scaling steps", + required=False, + allow_null=True) + scaleNsToLevelData = ScaleNsToLevelDataSerializer( + help_text="The information used to scale an NS instance to a target size. ", + required=False, + allow_null=True) + additionalParamsForNs = serializers.DictField( + help_text="Allows the OSS/BSS to provide additional parameter(s) at the NS level necessary for the NS scaling ", + child=serializers.CharField(help_text="KeyValue Pairs", + allow_blank=True), + required=False, + allow_null=True) + additionalParamsForVnf = serializers.ListField( + help_text="Allows the OSS/BSS to provide additional parameter(s) per VNF instance", + child=ParamsForVnfSerializer( + help_text="This type defines the additional parameters for the VNF instance to be created associated with an NS instance.", + required=True), + required=False, + allow_null=True) + locationConstraints = serializers.ListField( + help_text="The location constraints for the VNF to be instantiated as part of the NS scaling.", + child=VnfLocationConstraintSerializer( + help_text="This type represents the association of location constraints to a VNF instance to" + "be created according to a specific VNF profile", + required=True), + required=False, + allow_null=True) class ScaleToLevelDataSerializer(serializers.Serializer): - vnfInstantiationLevelId = serializers.CharField(help_text="Identifier of the target instantiation level " - "of the current deployment flavour to which " - "the VNF is requested to be scaled.", - required=False, allow_null=True) - vnfScaleInfo = serializers.ListField(help_text="For each scaling aspect of the current deployment " - "flavour", - child=VnfScaleInfoSerializer(help_text="This type describes the " - "provides information about" - " the scale level of a VNF" - " instance with respect to " - "one scaling aspect", - required=True), - required=False, allow_null=True) - - additionalParams = serializers.DictField(help_text="Additional parameters passed by the NFVO as input to " - "the scaling process", required=False, allow_null=True) + vnfInstantiationLevelId = serializers.CharField( + help_text="Identifier of the target instantiation level of the current deployment flavour to which the VNF is requested to be scaled.", + required=False, + allow_null=True) + vnfScaleInfo = serializers.ListField( + help_text="For each scaling aspect of the current deployment flavour", + child=VnfScaleInfoSerializer( + help_text="This type describes the provides information about the scale level of a VNF instance with respect to one scaling aspect", + required=True), + required=False, + allow_null=True) + additionalParams = serializers.DictField( + help_text="Additional parameters passed by the NFVO as input to the scaling process", + required=False, + allow_null=True) class ScaleByStepDataSerializer(serializers.Serializer): - aspectId = serializers.CharField(help_text="Identifier of (reference to) the aspect of the VNF that is " - "requested to be scaled", required=True) - numberOfSteps = serializers.CharField(help_text="Number of scaling steps.", - required=False, allow_null=True) - additionalParams = serializers.DictField(help_text="Additional parameters passed by the NFVO as input to" - "he scaling process", required=False, allow_null=True) + aspectId = serializers.CharField( + help_text="Identifier of (reference to) the aspect of the VNF that is requested to be scaled.", + required=True) + numberOfSteps = serializers.CharField( + help_text="Number of scaling steps.", + required=False, + allow_null=True) + additionalParams = serializers.DictField( + help_text="Additional parameters passed by the NFVO as input to the scaling process.", + required=False, + allow_null=True) class ScaleVnfDataSerializer(serializers.Serializer): - vnfInstanceid = serializers.CharField(help_text="Identifier of the VNF instance being scaled.", - required=True) + vnfInstanceid = serializers.CharField( + help_text="Identifier of the VNF instance being scaled.", + required=True) - scaleVnfType = serializers.ChoiceField(help_text="Type of the scale VNF operation requested.", - choices=["SCALE_OUT", "SCALE_IN", "SCALE_TO_INSTANTIATION_LEVEL", - "SCALE_TO_SCALE_LEVEL(S)"], required=True) + scaleVnfType = serializers.ChoiceField( + help_text="Type of the scale VNF operation requested.", + choices=["SCALE_OUT", "SCALE_IN", "SCALE_TO_INSTANTIATION_LEVEL", "SCALE_TO_SCALE_LEVEL(S)"], + required=True) - scaleToLevelData = ScaleToLevelDataSerializer(help_text="The information used for scaling to a " - "given level.", required=False) + scaleToLevelData = ScaleToLevelDataSerializer( + help_text="The information used for scaling to a given level.", + required=False) - scaleByStepData = ScaleByStepDataSerializer(help_text="The information used for scaling by steps", - required=False) + scaleByStepData = ScaleByStepDataSerializer( + help_text="The information used for scaling by steps.", + required=False) class ScaleNsRequestSerializer(serializers.Serializer): - scaleType = serializers.ChoiceField(help_text="Indicates the type of scaling to be performed", - choices=["SCALE_NS ", "SCALE_VNF"], required=True) - scaleNsData = ScaleNsDataSerializer(help_text="The necessary information to scale the referenced NS " - "instance. ", required=False, allow_null=True) - scaleVnfData = serializers.ListField(help_text="Timestamp indicating the scale time of the NS", - child=ScaleVnfDataSerializer(help_text="This type represents defines" - "the information to scale a " - "VNF instance to a given " - "level", required=True), - required=False, allow_null=True) - scaleTime = serializers.CharField(help_text="Timestamp indicating the scale time of the NS", - required=False, allow_null=True) - - -class ManualScaleNsReqSerializer(serializers.Serializer): - scaleType = serializers.CharField(help_text="Type of NS Scale", required=True) - scaleNsData = ScaleNsDataSerializer(help_text="Scale NS data", many=True) + scaleType = serializers.ChoiceField( + help_text="Indicates the type of scaling to be performed", + choices=["SCALE_NS", "SCALE_VNF"], + required=True) + scaleNsData = ScaleNsDataSerializer( + help_text="The necessary information to scale the referenced NS instance.", + required=False, + allow_null=True) + scaleVnfData = serializers.ListField( + help_text="Timestamp indicating the scale time of the NS", + child=ScaleVnfDataSerializer( + help_text="This type represents defines the information to scale a VNF instance to a given level", + required=True), + required=False, + allow_null=True) + scaleTime = serializers.CharField( + help_text="Timestamp indicating the scale time of the NS", + required=False, + allow_null=True) + + +# class ManualScaleNsReqSerializer(serializers.Serializer): +# scaleType = serializers.CharField( +# help_text="Type of NS Scale", +# required=True) +# scaleNsData = ScaleNsDataSerializer( +# help_text="Scale NS data", +# many=True) diff --git a/lcm/ns/tests/test_ns_manual_scale.py b/lcm/ns/tests/test_ns_manual_scale.py index 4d32d98e..6ebd5bb0 100644 --- a/lcm/ns/tests/test_ns_manual_scale.py +++ b/lcm/ns/tests/test_ns_manual_scale.py @@ -17,6 +17,7 @@ import uuid import mock from django.test import Client +from rest_framework.test import APIClient from django.test import TestCase from lcm.ns.biz.scaleaspect import get_json_data from rest_framework import status @@ -114,6 +115,7 @@ class TestNsManualScale(TestCase): "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id) self.package_id = "7" self.client = Client() + self.apiClient = APIClient() NSInstModel( id=self.ns_inst_id, name="abc", @@ -179,17 +181,19 @@ class TestNsManualScale(TestCase): def test_ns_manual_scale(self, mock_run): data = { "scaleType": "SCALE_NS", - "scaleNsData": [{ - "scaleNsByStepsData": [{ - "aspectId": "1", - "numberOfSteps": 1, - "scalingDirection": "0" - }] - }] + "scaleNsData": [ + { + "scaleNsByStepsData": [{ + "aspectId": "1", + "numberOfSteps": 1, + "scalingDirection": "0" + }] + } + ] } - response = self.client.post( + response = self.apiClient.post( "/api/nslcm/v1/ns/%s/scale" % - self.ns_inst_id, data=data) + self.ns_inst_id, data=data, format='json') self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code) def test_ns_manual_scale_error_scaletype(self): diff --git a/lcm/ns/tests/test_sol_ns_scale_api.py b/lcm/ns/tests/test_sol_ns_scale_api.py index c484a88d..50e7bc4a 100644 --- a/lcm/ns/tests/test_sol_ns_scale_api.py +++ b/lcm/ns/tests/test_sol_ns_scale_api.py @@ -187,13 +187,13 @@ class TestScaleNsApi(TestCase): def test_ns_scale(self, mock_run): data = { "scaleType": "SCALE_NS", - "scaleNsData": [{ - "scaleNsByStepsData": [{ + "scaleNsData": { + "scaleNsByStepsData": { "aspectId": "1", "numberOfSteps": 1, "scalingDirection": "0" - }] - }] + } + } } response = self.client.post(self.url % self.ns_inst_id, data=data) self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code) @@ -214,13 +214,13 @@ class TestScaleNsApi(TestCase): mock_start.side_effect = NSLCMException("NS scale failed.") data = { "scaleType": "SCALE_NS", - "scaleNsData": [{ - "scaleNsByStepsData": [{ + "scaleNsData": { + "scaleNsByStepsData": { "aspectId": "1", "numberOfSteps": 1, "scalingDirection": "0" - }] - }] + } + } } response = self.client.post(self.url % '11', data=data) self.assertEqual( diff --git a/lcm/ns/views/deprecated/scale_ns_views.py b/lcm/ns/views/deprecated/scale_ns_views.py index ee37532c..20f7b51e 100644 --- a/lcm/ns/views/deprecated/scale_ns_views.py +++ b/lcm/ns/views/deprecated/scale_ns_views.py @@ -13,7 +13,6 @@ # limitations under the License. import logging import traceback - from drf_yasg.utils import swagger_auto_schema from rest_framework import status from rest_framework.response import Response @@ -43,8 +42,12 @@ class NSManualScaleView(APIView): req_serializer = _ManualScaleNsReqSerializer(data=request.data) if not req_serializer.is_valid(): raise NSLCMException(req_serializer.errors) - - NSManualScaleService(ns_instance_id, request.data, job_id).start() + req = request.data + scale_data = {} + scale_data['scaleType'] = req['scaleType'] + scaleNsData = req['scaleNsData'][0] + scale_data['scaleNsData'] = {"scaleNsByStepsData": scaleNsData['scaleNsByStepsData'][0]} + NSManualScaleService(ns_instance_id, scale_data, job_id).start() resp_serializer = _NsOperateJobSerializer(data={'jobId': job_id}) if not resp_serializer.is_valid(): diff --git a/lcm/ns/views/sol/scale_ns_views.py b/lcm/ns/views/sol/scale_ns_views.py index 8a5c19d5..07de7d4c 100644 --- a/lcm/ns/views/sol/scale_ns_views.py +++ b/lcm/ns/views/sol/scale_ns_views.py @@ -18,7 +18,7 @@ from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView from lcm.ns.biz.ns_manual_scale import NSManualScaleService -from lcm.ns.serializers.sol.scale_ns_serializers import ManualScaleNsReqSerializer +from lcm.ns.serializers.sol.scale_ns_serializers import ScaleNsRequestSerializer from lcm.pub.exceptions import NSLCMException from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE from lcm.ns.const import NS_OCC_BASE_URI @@ -30,7 +30,7 @@ logger = logging.getLogger(__name__) class ScaleNSView(APIView): @swagger_auto_schema( - request_body=ManualScaleNsReqSerializer(help_text="NS Scale"), + request_body=ScaleNsRequestSerializer(help_text="NS Scale"), responses={ status.HTTP_202_ACCEPTED: "HTTP_202_ACCEPTED", status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer() @@ -40,7 +40,7 @@ class ScaleNSView(APIView): logger.debug("Enter ScaleNSView::post %s, %s", request.data, ns_instance_id) job_id = JobUtil.create_job("NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_instance_id) try: - req_serializer = ManualScaleNsReqSerializer(data=request.data) + req_serializer = ScaleNsRequestSerializer(data=request.data) if not req_serializer.is_valid(): raise NSLCMException(req_serializer.errors) nsManualScaleService = NSManualScaleService(ns_instance_id, request.data, job_id) -- 2.16.6