aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfujinhua <fu.jinhua@zte.com.cn>2019-04-20 16:53:33 +0800
committerfujinhua <fu.jinhua@zte.com.cn>2019-04-20 16:53:33 +0800
commit1ffd227f233a52636f5ba1e0326dd12ac08ff7ce (patch)
treeba2b37fe06596b182cce748cd4ae02954385e124
parentb13f140070ad7df65bdb078d8dc3cef5abb9d10d (diff)
Refactor codes for vnf subscription view
Change-Id: Id568a371a24f417245aa886bce3532c6b3ce439d Issue-ID: VFC-1307 Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
-rw-r--r--catalog/packages/views/common.py14
-rwxr-xr-xcatalog/packages/views/vnf_package_subscription_views.py113
2 files changed, 51 insertions, 76 deletions
diff --git a/catalog/packages/views/common.py b/catalog/packages/views/common.py
index 73f82c47..43455c5c 100644
--- a/catalog/packages/views/common.py
+++ b/catalog/packages/views/common.py
@@ -24,6 +24,8 @@ from catalog.pub.exceptions import PackageNotFoundException
from catalog.pub.exceptions import ResourceNotFoundException
from catalog.pub.exceptions import ArtifactNotFoundException
from catalog.pub.exceptions import NsdmDuplicateSubscriptionException
+from catalog.pub.exceptions import VnfPkgDuplicateSubscriptionException
+from catalog.pub.exceptions import VnfPkgSubscriptionException
logger = logging.getLogger(__name__)
@@ -61,6 +63,12 @@ def view_safe_call_with_log(logger):
detail=e.message,
status=status.HTTP_303_SEE_OTHER
)
+ except VnfPkgDuplicateSubscriptionException as e:
+ logger.error(e.message)
+ return make_error_resp(
+ detail=e.message,
+ status=status.HTTP_303_SEE_OTHER
+ )
except PackageNotFoundException as e:
logger.error(e.message)
return make_error_resp(
@@ -85,6 +93,12 @@ def view_safe_call_with_log(logger):
detail=e.message,
status=status.HTTP_400_BAD_REQUEST
)
+ except VnfPkgSubscriptionException as e:
+ logger.error(e.message)
+ return make_error_resp(
+ detail=e.message,
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR
+ )
except CatalogException as e:
logger.error(e.message)
return make_error_resp(
diff --git a/catalog/packages/views/vnf_package_subscription_views.py b/catalog/packages/views/vnf_package_subscription_views.py
index 70c9abc6..303f0d40 100755
--- a/catalog/packages/views/vnf_package_subscription_views.py
+++ b/catalog/packages/views/vnf_package_subscription_views.py
@@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-
-import traceback
import logging
from drf_yasg.utils import swagger_auto_schema
@@ -21,13 +19,16 @@ from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
-from catalog.packages.serializers.vnf_pkg_subscription import PkgmSubscriptionRequestSerializer, \
- PkgmSubscriptionSerializer, PkgmSubscriptionsSerializer
+from catalog.packages.serializers.vnf_pkg_subscription import PkgmSubscriptionRequestSerializer
+from catalog.packages.serializers.vnf_pkg_subscription import PkgmSubscriptionSerializer
+from catalog.packages.serializers.vnf_pkg_subscription import PkgmSubscriptionsSerializer
from catalog.packages.serializers.response import ProblemDetailsSerializer
-from catalog.packages.biz.vnf_pkg_subscription import CreateSubscription, QuerySubscription, TerminateSubscription
+from catalog.packages.biz.vnf_pkg_subscription import CreateSubscription
+from catalog.packages.biz.vnf_pkg_subscription import QuerySubscription
+from catalog.packages.biz.vnf_pkg_subscription import TerminateSubscription
from catalog.packages.views.common import validate_data
-from catalog.pub.exceptions import VnfPkgDuplicateSubscriptionException, VnfPkgSubscriptionException, \
- SubscriptionDoesNotExistsException
+from catalog.pub.exceptions import VnfPkgSubscriptionException
+from .common import view_safe_call_with_log
logger = logging.getLogger(__name__)
VALID_FILTERS = ["callbackUri", "notificationTypes", "vnfdId", "vnfPkgId", "operationalState", "usageState"]
@@ -52,25 +53,14 @@ class CreateQuerySubscriptionView(APIView):
status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
}
)
+ @view_safe_call_with_log(logger=logger)
def post(self, request):
logger.debug("Create VNF package Subscription> %s" % request.data)
- try:
- vnf_pkg_subscription_request = validate_data(request.data, PkgmSubscriptionRequestSerializer)
- data = CreateSubscription(vnf_pkg_subscription_request.data).do_biz()
- subscription_info = validate_data(data, PkgmSubscriptionSerializer)
- return Response(data=subscription_info.data, status=status.HTTP_201_CREATED)
- except VnfPkgDuplicateSubscriptionException as e:
- logger.error(e.message)
- logger.error(traceback.format_exc())
- problem_details_serializer = get_problem_details_serializer(status.HTTP_303_SEE_OTHER,
- traceback.format_exc())
- return Response(data=problem_details_serializer.data, status=status.HTTP_303_SEE_OTHER)
- except Exception as e:
- logger.error(e.message)
- logger.error(traceback.format_exc())
- problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR,
- traceback.format_exc())
- return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ vnf_pkg_subscription_request = validate_data(request.data, PkgmSubscriptionRequestSerializer)
+ data = CreateSubscription(vnf_pkg_subscription_request.data).do_biz()
+ subscription_info = validate_data(data, PkgmSubscriptionSerializer)
+ return Response(data=subscription_info.data, status=status.HTTP_201_CREATED)
@swagger_auto_schema(
responses={
@@ -79,27 +69,21 @@ class CreateQuerySubscriptionView(APIView):
status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
}
)
+ @view_safe_call_with_log(logger=logger)
def get(self, request):
logger.debug("SubscribeNotification--get::> %s" % request.query_params)
- try:
- if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
- problem_details_serializer = get_problem_details_serializer(status.HTTP_400_BAD_REQUEST,
- "Not a valid filter")
- return Response(data=problem_details_serializer.data, status=status.HTTP_400_BAD_REQUEST)
- resp_data = QuerySubscription().query_multi_subscriptions(request.query_params)
- subscriptions_serializer = PkgmSubscriptionsSerializer(data=resp_data)
- if not subscriptions_serializer.is_valid():
- raise VnfPkgSubscriptionException(subscriptions_serializer.errors)
+ if request.query_params and not set(request.query_params).issubset(set(VALID_FILTERS)):
+ problem_details_serializer = get_problem_details_serializer(status.HTTP_400_BAD_REQUEST,
+ "Not a valid filter")
+ return Response(data=problem_details_serializer.data, status=status.HTTP_400_BAD_REQUEST)
+ resp_data = QuerySubscription().query_multi_subscriptions(request.query_params)
- return Response(data=subscriptions_serializer.data, status=status.HTTP_200_OK)
+ subscriptions_serializer = PkgmSubscriptionsSerializer(data=resp_data)
+ if not subscriptions_serializer.is_valid():
+ raise VnfPkgSubscriptionException(subscriptions_serializer.errors)
- except Exception as e:
- logger.error(e.message)
- logger.error(traceback.format_exc())
- problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR,
- traceback.format_exc())
- return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ return Response(data=subscriptions_serializer.data, status=status.HTTP_200_OK)
class QueryTerminateSubscriptionView(APIView):
@@ -111,29 +95,17 @@ class QueryTerminateSubscriptionView(APIView):
status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
}
)
+ @view_safe_call_with_log(logger=logger)
def get(self, request, subscriptionId):
logger.debug("SubscribeNotification--get::> %s" % subscriptionId)
- try:
-
- resp_data = QuerySubscription().query_single_subscription(subscriptionId)
-
- subscription_serializer = PkgmSubscriptionSerializer(data=resp_data)
- if not subscription_serializer.is_valid():
- raise VnfPkgSubscriptionException(subscription_serializer.errors)
-
- return Response(data=subscription_serializer.data, status=status.HTTP_200_OK)
- except SubscriptionDoesNotExistsException as e:
- logger.error(e.message)
- logger.error(traceback.format_exc())
- problem_details_serializer = get_problem_details_serializer(status.HTTP_404_NOT_FOUND,
- traceback.format_exc())
- return Response(data=problem_details_serializer.data, status=status.HTTP_404_NOT_FOUND)
- except Exception as e:
- logger.error(e.message)
- logger.error(traceback.format_exc())
- problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR,
- traceback.format_exc())
- return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ resp_data = QuerySubscription().query_single_subscription(subscriptionId)
+
+ subscription_serializer = PkgmSubscriptionSerializer(data=resp_data)
+ if not subscription_serializer.is_valid():
+ raise VnfPkgSubscriptionException(subscription_serializer.errors)
+
+ return Response(data=subscription_serializer.data, status=status.HTTP_200_OK)
@swagger_auto_schema(
responses={
@@ -142,20 +114,9 @@ class QueryTerminateSubscriptionView(APIView):
status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
}
)
+ @view_safe_call_with_log(logger=logger)
def delete(self, request, subscriptionId):
logger.debug("SubscribeNotification--get::> %s" % subscriptionId)
- try:
- TerminateSubscription().terminate(subscriptionId)
- return Response(status=status.HTTP_204_NO_CONTENT)
- except SubscriptionDoesNotExistsException as e:
- logger.error(e.message)
- logger.error(traceback.format_exc())
- problem_details_serializer = get_problem_details_serializer(status.HTTP_404_NOT_FOUND,
- traceback.format_exc())
- return Response(data=problem_details_serializer.data, status=status.HTTP_404_NOT_FOUND)
- except Exception as e:
- logger.error(e.message)
- logger.error(traceback.format_exc())
- problem_details_serializer = get_problem_details_serializer(status.HTTP_500_INTERNAL_SERVER_ERROR,
- traceback.format_exc())
- return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ TerminateSubscription().terminate(subscriptionId)
+ return Response(status=status.HTTP_204_NO_CONTENT)