Merge "Logs events for VNF monitoring and actions"

This commit is contained in:
Jenkins 2016-09-06 15:51:58 +00:00 committed by Gerrit Code Review
commit 80b638fcac
4 changed files with 66 additions and 7 deletions

View File

@ -399,13 +399,14 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
id=str(uuid.uuid4()), vnf_id=vnf_id,
key=key, value=value)
context.session.add(arg)
evt_details = "VNF UUID assigned."
self._cos_db_plg.create_event(
context, res_id=vnf_id,
res_type=constants.RES_TYPE_VNF,
res_state=constants.PENDING_CREATE,
evt_type=constants.RES_EVT_CREATE,
tstamp=timeutils.utcnow(),
details="VNF UUID assigned")
tstamp=vnf_db[constants.RES_EVT_CREATED_FLD],
details=evt_details)
return self._make_vnf_dict(vnf_db)
# called internally, not by REST API
@ -622,6 +623,12 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
return False
vnf_db.update({'status': new_status})
self._cos_db_plg.create_event(
context, res_id=vnf_id,
res_type=constants.RES_TYPE_VNF,
res_state=new_status,
evt_type=constants.RES_EVT_MONITOR,
tstamp=timeutils.utcnow())
return True
def _mark_vnf_error(self, vnf_id):

View File

@ -60,6 +60,7 @@ RES_TYPE_VIM = "vim"
RES_EVT_CREATE = "CREATE"
RES_EVT_DELETE = "DELETE"
RES_EVT_UPDATE = "UPDATE"
RES_EVT_MONITOR = "MONITOR"
RES_EVT_VNFD_NA_STATE = "Not Applicable"
RES_EVT_CREATED_FLD = "created_at"

View File

@ -18,6 +18,8 @@ import mock
from oslo_utils import timeutils
import testtools
from tacker.db.common_services import common_services_db
from tacker.plugins.common import constants
from tacker.vnfm.monitor import VNFMonitor
MOCK_DEVICE_ID = 'a737497c-761c-11e5-89c3-9cb6541d805d'
@ -54,6 +56,10 @@ class TestVNFMonitor(testtools.TestCase):
super(TestVNFMonitor, self).setUp()
p = mock.patch('tacker.common.driver_manager.DriverManager')
self.mock_monitor_manager = p.start()
mock.patch('tacker.db.common_services.common_services_db.'
'CommonServicesPluginDb.create_event'
).start()
self._cos_db_plugin = common_services_db.CommonServicesPluginDb()
self.addCleanup(p.stop)
def test_to_hosting_vnf(self):
@ -81,12 +87,26 @@ class TestVNFMonitor(testtools.TestCase):
@mock.patch('tacker.vnfm.monitor.VNFMonitor.__run__')
def test_add_hosting_vnf(self, mock_monitor_run):
test_device_dict = MOCK_VNF_DEVICE
test_device_dict = {
'id': MOCK_DEVICE_ID,
'mgmt_url': '{"vdu1": "a.b.c.d"}',
'attributes': {
'monitoring_policy': json.dumps(
MOCK_VNF_DEVICE['monitoring_policy'])
},
'status': 'ACTIVE'
}
action_cb = mock.MagicMock()
test_boot_wait = 30
test_vnfmonitor = VNFMonitor(test_boot_wait)
test_vnfmonitor.add_hosting_vnf(test_device_dict)
new_dict = test_vnfmonitor.to_hosting_vnf(test_device_dict, action_cb)
test_vnfmonitor.add_hosting_vnf(new_dict)
test_device_id = list(test_vnfmonitor._hosting_vnfs.keys())[0]
self.assertEqual(MOCK_DEVICE_ID, test_device_id)
self._cos_db_plugin.create_event.assert_called_with(
mock.ANY, res_id=mock.ANY, res_type=constants.RES_TYPE_VNF,
res_state=mock.ANY, evt_type=constants.RES_EVT_MONITOR,
tstamp=mock.ANY, details=mock.ANY)
@mock.patch('tacker.vnfm.monitor.VNFMonitor.__run__')
def test_run_monitor(self, mock_monitor_run):

View File

@ -28,6 +28,8 @@ import six
from tacker.common import clients
from tacker.common import driver_manager
from tacker import context as t_context
from tacker.db.common_services import common_services_db
from tacker.plugins.common import constants
from tacker.vnfm.infra_drivers.heat import heat
@ -45,6 +47,16 @@ def config_opts():
return [('monitor', OPTS), ('tacker', VNFMonitor.OPTS)]
def _log_monitor_events(context, vnf_dict, evt_details):
_cos_db_plg = common_services_db.CommonServicesPluginDb()
_cos_db_plg.create_event(context, res_id=vnf_dict['id'],
res_type=constants.RES_TYPE_VNF,
res_state=vnf_dict['status'],
evt_type=constants.RES_EVT_MONITOR,
tstamp=timeutils.utcnow(),
details=evt_details)
class VNFMonitor(object):
"""VNF Monitor."""
@ -110,6 +122,13 @@ class VNFMonitor(object):
with self._lock:
self._hosting_vnfs[new_vnf['id']] = new_vnf
attrib_dict = new_vnf['vnf']['attributes']
mon_policy_dict = attrib_dict['monitoring_policy']
evt_details = (("VNF added for monitoring. "
"mon_policy_dict = %s,") % (mon_policy_dict))
_log_monitor_events(t_context.get_admin_context(), new_vnf['vnf'],
evt_details)
def delete_hosting_vnf(self, vnf_id):
LOG.debug('deleting vnf_id %(vnf_id)s', {'vnf_id': vnf_id})
with self._lock:
@ -211,7 +230,7 @@ class ActionPolicy(object):
@ActionPolicy.register('respawn')
class ActionRespawn(ActionPolicy):
@classmethod
def execute_action(cls, plugin, vnf_dict):
def execute_action(cls, plugin, vnf_dict, auth_attr):
LOG.error(_('vnf %s dead'), vnf_dict['id'])
if plugin._mark_vnf_dead(vnf_dict['id']):
plugin._vnf_monitor.mark_dead(vnf_dict['id'])
@ -233,8 +252,12 @@ class ActionRespawn(ActionPolicy):
context.auth_token = token['id']
context.tenant_id = token['tenant_id']
context.user_id = token['user_id']
_log_monitor_events(context, vnf_dict,
"ActionRespawnPolicy invoked")
new_vnf_dict = plugin.create_vnf(context,
{'vnf': new_vnf})
_log_monitor_events(context, new_vnf_dict,
"ActionRespawnPolicy complete")
LOG.info(_('respawned new vnf %s'), new_vnf_dict['id'])
@ -261,6 +284,8 @@ class ActionRespawnHeat(ActionPolicy):
# TODO(anyone) set the current request ctxt instead of admin ctxt
context = t_context.get_admin_context()
_log_monitor_events(context, vnf_dict,
"ActionRespawnHeat invoked")
update_vnf_dict = plugin.create_vnf_sync(context,
vnf_dict)
plugin.config_vnf(context, update_vnf_dict)
@ -270,15 +295,21 @@ class ActionRespawnHeat(ActionPolicy):
@ActionPolicy.register('log')
class ActionLogOnly(ActionPolicy):
@classmethod
def execute_action(cls, plugin, vnf_dict):
def execute_action(cls, plugin, vnf_dict, auth_attr):
vnf_id = vnf_dict['id']
LOG.error(_('vnf %s dead'), vnf_id)
_log_monitor_events(t_context.get_admin_context(),
vnf_dict,
"ActionLogOnly invoked")
@ActionPolicy.register('log_and_kill')
class ActionLogAndKill(ActionPolicy):
@classmethod
def execute_action(cls, plugin, vnf_dict):
def execute_action(cls, plugin, vnf_dict, auth_attr):
_log_monitor_events(t_context.get_admin_context(),
vnf_dict,
"ActionLogAndKill invoked")
vnf_id = vnf_dict['id']
if plugin._mark_vnf_dead(vnf_dict['id']):
plugin._vnf_monitor.mark_dead(vnf_dict['id'])