summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorying.yunlong <ying.yunlong@zte.com.cn>2018-03-01 11:47:43 +0800
committerying.yunlong <ying.yunlong@zte.com.cn>2018-03-01 13:49:02 +0800
commitadbda5b9c0365eae74b1c37d37c0e960eb1828ad (patch)
treef3ad0750ae635dd9120dd9110f14bc9ce28bc517
parent3067c076ef23cc64f800f0faf197030695d61f0b (diff)
Interface aligement for vfc-vnflcm queryVnf
Change-Id: I62fd8da855fd66d171abf2484d47603acfce8104 Issue-ID: VFC-780 Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
-rw-r--r--lcm/lcm/v2/views.py30
-rw-r--r--lcm/lcm/v2/vnf_query/__init__.py13
-rw-r--r--lcm/lcm/v2/vnf_query/query_vnf.py146
3 files changed, 189 insertions, 0 deletions
diff --git a/lcm/lcm/v2/views.py b/lcm/lcm/v2/views.py
index 8fdb8e63..4b569584 100644
--- a/lcm/lcm/v2/views.py
+++ b/lcm/lcm/v2/views.py
@@ -24,12 +24,42 @@ from lcm.nf.serializers import CreateVnfReqSerializer
from lcm.nf.vnf_create.create_vnf_identifier import CreateVnf
from lcm.pub.exceptions import NFLCMException
from lcm.v2.serializers import VnfInstanceSerializer
+from lcm.v2.vnf_query.query_vnf import QueryVnf
logger = logging.getLogger(__name__)
class CreateVnfAndQueryVnfs(APIView):
@swagger_auto_schema(
+ responses={
+ status.HTTP_200_OK: VnfInstanceSerializer(),
+ status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
+ }
+ )
+ def get(self, request):
+ logger.debug("QueryMultiVnf--get::> %s" % request.data)
+ try:
+ resp_data = QueryVnf(request.data).query_multi_vnf()
+
+ # vnfs_info_serializer = VnfsInfoSerializer(data=resp_data)
+ # if not vnfs_info_serializer.is_valid():
+ # raise NFLCMException(vnfs_info_serializer.errors)
+ #
+ # return Response(data=vnfs_info_serializer.data, status=status.HTTP_200_OK)
+
+ vnfInstanceSerializer = VnfInstanceSerializer(data=resp_data)
+ if not vnfInstanceSerializer.is_valid():
+ raise NFLCMException(vnfInstanceSerializer.errors)
+ return Response(data=vnfInstanceSerializer.data, status=status.HTTP_200_OK)
+ except NFLCMException as e:
+ logger.error(e.message)
+ return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ except Exception as e:
+ logger.error(e.message)
+ logger.error(traceback.format_exc())
+ return Response(data={'error': 'Failed to get Vnfs'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ @swagger_auto_schema(
request_body=CreateVnfReqSerializer(),
responses={
status.HTTP_201_CREATED: VnfInstanceSerializer(),
diff --git a/lcm/lcm/v2/vnf_query/__init__.py b/lcm/lcm/v2/vnf_query/__init__.py
new file mode 100644
index 00000000..342c2a8c
--- /dev/null
+++ b/lcm/lcm/v2/vnf_query/__init__.py
@@ -0,0 +1,13 @@
+# Copyright 2018 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
diff --git a/lcm/lcm/v2/vnf_query/query_vnf.py b/lcm/lcm/v2/vnf_query/query_vnf.py
new file mode 100644
index 00000000..a8fc4d15
--- /dev/null
+++ b/lcm/lcm/v2/vnf_query/query_vnf.py
@@ -0,0 +1,146 @@
+# Copyright 2018 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import logging
+
+from lcm.pub.database.models import NfInstModel, StorageInstModel, VLInstModel, NetworkInstModel, VNFCInstModel, \
+ VmInstModel
+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
+
+ 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):
+ vnf_insts = NfInstModel.objects.all()
+ if not vnf_insts:
+ raise NFLCMException('VnfInsts does not exist')
+ resp_data = []
+ for vnf_inst in vnf_insts:
+ resp_data.append(self.fill_resp_data(vnf_inst))
+ return resp_data
+
+ 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(instid=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)
+ 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 = {
+ 'id': vnf.nfinstid,
+ 'vnfInstanceName': vnf.nf_name,
+ 'vnfInstanceDescription': 'Human-readable description of the VNF instance.',
+ 'vnfdId': vnf.vnfdid,
+ 'vnfProvider': vnf.vendor,
+ 'vnfProductName': vnf.nf_name,
+ 'vnfSoftwareVersion': vnf.vnfSoftwareVersion,
+ 'vnfdVersion': vnf.version,
+ 'vnfPkgId': vnf.package_id,
+ 'vnfConfigurableProperties': {},
+ 'instantiationState': '',
+ 'vimConnectionInfo': {},
+ "instantiatedVnfInfo": {
+ "flavourId": vnf.flavour_id,
+ "vnfState": vnf.status,
+ "scaleStatus": [],
+ "extCpInfo": [],
+ "extVirtualLink": [],
+ "monitoringParameters": {},
+ "vimInfo": vm_arr,
+ "vnfcResourceInfo": vnfc_arr,
+ "virtualLinkResourceInfo": vl_arr,
+ "virtualStorageResourceInfo": arr
+ }
+ }
+ return resp_data