diff options
author | ying.yunlong <ying.yunlong@zte.com.cn> | 2017-09-22 16:45:46 +0800 |
---|---|---|
committer | ying.yunlong <ying.yunlong@zte.com.cn> | 2017-09-22 16:45:46 +0800 |
commit | 47bfb11ebbdfd0257238caeb6a6924cee9d2d7a5 (patch) | |
tree | 3b1e5dc6fd8893e646d81de7682b457792ff79be /gvnfmadapter | |
parent | 5a179acca51109cc6ca662996f7405370d39dffb (diff) |
Refactor gvnfmadapter query vnf and status
Change-Id: Ie16138d83c89005aac129667cc1160807e3ec877
Issue-ID: VFC-418
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
Diffstat (limited to 'gvnfmadapter')
-rw-r--r-- | gvnfmadapter/driver/interfaces/tests.py | 170 | ||||
-rw-r--r-- | gvnfmadapter/driver/interfaces/views.py | 76 |
2 files changed, 122 insertions, 124 deletions
diff --git a/gvnfmadapter/driver/interfaces/tests.py b/gvnfmadapter/driver/interfaces/tests.py index e0c18c6..a05ea80 100644 --- a/gvnfmadapter/driver/interfaces/tests.py +++ b/gvnfmadapter/driver/interfaces/tests.py @@ -126,92 +126,92 @@ class InterfacesTest(TestCase): self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) self.assertEqual(job_info, response.data) - # @mock.patch.object(restcall, 'call_req') - # def test_query_vnf(self, mock_call_req): - # vnfm_info = { - # "vnfmId": "19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee", - # "name": "g_vnfm", - # "type": "vnfm", - # "vimId": "", - # "vendor": "ZTE", - # "version": "v1.0", - # "description": "vnfm", - # "certificateUrl": "", - # "url": "http://10.74.44.11", - # "userName": "admin", - # "password": "admin", - # "createTime": "2016-07-06 15:33:18" - # } - # job_info = {"ResponseInfo": {"vnfInstanceId":"88","instantiationState":"INSTANTIATED","vnfSoftwareVersion":"v1.2.3"}} - # r1 = [0, json.JSONEncoder().encode(vnfm_info), "200"] - # r2 = [0, json.JSONEncoder().encode(job_info), "200"] - # mock_call_req.side_effect = [r1, r2] - # response = self.client.get("/api/gvnfmdriver/v1/19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee/vnfs/88") - # self.assertEqual(status.HTTP_200_OK, response.status_code) - # expect_resp_data = {"vnfInfo": {"vnfInstanceId": "88", "vnfStatus": "ACTIVE","version":"v1.2.3"}} - # self.assertEqual(expect_resp_data, response.data) + @mock.patch.object(restcall, 'call_req') + def test_query_vnf(self, mock_call_req): + vnfm_info = { + "vnfmId": "19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee", + "name": "g_vnfm", + "type": "vnfm", + "vimId": "", + "vendor": "ZTE", + "version": "v1.0", + "description": "vnfm", + "certificateUrl": "", + "url": "http://10.74.44.11", + "userName": "admin", + "password": "admin", + "createTime": "2016-07-06 15:33:18" + } + job_info = {"ResponseInfo": {"vnfInstanceId":"88","instantiationState":"INSTANTIATED","vnfSoftwareVersion":"v1.2.3"}} + r1 = [0, json.JSONEncoder().encode(vnfm_info), "200"] + r2 = [0, json.JSONEncoder().encode(job_info), "200"] + mock_call_req.side_effect = [r1, r2] + response = self.client.get("/api/gvnfmdriver/v1/19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee/vnfs/88") + self.assertEqual(status.HTTP_200_OK, response.status_code) + expect_resp_data = {"vnfInfo": {"vnfInstanceId": "88", "vnfStatus": "ACTIVE","version":"v1.2.3"}} + self.assertEqual(expect_resp_data, response.data) - # @mock.patch.object(restcall, 'call_req') - # def test_operation_status(self, mock_call_req): - # vnfm_info = { - # 'userName': 'admin', - # 'vendor': 'ZTE', - # 'name': 'ZTE_VNFM_237_62', - # 'vimId': '516cee95-e8ca-4d26-9268-38e343c2e31e', - # 'url': 'http://192.168.237.165:2324', - # 'certificateUrl': '', - # 'version': 'V1.0', - # 'vnfmId': 'b0797c9b-3da9-459c-b25c-3813e9d8fd70', - # 'password': 'admin', - # 'type': 'ztevmanagerdriver', - # 'createTime': '2016-10-31 11:08:39', - # 'description': '' - # } - # expected_body = { - # "jobId": "NF-CREATE-11-ec6c2f2a-9f48-11e6-9405-fa163e91c2f9", - # "responseDescriptor":{ - # "responseId": 3, - # "progress": 40, - # "status": "PROCESSING", - # "statusDescription": "OMC VMs are decommissioned in VIM", - # "errorCode": "null", - # "responseHistoryList": [ - # { - # "status": "error", - # "progress": 255, - # "errorcode": "", - # "responseid": 20, - # "statusdescription": "'JsonParser' object has no attribute 'parser_info'" - # } - # ] - # } - # } - # resp_body = { - # "ResponseInfo": { - # "vnfLcOpId":"NF-CREATE-11-ec6c2f2a-9f48-11e6-9405-fa163e91c2f9", - # "responseDescriptor":{ - # "responseId": 3, - # "progress": 40, - # "lcmOperationStatus": "PROCESSING", - # "statusDescription": "OMC VMs are decommissioned in VIM", - # "errorCode": "null", - # "responseHistoryList": [ - # {"status": "error", - # "progress": 255, - # "errorcode": "", - # "responseid": 20, - # "statusdescription": "'JsonParser' object has no attribute 'parser_info'"}] - # } - # } - # } - # r1 = [0, json.JSONEncoder().encode(vnfm_info), '200'] - # r2 = [0, json.JSONEncoder().encode(resp_body), '200'] - # mock_call_req.side_effect = [r1, r2] - # response = self.client.get("/api/gvnfmdriver/v1/{vnfmid}/jobs/{jobid}?responseId={responseId}". - # format(vnfmid=vnfm_info["vnfmId"],jobid=resp_body["ResponseInfo"]["vnfLcOpId"], - # responseId=resp_body["ResponseInfo"]["responseDescriptor"]["responseId"])) - # self.assertEqual(status.HTTP_200_OK, response.status_code) - # self.assertDictEqual(expected_body, response.data) + @mock.patch.object(restcall, 'call_req') + def test_operation_status(self, mock_call_req): + vnfm_info = { + 'userName': 'admin', + 'vendor': 'ZTE', + 'name': 'ZTE_VNFM_237_62', + 'vimId': '516cee95-e8ca-4d26-9268-38e343c2e31e', + 'url': 'http://192.168.237.165:2324', + 'certificateUrl': '', + 'version': 'V1.0', + 'vnfmId': 'b0797c9b-3da9-459c-b25c-3813e9d8fd70', + 'password': 'admin', + 'type': 'ztevmanagerdriver', + 'createTime': '2016-10-31 11:08:39', + 'description': '' + } + expected_body = { + "jobId": "NF-CREATE-11-ec6c2f2a-9f48-11e6-9405-fa163e91c2f9", + "responseDescriptor":{ + "responseId": 3, + "progress": 40, + "status": "PROCESSING", + "statusDescription": "OMC VMs are decommissioned in VIM", + "errorCode": "null", + "responseHistoryList": [ + { + "status": "error", + "progress": 255, + "errorcode": "", + "responseid": 20, + "statusdescription": "'JsonParser' object has no attribute 'parser_info'" + } + ] + } + } + resp_body = { + "ResponseInfo": { + "vnfLcOpId":"NF-CREATE-11-ec6c2f2a-9f48-11e6-9405-fa163e91c2f9", + "responseDescriptor":{ + "responseId": 3, + "progress": 40, + "lcmOperationStatus": "PROCESSING", + "statusDescription": "OMC VMs are decommissioned in VIM", + "errorCode": "null", + "responseHistoryList": [ + {"status": "error", + "progress": 255, + "errorcode": "", + "responseid": 20, + "statusdescription": "'JsonParser' object has no attribute 'parser_info'"}] + } + } + } + r1 = [0, json.JSONEncoder().encode(vnfm_info), '200'] + r2 = [0, json.JSONEncoder().encode(resp_body), '200'] + mock_call_req.side_effect = [r1, r2] + response = self.client.get("/api/gvnfmdriver/v1/{vnfmid}/jobs/{jobid}?responseId={responseId}". + format(vnfmid=vnfm_info["vnfmId"],jobid=resp_body["ResponseInfo"]["vnfLcOpId"], + responseId=resp_body["ResponseInfo"]["responseDescriptor"]["responseId"])) + self.assertEqual(status.HTTP_200_OK, response.status_code) + self.assertDictEqual(expected_body, response.data) @mock.patch.object(restcall, 'call_req') def test_grantvnf(self, mock_call_req): diff --git a/gvnfmadapter/driver/interfaces/views.py b/gvnfmadapter/driver/interfaces/views.py index f0d5213..bfb91e7 100644 --- a/gvnfmadapter/driver/interfaces/views.py +++ b/gvnfmadapter/driver/interfaces/views.py @@ -108,13 +108,12 @@ def terminate_vnf(request, *args, **kwargs): @api_view(http_method_names=['GET']) def query_vnf(request, *args, **kwargs): + logger.debug("query_vnf--post::> %s" % request.data) vnfm_id = ignorcase_get(kwargs, "vnfmid") vnfInstanceId = ignorcase_get(kwargs, "vnfInstanceId") try: logger.debug("[%s] request.data=%s", fun_name(), request.data) - ret, resp = do_queryvnf(request, vnfm_id, vnfInstanceId) - if ret != 0: - return resp + resp = do_queryvnf(request, vnfm_id, vnfInstanceId) query_vnf_resp_mapping = { "vnfInstanceId": "", "vnfInstanceName": "", @@ -137,43 +136,48 @@ def query_vnf(request, *args, **kwargs): if ignorcase_get(ignorcase_get(resp, "ResponseInfo"), "vnfInstanceId"): resp_data["vnfInfo"]["vnfInstanceId"] = ignorcase_get(ignorcase_get(resp, "ResponseInfo"), "vnfInstanceId") logger.debug("[%s]resp_data=%s", fun_name(), resp_data) - except Exception as e: - logger.error("Error occurred when querying VNF information.") - raise e - return Response(data=resp_data, status=status.HTTP_200_OK) + return Response(data=resp_data, status=status.HTTP_200_OK) + except GvnfmDriverException as e: + logger.error('Query vnf failed, detail message: %s' % e.message) + return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + except: + logger.error(traceback.format_exc()) + return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) @api_view(http_method_names=['GET']) def operation_status(request, *args, **kwargs): - data = {} + logger.debug("operation_status--post::> %s" % request.data) try: logger.debug("[%s] request.data=%s", fun_name(), request.data) vnfm_id = ignorcase_get(kwargs, "vnfmid") jobId = ignorcase_get(kwargs, "jobId") responseId = ignorcase_get(kwargs, "responseId") - ret, vnfm_info = get_vnfminfo_from_nslcm(vnfm_id) - if ret != 0: - return Response(data={'error': ret[1]}, status=ret[2]) - logger.debug("[%s] vnfm_info=%s", fun_name(), vnfm_info) + logger.debug("[operation_status] vnfm_id=%s", vnfm_id) + vnfm_info = get_vnfminfo_from_nslcm( vnfm_id ) + logger.debug("[operation_status] vnfm_info=[%s]", vnfm_info) + ret = call_vnfm("api/vnflcm/v1/vnf_lc_ops/%s?responseId=%s" % (jobId, responseId), "GET", vnfm_info) if ret[0] != 0: - return Response(data={'error': ret[1]}, status=ret[2]) + logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) + raise GvnfmDriverException('Failed to query vnf operation status.') resp_data = json.JSONDecoder().decode(ret[1]) logger.info("[%s]resp_data=%s", fun_name(), resp_data) ResponseInfo = ignorcase_get(resp_data, "ResponseInfo") + responseDescriptor = ignorcase_get(ResponseInfo, "responseDescriptor") operation_data = {} operation_data["jobId"] = ignorcase_get(ResponseInfo, "vnfLcOpId") - operation_data["responseDescriptor"] = {} - operation_data["responseDescriptor"]["status"] = ignorcase_get(ignorcase_get(ResponseInfo, "responseDescriptor"),"lcmOperationStatus") - operation_data["responseDescriptor"]["progress"] = ignorcase_get(ignorcase_get(ResponseInfo, "responseDescriptor"),"progress") - operation_data["responseDescriptor"]["statusDescription"] = ignorcase_get(ignorcase_get(ResponseInfo, "responseDescriptor"),"statusDescription") - operation_data["responseDescriptor"]["errorCode"] = ignorcase_get(ignorcase_get(ResponseInfo, "responseDescriptor"),"errorCode") - operation_data["responseDescriptor"]["responseId"] = ignorcase_get(ignorcase_get(ResponseInfo, "responseDescriptor"),"responseId") - operation_data["responseDescriptor"]["responseHistoryList"] = ignorcase_get(ignorcase_get(ResponseInfo, "responseDescriptor"),"responseHistoryList") - except Exception as e: - logger.error("Error occurred when getting operation status information.") - raise e - return Response(data=operation_data, status=status.HTTP_200_OK) + status_tmp = ignorcase_get(responseDescriptor,"lcmOperationStatus") + del responseDescriptor["lcmOperationStatus"] + responseDescriptor["status"] = status_tmp + operation_data["responseDescriptor"] = responseDescriptor + return Response(data=operation_data, status=status.HTTP_200_OK) + except GvnfmDriverException as e: + logger.error('Query vnf failed, detail message: %s' % e.message) + return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + except: + logger.error(traceback.format_exc()) + return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) @api_view(http_method_names=['PUT']) @@ -386,18 +390,12 @@ def do_deletevnf(vnfm_id, vnfInstanceId, data): return json.JSONDecoder().decode(ret[1]) -def do_queryvnf(request, vnfm_id, vnfInstanceId): - logger.debug("[%s] request.data=%s", fun_name(), request.data) - try: - ret, vnfm_info = get_vnfminfo_from_nslcm(vnfm_id) - if ret != 0: - return ret, vnfm_info - ret = call_vnfm("api/vnflcm/v1/vnf_instances/%s" % vnfInstanceId, "GET", vnfm_info) - if ret[0] != 0: - return 255, Response(data={'error': ret[1]}, status=ret[2]) - resp_data = json.JSONDecoder().decode(ret[1]) - logger.debug("[%s]resp_data=%s", fun_name(), resp_data) - except Exception as e: - logger.error("Error occurred when do_query vnf") - raise e - return 0, resp_data +def do_queryvnf(data, vnfm_id, vnfInstanceId): + logger.debug("[%s] request.data=%s", fun_name(), data) + vnfm_info = get_vnfminfo_from_nslcm(vnfm_id) + logger.debug("[do_deletevnf] vnfm_info=[%s]", vnfm_info) + ret = call_vnfm("api/vnflcm/v1/vnf_instances/%s" % vnfInstanceId, "GET", vnfm_info) + if ret[0] != 0: + logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) + raise GvnfmDriverException('Failed to query vnf.') + return json.JSONDecoder().decode(ret[1]) |