diff options
Diffstat (limited to 'res')
-rw-r--r-- | res/requirements.txt | 4 | ||||
-rw-r--r-- | res/res/biz/__init__.py | 0 | ||||
-rw-r--r-- | res/res/biz/base.py | 40 | ||||
-rw-r--r-- | res/res/biz/flavors_get.py | 51 | ||||
-rw-r--r-- | res/res/biz/vms_get.py | 56 | ||||
-rw-r--r-- | res/res/biz/vnfs_get.py | 161 | ||||
-rw-r--r-- | res/res/pub/utils/restcall.py | 1 | ||||
-rw-r--r-- | res/res/resources/urls.py | 14 | ||||
-rw-r--r-- | res/res/resources/views.py | 454 | ||||
-rw-r--r-- | res/res/resources/views/__init__.py | 0 | ||||
-rw-r--r-- | res/res/resources/views/base_view.py | 64 | ||||
-rw-r--r-- | res/res/resources/views/get_flavors_view.py | 34 | ||||
-rw-r--r-- | res/res/resources/views/get_vms_view.py | 34 | ||||
-rw-r--r-- | res/res/resources/views/get_vnfs_view.py | 62 | ||||
-rw-r--r-- | res/res/resources/views/views.py | 174 |
15 files changed, 688 insertions, 461 deletions
diff --git a/res/requirements.txt b/res/requirements.txt index 3ba86b5..126669e 100644 --- a/res/requirements.txt +++ b/res/requirements.txt @@ -1,6 +1,6 @@ # rest framework Django==2.1.10 -djangorestframework==3.9.4 +djangorestframework==3.10.0 # for access MySQL PyMySQL==0.9.3 @@ -21,7 +21,7 @@ unittest_xml_reporting==1.12.0 # for auto-swagger ruamel.yaml==0.15.97 -drf-yasg==1.14.0 +drf-yasg==1.17.0 flex>=6.11.1 swagger-spec-validator>=2.1.0 diff --git a/res/res/biz/__init__.py b/res/res/biz/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/res/res/biz/__init__.py diff --git a/res/res/biz/base.py b/res/res/biz/base.py new file mode 100644 index 0000000..b7d2aea --- /dev/null +++ b/res/res/biz/base.py @@ -0,0 +1,40 @@ +# Copyright @ 2020 China Mobile (SuZhou) Software Technology Co.,Ltd.
+#
+# 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
+
+from rest_framework import status
+from rest_framework.response import Response
+
+from res.pub.exceptions import VNFRESException
+
+logger = logging.getLogger(__name__)
+
+
+class BaseService(object):
+
+ def query_resources(self, 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
+ )
diff --git a/res/res/biz/flavors_get.py b/res/res/biz/flavors_get.py new file mode 100644 index 0000000..5ae41e2 --- /dev/null +++ b/res/res/biz/flavors_get.py @@ -0,0 +1,51 @@ +# Copyright @ 2020 China Mobile (SuZhou) Software Technology Co.,Ltd.
+#
+# 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
+
+from res.biz.base import BaseService
+from res.pub.database.models import FlavourInstModel
+from res.resources.serializers import FlavorInfoSerializer
+
+logger = logging.getLogger(__name__)
+
+
+class GetFlavorsService(BaseService):
+
+ def __init__(self):
+ super(GetFlavorsService, self).__init__()
+
+ def get_flavors(self, vnf_instance_id):
+ return self.query_resources(
+ res_type="Flavors",
+ logger=logger,
+ resources=FlavourInstModel.objects.filter(
+ instid=vnf_instance_id),
+ cvt_fun=self.fill_flavours_data,
+ res_serializer=FlavorInfoSerializer
+ )
+
+ def fill_flavours_data(self, flavor):
+ flavours_data = {
+ "flavourid": flavor.flavourid,
+ "name": flavor.name,
+ "vcpu": flavor.vcpu,
+ "memory": flavor.memory,
+ "extraspecs": flavor.extraspecs,
+ "instid": flavor.instid,
+ "tenant": flavor.tenant,
+ "vimid": flavor.vimid,
+ "resouceid": flavor.resouceid,
+ "create_time": flavor.create_time
+ }
+ return flavours_data
diff --git a/res/res/biz/vms_get.py b/res/res/biz/vms_get.py new file mode 100644 index 0000000..091a278 --- /dev/null +++ b/res/res/biz/vms_get.py @@ -0,0 +1,56 @@ +# Copyright @ 2020 China Mobile (SuZhou) Software Technology Co.,Ltd.
+#
+# 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
+
+from res.biz.base import BaseService
+from res.pub.database.models import VmInstModel
+from res.resources.serializers import VmInfoSerializer
+
+logger = logging.getLogger(__name__)
+
+
+class GetVmsService(BaseService):
+
+ def __init__(self):
+ super(GetVmsService, self).__init__()
+
+ def get_vms(self, vnf_instance_id):
+ return self.query_resources(
+ res_type="Vms",
+ logger=logger,
+ resources=VmInstModel.objects.filter(instid=vnf_instance_id),
+ cvt_fun=self.fill_vms_data,
+ res_serializer=VmInfoSerializer
+ )
+
+ def fill_vms_data(self, 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
diff --git a/res/res/biz/vnfs_get.py b/res/res/biz/vnfs_get.py new file mode 100644 index 0000000..d767ae8 --- /dev/null +++ b/res/res/biz/vnfs_get.py @@ -0,0 +1,161 @@ +# Copyright @ 2020 China Mobile (SuZhou) Software Technology Co.,Ltd.
+#
+# 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
+
+from res.biz.base import BaseService
+from res.pub.exceptions import VNFRESException
+from res.pub.exceptions import NotFoundException
+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.resources.serializers import VnfsInfoSerializer
+
+logger = logging.getLogger(__name__)
+
+
+class GetVnfsService(BaseService):
+
+ def __init__(self):
+ super(GetVnfsService, self).__init__()
+
+ def get_vnfs(self):
+ return self.query_resources(
+ res_type="Vnfs",
+ logger=logger,
+ resources=NfInstModel.objects.all(),
+ cvt_fun=self._fill_resp_data,
+ res_serializer=VnfsInfoSerializer
+ )
+
+ def get_vnf(self, vnf_instance_id):
+ vnf = NfInstModel.objects.filter(nfinstid=vnf_instance_id)
+ if not vnf:
+ raise NotFoundException('Vnf(%s) does not exist' % vnf_instance_id)
+
+ return self._fill_resp_data(vnf[0])
+
+ def _fill_resp_data(self, vnf):
+ def make_virtual_storage_resource():
+ logger.info('Get the StorageInstModel of list')
+ return [{"virtualStorageInstanceId": s.storageid,
+ "virtualStorageDescId": s.storagetype,
+ "storageResource": {
+ "vimId": s.vimid,
+ "resourceId": s.resouceid
+ }}
+ for s in StorageInstModel.objects.filter(instid=vnf.nfinstid)]
+
+ def make_virtual_link_resource():
+ 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)
+ return vl_arr
+
+ def make_vnfc_resource():
+ 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)
+ return vnfc_arr
+
+ def make_vm():
+ logger.info('Get the VimInstModel of list.')
+ return [{"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}
+ for vm in VmInstModel.objects.filter(instid=vnf.nfinstid)]
+
+ virtual_storage_resources = make_virtual_storage_resource()
+ virtual_link_resource = make_virtual_link_resource()
+ vnfc_resource = make_vnfc_resource()
+ vm_info = make_vm()
+ return {
+ "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_info,
+ "vnfcResourceInfo": vnfc_resource,
+ "virtualLinkResourceInfo": virtual_link_resource,
+ "virtualStorageResourceInfo": virtual_storage_resources
+ },
+ "metadata": vnf.input_params,
+ "extensions": vnf.vnfd_model
+ }
diff --git a/res/res/pub/utils/restcall.py b/res/res/pub/utils/restcall.py index 852bf0b..a609a9a 100644 --- a/res/res/pub/utils/restcall.py +++ b/res/res/pub/utils/restcall.py @@ -69,6 +69,7 @@ def call_req(base_url, user, passwd, auth_type, resource, method, content=''): if 'httplib.ResponseNotReady' in res_info: res_info = "The URL[%s] request failed or is not responding." % full_url ret = [3, res_info, resp_status] + logger.debug(ex) except: logger.error(traceback.format_exc()) ret = [4, str(sys.exc_info()), resp_status] diff --git a/res/res/resources/urls.py b/res/res/resources/urls.py index 41c9d08..2088caf 100644 --- a/res/res/resources/urls.py +++ b/res/res/resources/urls.py @@ -14,14 +14,18 @@ from django.conf.urls import url -from res.resources import views +from res.resources.views.get_flavors_view import GetFlavorsView +from res.resources.views.get_vnfs_view import GetVnfView +from res.resources.views.get_vnfs_view import GetVnfsView +from res.resources.views.get_vms_view import GetVmsView +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/(?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/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<vnf_instance_id>[0-9a-zA-Z\-\_]+)/vms$', GetVmsView.as_view(), name='get_vms'), + url(r'^api/vnfres/v1/(?P<vnf_instance_id>[0-9a-zA-Z\-\_]+)/flavors$', GetFlavorsView.as_view(), name='get_flavors'), url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/networks$', views.getNetworks.as_view(), name='get_networks'), url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/subnets$', views.getSubnets.as_view(), name='get_subnets'), url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/cps$', views.getCps.as_view(), name='get_cps'), diff --git a/res/res/resources/views.py b/res/res/resources/views.py deleted file mode 100644 index e3f2014..0000000 --- a/res/res/resources/views.py +++ /dev/null @@ -1,454 +0,0 @@ -# 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 diff --git a/res/res/resources/views/__init__.py b/res/res/resources/views/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/res/res/resources/views/__init__.py diff --git a/res/res/resources/views/base_view.py b/res/res/resources/views/base_view.py new file mode 100644 index 0000000..2d7ba9a --- /dev/null +++ b/res/res/resources/views/base_view.py @@ -0,0 +1,64 @@ +# Copyright @ 2020 China Mobile (SuZhou) Software Technology Co.,Ltd.
+#
+# 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 rest_framework import status
+from rest_framework.response import Response
+
+from res.pub.exceptions import VNFRESException
+from res.pub.exceptions import NotFoundException
+
+
+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
diff --git a/res/res/resources/views/get_flavors_view.py b/res/res/resources/views/get_flavors_view.py new file mode 100644 index 0000000..583ea04 --- /dev/null +++ b/res/res/resources/views/get_flavors_view.py @@ -0,0 +1,34 @@ +# Copyright @ 2020 China Mobile (SuZhou) Software Technology Co.,Ltd.
+#
+# 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
+
+from rest_framework import status
+from rest_framework.views import APIView
+from drf_yasg.utils import swagger_auto_schema
+from res.resources.serializers import FlavorInfoSerializer
+from res.resources.views.base_view import view_safe_call_with_log
+from res.biz.flavors_get import GetFlavorsService
+logger = logging.getLogger(__name__)
+
+
+class GetFlavorsView(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, vnf_instance_id):
+ return GetFlavorsService().get_flavors(vnf_instance_id)
diff --git a/res/res/resources/views/get_vms_view.py b/res/res/resources/views/get_vms_view.py new file mode 100644 index 0000000..a58334a --- /dev/null +++ b/res/res/resources/views/get_vms_view.py @@ -0,0 +1,34 @@ +# Copyright @ 2020 China Mobile (SuZhou) Software Technology Co.,Ltd.
+#
+# 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
+
+from rest_framework import status
+from rest_framework.views import APIView
+from drf_yasg.utils import swagger_auto_schema
+from res.resources.serializers import VmInfoSerializer
+from res.resources.views.base_view import view_safe_call_with_log
+from res.biz.vms_get import GetVmsService
+logger = logging.getLogger(__name__)
+
+
+class GetVmsView(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, vnf_instance_id):
+ return GetVmsService().get_vms(vnf_instance_id)
diff --git a/res/res/resources/views/get_vnfs_view.py b/res/res/resources/views/get_vnfs_view.py new file mode 100644 index 0000000..eb6bced --- /dev/null +++ b/res/res/resources/views/get_vnfs_view.py @@ -0,0 +1,62 @@ +# Copyright @ 2020 China Mobile (SuZhou) Software Technology Co.,Ltd.
+#
+# 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
+
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+from drf_yasg.utils import swagger_auto_schema
+from res.biz.vnfs_get import GetVnfsService
+from res.pub.exceptions import VNFRESException
+from res.pub.utils.syscomm import fun_name
+from res.resources.serializers import VnfInfoSerializer, VnfsInfoSerializer
+from res.resources.views.base_view import view_safe_call_with_log
+
+logger = logging.getLogger(__name__)
+
+
+class GetVnfView(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, vnf_instance_id):
+ logger.debug("[%s]vnf_inst_id=%s", fun_name(), vnf_instance_id)
+
+ resp_data = GetVnfsService().get_vnf(vnf_instance_id)
+
+ 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
+ )
+
+
+class GetVnfsView(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 GetVnfsService().get_vnfs()
diff --git a/res/res/resources/views/views.py b/res/res/resources/views/views.py new file mode 100644 index 0000000..cb5e6b5 --- /dev/null +++ b/res/res/resources/views/views.py @@ -0,0 +1,174 @@ +# 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
+
+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.database.models import StorageInstModel
+from res.pub.database.models import NetworkInstModel
+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.views.base_view import view_safe_call_with_log
+
+logger = logging.getLogger(__name__)
+
+
+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 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
|