Merge "refactor the '_extract_action_type' function"

This commit is contained in:
Jenkins 2016-11-29 08:15:25 +00:00 committed by Gerrit Code Review
commit fbfa617246
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)) old_alarm, timestamp = self.cache.get(alarm_key, (None, None))
if filter_(self, alarm, old_alarm): 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) alarms_to_update.append(alarm)
self.cache[alarm_key] = alarm, now self.cache[alarm_key] = alarm, now

View File

@ -14,11 +14,9 @@
from oslo_log import log as logging 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 DatasourceProperties as DSProps
from vitrage.common.constants import EntityCategory from vitrage.common.constants import EntityCategory
from vitrage.common.constants import EventAction from vitrage.common.constants import EventAction
from vitrage.common.exception import VitrageTransformerError
from vitrage.datasources.alarm_properties import AlarmProperties as AlarmProps from vitrage.datasources.alarm_properties import AlarmProperties as AlarmProps
from vitrage.datasources import transformer_base as tbase from vitrage.datasources import transformer_base as tbase
@ -37,24 +35,6 @@ class AlarmTransformerBase(tbase.TransformerBase):
LOG.info('An alarm cannot be a placeholder') LOG.info('An alarm cannot be a placeholder')
pass 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): def _key_values(self, *args):
return (EntityCategory.ALARM,) + args return (EntityCategory.ALARM,) + args

View File

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

View File

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

View File

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

View File

@ -103,12 +103,14 @@ class NagiosTransformerTest(base.BaseTest):
self._validate_action(alarm, wrapper) self._validate_action(alarm, wrapper)
def _validate_action(self, 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] action_type = alarm[DSProps.ACTION_TYPE]
if action_type in (ActionType.SNAPSHOT, ActionType.UPDATE): if action_type in (ActionType.SNAPSHOT, ActionType.UPDATE):
if alarm[NagiosProperties.STATUS] == 'OK': self.assertEqual(EventAction.UPDATE_ENTITY, wrapper.action)
self.assertEqual(EventAction.DELETE_ENTITY, wrapper.action)
else:
self.assertEqual(EventAction.UPDATE_ENTITY, wrapper.action)
else: else:
self.assertEqual(EventAction.CREATE_ENTITY, wrapper.action) self.assertEqual(EventAction.CREATE_ENTITY, wrapper.action)

View File

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

View File

@ -109,12 +109,14 @@ class ZabbixTransformerTest(base.BaseTest):
self._validate_action(alarm, wrapper) self._validate_action(alarm, wrapper)
def _validate_action(self, 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] action_type = alarm[DSProps.ACTION_TYPE]
if action_type in (ActionType.SNAPSHOT, ActionType.UPDATE): if action_type in (ActionType.SNAPSHOT, ActionType.UPDATE):
if alarm[ZabbixProps.VALUE] == ZabbixTriggerValue.OK: self.assertEqual(EventAction.UPDATE_ENTITY, wrapper.action)
self.assertEqual(EventAction.DELETE_ENTITY, wrapper.action)
else:
self.assertEqual(EventAction.UPDATE_ENTITY, wrapper.action)
else: else:
self.assertEqual(EventAction.CREATE_ENTITY, wrapper.action) self.assertEqual(EventAction.CREATE_ENTITY, wrapper.action)