From 951cbddb0318c77a755eecf2b19d6f7f176c7523 Mon Sep 17 00:00:00 2001 From: "ying.yunlong" Date: Mon, 20 Feb 2017 11:53:56 +0800 Subject: Modify code of create vnf identifier Change-Id: I2821ba05c6c3cda5a938554dd08b169c9873e6ee Issue-Id: GVNFM-12 Signed-off-by: ying.yunlong --- lcm/lcm/nf/vnfs/tests/test_vnf_create.py | 25 ++++- .../nf/vnfs/vnf_create/create_vnf_identifier.py | 24 ++++- lcm/lcm/nf/vnfs/vnf_create/inst_vnf.py | 20 +++- lcm/lcm/pub/msapi/catalog.py | 104 +++++++++++++++++++++ lcm/lcm/pub/msapi/nfvolcm.py | 17 +++- 5 files changed, 172 insertions(+), 18 deletions(-) create mode 100644 lcm/lcm/pub/msapi/catalog.py diff --git a/lcm/lcm/nf/vnfs/tests/test_vnf_create.py b/lcm/lcm/nf/vnfs/tests/test_vnf_create.py index 3aa583bf..903c6a3d 100644 --- a/lcm/lcm/nf/vnfs/tests/test_vnf_create.py +++ b/lcm/lcm/nf/vnfs/tests/test_vnf_create.py @@ -18,12 +18,12 @@ import mock from django.test import TestCase, Client from rest_framework import status -from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf -from lcm.pub.database.models import NfInstModel, JobStatusModel, NfvoRegInfoModel, VmInstModel, NetworkInstModel, \ +from lcm.pub.database.models import NfInstModel, JobStatusModel, VmInstModel, NetworkInstModel, \ SubNetworkInstModel, PortInstModel from lcm.pub.utils import restcall from lcm.pub.utils.jobutil import JobUtil +from lcm.pub.utils.timeutil import now_time class TestNFInstantiate(TestCase): @@ -60,12 +60,13 @@ class TestNFInstantiate(TestCase): @mock.patch.object(restcall, 'call_req') def test_create_vnf_identifier(self, mock_call_req): - r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200'] - mock_call_req.side_effect = [r1] + r1 = [0, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200'] # get csar_id from nslcm by vnfd_id + r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200'] + mock_call_req.side_effect = [r1, r2] data = { "vnfdId": "111", "vnfInstanceName": "vFW_01", - "vnfInstanceDescription": " vFW in Nanjing TIC Edge"} + "vnfInstanceDescription": "vFW in Nanjing TIC Edge"} response = self.client.post("/openoapi/vnflcm/v1/vnf_instances", data=data, format='json') self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code) context = json.loads(response.content) @@ -85,6 +86,20 @@ class TestNFInstantiate(TestCase): InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run() self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.") + @mock.patch.object(restcall, 'call_req') + def test_instantiate_vnf_when_get_package_info_by_vnfdid_failed(self, mock_call_req): + NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='todo', + version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED', + nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time()) + r1 = [1, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200'] # get csar_id from nslcm by vnfd_id + mock_call_req.side_effect = [r1] + self.nf_inst_id = '1111' + self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id) + JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY") + data = inst_req_data + InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run() + self.assert_job_result(self.job_id, 255, "Failed to query package_info of vnfdid(111) from nslcm.") + # @mock.patch.object(restcall, 'call_req') # def test_instantiate_vnf_when_input_para_not_define_in_vnfd(self, mock_call_req): # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200'] diff --git a/lcm/lcm/nf/vnfs/vnf_create/create_vnf_identifier.py b/lcm/lcm/nf/vnfs/vnf_create/create_vnf_identifier.py index bff16d76..4e4656a6 100644 --- a/lcm/lcm/nf/vnfs/vnf_create/create_vnf_identifier.py +++ b/lcm/lcm/nf/vnfs/vnf_create/create_vnf_identifier.py @@ -17,7 +17,9 @@ import uuid from lcm.pub.database.models import NfInstModel from lcm.pub.exceptions import NFLCMException -from lcm.pub.msapi.nfvolcm import vnfd_rawdata_get +from lcm.pub.msapi.catalog import query_rawdata_from_catalog +from lcm.pub.msapi.nfvolcm import vnfd_rawdata_get, get_packageinfo_by_vnfdid +from lcm.pub.utils import toscautil from lcm.pub.utils.timeutil import now_time from lcm.pub.utils.values import ignore_case_get @@ -38,10 +40,22 @@ class CreateVnf: if is_exist: raise NFLCMException('VNF is already exist.') - ret = vnfd_rawdata_get(self.vnfd_id) - if ret[0] != 0: - raise NFLCMException('Get vnfd data failed.') - vnfd_info = json.JSONDecoder().decode(ret[1]) + # ret = vnfd_rawdata_get(self.vnfd_id) + # if ret[0] != 0: + # raise NFLCMException('Get vnfd data failed.') + # vnfd_info = json.JSONDecoder().decode(ret[1]) + + # get csar_id from nslcm by vnfd_id + self.package_info = get_packageinfo_by_vnfdid(self.vnfd_id) + self.package_id = ignore_case_get(self.package_info, "package_id") + self.csar_id = ignore_case_get(self.package_info, "csar_id") + + #get rawdata from catalog by csar_id + raw_data = query_rawdata_from_catalog(self.csar_id, self.data) + # self.vnfd = toscautil.convert_vnfd_model(raw_data["rawData"]) # convert to inner json + # self.vnfd = json.JSONDecoder().decode(self.vnfd) + + vnfd_info = raw_data metadata = ignore_case_get(vnfd_info, "metadata") version = ignore_case_get(metadata, "vnfd_version") vendor = ignore_case_get(metadata, "vendor") diff --git a/lcm/lcm/nf/vnfs/vnf_create/inst_vnf.py b/lcm/lcm/nf/vnfs/vnf_create/inst_vnf.py index b9bc15a9..6e51f64a 100644 --- a/lcm/lcm/nf/vnfs/vnf_create/inst_vnf.py +++ b/lcm/lcm/nf/vnfs/vnf_create/inst_vnf.py @@ -16,10 +16,12 @@ import logging import traceback from threading import Thread -from lcm.pub.database.models import NfInstModel, JobStatusModel, NfvoRegInfoModel, VmInstModel, NetworkInstModel, \ +from lcm.pub.database.models import NfInstModel, NfvoRegInfoModel, VmInstModel, NetworkInstModel, \ SubNetworkInstModel, PortInstModel, StorageInstModel, FlavourInstModel, VNFCInstModel, VLInstModel, CPInstModel from lcm.pub.exceptions import NFLCMException -from lcm.pub.msapi.nfvolcm import vnfd_rawdata_get, apply_grant_to_nfvo, notify_lcm_to_nfvo +from lcm.pub.msapi.catalog import query_rawdata_from_catalog +from lcm.pub.msapi.nfvolcm import vnfd_rawdata_get, apply_grant_to_nfvo, notify_lcm_to_nfvo, get_packageinfo_by_vnfdid +from lcm.pub.utils import toscautil from lcm.pub.utils.jobutil import JobUtil from lcm.pub.utils.timeutil import now_time from lcm.pub.utils.values import ignore_case_get @@ -36,6 +38,7 @@ class InstVnf(Thread): self.job_id = job_id self.nfvo_inst_id = '' self.vnfm_inst_id = '' + self.csar_id = '' self.vnfd_info = [] self.inst_resource = {'volumn': [], # [{"vim_id": ignore_case_get(ret, "vim_id")},{}] 'network': [], @@ -106,8 +109,17 @@ class InstVnf(Thread): if vnf_insts[0].status != 'NOT_INSTANTIATED': raise NFLCMException('VNF instantiationState is not NOT_INSTANTIATED.') - # get rawdata by vnfd_id - ret = vnfd_rawdata_get(vnf_insts[0].vnfdid) + # get csar_id from nslcm by vnfd_id + self.package_info = get_packageinfo_by_vnfdid(vnf_insts[0].vnfdid) + self.package_id = ignore_case_get(self.package_info, "package_id") + self.csar_id = ignore_case_get(self.package_info, "csar_id") + + #get rawdata from catalog by csar_id + raw_data = query_rawdata_from_catalog(self.csar_id, self.data) + self.vnfd = toscautil.convert_vnfd_model(raw_data["rawData"]) # convert to inner json + self.vnfd = json.JSONDecoder().decode(self.vnfd) + + ret = vnfd_rawdata_get(vnf_insts[0].vnfdid, self.data) if ret[0] != 0: raise NFLCMException("Get vnfd_raw_data failed.") self.vnfd_info = json.JSONDecoder().decode(ret[1]) diff --git a/lcm/lcm/pub/msapi/catalog.py b/lcm/lcm/pub/msapi/catalog.py new file mode 100644 index 00000000..39409aed --- /dev/null +++ b/lcm/lcm/pub/msapi/catalog.py @@ -0,0 +1,104 @@ +# Copyright 2016 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 json +import logging + +from lcm.pub.utils.restcall import req_by_msb +from lcm.pub.utils.values import ignore_case_get +from lcm.pub.exceptions import NFLCMException + +logger = logging.getLogger(__name__) + +STATUS_ONBOARDED, STATUS_NON_ONBOARDED = "onBoarded", "non-onBoarded" + +P_STATUS_NORMAL, P_STATUS_ONBOARDING, P_STATUS_ONBOARDFAILED = "normal", "onBoarding", "onBoardFailed" +P_STATUS_DELETING, P_STATUS_DELETEFAILED = "deleting", "deleteFailed" + + +def query_csar_from_catalog(csar_id, key=''): + ret = req_by_msb("/openoapi/catalog/v1/csars/%s" % csar_id, "GET") + if ret[0] != 0: + logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) + if ret[2] == '404': + raise NFLCMException("CSAR(%s) does not exist." % csar_id) + raise NFLCMException("Failed to query CSAR(%s) from catalog." % csar_id) + csar_info = json.JSONDecoder().decode(ret[1]) + return ignore_case_get(csar_info, key) if key else csar_info + + +def query_rawdata_from_catalog(csar_id, input_parameters=[]): + req_param = json.JSONEncoder().encode({"csarId": csar_id, "inputParameters": input_parameters}) + ret = req_by_msb("/openoapi/catalog/v1/servicetemplates/queryingrawdata", "POST", req_param) + if ret[0] != 0: + logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) + raise NFLCMException("Failed to query rawdata of CSAR(%s) from catalog." % csar_id) + return json.JSONDecoder().decode(ret[1]) + + +def set_csar_state(csar_id, prop, val): + ret = req_by_msb("/openoapi/catalog/v1/csars/%s?%s=%s" % (csar_id, prop, val), "PUT") + if ret[0] != 0: + logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) + return [1, "Failed to set %s to %s of CSAR(%s)." % (prop, val, csar_id)] + return [0, "Set %s to %s of CSAR(%s) successfully." % (prop, val, csar_id)] + + +def delete_csar_from_catalog(csar_id): + ret = req_by_msb("/openoapi/catalog/v1/csars/%s" % csar_id, "DELETE") + if ret[0] != 0 and ret[2] != '404': + logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) + set_csar_state(csar_id, "processState", P_STATUS_DELETEFAILED) + return [1, "Failed to delete CSAR(%s) from catalog." % csar_id] + return [0, "Delete CSAR(%s) successfully." % csar_id] + + +def get_download_url_from_catalog(csar_id, relative_path): + ret = req_by_msb("/openoapi/catalog/v1/csars/%s/files?relativePath=%s" % (csar_id, relative_path), "GET") + if ret[0] != 0: + logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) + raise NFLCMException("Failed to get download url of CSAR(%s)." % csar_id) + csar_file_info = json.JSONDecoder().decode(ret[1]) + return ignore_case_get(csar_file_info, "downloadUri"), ignore_case_get(csar_file_info, "localPath") + + +def get_process_id(name, srv_template_id): + ret = req_by_msb('/openoapi/catalog/v1/servicetemplates/%s/operations' % srv_template_id, 'GET') + if ret[0] != 0: + raise NFLCMException('Failed to get service[%s,%s] process id' % (name, srv_template_id)) + items = json.JSONDecoder().decode(ret[1]) + for item in items: + if name in item['name']: + return item['processId'] + raise NFLCMException('service[%s,%s] process id not exist' % (name, srv_template_id)) + +def get_servicetemplate_id(nsd_id): + ret = req_by_msb('/openoapi/catalog/v1/servicetemplates', 'GET') + if ret[0] != 0: + raise NFLCMException('Failed to get servicetemplates info') + stpls = json.JSONDecoder().decode(ret[1]) + for stpl in stpls: + if stpl["id"] == nsd_id: + return stpl["serviceTemplateId"] + raise NFLCMException('servicetemplate(%s) does not exist.' % nsd_id) + +def get_servicetemplate(nsd_id): + ret = req_by_msb('/openoapi/catalog/v1/servicetemplates', 'GET') + if ret[0] != 0: + raise NFLCMException('Failed to get servicetemplates info') + stpls = json.JSONDecoder().decode(ret[1]) + for stpl in stpls: + if stpl["id"] == nsd_id: + return stpl + return NFLCMException('servicetemplate(%s) does not exist.' % nsd_id) diff --git a/lcm/lcm/pub/msapi/nfvolcm.py b/lcm/lcm/pub/msapi/nfvolcm.py index fa906535..b7a0a9a6 100644 --- a/lcm/lcm/pub/msapi/nfvolcm.py +++ b/lcm/lcm/pub/msapi/nfvolcm.py @@ -11,17 +11,26 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import json +import logging +from lcm.pub.exceptions import NFLCMException from lcm.pub.utils.restcall import req_by_msb +logger = logging.getLogger(__name__) + + #call gvnfm driver -def vnfd_rawdata_get(vnfdid): +def get_packageinfo_by_vnfdid(vnfdid): ret = req_by_msb("openoapi/nslcm/v1/vnfs/%s" % vnfdid, "GET") - return ret + if ret[0] != 0: + logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) + raise NFLCMException("Failed to query package_info of vnfdid(%s) from nslcm." % vnfdid) + return json.JSONDecoder().decode(ret[1]) #call gvnfm driver -def get_packageid_by_vnfdid(vnfdid): - ret = req_by_msb("openoapi/nslcm/v1/vnfs/%s" % vnfdid, "GET") +def vnfd_rawdata_get(vnfdid, data): + ret = req_by_msb("openoapi/nslcm/v1/vnfs/%s" % vnfdid, "GET", data) return ret #call gvnfm driver -- cgit 1.2.3-korg