From: ying.yunlong Date: Wed, 13 Sep 2017 02:07:43 +0000 (+0800) Subject: Modify gvnfm-vnflcm code and unit tests X-Git-Tag: v1.0.0~22 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=vfc%2Fgvnfm%2Fvnflcm.git;a=commitdiff_plain;h=c0d7d8adb857c37bf282e4d7481a8fa960e20e09 Modify gvnfm-vnflcm code and unit tests Modify call aai logic and fix related unit tests in gvnfm vnflcm Change-Id: I3483f723ce91310d500e340fcdbb7e173972d777 Issue-ID: VFC-346 Signed-off-by: ying.yunlong --- diff --git a/lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py b/lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py index 4f27cee0..dee84622 100644 --- a/lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py +++ b/lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + import json import uuid @@ -44,12 +45,14 @@ class TestNFTerminate(TestCase): is_predefined=1, operationalstate=1) NfvoRegInfoModel.objects.create(nfvoid='1111', vnfminstid='11111', apiurl='1') + def tearDown(self): VmInstModel.objects.all().delete() NetworkInstModel.objects.all().delete() SubNetworkInstModel.objects.all().delete() PortInstModel.objects.all().delete() + def assert_job_result(self, job_id, job_progress, job_detail): jobs = JobStatusModel.objects.filter( jobid=job_id, @@ -57,33 +60,24 @@ class TestNFTerminate(TestCase): descp=job_detail) self.assertEqual(1, len(jobs)) - @mock.patch.object(restcall, 'call_req_aai') - def test_delete_vnf_identifier(self, mock_call_req_aai): + + @mock.patch.object(restcall, 'call_req') + def test_delete_vnf_identifier(self, mock_call_req): NfInstModel.objects.create(nfinstid='1111', nf_name='2222', package_id='todo', version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED', nf_desc='', vnfdid='', vnfSoftwareVersion='', vnfConfigurableProperties='todo', localizationLanguage='EN_US', create_time=now_time()) r1_create_vnf_to_aai = [0, json.JSONEncoder().encode({}), '200'] - mock_call_req_aai.side_effect = [r1_create_vnf_to_aai] + mock_call_req.side_effect = [r1_create_vnf_to_aai] response = self.client.delete("/api/vnflcm/v1/vnf_instances/1111") self.failUnlessEqual(status.HTTP_204_NO_CONTENT, response.status_code) self.assertEqual(None, response.data) - """ + def test_delete_vnf_identifier_when_vnf_not_exist(self): response = self.client.delete("/api/vnflcm/v1/vnf_instances/1111") - self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code) - self.assertEqual("VnfInst(1111) does not exist", response.data["error"]) + self.failUnlessEqual(status.HTTP_204_NO_CONTENT, response.status_code) - def test_delete_vnf_identifier_when_status_check_failed(self): - NfInstModel.objects.create(nfinstid='1111', nf_name='2222', package_id='todo', version='', vendor='', - netype='', vnfd_model='', status='VNF_INSTANTIATED', nf_desc='', vnfdid='', - vnfSoftwareVersion='', vnfConfigurableProperties='todo', - localizationLanguage='EN_US', create_time=now_time()) - response = self.client.delete("/api/vnflcm/v1/vnf_instances/1111") - self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code) - self.assertEqual("Don't allow to delete vnf(status:[VNF_INSTANTIATED])", response.data["error"]) - """ @mock.patch.object(TermVnf, 'run') def test_terminate_vnf(self, mock_run): @@ -91,7 +85,7 @@ class TestNFTerminate(TestCase): response = self.client.post("/api/vnflcm/v1/vnf_instances/12/terminate", data={}, format='json') self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code) - """ + def test_terminate_vnf_when_inst_id_not_exist(self): data = {"terminationType": "GRACEFUL", "gracefulTerminationTimeout": 120} @@ -99,8 +93,8 @@ class TestNFTerminate(TestCase): self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id) JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY") TermVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run() - self.assert_job_result(self.job_id, 255, "VnfInst(%s) does not exist" % self.nf_inst_id) - """ + self.assert_job_result(self.job_id, 100, "Terminate Vnf success.") + @mock.patch.object(restcall, 'call_req') @mock.patch.object(api, 'call') diff --git a/lcm/lcm/nf/vnfs/tests/test_vnf_create.py b/lcm/lcm/nf/vnfs/tests/test_vnf_create.py index aebfdb3b..c715ced2 100644 --- a/lcm/lcm/nf/vnfs/tests/test_vnf_create.py +++ b/lcm/lcm/nf/vnfs/tests/test_vnf_create.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + import json import uuid @@ -49,15 +50,13 @@ class TestNFInstantiate(TestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) @mock.patch.object(restcall, 'call_req') - @mock.patch.object(restcall, 'call_req_aai') - def test_create_vnf_identifier(self, mock_call_req_aai, mock_call_req): + def test_create_vnf_identifier(self, mock_call_req): r1_get_csarid_by_vnfdid = [0, json.JSONEncoder().encode({'csars':[{'package_id': '222', 'csarId': '2222', 'vnfdId': '111'}]}), '200'] r2_get_rawdata_from_catalog = [0, json.JSONEncoder().encode(vnfd_rawdata), '200'] r3_create_vnf_to_aai = [0, json.JSONEncoder().encode({}), '200'] - mock_call_req.side_effect = [r1_get_csarid_by_vnfdid, r2_get_rawdata_from_catalog] - mock_call_req_aai.side_effect = [r3_create_vnf_to_aai] + mock_call_req.side_effect = [r1_get_csarid_by_vnfdid, r2_get_rawdata_from_catalog, r3_create_vnf_to_aai] data = { "vnfdId": "111", "vnfInstanceName": "vFW_01", diff --git a/lcm/lcm/nf/vnfs/tests/test_vnf_query.py b/lcm/lcm/nf/vnfs/tests/test_vnf_query.py index 7f21d47b..42295085 100644 --- a/lcm/lcm/nf/vnfs/tests/test_vnf_query.py +++ b/lcm/lcm/nf/vnfs/tests/test_vnf_query.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from django.test import TestCase, Client from rest_framework import status diff --git a/lcm/lcm/pub/msapi/aai.py b/lcm/lcm/pub/msapi/aai.py index 67b25c2b..c62e4e97 100644 --- a/lcm/lcm/pub/msapi/aai.py +++ b/lcm/lcm/pub/msapi/aai.py @@ -14,15 +14,28 @@ import json import logging +import uuid from lcm.pub.config.config import AAI_BASE_URL, AAI_USER, AAI_PASSWORD from lcm.pub.exceptions import NFLCMException -from lcm.pub.utils.restcall import call_req_aai, rest_no_auth +from lcm.pub.utils.restcall import rest_no_auth, call_req logger = logging.getLogger(__name__) + def call_aai(resource, method, data=''): - return call_req_aai(AAI_BASE_URL, AAI_USER, AAI_PASSWORD, rest_no_auth, resource, method, data) + additional_headers = { + 'X-FromAppId': 'VFC-GVNFM-VNFLCM', + 'X-TransactionId': str(uuid.uuid1()) + } + return call_req(AAI_BASE_URL, + AAI_USER, + AAI_PASSWORD, + rest_no_auth, + resource, + method, + data, + additional_headers) def create_ns(global_customer_id, service_type, service_instance_id, data): diff --git a/lcm/lcm/pub/utils/restcall.py b/lcm/lcm/pub/utils/restcall.py index 89c4d224..6bcb0331 100644 --- a/lcm/lcm/pub/utils/restcall.py +++ b/lcm/lcm/pub/utils/restcall.py @@ -29,7 +29,7 @@ HTTP_404_NOTFOUND, HTTP_403_FORBIDDEN, HTTP_401_UNAUTHORIZED, HTTP_400_BADREQUES logger = logging.getLogger(__name__) -def call_req(base_url, user, passwd, auth_type, resource, method, content=''): +def call_req(base_url, user, passwd, auth_type, resource, method, content='', additional_headers={}): callid = str(uuid.uuid1()) logger.debug("[%s]call_req('%s','%s','%s',%s,'%s','%s','%s')" % ( callid, base_url, user, passwd, auth_type, resource, method, content)) @@ -41,6 +41,8 @@ def call_req(base_url, user, passwd, auth_type, resource, method, content=''): if user: headers['Authorization'] = 'Basic ' + ('%s:%s' % (user, passwd)).encode("base64") ca_certs = None + if additional_headers: + headers.update(additional_headers) for retry_times in range(3): http = httplib2.Http(ca_certs=ca_certs, disable_ssl_certificate_validation=(auth_type == rest_no_auth)) http.follow_all_redirects = True @@ -93,52 +95,3 @@ def combine_url(base_url, resource): else: full_url = base_url + '/' + resource return full_url - - -def call_req_aai(base_url, user, passwd, auth_type, resource, method, content=''): - callid = str(uuid.uuid1()) - logger.debug("[%s]call_req('%s','%s','%s',%s,'%s','%s','%s')" % ( - callid, base_url, user, passwd, auth_type, resource, method, content)) - ret = None - resp_status = '' - try: - full_url = combine_url(base_url, resource) - headers = {'content-type': 'application/json', 'accept': 'application/json', - 'X-FromAppId': 'VFC-GVNFM-VNFLCM', 'X-TransactionId': str(uuid.uuid1())} - if user: - headers['Authorization'] = 'Basic ' + ('%s:%s' % (user, passwd)).encode("base64") - ca_certs = None - for retry_times in range(3): - http = httplib2.Http(ca_certs=ca_certs, disable_ssl_certificate_validation=(auth_type == rest_no_auth)) - http.follow_all_redirects = True - try: - resp, resp_content = http.request(full_url, method=method.upper(), body=content, headers=headers) - resp_status, resp_body = resp['status'], resp_content.decode('UTF-8') - logger.debug("[%s][%d]status=%s,resp_body=%s)" % (callid, retry_times, resp_status, resp_body)) - if resp_status in status_ok_list: - ret = [0, resp_body, resp_status] - else: - ret = [1, resp_body, resp_status] - break - except Exception as ex: - if 'httplib.ResponseNotReady' in str(sys.exc_info()): - logger.debug("retry_times=%d", retry_times) - logger.error(traceback.format_exc()) - ret = [1, "Unable to connect to %s" % full_url, resp_status] - continue - raise ex - except urllib2.URLError as err: - ret = [2, str(err), resp_status] - except Exception as ex: - logger.error(traceback.format_exc()) - logger.error("[%s]ret=%s" % (callid, str(sys.exc_info()))) - res_info = str(sys.exc_info()) - 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] - except: - logger.error(traceback.format_exc()) - ret = [4, str(sys.exc_info()), resp_status] - - logger.debug("[%s]ret=%s" % (callid, str(ret))) - return ret