From 89d8c84975086a670864fdf3e84e26c83280fab2 Mon Sep 17 00:00:00 2001 From: "ying.yunlong" Date: Fri, 24 Feb 2017 11:53:58 +0800 Subject: [PATCH] Add code and testcase of query single vnf Change-Id: I41912ba9d29a3a98f59eb8e2c140a4f775b7dd52 Issue-Id: GVNFM-12 Signed-off-by: ying.yunlong --- lcm/lcm/nf/vnfs/tests/test_vnf_query.py | 52 ++++++++++++- lcm/lcm/nf/vnfs/urls.py | 10 +-- lcm/lcm/nf/vnfs/views.py | 36 +++++++-- lcm/lcm/nf/vnfs/vnf_query/query_vnf.py | 130 +++++++++++++++++++++++++++++++- lcm/lcm/pub/database/models.py | 35 ++++++++- 5 files changed, 249 insertions(+), 14 deletions(-) diff --git a/lcm/lcm/nf/vnfs/tests/test_vnf_query.py b/lcm/lcm/nf/vnfs/tests/test_vnf_query.py index 650d17ec..9e7490a7 100644 --- a/lcm/lcm/nf/vnfs/tests/test_vnf_query.py +++ b/lcm/lcm/nf/vnfs/tests/test_vnf_query.py @@ -10,4 +10,54 @@ # 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. \ No newline at end of file +# limitations under the License. +from django.test import TestCase, Client +from rest_framework import status + +from lcm.pub.database.models import NfInstModel, StorageInstModel + + +class ResourceTest(TestCase): + def setUp(self): + self.client = Client() + self.nf_inst_id = "01" + NfInstModel.objects.all().delete() + self.test_data = { + "vnfInstanceId": "1", + "vnfInstanceName": "VNF1", + "vnfProvider": None, + "instantiatedVnfInfo": { + "vnfState": None, + "extCpInfo": [], + "virtualStorageResourceInfo": [ + { + "virtualStorageInstanceId": "s02", + "storageResource": { + "resourceId": "resource01", + "vimId": "vim01" + } + } + ], + "extVirtualLink": [], + "vnfcResourceInfo": [], + "monitoringParameters": {}, + "vimInfo": [], + "flavourId": None, + "virtualLinkResourceInfo": [], + "scaleStatus": [] + }, + "vnfdVersion": None, + "onboardedVnfPkgInfoId": None + } + + def tearDown(self): + pass + + def test_get_vnf(self): + vnf_inst_id = "1" + NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1').save() + StorageInstModel(storageid='s02', vimid='vim01', resouceid='resource01', insttype=1, \ + instid=vnf_inst_id).save() + response = self.client.get("/openoapi/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(self.test_data, response.data) \ No newline at end of file diff --git a/lcm/lcm/nf/vnfs/urls.py b/lcm/lcm/nf/vnfs/urls.py index d4968482..00788783 100644 --- a/lcm/lcm/nf/vnfs/urls.py +++ b/lcm/lcm/nf/vnfs/urls.py @@ -15,20 +15,20 @@ from django.conf.urls import patterns, url from rest_framework.urlpatterns import format_suffix_patterns -from lcm.nf.vnfs.views import CreateVnfIdentifier, InstantiateVnf, DeleteVnfIdentifier, QueryMultipleVnf,\ - TerminateVnf, QuerySingleVnf, SwaggerJsonView +from lcm.nf.vnfs.views import CreateVnfIdentifier, InstantiateVnf, QueryMultipleVnf,\ + TerminateVnf, SwaggerJsonView, DetailVnf urlpatterns = patterns('', url(r'^openoapi/vnflcm/v1/vnf_instances$', CreateVnfIdentifier.as_view()), url(r'^openoapi/vnflcm/v1/vnf_instances/(?P[0-9a-zA-Z_-]+)/instantiate$', InstantiateVnf.as_view()), url(r'^openoapi/vnflcm/v1/vnf_instances/(?P[0-9a-zA-Z_-]+)$', - DeleteVnfIdentifier.as_view()), + DetailVnf.as_view()), url(r'^openoapi/vnflcm/v1/vnf_instances/(?P[0-9a-zA-Z_-]+)/terminate$', TerminateVnf.as_view()), url(r'^openoapi/vnflcm/v1/vnf_instances$', QueryMultipleVnf.as_view()), - url(r'^openoapi/vnflcm/v1/vnf_instances/(?P[0-9a-zA-Z_-]+)$', - QuerySingleVnf.as_view()), + # url(r'^openoapi/vnflcm/v1/vnf_instances/(?P[0-9a-zA-Z_-]+)$', + # QuerySingleVnf.as_view()), # url(r'^openoapi/vnflcm/v1/vnf_lc_ops/(?P[0-9a-zA-Z_-]+)&' # r'responseId=(?P[0-9a-zA-Z_-]+)$', GetOperationStatus.as_view()), url(r'^openoapi/vnflcm/v1/swagger.json$', SwaggerJsonView.as_view()), diff --git a/lcm/lcm/nf/vnfs/views.py b/lcm/lcm/nf/vnfs/views.py index 01db9db5..b88a3e16 100644 --- a/lcm/lcm/nf/vnfs/views.py +++ b/lcm/lcm/nf/vnfs/views.py @@ -24,8 +24,10 @@ from lcm.nf.vnfs.vnf_cancel.delete_vnf_identifier import DeleteVnf from lcm.nf.vnfs.vnf_cancel.term_vnf import TermVnf from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf +from lcm.nf.vnfs.vnf_query.query_vnf import QueryVnf from lcm.pub.exceptions import NFLCMException from lcm.pub.utils.jobutil import JobUtil +from lcm.pub.utils.values import ignore_case_get logger = logging.getLogger(__name__) @@ -62,7 +64,21 @@ class InstantiateVnf(APIView): return Response(data=rsp, status=status.HTTP_202_ACCEPTED) -class DeleteVnfIdentifier(APIView): +class DetailVnf(APIView): + def get(self, request, instanceid): + logger.debug("QuerySingleVnf--get::> %s" % request.data) + try: + resp_data = QueryVnf(request.data, instanceid).query_single_vnf() + except NFLCMException as e: + logger.error(e.message) + return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + except: + logger.error(traceback.format_exc()) + tt = traceback.format_exc() + return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + return Response(data=resp_data, status=status.HTTP_200_OK) + def delete(self, request, instanceid): logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data) try: @@ -91,11 +107,19 @@ class QueryMultipleVnf(APIView): logger.debug("QueryMultipleVnf--get::> %s" % request.data) return Response(data='', status=status.HTTP_202_ACCEPTED) - -class QuerySingleVnf(APIView): - def get(self, request): - logger.debug("QuerySingleVnf--get::> %s" % request.data) - return Response(data='', status=status.HTTP_202_ACCEPTED) +# class QuerySingleVnf(APIView): +# def get(self, request, instanceid): +# logger.debug("QuerySingleVnf--get::> %s" % request.data) +# try: +# resp_data = QueryVnf(request.data, instanceid).query_single_vnf(instanceid) +# except NFLCMException as e: +# logger.error(e.message) +# return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) +# except: +# logger.error(traceback.format_exc()) +# return Response(data={'error': 'Failed to get Vnf(%s)' % instanceid}, +# status=status.HTTP_500_INTERNAL_SERVER_ERROR) +# return Response(data=resp_data, status=status.HTTP_202_ACCEPTED) # class GetOperationStatus(APIView): diff --git a/lcm/lcm/nf/vnfs/vnf_query/query_vnf.py b/lcm/lcm/nf/vnfs/vnf_query/query_vnf.py index 650d17ec..6526e731 100644 --- a/lcm/lcm/nf/vnfs/vnf_query/query_vnf.py +++ b/lcm/lcm/nf/vnfs/vnf_query/query_vnf.py @@ -10,4 +10,132 @@ # 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. \ No newline at end of file +# limitations under the License. +import logging + +from lcm.pub.database.models import NfInstModel, StorageInstModel, VLInstModel, NetworkInstModel, VNFCInstModel, \ + VmInstModel, VimModel, VimUserModel +from lcm.pub.exceptions import NFLCMException + +logger = logging.getLogger(__name__) + + +class QueryVnf: + def __init__(self, data, instanceid): + self.vnf_inst_id = instanceid + self.data = data + pass + + def query_single_vnf(self): + vnf_inst = NfInstModel.objects.filter(nfinstid=self.vnf_inst_id) + if not vnf_inst.exists(): + raise NFLCMException('VnfInst(%s) does not exist' % self.vnf_inst_id) + resp_data = self.fill_resp_data(vnf_inst[0]) + return resp_data + + def query_multi_vnf(self): + pass + + def fill_resp_data(self, vnf): + logger.info('Get the list of vloumes') + storage_inst = StorageInstModel.objects.filter(instid=vnf.nfinstid) + arr = [] + for s in storage_inst: + storage = { + "virtualStorageInstanceId": s.storageid, + "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 NFLCMException('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(nfinstid=vnf.nfinstid) + vnfc_arr = [] + for vnfc in vnfc_insts: + vm = VmInstModel.objects.filter(vmid=vnfc.vmid) + if not vm: + raise NFLCMException('VmInst(%s) does not exist.' % vnfc.vmid) + storage = StorageInstModel.objects.filter(ownerid=vm[0].vmid) + if not storage: + raise NFLCMException('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) + vim_arr = [] + # The 'vimInfoId' and 'vimId' each value are same + for vm in vms: + vims = VimModel.objects.filter(vimid=vm.vimid) + for vim in vims: + vim_users = VimUserModel.objects.filter(vimid=vim.vimid) + vim_dic = { + "vimInfoId": vim.vimid, + "vimId": vim.vimid, + "interfaceInfo": { + "vimType": vim.type, + "apiVersion": vim.version, + "protocolType": (vim.apiurl.split(':')[0] if vim.apiurl and vim.apiurl.index(':') else 'http') + }, + "accessInfo": { + "tenant": (vim_users[0].defaulttenant if vim_users and vim_users[0].defaulttenant else ''), + "username": (vim_users[0].username if vim_users and vim_users[0].username else ''), + "password": (vim_users[0].password if vim_users and vim_users[0].password else '') + }, + "interfaceEndpoint": vim.apiurl + } + vim_arr.append(vim_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.producttype, + # "vnfConfigurableProperties": {vnf.vnfConfigurableProperties}, + # "instantiationState": vnf.status, + "instantiatedVnfInfo": { + "flavourId": vnf.flavour_id, + "vnfState": vnf.status, + "scaleStatus": [], + "extCpInfo": [], + "extVirtualLink": [], + "monitoringParameters": {}, + # "localizationLanguage": vnf.localizationLanguage, + "vimInfo": vim_arr, + "vnfcResourceInfo": vnfc_arr, + "virtualLinkResourceInfo": vl_arr, + "virtualStorageResourceInfo": arr + }, + # "metadata": vnf.input_params, + # "extensions": vnf.extension + } + return resp_data diff --git a/lcm/lcm/pub/database/models.py b/lcm/lcm/pub/database/models.py index 3f10d6f2..ec0c25da 100644 --- a/lcm/lcm/pub/database/models.py +++ b/lcm/lcm/pub/database/models.py @@ -276,4 +276,37 @@ class CPInstModel(models.Model): relatedtype = models.IntegerField(db_column='RELATEDTYPE') relatedvl = models.CharField(db_column='RELATEDVL', max_length=255, blank=True, null=True) relatedcp = models.CharField(db_column='RELATEDCP', max_length=255, blank=True, null=True) - relatedport = models.CharField(db_column='RELATEDPORT', max_length=255, blank=True, null=True) \ No newline at end of file + relatedport = models.CharField(db_column='RELATEDPORT', max_length=255, blank=True, null=True) + +class VimModel(models.Model): + vimid = models.IntegerField(db_column='CMSERVERID', primary_key=True) + name = models.CharField(db_column='NAME', max_length=255, blank=True, null=True) + type = models.CharField(db_column='CMSTYPE', max_length=255, blank=True, null=True) + imageurl = models.CharField(db_column='IMAGEURL', max_length=1024, blank=True, null=True) + apiurl = models.CharField(db_column='APIURL', max_length=1024, blank=True, null=True) + version = models.CharField(db_column='VER', max_length=1024, blank=True, null=True) + supportnotification = models.IntegerField(db_column='SUPPORTNOTIFICATION', default=0) + longitude = models.CharField(db_column='LONGITUDE', max_length=1024, blank=True, null=True) + latitude = models.CharField(db_column='LATITUDE', max_length=1024, blank=True, null=True) + grantexclude = models.CharField(db_column='GRANTEXCLUDE', max_length=1, default='0', blank=True, null=True) + slalevel = models.IntegerField(db_column='SLALEVEL', default=0) + + class Meta: + db_table = 'CMSSERVER' + + def __unicode__(self): + return '%s' % self.name + + +class VimUserModel(models.Model): + class Meta: + db_table = 'CMSSERVER_USER' + + uuid = models.CharField(db_column='UUID', primary_key=True, max_length=255) + vimid = models.IntegerField(db_column='CMSERVERID') + username = models.CharField(db_column='USERNAME', max_length=255) + password = models.CharField(db_column='PWD', max_length=255, blank=True) + defaulttenant = models.CharField(db_column='TENANT', max_length=255, blank=True) + + def __unicode__(self): + return '%s' % self.username \ No newline at end of file -- 2.16.6