Optimizing instantiation code
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / views.py
index a1caefa..e1248e7 100644 (file)
 # 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
+import os
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
 from rest_framework.views import APIView
 
-from lcm.pub.database.models import VnfInstModel
+from lcm.nf.vnfs.vnf_cancel.delete_vnf_identifier import DeleteVnf
+from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf
+from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
+from lcm.pub.exceptions import NFLCMException
 from lcm.pub.utils.jobutil import JobUtil
-from lcm.pub.utils.timeutil import now_time
-from lcm.pub.utils.values import ignore_case_get
 
 logger = logging.getLogger(__name__)
 
@@ -30,40 +32,40 @@ logger = logging.getLogger(__name__)
 class CreateVnfIdentifier(APIView):
     def post(self, request):
         logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
-        self.vnfd_id = ignore_case_get(request.data, "vnfdId")
-        self.vnf_instance_mame = ignore_case_get(request.data, "vnfInstanceName")
-        self.description = ignore_case_get(request.data, "vnfInstanceDescription")
-        self.nf_inst_id = str(uuid.uuid4())
-        VnfInstModel(id=self.nf_inst_id, name=self.vnf_instance_mame, vnfd_id=self.vnfd_id,
-                     description=self.description, status='empty', create_time=now_time(), lastuptime=now_time()).save()
-        vnf_inst = VnfInstModel.objects.get(id=self.nf_inst_id)
-        logger.debug('id is [%s],name is [%s],vnfd_id is [%s],description is [%s],create_time is [%s],lastuptime is [%s],' %
-                     (vnf_inst.id, vnf_inst.name, vnf_inst.vnfd_id, vnf_inst.description, vnf_inst.create_time, vnf_inst.lastuptime))
-        rsp = {"vnfInstanceId": self.nf_inst_id}
+        try:
+            nf_inst_id = CreateVnf(request.data).do_biz()
+        except NFLCMException as e:
+            logger.error(e.message)
+            return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+        except Exception:
+            logger.error(traceback.format_exc())
+            return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+        rsp = {"vnfInstanceId": nf_inst_id}
         return Response(data=rsp, status=status.HTTP_201_CREATED)
 
 
 class InstantiateVnf(APIView):
-    def post(self, request, instanceId):
+    def post(self, request, instanceid):
         logger.debug("InstantiateVnf--post::> %s" % request.data)
-        data = {'flavourId': ignore_case_get(request.data, 'flavourId'),
-                'instantiationLevelId': ignore_case_get(request.data, 'instantiationLevelId'),
-                'extVirtualLinks': ignore_case_get(request.data, 'extVirtualLinks'),
-                'localizationLanguage': ignore_case_get(request.data, 'localizationLanguage'),
-                'additionalParams': ignore_case_get(request.data, 'additionalParams')}
-        nf_inst_id = instanceId
-        job_id = JobUtil.create_job('NF', 'CREATE', nf_inst_id)
+        job_id = JobUtil.create_job('NF', 'INSTANTIATE', instanceid)
         JobUtil.add_job_status(job_id, 0, "INST_VNF_READY")
-
-        # CreateVnfs(data, nf_inst_id, job_id).start()
+        InstVnf(request.data, instanceid, job_id).start()
         rsp = {"jobId": job_id}
         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
 
 
 class DeleteVnfIdentifier(APIView):
-    def delete(self, request):
+    def delete(self, request, instanceid):
         logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data)
-        return Response(data='', status=status.HTTP_202_ACCEPTED)
+        try:
+            DeleteVnf(request.data, instanceid).do_biz()
+        except NFLCMException as e:
+            logger.error(e.message)
+            return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+        except Exception:
+            logger.error(traceback.format_exc())
+            return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+        return Response(data={}, status=status.HTTP_204_NO_CONTENT)
 
 
 class TerminateVnf(APIView):
@@ -87,4 +89,13 @@ class QuerySingleVnf(APIView):
 class GetOperationStatus(APIView):
     def get(self, request):
         logger.debug("GetOperationStatus--get::> %s" % request.data)
-        return Response(data='', status=status.HTTP_202_ACCEPTED)
\ No newline at end of file
+        return Response(data='', status=status.HTTP_202_ACCEPTED)
+
+
+class SwaggerJsonView(APIView):
+    def get(self, request):
+        json_file = os.path.join(os.path.dirname(__file__), 'swagger.json')
+        f = open(json_file)
+        json_data = json.JSONDecoder().decode(f.read())
+        f.close()
+        return Response(json_data)