From 37d435663f0f18724bfc19ba08d68167f05b14c8 Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 26 Apr 2017 05:33:45 +0000 Subject: [PATCH] create default datasource info mapper for static entities Change-Id: I29d7b6a7643b3455b8f44b785a9c102644956679 --- etc/vitrage/datasources_values/default.yaml | 24 ++++++++++ etc/vitrage/datasources_values/static.yaml | 24 ++++++++++ .../datasources_values/static_physical.yaml | 2 + .../mappings/datasource_info_mapper.py | 12 ++++- vitrage/evaluator/actions/priority_tools.py | 7 ++- .../resources/datasources_values/default.yaml | 24 ++++++++++ .../erroneous_values/default.yaml | 24 ++++++++++ .../states/test_datasource_info_mapper.py | 46 ++++++++++++++++--- 8 files changed, 154 insertions(+), 9 deletions(-) create mode 100644 etc/vitrage/datasources_values/default.yaml create mode 100644 etc/vitrage/datasources_values/static.yaml create mode 100644 vitrage/tests/resources/datasources_values/default.yaml create mode 100644 vitrage/tests/resources/datasources_values/erroneous_values/default.yaml diff --git a/etc/vitrage/datasources_values/default.yaml b/etc/vitrage/datasources_values/default.yaml new file mode 100644 index 000000000..8bf025312 --- /dev/null +++ b/etc/vitrage/datasources_values/default.yaml @@ -0,0 +1,24 @@ +category: RESOURCE +values: + - aggregated values: + priority: 40 + original values: + - name: DELETED + operational_value: DELETED + - aggregated values: + priority: 30 + original values: + - name: ERROR + operational_value: ERROR + - aggregated values: + priority: 20 + original values: + - name: SUBOPTIMAL + operational_value: SUBOPTIMAL + - aggregated values: + priority: 10 + original values: + - name: AVAILABLE + operational_value: OK + - name: ACTIVE + operational_value: OK diff --git a/etc/vitrage/datasources_values/static.yaml b/etc/vitrage/datasources_values/static.yaml new file mode 100644 index 000000000..8bf025312 --- /dev/null +++ b/etc/vitrage/datasources_values/static.yaml @@ -0,0 +1,24 @@ +category: RESOURCE +values: + - aggregated values: + priority: 40 + original values: + - name: DELETED + operational_value: DELETED + - aggregated values: + priority: 30 + original values: + - name: ERROR + operational_value: ERROR + - aggregated values: + priority: 20 + original values: + - name: SUBOPTIMAL + operational_value: SUBOPTIMAL + - aggregated values: + priority: 10 + original values: + - name: AVAILABLE + operational_value: OK + - name: ACTIVE + operational_value: OK diff --git a/etc/vitrage/datasources_values/static_physical.yaml b/etc/vitrage/datasources_values/static_physical.yaml index 129ba9800..8bf025312 100644 --- a/etc/vitrage/datasources_values/static_physical.yaml +++ b/etc/vitrage/datasources_values/static_physical.yaml @@ -20,3 +20,5 @@ values: original values: - name: AVAILABLE operational_value: OK + - name: ACTIVE + operational_value: OK diff --git a/vitrage/entity_graph/mappings/datasource_info_mapper.py b/vitrage/entity_graph/mappings/datasource_info_mapper.py index 1c718555d..1b06ccbcc 100644 --- a/vitrage/entity_graph/mappings/datasource_info_mapper.py +++ b/vitrage/entity_graph/mappings/datasource_info_mapper.py @@ -26,6 +26,9 @@ from vitrage.utils import file as file_utils LOG = log.getLogger(__name__) +DEFAULT_INFO_MAPPER = 'default' + + class DatasourceInfoMapper(object): OPERATIONAL_VALUES = 'operational_values' PRIORITY_VALUES = 'priority_values' @@ -55,7 +58,8 @@ class DatasourceInfoMapper(object): VProps.CATEGORY in new_vertex.properties else \ graph_vertex[VProps.CATEGORY] - if datasource_name in self.datasources_state_confs: + if datasource_name in self.datasources_state_confs or \ + datasource_name not in self.conf.datasources.types: value_properties = \ self.category_normalizer[category].value_properties() operational_state, aggregated_state, state_priority = \ @@ -184,7 +188,11 @@ class DatasourceInfoMapper(object): return values_conf[upper_state] if upper_state in values_conf \ else values_conf[None] else: - return self.UNDEFINED_DATASOURCE + values_conf = self.datasources_state_confs[ + DEFAULT_INFO_MAPPER][data_type] + + return values_conf[upper_state] if upper_state in values_conf \ + else values_conf[None] except Exception: LOG.error('Exception in datasource: %s', datasource_name) raise diff --git a/vitrage/evaluator/actions/priority_tools.py b/vitrage/evaluator/actions/priority_tools.py index d7d0ee2da..ad04a5f14 100644 --- a/vitrage/evaluator/actions/priority_tools.py +++ b/vitrage/evaluator/actions/priority_tools.py @@ -13,6 +13,8 @@ # under the License. from vitrage.common.constants import VertexProperties as VProps +from vitrage.entity_graph.mappings.datasource_info_mapper \ + import DEFAULT_INFO_MAPPER from vitrage.evaluator.template_fields import TemplateFields @@ -51,7 +53,10 @@ class SetStateTools(object): def get_score(self, action_info): state = action_info.specs.properties[TemplateFields.STATE].upper() target_resource = action_info.specs.targets[TemplateFields.TARGET] - return self.scores[target_resource[VProps.TYPE]].get(state, 0) + target_type = target_resource[VProps.TYPE] + score_name = \ + target_type if target_type in self.scores else DEFAULT_INFO_MAPPER + return self.scores[score_name].get(state, 0) @staticmethod def get_key(action_specs): diff --git a/vitrage/tests/resources/datasources_values/default.yaml b/vitrage/tests/resources/datasources_values/default.yaml new file mode 100644 index 000000000..8bf025312 --- /dev/null +++ b/vitrage/tests/resources/datasources_values/default.yaml @@ -0,0 +1,24 @@ +category: RESOURCE +values: + - aggregated values: + priority: 40 + original values: + - name: DELETED + operational_value: DELETED + - aggregated values: + priority: 30 + original values: + - name: ERROR + operational_value: ERROR + - aggregated values: + priority: 20 + original values: + - name: SUBOPTIMAL + operational_value: SUBOPTIMAL + - aggregated values: + priority: 10 + original values: + - name: AVAILABLE + operational_value: OK + - name: ACTIVE + operational_value: OK diff --git a/vitrage/tests/resources/datasources_values/erroneous_values/default.yaml b/vitrage/tests/resources/datasources_values/erroneous_values/default.yaml new file mode 100644 index 000000000..8bf025312 --- /dev/null +++ b/vitrage/tests/resources/datasources_values/erroneous_values/default.yaml @@ -0,0 +1,24 @@ +category: RESOURCE +values: + - aggregated values: + priority: 40 + original values: + - name: DELETED + operational_value: DELETED + - aggregated values: + priority: 30 + original values: + - name: ERROR + operational_value: ERROR + - aggregated values: + priority: 20 + original values: + - name: SUBOPTIMAL + operational_value: SUBOPTIMAL + - aggregated values: + priority: 10 + original values: + - name: AVAILABLE + operational_value: OK + - name: ACTIVE + operational_value: OK diff --git a/vitrage/tests/unit/entity_graph/states/test_datasource_info_mapper.py b/vitrage/tests/unit/entity_graph/states/test_datasource_info_mapper.py index 68a2e859a..fccdae6a1 100644 --- a/vitrage/tests/unit/entity_graph/states/test_datasource_info_mapper.py +++ b/vitrage/tests/unit/entity_graph/states/test_datasource_info_mapper.py @@ -16,6 +16,7 @@ from oslo_config import cfg from vitrage.common.constants import EntityCategory from vitrage.common.constants import VertexProperties as VProps +from vitrage.datasources.aodh import AODH_DATASOURCE from vitrage.datasources.nagios import NAGIOS_DATASOURCE from vitrage.datasources.nova.host import NOVA_HOST_DATASOURCE from vitrage.datasources.nova.instance import NOVA_INSTANCE_DATASOURCE @@ -42,7 +43,8 @@ class TestDatasourceInfoMapper(base.BaseTest): default=[NAGIOS_DATASOURCE, NOVA_HOST_DATASOURCE, NOVA_INSTANCE_DATASOURCE, - NOVA_ZONE_DATASOURCE], + NOVA_ZONE_DATASOURCE, + AODH_DATASOURCE], help='Names of supported data sources'), cfg.ListOpt('path', @@ -121,7 +123,7 @@ class TestDatasourceInfoMapper(base.BaseTest): # test assertions self.assertEqual(OperationalResourceState.NA, operational_state) - def test_operational_state_datasource_not_exists(self): + def test_operational_state_datasource_not_exists_and_state_not_exist(self): # setup state_manager = DatasourceInfoMapper(self.conf) @@ -131,7 +133,20 @@ class TestDatasourceInfoMapper(base.BaseTest): 'BUILDING') # test assertions - self.assertEqual(DatasourceInfoMapper.UNDEFINED_DATASOURCE, + self.assertEqual(OperationalResourceState.NA, + operational_state) + + def test_operational_state_datasource_not_exists_and_state_exist(self): + # setup + state_manager = DatasourceInfoMapper(self.conf) + + # action + operational_state = \ + state_manager.operational_state('NON EXISTING DATASOURCE', + 'AVAILABLE') + + # test assertions + self.assertEqual(OperationalResourceState.OK, operational_state) def test_state_priority(self): @@ -168,7 +183,7 @@ class TestDatasourceInfoMapper(base.BaseTest): 'ACTIVE') # test assertions - self.assertEqual(DatasourceInfoMapper.UNDEFINED_DATASOURCE, + self.assertEqual(10, state_priority) def test_aggregated_state(self): @@ -255,7 +270,26 @@ class TestDatasourceInfoMapper(base.BaseTest): state_manager.aggregated_state(new_vertex, None) # test assertions - self.assertEqual(DatasourceInfoMapper.UNDEFINED_DATASOURCE, + self.assertEqual('ACTIVE', new_vertex[VProps.AGGREGATED_STATE]) - self.assertEqual(DatasourceInfoMapper.UNDEFINED_DATASOURCE, + self.assertEqual(OperationalResourceState.OK, + new_vertex[VProps.OPERATIONAL_STATE]) + + def test_aggregated_state_datasource_not_exists_and_wrong_state(self): + # setup + state_manager = DatasourceInfoMapper(self.conf) + metadata = {VProps.VITRAGE_STATE: 'SUSPENDED'} + new_vertex = create_vertex('12345', + entity_state='NON EXISTING STATE', + entity_category=EntityCategory.RESOURCE, + entity_type='NON EXISTING DATASOURCE', + metadata=metadata) + + # action + state_manager.aggregated_state(new_vertex, None) + + # test assertions + self.assertEqual('NON EXISTING STATE', + new_vertex[VProps.AGGREGATED_STATE]) + self.assertEqual(OperationalResourceState.NA, new_vertex[VProps.OPERATIONAL_STATE])