Add vnf Grant bussiness logic 37/34237/2
authorfujinhua <fu.jinhua@zte.com.cn>
Tue, 6 Mar 2018 08:35:02 +0000 (16:35 +0800)
committerfujinhua <fu.jinhua@zte.com.cn>
Tue, 6 Mar 2018 08:44:06 +0000 (16:44 +0800)
Change-Id: I15acd9944ff4aa3fb55d189bde70b65302814f2a
Issue-ID: VFC-779
Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
lcm/v2/grant_vnf.py [new file with mode: 0644]
lcm/v2/views.py

diff --git a/lcm/v2/grant_vnf.py b/lcm/v2/grant_vnf.py
new file mode 100644 (file)
index 0000000..832c781
--- /dev/null
@@ -0,0 +1,46 @@
+# Copyright 2018 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import json
+import logging
+import uuid
+
+logger = logging.getLogger(__name__)
+
+
+class GrantVnf(object):
+    def __init__(self, grant_data):
+        self.data = grant_data
+
+    def exec_grant(self):
+        if isinstance(self.data, (unicode, str)):
+            self.data = json.JSONDecoder().decode(self.data)
+        grant_resp = {
+            "id": str(uuid.uuid4()),
+            "vnfInstanceId": self.data.get("vnfInstanceId"),
+            "vnfLcmOpOccId": self.data.get("vnfLcmOpOccId"),
+            "_links": {
+                "self": {
+                    "href": "/grants"
+                },
+                "vnfLcmOpOcc": {
+                    "href": self.data.get("_links").get("vnfLcmOpOcc")
+                },
+                "vnfInstance": {
+                    "href": self.data.get("_links").get("vnfInstance")
+                }
+            }
+        }
+        logger.debug("grant_resp=%s", grant_resp)
+        return grant_resp
index 19f6740..3fa240d 100644 (file)
@@ -21,6 +21,7 @@ from drf_yasg.utils import swagger_auto_schema
 
 from lcm.v2.serializers import GrantRequestSerializer
 from lcm.v2.serializers import GrantSerializer
+from lcm.v2.grant_vnf import GrantVnf
 
 logger = logging.getLogger(__name__)
 
@@ -29,7 +30,9 @@ class VnfGrantView(APIView):
     @swagger_auto_schema(
         request_body=GrantRequestSerializer(),
         responses={
-            status.HTTP_201_CREATED: GrantSerializer(),
+            status.HTTP_201_CREATED: GrantSerializer(
+                help_text="The grant was created successfully (synchronous mode)."
+            ),
             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
         }
     )
@@ -39,8 +42,15 @@ class VnfGrantView(APIView):
             req_serializer = GrantRequestSerializer(data=request.data)
             if not req_serializer.is_valid():
                 raise Exception(req_serializer.errors)
+
+            grant_resp = GrantVnf(request.data).exec_grant()
+
+            resp_serializer = GrantSerializer(data=grant_resp)
+            if not resp_serializer.is_valid():
+                raise Exception(resp_serializer.errors)
+
+            return Response(data=resp_serializer.data, status=status.HTTP_201_CREATED)
         except Exception as e:
             logger.error(traceback.format_exc())
             logger.error("Exception in VnfGrant: %s", e.message)
             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
-        return Response(data={}, status=status.HTTP_201_CREATED)