Add subscriptions API to GVNFM
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / views / subscriptions_view.py
diff --git a/lcm/lcm/nf/views/subscriptions_view.py b/lcm/lcm/nf/views/subscriptions_view.py
new file mode 100644 (file)
index 0000000..876798d
--- /dev/null
@@ -0,0 +1,74 @@
+# Copyright (C) 2018 Verizon. All Rights Reserved\r
+#\r
+# Licensed under the Apache License, Version 2.0 (the "License");\r
+# you may not use this file except in compliance with the License.\r
+# You may obtain a copy of the License at\r
+#\r
+#         http://www.apache.org/licenses/LICENSE-2.0\r
+#\r
+# Unless required by applicable law or agreed to in writing, software\r
+# distributed under the License is distributed on an "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+# See the License for the specific language governing permissions and\r
+# limitations under the License.\r
+\r
+import ast\r
+import json\r
+import logging\r
+import traceback\r
+\r
+from drf_yasg.utils import swagger_auto_schema\r
+from lcm.nf.biz.create_subscription import CreateSubscription\r
+from rest_framework import status\r
+from rest_framework.response import Response\r
+from rest_framework.views import APIView\r
+\r
+from lcm.nf.serializers.lccn_subscription_request import LccnSubscriptionRequestSerializer\r
+from lcm.nf.serializers.lccn_subscription import LccnSubscriptionSerializer\r
+from lcm.pub.exceptions import NFLCMException\r
+\r
+logger = logging.getLogger(__name__)\r
+\r
+\r
+class SubscriptionsView(APIView):\r
+    @swagger_auto_schema(\r
+        request_body=LccnSubscriptionRequestSerializer(),\r
+        responses={\r
+            status.HTTP_201_CREATED: LccnSubscriptionSerializer(),\r
+            status.HTTP_303_SEE_OTHER: "",\r
+            status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"\r
+        }\r
+    )\r
+    def post(self, request):\r
+        logger.debug("SubscribeNotification--post::> %s" % request.data)\r
+        try:\r
+            lccn_subscription_request_serializer = LccnSubscriptionRequestSerializer(data=request.data)\r
+            if not lccn_subscription_request_serializer.is_valid():\r
+                raise NFLCMException(lccn_subscription_request_serializer.errors)\r
+            subscription = CreateSubscription(\r
+                lccn_subscription_request_serializer.data).do_biz()\r
+            lccn_notifications_filter = {\r
+                "notificationTypes": ast.literal_eval(subscription.notification_types),\r
+                "operationTypes": ast.literal_eval(subscription.operation_types),\r
+                "operationStates": ast.literal_eval(subscription.operation_states),\r
+                "vnfInstanceSubscriptionFilter": json.loads(subscription.vnf_instance_filter)\r
+            }\r
+            subscription_data = {\r
+                "id": subscription.subscription_id,\r
+                "callbackUri": subscription.callback_uri,\r
+                "_links": json.loads(subscription.links),\r
+                "filter": lccn_notifications_filter\r
+            }\r
+            sub_resp_serializer = LccnSubscriptionSerializer(data=subscription_data)\r
+            if not sub_resp_serializer.is_valid():\r
+                raise NFLCMException(sub_resp_serializer.errors)\r
+            return Response(data=sub_resp_serializer.data, status=status.HTTP_201_CREATED)\r
+        except NFLCMException as e:\r
+            logger.error(e.message)\r
+            if "exists" in e.message:\r
+                return Response(data={'error': '%s' % e.message}, status=status.HTTP_303_SEE_OTHER)\r
+            return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)\r
+        except Exception as e:\r
+            logger.error(e.message)\r
+            logger.error(traceback.format_exc())\r
+            return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)\r