refactor get vnf and vnfs, others will be refactored after. 36/100036/5
authorhewei-cmss <hewei@cmss.chinamobile.com>
Tue, 7 Jan 2020 07:32:23 +0000 (15:32 +0800)
committerhewei-cmss <hewei@cmss.chinamobile.com>
Tue, 7 Jan 2020 08:54:33 +0000 (16:54 +0800)
Issue-ID: VFC-1599

Signed-off-by: hewei-cmss <hewei@cmss.chinamobile.com>
Change-Id: Id4bc25fa8440fc2fa3971dd06dac52b502505b29

res/res/biz/__init__.py [new file with mode: 0644]
res/res/biz/base.py [new file with mode: 0644]
res/res/biz/vnfs_get.py [new file with mode: 0644]
res/res/resources/urls.py
res/res/resources/views/__init__.py [new file with mode: 0644]
res/res/resources/views/base_view.py [new file with mode: 0644]
res/res/resources/views/get_vnfs_view.py [new file with mode: 0644]
res/res/resources/views/views.py [moved from res/res/resources/views.py with 52% similarity]

diff --git a/res/res/biz/__init__.py b/res/res/biz/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/res/res/biz/base.py b/res/res/biz/base.py
new file mode 100644 (file)
index 0000000..b7d2aea
--- /dev/null
@@ -0,0 +1,40 @@
+# Copyright @ 2020 China Mobile (SuZhou) Software Technology Co.,Ltd.\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
+import logging\r
+\r
+from rest_framework import status\r
+from rest_framework.response import Response\r
+\r
+from res.pub.exceptions import VNFRESException\r
+\r
+logger = logging.getLogger(__name__)\r
+\r
+\r
+class BaseService(object):\r
+\r
+    def query_resources(self, res_type, logger, resources, cvt_fun, res_serializer):\r
+        logger.debug("Enter query %s", res_type)\r
+\r
+        resp = {\r
+            'resp_data': [cvt_fun(res) for res in resources]\r
+        }\r
+\r
+        resp_serializer = res_serializer(data=resp)\r
+        if not resp_serializer.is_valid():\r
+            raise VNFRESException(resp_serializer.errors)\r
+\r
+        return Response(\r
+            data=resp,\r
+            status=status.HTTP_200_OK\r
+        )\r
diff --git a/res/res/biz/vnfs_get.py b/res/res/biz/vnfs_get.py
new file mode 100644 (file)
index 0000000..d767ae8
--- /dev/null
@@ -0,0 +1,161 @@
+# Copyright @ 2020 China Mobile (SuZhou) Software Technology Co.,Ltd.\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
+import logging\r
+\r
+from res.biz.base import BaseService\r
+from res.pub.exceptions import VNFRESException\r
+from res.pub.exceptions import NotFoundException\r
+from res.pub.database.models import NfInstModel\r
+from res.pub.database.models import StorageInstModel\r
+from res.pub.database.models import NetworkInstModel\r
+from res.pub.database.models import VLInstModel\r
+from res.pub.database.models import VNFCInstModel\r
+from res.pub.database.models import VmInstModel\r
+from res.resources.serializers import VnfsInfoSerializer\r
+\r
+logger = logging.getLogger(__name__)\r
+\r
+\r
+class GetVnfsService(BaseService):\r
+\r
+    def __init__(self):\r
+        super(GetVnfsService, self).__init__()\r
+\r
+    def get_vnfs(self):\r
+        return self.query_resources(\r
+            res_type="Vnfs",\r
+            logger=logger,\r
+            resources=NfInstModel.objects.all(),\r
+            cvt_fun=self._fill_resp_data,\r
+            res_serializer=VnfsInfoSerializer\r
+        )\r
+\r
+    def get_vnf(self, vnf_instance_id):\r
+        vnf = NfInstModel.objects.filter(nfinstid=vnf_instance_id)\r
+        if not vnf:\r
+            raise NotFoundException('Vnf(%s) does not exist' % vnf_instance_id)\r
+\r
+        return self._fill_resp_data(vnf[0])\r
+\r
+    def _fill_resp_data(self, vnf):\r
+        def make_virtual_storage_resource():\r
+            logger.info('Get the StorageInstModel of list')\r
+            return [{"virtualStorageInstanceId": s.storageid,\r
+                     "virtualStorageDescId": s.storagetype,\r
+                     "storageResource": {\r
+                         "vimId": s.vimid,\r
+                         "resourceId": s.resouceid\r
+                     }}\r
+                    for s in StorageInstModel.objects.filter(instid=vnf.nfinstid)]\r
+\r
+        def make_virtual_link_resource():\r
+            logger.info('Get the VLInstModel of list.')\r
+            vl_inst = VLInstModel.objects.filter(ownerid=vnf.nfinstid)\r
+            vl_arr = []\r
+            for v in vl_inst:\r
+                net = NetworkInstModel.objects.filter(networkid=v.relatednetworkid)\r
+                if not net:\r
+                    raise VNFRESException(\r
+                        'NetworkInst(%s) does not exist.' %\r
+                        v.relatednetworkid)\r
+                v_dic = {\r
+                    "virtualLinkInstanceId": v.vlinstanceid,\r
+                    "virtualLinkDescId": v.vldid,\r
+                    "networkResource": {\r
+                        "vimId": net[0].vimid,\r
+                        "resourceId": net[0].resouceid\r
+                    }\r
+                }\r
+                vl_arr.append(v_dic)\r
+            return vl_arr\r
+\r
+        def make_vnfc_resource():\r
+            logger.info('Get VNFCInstModel of list.')\r
+            vnfc_insts = VNFCInstModel.objects.filter(instid=vnf.nfinstid)\r
+            vnfc_arr = []\r
+            for vnfc in vnfc_insts:\r
+                vm = VmInstModel.objects.filter(vmid=vnfc.vmid)\r
+                if not vm:\r
+                    raise VNFRESException('VmInst(%s) does not exist.' % vnfc.vmid)\r
+                storage = StorageInstModel.objects.filter(ownerid=vm[0].vmid)\r
+                if not storage:\r
+                    raise VNFRESException(\r
+                        'StorageInst(%s) does not exist.' %\r
+                        vm[0].vmid)\r
+                vnfc_dic = {\r
+                    "vnfcInstanceId": vnfc.vnfcinstanceid,\r
+                    "vduId": vnfc.vduid,\r
+                    "computeResource": {\r
+                        "vimId": vm[0].vimid,\r
+                        "resourceId": vm[0].resouceid\r
+                    },\r
+                    "storageResourceIds": [s.storageid for s in storage]\r
+                }\r
+                vnfc_arr.append(vnfc_dic)\r
+            return vnfc_arr\r
+\r
+        def make_vm():\r
+            logger.info('Get the VimInstModel of list.')\r
+            return [{"vmid": vm.vmid,\r
+                     "vimid": vm.vimid,\r
+                     "tenant": vm.tenant,\r
+                     "resouceid": vm.resouceid,\r
+                     "vmname": vm.vmname,\r
+                     "nic_array": vm.nic_array,\r
+                     "metadata": vm.metadata,\r
+                     "volume_array": vm.volume_array,\r
+                     "server_group": vm.server_group,\r
+                     "availability_zone": vm.availability_zone,\r
+                     "flavor_id": vm.flavor_id,\r
+                     "security_groups": vm.security_groups,\r
+                     "operationalstate": vm.operationalstate,\r
+                     "insttype": vm.insttype,\r
+                     "is_predefined": vm.is_predefined,\r
+                     "create_time": vm.create_time,\r
+                     "instid": vm.instid,\r
+                     "nodeId": vm.nodeId}\r
+                    for vm in VmInstModel.objects.filter(instid=vnf.nfinstid)]\r
+\r
+        virtual_storage_resources = make_virtual_storage_resource()\r
+        virtual_link_resource = make_virtual_link_resource()\r
+        vnfc_resource = make_vnfc_resource()\r
+        vm_info = make_vm()\r
+        return {\r
+            "vnfInstanceId": vnf.nfinstid,\r
+            "vnfInstanceName": vnf.nf_name,\r
+            "vnfInstanceDescription": vnf.nf_desc,\r
+            "onboardedVnfPkgInfoId": vnf.package_id,\r
+            "vnfdId": vnf.vnfdid,\r
+            "vnfdVersion": vnf.version,\r
+            "vnfSoftwareVersion": vnf.vnfSoftwareVersion,\r
+            "vnfProvider": vnf.vendor,\r
+            "vnfProductName": vnf.netype,\r
+            "vnfConfigurableProperties": vnf.vnfConfigurableProperties,\r
+            "instantiationState": vnf.status,\r
+            "instantiatedVnfInfo": {\r
+                "flavourId": vnf.flavour_id,\r
+                "vnfState": vnf.status,\r
+                "scaleStatus": [],\r
+                "extCpInfo": [],\r
+                "extVirtualLink": [],\r
+                "monitoringParameters": {},\r
+                "localizationLanguage": vnf.localizationLanguage,\r
+                "vmInfo": vm_info,\r
+                "vnfcResourceInfo": vnfc_resource,\r
+                "virtualLinkResourceInfo": virtual_link_resource,\r
+                "virtualStorageResourceInfo": virtual_storage_resources\r
+            },\r
+            "metadata": vnf.input_params,\r
+            "extensions": vnf.vnfd_model\r
+        }\r
index 41c9d08..f0906d4 100644 (file)
 
 from django.conf.urls import url
 
-from res.resources import views
+from res.resources.views.get_vnfs_view import GetVnfView
+from res.resources.views.get_vnfs_view import GetVnfsView
+from res.resources.views import views
 from res.resources.health_check_views import HealthCheckView
 
 urlpatterns = [
-    url(r'^api/vnfres/v1/vnfs/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)$', views.getVnf.as_view(), name='get_vnf'),
-    url(r'^api/vnfres/v1/vnfs$', views.getVnfs.as_view(), name='get_vnfs'),
+    url(r'^api/vnfres/v1/vnfs/(?P<vnf_instance_id>[0-9a-zA-Z\-\_]+)$', GetVnfView.as_view(), name='get_vnf'),
+    url(r'^api/vnfres/v1/vnfs$', GetVnfsView.as_view(), name='get_vnfs'),
     url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/vms$', views.getVms.as_view(), name='get_vms'),
     url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/flavors$', views.getFlavors.as_view(), name='get_flavors'),
     url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/networks$', views.getNetworks.as_view(), name='get_networks'),
diff --git a/res/res/resources/views/__init__.py b/res/res/resources/views/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/res/res/resources/views/base_view.py b/res/res/resources/views/base_view.py
new file mode 100644 (file)
index 0000000..2d7ba9a
--- /dev/null
@@ -0,0 +1,64 @@
+# Copyright @ 2020 China Mobile (SuZhou) Software Technology Co.,Ltd.\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
+import logging\r
+import traceback\r
+\r
+from rest_framework import status\r
+from rest_framework.response import Response\r
+\r
+from res.pub.exceptions import VNFRESException\r
+from res.pub.exceptions import NotFoundException\r
+\r
+\r
+logger = logging.getLogger(__name__)\r
+\r
+\r
+def make_error_resp(status, detail):\r
+    return Response(\r
+        data={\r
+            'status': status,\r
+            'detail': detail\r
+        },\r
+        status=status\r
+    )\r
+\r
+\r
+def view_safe_call_with_log(logger):\r
+    def view_safe_call(func):\r
+        def wrapper(*args, **kwargs):\r
+            try:\r
+                return func(*args, **kwargs)\r
+            except NotFoundException as e:\r
+                logger.error(e.args[0])\r
+                return make_error_resp(\r
+                    detail=e.args[0],\r
+                    status=status.HTTP_404_NOT_FOUND\r
+                )\r
+            except VNFRESException as e:\r
+                logger.error(e.args[0])\r
+                return make_error_resp(\r
+                    detail=e.args[0],\r
+                    status=status.HTTP_500_INTERNAL_SERVER_ERROR\r
+                )\r
+            except Exception as e:\r
+                logger.error(e.args[0])\r
+                logger.error(traceback.format_exc())\r
+                return make_error_resp(\r
+                    detail='Unexpected exception',\r
+                    status=status.HTTP_500_INTERNAL_SERVER_ERROR\r
+                )\r
+\r
+        return wrapper\r
+\r
+    return view_safe_call\r
diff --git a/res/res/resources/views/get_vnfs_view.py b/res/res/resources/views/get_vnfs_view.py
new file mode 100644 (file)
index 0000000..eb6bced
--- /dev/null
@@ -0,0 +1,62 @@
+# Copyright @ 2020 China Mobile (SuZhou) Software Technology Co.,Ltd.\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
+import logging\r
+\r
+from rest_framework import status\r
+from rest_framework.response import Response\r
+from rest_framework.views import APIView\r
+from drf_yasg.utils import swagger_auto_schema\r
+from res.biz.vnfs_get import GetVnfsService\r
+from res.pub.exceptions import VNFRESException\r
+from res.pub.utils.syscomm import fun_name\r
+from res.resources.serializers import VnfInfoSerializer, VnfsInfoSerializer\r
+from res.resources.views.base_view import view_safe_call_with_log\r
+\r
+logger = logging.getLogger(__name__)\r
+\r
+\r
+class GetVnfView(APIView):\r
+    @swagger_auto_schema(\r
+        responses={\r
+            status.HTTP_200_OK: VnfInfoSerializer(),\r
+            status.HTTP_404_NOT_FOUND: 'Vnf does not exist',\r
+            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'\r
+        }\r
+    )\r
+    @view_safe_call_with_log(logger=logger)\r
+    def get(self, request, vnf_instance_id):\r
+        logger.debug("[%s]vnf_inst_id=%s", fun_name(), vnf_instance_id)\r
+\r
+        resp_data = GetVnfsService().get_vnf(vnf_instance_id)\r
+\r
+        vnf_info_serializer = VnfInfoSerializer(data=resp_data)\r
+        if not vnf_info_serializer.is_valid():\r
+            raise VNFRESException(vnf_info_serializer.errors)\r
+\r
+        return Response(\r
+            data=resp_data,\r
+            status=status.HTTP_200_OK\r
+        )\r
+\r
+\r
+class GetVnfsView(APIView):\r
+    @swagger_auto_schema(\r
+        responses={\r
+            status.HTTP_200_OK: VnfsInfoSerializer(),\r
+            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'\r
+        }\r
+    )\r
+    @view_safe_call_with_log(logger=logger)\r
+    def get(self, request):\r
+        return GetVnfsService().get_vnfs()\r
similarity index 52%
rename from res/res/resources/views.py
rename to res/res/resources/views/views.py
index e3f2014..a0a5897 100644 (file)
-# Copyright 2017 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 logging
-import traceback
-
-from drf_yasg.utils import swagger_auto_schema
-from rest_framework import status
-from rest_framework.response import Response
-from rest_framework.views import APIView
-
-from res.pub.exceptions import VNFRESException
-from res.pub.exceptions import NotFoundException
-from res.pub.utils.syscomm import fun_name
-from res.pub.database.models import NfInstModel
-from res.pub.database.models import StorageInstModel
-from res.pub.database.models import NetworkInstModel
-from res.pub.database.models import VLInstModel
-from res.pub.database.models import VNFCInstModel
-from res.pub.database.models import VmInstModel
-from res.pub.database.models import FlavourInstModel
-from res.pub.database.models import SubNetworkInstModel
-from res.pub.database.models import CPInstModel
-from res.resources.serializers import VolumeInfoSerializer
-from res.resources.serializers import CpsInfoSerializer
-from res.resources.serializers import SubnetInfoSerializer
-from res.resources.serializers import NetworkInfoSerializer
-from res.resources.serializers import FlavorInfoSerializer
-from res.resources.serializers import VmInfoSerializer
-from res.resources.serializers import VnfInfoSerializer
-from res.resources.serializers import VnfsInfoSerializer
-
-logger = logging.getLogger(__name__)
-
-
-def make_error_resp(status, detail):
-    return Response(
-        data={
-            'status': status,
-            'detail': detail
-        },
-        status=status
-    )
-
-
-def view_safe_call_with_log(logger):
-    def view_safe_call(func):
-        def wrapper(*args, **kwargs):
-            try:
-                return func(*args, **kwargs)
-            except NotFoundException as e:
-                logger.error(e.args[0])
-                return make_error_resp(
-                    detail=e.args[0],
-                    status=status.HTTP_404_NOT_FOUND
-                )
-            except VNFRESException as e:
-                logger.error(e.args[0])
-                return make_error_resp(
-                    detail=e.args[0],
-                    status=status.HTTP_500_INTERNAL_SERVER_ERROR
-                )
-            except Exception as e:
-                logger.error(e.args[0])
-                logger.error(traceback.format_exc())
-                return make_error_resp(
-                    detail='Unexpected exception',
-                    status=status.HTTP_500_INTERNAL_SERVER_ERROR
-                )
-        return wrapper
-    return view_safe_call
-
-
-def query_resources(res_type, logger, resources, cvt_fun, res_serializer):
-    logger.debug("Enter query %s", res_type)
-
-    resp = {
-        'resp_data': [cvt_fun(res) for res in resources]
-    }
-
-    resp_serializer = res_serializer(data=resp)
-    if not resp_serializer.is_valid():
-        raise VNFRESException(resp_serializer.errors)
-
-    return Response(
-        data=resp,
-        status=status.HTTP_200_OK
-    )
-
-
-class getVnf(APIView):
-    @swagger_auto_schema(
-        responses={
-            status.HTTP_200_OK: VnfInfoSerializer(),
-            status.HTTP_404_NOT_FOUND: 'Vnf does not exist',
-            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'
-        }
-    )
-    @view_safe_call_with_log(logger=logger)
-    def get(self, request, vnfInstanceId):
-        logger.debug("[%s]vnf_inst_id=%s", fun_name(), vnfInstanceId)
-
-        vnf_inst = NfInstModel.objects.filter(nfinstid=vnfInstanceId)
-        if not vnf_inst:
-            raise NotFoundException('Vnf(%s) does not exist' % vnfInstanceId)
-
-        resp_data = fill_resp_data(vnf_inst[0])
-
-        vnf_info_serializer = VnfInfoSerializer(data=resp_data)
-        if not vnf_info_serializer.is_valid():
-            raise VNFRESException(vnf_info_serializer.errors)
-
-        return Response(
-            data=resp_data,
-            status=status.HTTP_200_OK
-        )
-
-
-def fill_resp_data(vnf):
-    logger.info('Get the StorageInstModel of list')
-    storage_inst = StorageInstModel.objects.filter(instid=vnf.nfinstid)
-    arr = []
-    for s in storage_inst:
-        storage = {
-            "virtualStorageInstanceId": s.storageid,
-            "virtualStorageDescId": s.storagetype,
-            "storageResource": {
-                "vimId": s.vimid,
-                "resourceId": s.resouceid
-            }
-        }
-        arr.append(storage)
-    logger.info('Get the VLInstModel of list.')
-    vl_inst = VLInstModel.objects.filter(ownerid=vnf.nfinstid)
-    vl_arr = []
-    for v in vl_inst:
-        net = NetworkInstModel.objects.filter(networkid=v.relatednetworkid)
-        if not net:
-            raise VNFRESException(
-                'NetworkInst(%s) does not exist.' %
-                v.relatednetworkid)
-        v_dic = {
-            "virtualLinkInstanceId": v.vlinstanceid,
-            "virtualLinkDescId": v.vldid,
-            "networkResource": {
-                "vimId": net[0].vimid,
-                "resourceId": net[0].resouceid
-            }
-        }
-        vl_arr.append(v_dic)
-    logger.info('Get VNFCInstModel of list.')
-    vnfc_insts = VNFCInstModel.objects.filter(instid=vnf.nfinstid)
-    vnfc_arr = []
-    for vnfc in vnfc_insts:
-        vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
-        if not vm:
-            raise VNFRESException('VmInst(%s) does not exist.' % vnfc.vmid)
-        storage = StorageInstModel.objects.filter(ownerid=vm[0].vmid)
-        if not storage:
-            raise VNFRESException(
-                'StorageInst(%s) does not exist.' %
-                vm[0].vmid)
-        vnfc_dic = {
-            "vnfcInstanceId": vnfc.vnfcinstanceid,
-            "vduId": vnfc.vduid,
-            "computeResource": {
-                "vimId": vm[0].vimid,
-                "resourceId": vm[0].resouceid
-            },
-            "storageResourceIds": [s.storageid for s in storage]
-        }
-        vnfc_arr.append(vnfc_dic)
-    logger.info('Get the VimInstModel of list.')
-    vms = VmInstModel.objects.filter(instid=vnf.nfinstid)
-    vm_arr = []
-    for vm in vms:
-        vm_dic = {
-            "vmid": vm.vmid,
-            "vimid": vm.vimid,
-            "tenant": vm.tenant,
-            "resouceid": vm.resouceid,
-            "vmname": vm.vmname,
-            "nic_array": vm.nic_array,
-            "metadata": vm.metadata,
-            "volume_array": vm.volume_array,
-            "server_group": vm.server_group,
-            "availability_zone": vm.availability_zone,
-            "flavor_id": vm.flavor_id,
-            "security_groups": vm.security_groups,
-            "operationalstate": vm.operationalstate,
-            "insttype": vm.insttype,
-            "is_predefined": vm.is_predefined,
-            "create_time": vm.create_time,
-            "instid": vm.instid,
-            "nodeId": vm.nodeId
-        }
-        vm_arr.append(vm_dic)
-
-    resp_data = {
-        "vnfInstanceId": vnf.nfinstid,
-        "vnfInstanceName": vnf.nf_name,
-        "vnfInstanceDescription": vnf.nf_desc,
-        "onboardedVnfPkgInfoId": vnf.package_id,
-        "vnfdId": vnf.vnfdid,
-        "vnfdVersion": vnf.version,
-        "vnfSoftwareVersion": vnf.vnfSoftwareVersion,
-        "vnfProvider": vnf.vendor,
-        "vnfProductName": vnf.netype,
-        "vnfConfigurableProperties": vnf.vnfConfigurableProperties,
-        "instantiationState": vnf.status,
-        "instantiatedVnfInfo": {
-            "flavourId": vnf.flavour_id,
-            "vnfState": vnf.status,
-            "scaleStatus": [],
-            "extCpInfo": [],
-            "extVirtualLink": [],
-            "monitoringParameters": {},
-            "localizationLanguage": vnf.localizationLanguage,
-            "vmInfo": vm_arr,
-            "vnfcResourceInfo": vnfc_arr,
-            "virtualLinkResourceInfo": vl_arr,
-            "virtualStorageResourceInfo": arr
-        },
-        "metadata": vnf.input_params,
-        "extensions": vnf.vnfd_model
-    }
-    return resp_data
-
-
-class getVnfs(APIView):
-    @swagger_auto_schema(
-        responses={
-            status.HTTP_200_OK: VnfsInfoSerializer(),
-            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'
-        }
-    )
-    @view_safe_call_with_log(logger=logger)
-    def get(self, request):
-        return query_resources(
-            res_type="Vnfs",
-            logger=logger,
-            resources=NfInstModel.objects.all(),
-            cvt_fun=fill_resp_data,
-            res_serializer=VnfsInfoSerializer
-        )
-
-
-class getVms(APIView):
-    @swagger_auto_schema(
-        responses={
-            status.HTTP_200_OK: VmInfoSerializer(),
-            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'
-        }
-    )
-    @view_safe_call_with_log(logger=logger)
-    def get(self, request, vnfInstanceId):
-        return query_resources(
-            res_type="Vms",
-            logger=logger,
-            resources=VmInstModel.objects.filter(instid=vnfInstanceId),
-            cvt_fun=fill_vms_data,
-            res_serializer=VmInfoSerializer
-        )
-
-
-def fill_vms_data(vm):
-    vms_data = {
-        "vmid": vm.vmid,
-        "vimid": vm.vimid,
-        "resouceid": vm.resouceid,
-        "insttype": vm.insttype,
-        "instid": vm.instid,
-        "vmname": vm.vmname,
-        "operationalstate": vm.operationalstate,
-        "tenant": vm.tenant,
-        "is_predefined": vm.is_predefined,
-        "security_groups": vm.security_groups,
-        "flavor_id": vm.flavor_id,
-        "availability_zone": vm.availability_zone,
-        "server_group": vm.server_group,
-        "volume_array": vm.volume_array,
-        "metadata": vm.metadata,
-        "nic_array": vm.nic_array
-    }
-    return vms_data
-
-
-class getFlavors(APIView):
-    @swagger_auto_schema(
-        responses={
-            status.HTTP_200_OK: FlavorInfoSerializer(),
-            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'
-        }
-    )
-    @view_safe_call_with_log(logger=logger)
-    def get(self, request, vnfInstanceId):
-        return query_resources(
-            res_type="Flavors",
-            logger=logger,
-            resources=FlavourInstModel.objects.filter(instid=vnfInstanceId),
-            cvt_fun=fill_flavours_data,
-            res_serializer=FlavorInfoSerializer
-        )
-
-
-def fill_flavours_data(f):
-    flavours_data = {
-        "flavourid": f.flavourid,
-        "name": f.name,
-        "vcpu": f.vcpu,
-        "memory": f.memory,
-        "extraspecs": f.extraspecs,
-        "instid": f.instid,
-        "tenant": f.tenant,
-        "vimid": f.vimid,
-        "resouceid": f.resouceid,
-        "create_time": f.create_time
-    }
-    return flavours_data
-
-
-class getNetworks(APIView):
-    @swagger_auto_schema(
-        responses={
-            status.HTTP_200_OK: NetworkInfoSerializer(),
-            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'
-        }
-    )
-    @view_safe_call_with_log(logger=logger)
-    def get(self, request, vnfInstanceId):
-        return query_resources(
-            res_type="Networks",
-            logger=logger,
-            resources=NetworkInstModel.objects.filter(instid=vnfInstanceId),
-            cvt_fun=fill_networks_data,
-            res_serializer=NetworkInfoSerializer
-        )
-
-
-def fill_networks_data(network):
-    networks_data = {
-        "networkid": network.networkid,
-        "vimid": network.vimid,
-        "resouceid": network.resouceid,
-        "insttype": network.insttype,
-        "instid": network.instid,
-        "name": network.name
-    }
-    return networks_data
-
-
-class getSubnets(APIView):
-    @swagger_auto_schema(
-        responses={
-            status.HTTP_200_OK: SubnetInfoSerializer(),
-            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'
-        }
-    )
-    @view_safe_call_with_log(logger=logger)
-    def get(self, request, vnfInstanceId):
-        return query_resources(
-            res_type="Subnets",
-            logger=logger,
-            resources=SubNetworkInstModel.objects.filter(instid=vnfInstanceId),
-            cvt_fun=fill_subnets_data,
-            res_serializer=SubnetInfoSerializer
-        )
-
-
-def fill_subnets_data(subnet):
-    subnets_data = {
-        "subnetworkid": subnet.subnetworkid,
-        "vimid": subnet.vimid,
-        "resouceid": subnet.resouceid,
-        "networkid": subnet.networkid,
-        "insttype": subnet.insttype,
-        "instid": subnet.instid,
-        "name": subnet.name,
-        "cidr": subnet.cidr
-    }
-    return subnets_data
-
-
-class getCps(APIView):
-    @swagger_auto_schema(
-        responses={
-            status.HTTP_200_OK: CpsInfoSerializer(),
-            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'
-        }
-    )
-    @view_safe_call_with_log(logger=logger)
-    def get(self, request, vnfInstanceId):
-        return query_resources(
-            res_type="Cps",
-            logger=logger,
-            resources=CPInstModel.objects.filter(ownerid=vnfInstanceId),
-            cvt_fun=fill_cps_data,
-            res_serializer=CpsInfoSerializer
-        )
-
-
-def fill_cps_data(cp):
-    cps_data = {
-        "cpinstanceid": cp.cpinstanceid,
-        "cpdid": cp.cpdid,
-        "cpinstancename": cp.cpinstancename,
-        "vlinstanceid": cp.vlinstanceid,
-        "ownertype": cp.ownertype,
-        "ownerid": cp.ownerid,
-        "relatedtype": cp.relatedtype
-    }
-    return cps_data
-
-
-class getVolumes(APIView):
-    @swagger_auto_schema(
-        responses={
-            status.HTTP_200_OK: VolumeInfoSerializer(),
-            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'
-        }
-    )
-    @view_safe_call_with_log(logger=logger)
-    def get(self, request, vnfInstanceId):
-        return query_resources(
-            res_type="Volumes",
-            logger=logger,
-            resources=StorageInstModel.objects.filter(instid=vnfInstanceId),
-            cvt_fun=fill_volumes_data,
-            res_serializer=VolumeInfoSerializer
-        )
-
-
-def fill_volumes_data(v):
-    volumes_data = {
-        "storageid": v.storageid,
-        "vimid": v.vimid,
-        "resouceid": v.resouceid,
-        "insttype": v.insttype,
-        "instid": v.instid,
-        "storagetype": v.storagetype,
-        "size": v.size
-    }
-    return volumes_data
+# Copyright 2017 ZTE Corporation.\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 logging\r
+\r
+from drf_yasg.utils import swagger_auto_schema\r
+from rest_framework import status\r
+from rest_framework.response import Response\r
+from rest_framework.views import APIView\r
+\r
+from res.pub.exceptions import VNFRESException\r
+from res.pub.database.models import StorageInstModel\r
+from res.pub.database.models import NetworkInstModel\r
+from res.pub.database.models import VmInstModel\r
+from res.pub.database.models import FlavourInstModel\r
+from res.pub.database.models import SubNetworkInstModel\r
+from res.pub.database.models import CPInstModel\r
+from res.resources.serializers import VolumeInfoSerializer\r
+from res.resources.serializers import CpsInfoSerializer\r
+from res.resources.serializers import SubnetInfoSerializer\r
+from res.resources.serializers import NetworkInfoSerializer\r
+from res.resources.serializers import FlavorInfoSerializer\r
+from res.resources.serializers import VmInfoSerializer\r
+from res.resources.views.base_view import view_safe_call_with_log\r
+\r
+logger = logging.getLogger(__name__)\r
+\r
+\r
+def query_resources(res_type, logger, resources, cvt_fun, res_serializer):\r
+    logger.debug("Enter query %s", res_type)\r
+\r
+    resp = {\r
+        'resp_data': [cvt_fun(res) for res in resources]\r
+    }\r
+\r
+    resp_serializer = res_serializer(data=resp)\r
+    if not resp_serializer.is_valid():\r
+        raise VNFRESException(resp_serializer.errors)\r
+\r
+    return Response(\r
+        data=resp,\r
+        status=status.HTTP_200_OK\r
+    )\r
+\r
+\r
+class getVms(APIView):\r
+    @swagger_auto_schema(\r
+        responses={\r
+            status.HTTP_200_OK: VmInfoSerializer(),\r
+            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'\r
+        }\r
+    )\r
+    @view_safe_call_with_log(logger=logger)\r
+    def get(self, request, vnfInstanceId):\r
+        return query_resources(\r
+            res_type="Vms",\r
+            logger=logger,\r
+            resources=VmInstModel.objects.filter(instid=vnfInstanceId),\r
+            cvt_fun=fill_vms_data,\r
+            res_serializer=VmInfoSerializer\r
+        )\r
+\r
+\r
+def fill_vms_data(vm):\r
+    vms_data = {\r
+        "vmid": vm.vmid,\r
+        "vimid": vm.vimid,\r
+        "resouceid": vm.resouceid,\r
+        "insttype": vm.insttype,\r
+        "instid": vm.instid,\r
+        "vmname": vm.vmname,\r
+        "operationalstate": vm.operationalstate,\r
+        "tenant": vm.tenant,\r
+        "is_predefined": vm.is_predefined,\r
+        "security_groups": vm.security_groups,\r
+        "flavor_id": vm.flavor_id,\r
+        "availability_zone": vm.availability_zone,\r
+        "server_group": vm.server_group,\r
+        "volume_array": vm.volume_array,\r
+        "metadata": vm.metadata,\r
+        "nic_array": vm.nic_array\r
+    }\r
+    return vms_data\r
+\r
+\r
+class getFlavors(APIView):\r
+    @swagger_auto_schema(\r
+        responses={\r
+            status.HTTP_200_OK: FlavorInfoSerializer(),\r
+            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'\r
+        }\r
+    )\r
+    @view_safe_call_with_log(logger=logger)\r
+    def get(self, request, vnfInstanceId):\r
+        return query_resources(\r
+            res_type="Flavors",\r
+            logger=logger,\r
+            resources=FlavourInstModel.objects.filter(instid=vnfInstanceId),\r
+            cvt_fun=fill_flavours_data,\r
+            res_serializer=FlavorInfoSerializer\r
+        )\r
+\r
+\r
+def fill_flavours_data(f):\r
+    flavours_data = {\r
+        "flavourid": f.flavourid,\r
+        "name": f.name,\r
+        "vcpu": f.vcpu,\r
+        "memory": f.memory,\r
+        "extraspecs": f.extraspecs,\r
+        "instid": f.instid,\r
+        "tenant": f.tenant,\r
+        "vimid": f.vimid,\r
+        "resouceid": f.resouceid,\r
+        "create_time": f.create_time\r
+    }\r
+    return flavours_data\r
+\r
+\r
+class getNetworks(APIView):\r
+    @swagger_auto_schema(\r
+        responses={\r
+            status.HTTP_200_OK: NetworkInfoSerializer(),\r
+            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'\r
+        }\r
+    )\r
+    @view_safe_call_with_log(logger=logger)\r
+    def get(self, request, vnfInstanceId):\r
+        return query_resources(\r
+            res_type="Networks",\r
+            logger=logger,\r
+            resources=NetworkInstModel.objects.filter(instid=vnfInstanceId),\r
+            cvt_fun=fill_networks_data,\r
+            res_serializer=NetworkInfoSerializer\r
+        )\r
+\r
+\r
+def fill_networks_data(network):\r
+    networks_data = {\r
+        "networkid": network.networkid,\r
+        "vimid": network.vimid,\r
+        "resouceid": network.resouceid,\r
+        "insttype": network.insttype,\r
+        "instid": network.instid,\r
+        "name": network.name\r
+    }\r
+    return networks_data\r
+\r
+\r
+class getSubnets(APIView):\r
+    @swagger_auto_schema(\r
+        responses={\r
+            status.HTTP_200_OK: SubnetInfoSerializer(),\r
+            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'\r
+        }\r
+    )\r
+    @view_safe_call_with_log(logger=logger)\r
+    def get(self, request, vnfInstanceId):\r
+        return query_resources(\r
+            res_type="Subnets",\r
+            logger=logger,\r
+            resources=SubNetworkInstModel.objects.filter(instid=vnfInstanceId),\r
+            cvt_fun=fill_subnets_data,\r
+            res_serializer=SubnetInfoSerializer\r
+        )\r
+\r
+\r
+def fill_subnets_data(subnet):\r
+    subnets_data = {\r
+        "subnetworkid": subnet.subnetworkid,\r
+        "vimid": subnet.vimid,\r
+        "resouceid": subnet.resouceid,\r
+        "networkid": subnet.networkid,\r
+        "insttype": subnet.insttype,\r
+        "instid": subnet.instid,\r
+        "name": subnet.name,\r
+        "cidr": subnet.cidr\r
+    }\r
+    return subnets_data\r
+\r
+\r
+class getCps(APIView):\r
+    @swagger_auto_schema(\r
+        responses={\r
+            status.HTTP_200_OK: CpsInfoSerializer(),\r
+            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'\r
+        }\r
+    )\r
+    @view_safe_call_with_log(logger=logger)\r
+    def get(self, request, vnfInstanceId):\r
+        return query_resources(\r
+            res_type="Cps",\r
+            logger=logger,\r
+            resources=CPInstModel.objects.filter(ownerid=vnfInstanceId),\r
+            cvt_fun=fill_cps_data,\r
+            res_serializer=CpsInfoSerializer\r
+        )\r
+\r
+\r
+def fill_cps_data(cp):\r
+    cps_data = {\r
+        "cpinstanceid": cp.cpinstanceid,\r
+        "cpdid": cp.cpdid,\r
+        "cpinstancename": cp.cpinstancename,\r
+        "vlinstanceid": cp.vlinstanceid,\r
+        "ownertype": cp.ownertype,\r
+        "ownerid": cp.ownerid,\r
+        "relatedtype": cp.relatedtype\r
+    }\r
+    return cps_data\r
+\r
+\r
+class getVolumes(APIView):\r
+    @swagger_auto_schema(\r
+        responses={\r
+            status.HTTP_200_OK: VolumeInfoSerializer(),\r
+            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'\r
+        }\r
+    )\r
+    @view_safe_call_with_log(logger=logger)\r
+    def get(self, request, vnfInstanceId):\r
+        return query_resources(\r
+            res_type="Volumes",\r
+            logger=logger,\r
+            resources=StorageInstModel.objects.filter(instid=vnfInstanceId),\r
+            cvt_fun=fill_volumes_data,\r
+            res_serializer=VolumeInfoSerializer\r
+        )\r
+\r
+\r
+def fill_volumes_data(v):\r
+    volumes_data = {\r
+        "storageid": v.storageid,\r
+        "vimid": v.vimid,\r
+        "resouceid": v.resouceid,\r
+        "insttype": v.insttype,\r
+        "instid": v.instid,\r
+        "storagetype": v.storagetype,\r
+        "size": v.size\r
+    }\r
+    return volumes_data\r