refactor the '_extract_action_type' function

1.extract common action from alarm_transformer to transformer_base
2.rename '_extract_action_type' as '_extract_event_action'

Change-Id: I3ded56b76ef51db455ba6b685dfcdcae998934bc
Signed-off-by: dongwenjuan <dong.wenjuan@zte.com.cn>
This commit is contained in:
dongwenjuan 2016-11-22 11:44:25 +08:00
parent aac14c51a2
commit d32c2b19ae
8 changed files with 31 additions and 36 deletions

View File

@ -103,6 +103,9 @@ class AlarmDriverBase(DriverBase):
old_alarm, timestamp = self.cache.get(alarm_key, (None, None))
if filter_(self, alarm, old_alarm):
# delete state changed alarm: alarm->OK
if not self._is_erroneous(alarm):
alarm[DSProps.EVENT_TYPE] = EventAction.DELETE_ENTITY
alarms_to_update.append(alarm)
self.cache[alarm_key] = alarm, now

View File

@ -14,11 +14,9 @@
from oslo_log import log as logging
from vitrage.common.constants import ActionType
from vitrage.common.constants import DatasourceProperties as DSProps
from vitrage.common.constants import EntityCategory
from vitrage.common.constants import EventAction
from vitrage.common.exception import VitrageTransformerError
from vitrage.datasources.alarm_properties import AlarmProperties as AlarmProps
from vitrage.datasources import transformer_base as tbase
@ -37,24 +35,6 @@ class AlarmTransformerBase(tbase.TransformerBase):
LOG.info('An alarm cannot be a placeholder')
pass
def _extract_action_type(self, entity_event):
# TODO(ifat_afek): this method should reside together with the cache,
# in the transformer code
if DSProps.EVENT_TYPE in entity_event and \
entity_event[DSProps.EVENT_TYPE] == EventAction.DELETE_ENTITY:
return entity_event[DSProps.EVENT_TYPE]
action_type = entity_event[DSProps.ACTION_TYPE]
if action_type in (ActionType.UPDATE, ActionType.SNAPSHOT):
return EventAction.DELETE_ENTITY if self._ok_status(entity_event) \
else EventAction.UPDATE_ENTITY
if ActionType.INIT_SNAPSHOT == action_type:
return EventAction.CREATE_ENTITY
raise VitrageTransformerError('Invalid action type: (%s)'
% action_type)
def _key_values(self, *args):
return (EntityCategory.ALARM,) + args

View File

@ -119,7 +119,7 @@ class TransformerBase(object):
if not self._is_end_message(entity_event):
entity_vertex = self._create_entity_vertex(entity_event)
neighbors = self._create_neighbors(entity_event)
action = self._extract_action_type(entity_event)
action = self._extract_event_action(entity_event)
return EntityWrapper(entity_vertex, neighbors, action)
else:
@ -201,8 +201,8 @@ class TransformerBase(object):
"""
pass
def _extract_action_type(self, entity_event):
"""Extract action type.
def _extract_event_action(self, entity_event):
"""Extract event action.
Decides what action (from constants.EventAction) the processor need
to perform according to the data received from the event.
@ -212,6 +212,10 @@ class TransformerBase(object):
:rtype: str
"""
if DSProps.EVENT_TYPE in entity_event and \
entity_event[DSProps.EVENT_TYPE] in EventAction.__dict__.values():
return entity_event[DSProps.EVENT_TYPE]
action_type = entity_event[DSProps.ACTION_TYPE]
if ActionType.UPDATE == action_type:

View File

@ -131,7 +131,7 @@ class EvaluatorEventTransformer(transformer_base.TransformerBase):
return []
def _extract_action_type(self, event):
def _extract_event_action(self, event):
event_type = event[EVALUATOR_EVENT_TYPE]
try:

View File

@ -16,7 +16,9 @@ from oslo_config import cfg
from oslo_log import log as logging
from six.moves import queue
from vitrage.common.constants import DatasourceProperties as DSProps
from vitrage.common.constants import EdgeProperties as EProps
from vitrage.common.constants import EventAction
from vitrage.common.constants import VertexProperties as VProps
from vitrage.datasources.nova.host import NOVA_HOST_DATASOURCE
from vitrage.evaluator.scenario_evaluator import ScenarioEvaluator
@ -235,6 +237,7 @@ class TestScenarioEvaluator(TestFunctionalBase):
# remove WARNING nagios alarm, leaving only CRITICAL one
warning_test['status'] = 'OK'
warning_test[DSProps.EVENT_TYPE] = EventAction.DELETE_ENTITY
host_v = self.get_host_after_event(event_queue, warning_test,
processor, _TARGET_HOST)
alarms = \
@ -246,6 +249,7 @@ class TestScenarioEvaluator(TestFunctionalBase):
# next disable the alarm
critical_test['status'] = 'OK'
critical_test[DSProps.EVENT_TYPE] = EventAction.DELETE_ENTITY
host_v = self.get_host_after_event(event_queue, critical_test,
processor, _TARGET_HOST)
alarms = \

View File

@ -103,12 +103,14 @@ class NagiosTransformerTest(base.BaseTest):
self._validate_action(alarm, wrapper)
def _validate_action(self, alarm, wrapper):
if DSProps.EVENT_TYPE in alarm \
and alarm[DSProps.EVENT_TYPE] in EventAction.__dict__.values():
self.assertEqual(alarm[DSProps.EVENT_TYPE], wrapper.action)
return
action_type = alarm[DSProps.ACTION_TYPE]
if action_type in (ActionType.SNAPSHOT, ActionType.UPDATE):
if alarm[NagiosProperties.STATUS] == 'OK':
self.assertEqual(EventAction.DELETE_ENTITY, wrapper.action)
else:
self.assertEqual(EventAction.UPDATE_ENTITY, wrapper.action)
self.assertEqual(EventAction.UPDATE_ENTITY, wrapper.action)
else:
self.assertEqual(EventAction.CREATE_ENTITY, wrapper.action)

View File

@ -192,8 +192,8 @@ class NovaHostTransformerTest(base.BaseTest):
is_deleted = vertex[VertexProperties.IS_DELETED]
self.assertFalse(is_deleted)
def test_extract_action_type(self):
LOG.debug('Test extract action type')
def test_extract_event_action(self):
LOG.debug('Test extract event action')
# Test setup
spec_list = mock_sync.simple_host_generators(
@ -206,7 +206,7 @@ class NovaHostTransformerTest(base.BaseTest):
host_transformer = self.transformers[NOVA_HOST_DATASOURCE]
# Test action
action = host_transformer._extract_action_type(hosts_events[0])
action = host_transformer._extract_event_action(hosts_events[0])
# Test assertion
self.assertEqual(EventAction.UPDATE_ENTITY, action)
@ -221,7 +221,7 @@ class NovaHostTransformerTest(base.BaseTest):
host_transformer = self.transformers[NOVA_HOST_DATASOURCE]
# Test action
action = host_transformer._extract_action_type(hosts_events[0])
action = host_transformer._extract_event_action(hosts_events[0])
# Test assertions
self.assertEqual(EventAction.CREATE_ENTITY, action)

View File

@ -109,12 +109,14 @@ class ZabbixTransformerTest(base.BaseTest):
self._validate_action(alarm, wrapper)
def _validate_action(self, alarm, wrapper):
if DSProps.EVENT_TYPE in alarm \
and alarm[DSProps.EVENT_TYPE] in EventAction.__dict__.values():
self.assertEqual(alarm[DSProps.EVENT_TYPE], wrapper.action)
return
action_type = alarm[DSProps.ACTION_TYPE]
if action_type in (ActionType.SNAPSHOT, ActionType.UPDATE):
if alarm[ZabbixProps.VALUE] == ZabbixTriggerValue.OK:
self.assertEqual(EventAction.DELETE_ENTITY, wrapper.action)
else:
self.assertEqual(EventAction.UPDATE_ENTITY, wrapper.action)
self.assertEqual(EventAction.UPDATE_ENTITY, wrapper.action)
else:
self.assertEqual(EventAction.CREATE_ENTITY, wrapper.action)