Logs events for VIM, VNFD and VNF operations

This commit invokes the create_event() to log events
for VIM VNFD and VNF operations

Change-Id: Ib82be521c5aa8b627e3f34a3696b10508371d3a0
Implements: blueprint: audit-support
Co-Authored-By: Kanagaraj Manickam <mkr1481@gmail.com>
This commit is contained in:
vish 2016-07-30 05:33:15 +00:00
parent e94357abac
commit 138371ecdc
5 changed files with 166 additions and 8 deletions

View File

@ -23,6 +23,7 @@ from sqlalchemy import orm
from sqlalchemy.orm import exc as orm_exc
from sqlalchemy import sql
from tacker.db.common_services import common_services_db
from tacker.db import db_base
from tacker.db import model_base
from tacker.db import models_v1
@ -30,6 +31,7 @@ from tacker.db import types
from tacker.db.vm import vm_db
from tacker.extensions import nfvo
from tacker import manager
from tacker.plugins.common import constants
VIM_ATTRIBUTES = ('id', 'type', 'tenant_id', 'name', 'description',
@ -68,6 +70,7 @@ class NfvoPluginDb(nfvo.NFVOPluginBase, db_base.CommonDbMixin):
def __init__(self):
super(NfvoPluginDb, self).__init__()
self._cos_db_plg = common_services_db.CommonServicesPluginDb()
@property
def _core_plugin(self):
@ -137,13 +140,26 @@ class NfvoPluginDb(nfvo.NFVOPluginBase, db_base.CommonDbMixin):
context.session.add(vim_auth_db)
else:
raise nfvo.VimDuplicateUrlException()
return self._make_vim_dict(vim_db)
vim_dict = self._make_vim_dict(vim_db)
self._cos_db_plg.create_event(
context, res_id=vim_dict['id'],
res_type=constants.RES_TYPE_VIM,
res_state=vim_dict['status'],
evt_type=constants.RES_EVT_CREATE,
tstamp=vim_dict['created_at'])
return vim_dict
def delete_vim(self, context, vim_id, soft_delete=True):
with context.session.begin(subtransactions=True):
vim_db = self._get_resource(context, Vim, vim_id)
if soft_delete:
vim_db.update({'deleted_at': timeutils.utcnow()})
self._cos_db_plg.create_event(
context, res_id=vim_db['id'],
res_type=constants.RES_TYPE_VIM,
res_state=vim_db['status'],
evt_type=constants.RES_EVT_DELETE,
tstamp=vim_db[constants.RES_EVT_DELETED_FLD])
else:
context.session.query(VimAuth).filter_by(
vim_id=vim_id).delete()
@ -183,6 +199,12 @@ class NfvoPluginDb(nfvo.NFVOPluginBase, db_base.CommonDbMixin):
vim_cred.pop('password'), 'vim_project':
vim_project})
vim_db.update({'updated_at': timeutils.utcnow()})
self._cos_db_plg.create_event(
context, res_id=vim_db['id'],
res_type=constants.RES_TYPE_VIM,
res_state=vim_db['status'],
evt_type=constants.RES_EVT_UPDATE,
tstamp=vim_db[constants.RES_EVT_UPDATED_FLD])
return self.get_vim(context, vim_id)
@ -194,6 +216,12 @@ class NfvoPluginDb(nfvo.NFVOPluginBase, db_base.CommonDbMixin):
except orm_exc.NoResultFound:
raise nfvo.VimNotFoundException(vim_id=vim_id)
vim_db.update({'status': status})
self._cos_db_plg.create_event(
context, res_id=vim_db['id'],
res_type=constants.RES_TYPE_VIM,
res_state=vim_db['status'],
evt_type=constants.RES_EVT_UPDATE,
tstamp=timeutils.utcnow())
return self._make_vim_dict(vim_db)
# Deprecated. Will be removed in Ocata release

View File

@ -25,6 +25,7 @@ from sqlalchemy.orm import exc as orm_exc
from tacker.api.v1 import attributes
from tacker import context as t_context
from tacker.db.common_services import common_services_db
from tacker.db import db_base
from tacker.db import model_base
from tacker.db import models_v1
@ -156,6 +157,7 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
def __init__(self):
super(VNFMPluginDb, self).__init__()
self._cos_db_plg = common_services_db.CommonServicesPluginDb()
def _get_resource(self, context, model, id):
try:
@ -266,7 +268,15 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
LOG.debug(_('template_db %(template_db)s %(attributes)s '),
{'template_db': template_db,
'attributes': template_db.attributes})
return self._make_template_dict(template_db)
vnfd_dict = self._make_template_dict(template_db)
LOG.debug(_('vnfd_dict %s'), vnfd_dict)
self._cos_db_plg.create_event(
context, res_id=vnfd_dict['id'],
res_type=constants.RES_TYPE_VNFD,
res_state=constants.RES_EVT_VNFD_NA_STATE,
evt_type=constants.RES_EVT_CREATE,
tstamp=vnfd_dict[constants.RES_EVT_CREATED_FLD])
return vnfd_dict
def update_device_template(self, context, device_template_id,
device_template):
@ -275,7 +285,14 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
device_template_id)
template_db.update(device_template['device_template'])
template_db.update({'updated_at': timeutils.utcnow()})
return self._make_template_dict(template_db)
vnfd_dict = self._make_template_dict(template_db)
self._cos_db_plg.create_event(
context, res_id=vnfd_dict['id'],
res_type=constants.RES_TYPE_VNFD,
res_state=constants.RES_EVT_VNFD_NA_STATE,
evt_type=constants.RES_EVT_UPDATE,
tstamp=vnfd_dict[constants.RES_EVT_UPDATED_FLD])
return vnfd_dict
def delete_device_template(self,
context,
@ -294,6 +311,12 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
device_template_id)
if soft_delete:
template_db.update({'deleted_at': timeutils.utcnow()})
self._cos_db_plg.create_event(
context, res_id=template_db['id'],
res_type=constants.RES_TYPE_VNFD,
res_state=constants.RES_EVT_VNFD_NA_STATE,
evt_type=constants.RES_EVT_DELETE,
tstamp=template_db[constants.RES_EVT_DELETED_FLD])
else:
context.session.query(ServiceType).filter_by(
vnfd_id=device_template_id).delete()
@ -377,7 +400,13 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
id=str(uuid.uuid4()), vnf_id=device_id,
key=key, value=value)
context.session.add(arg)
self._cos_db_plg.create_event(
context, res_id=device_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")
return self._make_device_dict(device_db)
# called internally, not by REST API
@ -399,6 +428,14 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
if 'vim_auth' not in key:
self._device_attribute_update_or_create(context, device_id,
key, value)
evt_details = ("Infra Instance ID created: %s and "
"Mgmt URL set: %s") % (instance_id, mgmt_url)
self._cos_db_plg.create_event(
context, res_id=device_dict['id'],
res_type=constants.RES_TYPE_VNF,
res_state=device_dict['status'],
evt_type=constants.RES_EVT_CREATE,
tstamp=timeutils.utcnow(), details=evt_details)
def _create_device_status(self, context, device_id, new_status):
with context.session.begin(subtransactions=True):
@ -406,6 +443,12 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
filter(VNF.id == device_id).
filter(VNF.status.in_(CREATE_STATES)).one())
query.update({'status': new_status})
self._cos_db_plg.create_event(
context, res_id=device_id,
res_type=constants.RES_TYPE_VNF,
res_state=new_status,
evt_type=constants.RES_EVT_CREATE,
tstamp=timeutils.utcnow(), details="VNF status updated")
def _get_device_db(self, context, device_id, current_statuses, new_status):
try:
@ -438,7 +481,14 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
with context.session.begin(subtransactions=True):
device_db = self._get_device_db(
context, device_id, _ACTIVE_UPDATE, constants.PENDING_UPDATE)
return self._make_device_dict(device_db)
updated_device_dict = self._make_device_dict(device_db)
self._cos_db_plg.create_event(
context, res_id=device_id,
res_type=constants.RES_TYPE_VNF,
res_state=updated_device_dict['status'],
evt_type=constants.RES_EVT_UPDATE,
tstamp=timeutils.utcnow())
return updated_device_dict
def _update_device_post(self, context, device_id, new_status,
new_device_dict=None):
@ -459,14 +509,26 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
if 'vim_auth' not in key:
self._device_attribute_update_or_create(context, device_id,
key, value)
self._cos_db_plg.create_event(
context, res_id=device_id,
res_type=constants.RES_TYPE_VNF,
res_state=new_device_dict['status'],
evt_type=constants.RES_EVT_UPDATE,
tstamp=new_device_dict[constants.RES_EVT_UPDATED_FLD])
def _delete_device_pre(self, context, device_id):
with context.session.begin(subtransactions=True):
device_db = self._get_device_db(
context, device_id, _ACTIVE_UPDATE_ERROR_DEAD,
constants.PENDING_DELETE)
return self._make_device_dict(device_db)
deleted_device_db = self._make_device_dict(device_db)
self._cos_db_plg.create_event(
context, res_id=device_id,
res_type=constants.RES_TYPE_VNF,
res_state=deleted_device_db['status'],
evt_type=constants.RES_EVT_DELETE,
tstamp=timeutils.utcnow(), details="VNF delete initiated")
return deleted_device_db
def _delete_device_post(self, context, device_id, error, soft_delete=True):
with context.session.begin(subtransactions=True):
@ -476,9 +538,24 @@ class VNFMPluginDb(vnfm.VNFMPluginBase, db_base.CommonDbMixin):
filter(VNF.status == constants.PENDING_DELETE))
if error:
query.update({'status': constants.ERROR})
self._cos_db_plg.create_event(
context, res_id=device_id,
res_type=constants.RES_TYPE_VNF,
res_state=constants.ERROR,
evt_type=constants.RES_EVT_DELETE,
tstamp=timeutils.utcnow(),
details="VNF Delete ERROR")
else:
if soft_delete:
query.update({'deleted_at': timeutils.utcnow()})
deleted_time_stamp = timeutils.utcnow()
query.update({'deleted_at': deleted_time_stamp})
self._cos_db_plg.create_event(
context, res_id=device_id,
res_type=constants.RES_TYPE_VNF,
res_state=constants.PENDING_DELETE,
evt_type=constants.RES_EVT_DELETE,
tstamp=deleted_time_stamp,
details="VNF Delete Complete")
else:
(self._model_query(context, VNFAttribute).
filter(VNFAttribute.vnf_id == device_id).delete())

View File

@ -52,3 +52,16 @@ POLICY_SCALING = 'tosca.policy.tacker.Scaling'
POLICY_SCALING_ACTIONS = (ACTION_SCALE_OUT,
ACTION_SCALE_IN) = ('out', 'in')
POLICY_ACTIONS = {POLICY_SCALING: POLICY_SCALING_ACTIONS}
RES_TYPE_VNFD = "vnfd"
RES_TYPE_VNF = "vnf"
RES_TYPE_VIM = "vim"
RES_EVT_CREATE = "CREATE"
RES_EVT_DELETE = "DELETE"
RES_EVT_UPDATE = "UPDATE"
RES_EVT_VNFD_NA_STATE = "Not Applicable"
RES_EVT_CREATED_FLD = "created_at"
RES_EVT_DELETED_FLD = "deleted_at"
RES_EVT_UPDATED_FLD = "updated_at"

View File

@ -18,8 +18,10 @@ import uuid
import mock
from tacker import context
from tacker.db.common_services import common_services_db
from tacker.db.nfvo import nfvo_db
from tacker.nfvo import nfvo_plugin
from tacker.plugins.common import constants
from tacker.tests.unit.db import base as db_base
SECRET_PASSWORD = '***'
@ -39,6 +41,10 @@ class TestNfvoPlugin(db_base.SqlTestCase):
self._mock_driver_manager()
mock.patch('tacker.nfvo.nfvo_plugin.NfvoPlugin.__run__').start()
self.nfvo_plugin = nfvo_plugin.NfvoPlugin()
mock.patch('tacker.db.common_services.common_services_db.'
'CommonServicesPluginDb.create_event'
).start()
self._cos_db_plugin = common_services_db.CommonServicesPluginDb()
def _mock_driver_manager(self):
self._driver_manager = mock.Mock(wraps=FakeDriverManager())
@ -80,6 +86,10 @@ class TestNfvoPlugin(db_base.SqlTestCase):
'tenant_id': 'test-project'}}
vim_type = 'openstack'
res = self.nfvo_plugin.create_vim(self.context, vim_dict)
self._cos_db_plugin.create_event.assert_any_call(
self.context, evt_type=constants.RES_EVT_CREATE, res_id=mock.ANY,
res_state=mock.ANY, res_type=constants.RES_TYPE_VIM,
tstamp=mock.ANY)
self._driver_manager.invoke.assert_any_call(vim_type,
'register_vim', vim_obj=vim_dict['vim'])
self._driver_manager.invoke.assert_any_call('openstack', 'vim_status',
@ -99,6 +109,10 @@ class TestNfvoPlugin(db_base.SqlTestCase):
self._driver_manager.invoke.assert_called_once_with(vim_type,
'deregister_vim',
vim_id=vim_id)
self._cos_db_plugin.create_event.assert_called_with(
self.context, evt_type=constants.RES_EVT_DELETE, res_id=mock.ANY,
res_state=mock.ANY, res_type=constants.RES_TYPE_VIM,
tstamp=mock.ANY)
def test_update_vim(self):
vim_dict = {'vim': {'id': '6261579e-d6f3-49ad-8bc3-a9cb974778ff',
@ -121,3 +135,7 @@ class TestNfvoPlugin(db_base.SqlTestCase):
self.assertEqual(vim_auth_username, res['auth_cred']['username'])
self.assertEqual(SECRET_PASSWORD, res['auth_cred']['password'])
self.assertIn('updated_at', res)
self._cos_db_plugin.create_event.assert_called_with(
self.context, evt_type=constants.RES_EVT_UPDATE, res_id=mock.ANY,
res_state=mock.ANY, res_type=constants.RES_TYPE_VIM,
tstamp=mock.ANY)

View File

@ -18,9 +18,11 @@ import uuid
import mock
from tacker import context
from tacker.db.common_services import common_services_db
from tacker.db.nfvo import nfvo_db
from tacker.db.vm import vm_db
from tacker.extensions import vnfm
from tacker.plugins.common import constants
from tacker.tests.unit.db import base as db_base
from tacker.tests.unit.db import utils
from tacker.vm import plugin
@ -56,6 +58,10 @@ class TestVNFMPlugin(db_base.SqlTestCase):
self._mock_green_pool()
self._insert_dummy_vim()
self.vnfm_plugin = plugin.VNFMPlugin()
mock.patch('tacker.db.common_services.common_services_db.'
'CommonServicesPluginDb.create_event'
).start()
self._cos_db_plugin = common_services_db.CommonServicesPluginDb()
def _mock_device_manager(self):
self._device_manager = mock.Mock(wraps=FakeDriverManager())
@ -160,6 +166,10 @@ class TestVNFMPlugin(db_base.SqlTestCase):
plugin=mock.ANY,
context=mock.ANY,
device_template=mock.ANY)
self._cos_db_plugin.create_event.assert_called_once_with(
self.context, evt_type=constants.RES_EVT_CREATE, res_id=mock.ANY,
res_state=constants.RES_EVT_VNFD_NA_STATE,
res_type=constants.RES_TYPE_VNFD, tstamp=mock.ANY)
def test_create_vnfd_no_service_types(self):
vnfd_obj = utils.get_dummy_vnfd_obj()
@ -193,6 +203,10 @@ class TestVNFMPlugin(db_base.SqlTestCase):
device=mock.ANY,
auth_attr=mock.ANY)
self._pool.spawn_n.assert_called_once_with(mock.ANY)
self._cos_db_plugin.create_event.assert_called_with(
self.context, evt_type=constants.RES_EVT_CREATE, res_id=mock.ANY,
res_state=mock.ANY, res_type=constants.RES_TYPE_VNF,
tstamp=mock.ANY, details=mock.ANY)
def test_delete_vnf(self):
self._insert_dummy_device_template()
@ -208,6 +222,10 @@ class TestVNFMPlugin(db_base.SqlTestCase):
self._vnf_monitor.delete_hosting_vnf.assert_called_with(mock.ANY)
self._pool.spawn_n.assert_called_once_with(mock.ANY, mock.ANY,
mock.ANY, mock.ANY)
self._cos_db_plugin.create_event.assert_called_with(
self.context, evt_type=constants.RES_EVT_DELETE, res_id=mock.ANY,
res_state=mock.ANY, res_type=constants.RES_TYPE_VNF,
tstamp=mock.ANY, details=mock.ANY)
def test_update_vnf(self):
self._insert_dummy_device_template()
@ -224,3 +242,7 @@ class TestVNFMPlugin(db_base.SqlTestCase):
self.assertIn('updated_at', result)
self._pool.spawn_n.assert_called_once_with(mock.ANY, mock.ANY,
mock.ANY, mock.ANY)
self._cos_db_plugin.create_event.assert_called_with(
self.context, evt_type=constants.RES_EVT_UPDATE, res_id=mock.ANY,
res_state=mock.ANY, res_type=constants.RES_TYPE_VNF,
tstamp=mock.ANY)