Refactor vnfm_db

vnfm_db.py has the following problems.

(i) ``_update_vnf_scaling`` and ``_update_vnf_rollback`` save these two
unrelated objects: vnf_instance and vnf_lcm_op_occ. These objects are
not updated in these functions, but in vnflcm_driver. Thus, saving these
objects should be done in vnflcm_driver.

(ii) ``_update_vnf_rollback_status_err``, ``_update_vnf_rollback_pre``
and ``_update_vnf_rollback`` have a redundant else block.

This patch solves these problems as follows.

(i) Moved lines saving the objects to ``vnflcm_driver``
(ii) Removed redundant else block

Change-Id: I5feb5385bc5a186c1e6f1db742a2fe02e472ad16
This commit is contained in:
Hiromu Asahina
2022-02-04 18:36:50 +09:00
parent 749fb90c0e
commit 141a1f464d
3 changed files with 42 additions and 71 deletions

View File

@@ -574,9 +574,7 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
context,
vnf_info,
previous_statuses,
status,
vnf_instance=None,
vnf_lcm_op_occ=None):
status):
with context.session.begin(subtransactions=True):
timestamp = timeutils.utcnow()
(self._model_query(context, VNF).
@@ -601,11 +599,6 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
res_state=status,
evt_type=constants.RES_EVT_SCALE,
tstamp=timestamp)
if vnf_lcm_op_occ:
vnf_lcm_op_occ.state_entered_time = timestamp
vnf_lcm_op_occ.save()
if vnf_instance:
vnf_instance.save()
def _update_vnf_pre(self, context, vnf_id, new_status):
with context.session.begin(subtransactions=True):
@@ -878,47 +871,31 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
context,
vnf_info):
vnf_lcm_op_occs = vnf_info['vnf_lcm_op_occ']
evt_type = constants.RES_EVT_CREATE
if vnf_lcm_op_occs.operation == 'SCALE':
self._cos_db_plg.create_event(
context, res_id=vnf_info['id'],
res_type=constants.RES_TYPE_VNF,
res_state='ERROR',
evt_type=constants.RES_EVT_SCALE,
tstamp=timeutils.utcnow())
else:
self._cos_db_plg.create_event(
context, res_id=vnf_info['id'],
res_type=constants.RES_TYPE_VNF,
res_state='ERROR',
evt_type=constants.RES_EVT_CREATE,
tstamp=timeutils.utcnow())
evt_type = constants.RES_EVT_SCALE,
self._cos_db_plg.create_event(context, res_id=vnf_info['id'],
res_type=constants.RES_TYPE_VNF,
res_state='ERROR', evt_type=evt_type,
tstamp=timeutils.utcnow())
def _update_vnf_rollback_pre(self,
context,
vnf_info):
vnf_lcm_op_occs = vnf_info['vnf_lcm_op_occ']
evt_type = constants.RES_EVT_CREATE
if vnf_lcm_op_occs.operation == 'SCALE':
self._cos_db_plg.create_event(
context, res_id=vnf_info['id'],
res_type=constants.RES_TYPE_VNF,
res_state='ROLL_BACK',
evt_type=constants.RES_EVT_SCALE,
tstamp=timeutils.utcnow())
else:
self._cos_db_plg.create_event(
context, res_id=vnf_info['id'],
res_type=constants.RES_TYPE_VNF,
res_state='ROLL_BACK',
evt_type=constants.RES_EVT_CREATE,
tstamp=timeutils.utcnow())
evt_type = constants.RES_EVT_CREATE
self._cos_db_plg.create_event(context, res_id=vnf_info['id'],
res_type=constants.RES_TYPE_VNF,
res_state='ROLL_BACK', evt_type=evt_type,
tstamp=timeutils.utcnow())
def _update_vnf_rollback(self,
context,
vnf_info,
previous_statuses,
status,
vnf_instance=None,
vnf_lcm_op_occ=None):
status):
with context.session.begin(subtransactions=True):
timestamp = timeutils.utcnow()
(self._model_query(context, VNF).
@@ -934,26 +911,14 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
delete(synchronize_session='fetch'))
vnf_lcm_op_occs = vnf_info['vnf_lcm_op_occ']
evt_type = constants.RES_EVT_CREATE
if vnf_lcm_op_occs.operation == 'SCALE':
for (key, value) in dev_attrs.items():
if 'vim_auth' not in key:
self._vnf_attribute_update_or_create(
context, vnf_info['id'], key, value)
self._cos_db_plg.create_event(
context, res_id=vnf_info['id'],
res_type=constants.RES_TYPE_VNF,
res_state=status,
evt_type=constants.RES_EVT_SCALE,
tstamp=timestamp)
else:
self._cos_db_plg.create_event(
context, res_id=vnf_info['id'],
res_type=constants.RES_TYPE_VNF,
res_state=status,
evt_type=constants.RES_EVT_CREATE,
tstamp=timestamp)
if vnf_lcm_op_occ:
vnf_lcm_op_occ.state_entered_time = timestamp
vnf_lcm_op_occ.save()
if vnf_instance:
vnf_instance.save()
evt_type = constants.RES_EVT_SCALE
self._cos_db_plg.create_event(context, res_id=vnf_info['id'],
res_type=constants.RES_TYPE_VNF,
res_state=status, evt_type=evt_type,
tstamp=timestamp)

View File

@@ -1875,11 +1875,12 @@ class TestVnflcmDriver(db_base.SqlTestCase):
@mock.patch.object(vim_client.VimClient, "get_vim")
@mock.patch.object(objects.VnfLcmOpOcc, "save")
@mock.patch.object(objects.VnfInstance, "get_by_id")
@mock.patch.object(objects.VnfInstance, "save")
@mock.patch.object(driver_manager.DriverManager, "invoke")
def test_scale_in_cnf(self, mock_invoke, mock_vnf_instance_get_by_id,
mock_lcm_save, mock_vim, mock_vnf_package_vnfd,
mock_vnfd_dict, mock_yaml_safe_load, mock_init_hash,
mock_get_service_plugins):
def test_scale_in_cnf(self, mock_invoke, mock_vnf_instance_save,
mock_vnf_instance_get_by_id, mock_lcm_save, mock_vim,
mock_vnf_package_vnfd, mock_vnfd_dict, mock_yaml_safe_load,
mock_init_hash, mock_get_service_plugins):
mock_init_hash.return_value = {
"vnflcm_noop": "ffea638bfdbde3fb01f191bbe75b031859"
"b18d663b127100eb72b19eecd7ed51"
@@ -1934,10 +1935,12 @@ class TestVnflcmDriver(db_base.SqlTestCase):
@mock.patch.object(objects.VnfPackageVnfd, 'get_by_id')
@mock.patch.object(objects.VnfLcmOpOcc, "save")
@mock.patch.object(objects.VnfInstance, "get_by_id")
@mock.patch.object(objects.VnfInstance, "save")
@mock.patch.object(driver_manager.DriverManager, "invoke")
def test_scale_out_cnf(self, mock_invoke, mock_vnf_instance_get_by_id,
mock_lcm_save, mock_vnf_package_vnfd, mock_vnfd_dict,
mock_yaml_safe_load, mock_get_service_plugins):
def test_scale_out_cnf(self, mock_invoke,
mock_vnf_instance_save, mock_vnf_instance_get_by_id,
mock_lcm_save, mock_vnf_package_vnfd, mock_vnfd_dict,
mock_yaml_safe_load, mock_get_service_plugins):
vnf_info = fakes.vnf_dict_cnf()
vnf_info['vnf_lcm_op_occ'] = fakes.vnflcm_scale_out_cnf()
vnf_info['scale_level'] = 0

View File

@@ -27,6 +27,7 @@ from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import encodeutils
from oslo_utils import excutils
from oslo_utils import timeutils
from toscaparser import tosca_template
from tacker.common import driver_manager
@@ -387,6 +388,7 @@ class VnfLcmDriver(abstract_driver.VnfInstanceAbstractDriver):
if vnf_instance.instantiated_vnf_info and\
vnf_instance.instantiated_vnf_info.instance_id != instance_id:
# TODO(h-asahina): rename instance_id to stack_id
vnf_instance.instantiated_vnf_info.instance_id = instance_id
if vnf_dict['attributes'].get('scaling_group_names'):
@@ -993,14 +995,15 @@ class VnfLcmDriver(abstract_driver.VnfInstanceAbstractDriver):
jsonutils.dump_as_bytes(scaleGroupDict)
if vnf_info['before_error_point'] < EP.NOTIFY_COMPLETED:
self._vnfm_plugin._update_vnf_scaling(context, vnf_info,
'PENDING_' + scale_vnf_request.type, 'ACTIVE')
vnf_lcm_op_occ = vnf_info['vnf_lcm_op_occ']
vnf_lcm_op_occ.operation_state = 'COMPLETED'
vnf_lcm_op_occ.resource_changes = resource_changes
vnf_lcm_op_occ.state_entered_time = timeutils.utcnow()
vnf_lcm_op_occ.save()
vnf_instance.task_state = None
self._vnfm_plugin._update_vnf_scaling(context, vnf_info,
'PENDING_' + scale_vnf_request.type,
'ACTIVE', vnf_instance=vnf_instance,
vnf_lcm_op_occ=vnf_lcm_op_occ)
vnf_instance.save()
vnf_info['current_error_point'] = EP.NOTIFY_COMPLETED
@@ -1612,11 +1615,11 @@ class VnfLcmDriver(abstract_driver.VnfInstanceAbstractDriver):
else:
status = 'INACTIVE'
vnf_instance.task_state = None
self._vnfm_plugin._update_vnf_rollback(context, vnf_info,
'ERROR',
status,
vnf_instance=vnf_instance,
vnf_lcm_op_occ=vnf_lcm_op_occs)
self._vnfm_plugin._update_vnf_rollback(context, vnf_info, 'ERROR',
status)
vnf_lcm_op_occs.state_entered_time = timeutils.utcnow()
vnf_lcm_op_occs.save()
vnf_instance.save()
def _update_vnf_rollback_status_err(self, context, vnf_info):
self._vnfm_plugin.update_vnf_rollback_status_err(context, vnf_info)