Merge "Skeleton for static datasource"

This commit is contained in:
Jenkins 2016-11-27 09:58:42 +00:00 committed by Gerrit Code Review
commit de1a397423
6 changed files with 329 additions and 0 deletions

View File

@ -0,0 +1,47 @@
# Copyright 2016 - Nokia, ZTE
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
from vitrage.common.constants import UpdateMethod
STATIC_DATASOURCE = 'static'
OPTS = [
cfg.StrOpt('transformer',
default='vitrage.datasources.static.transformer.'
'StaticTransformer',
help='Static transformer class path',
required=True),
cfg.StrOpt('driver',
default='vitrage.datasources.static.driver.'
'StaticDriver',
help='Static driver class path',
required=True),
cfg.StrOpt('update_method',
default=UpdateMethod.PULL,
help='None: updates only via Vitrage periodic snapshots.'
'Pull: updates periodically.'
'Push: updates by getting notifications from the'
' datasource itself.',
required=True),
cfg.IntOpt('changes_interval',
default=30,
help='interval in seconds between checking changes in the'
'static configuration files'),
# NOTE: This folder is already used by static_physical datasource. Legacy
# configuration files will NOT be converted automatically. But user will
# receive deprecation warnings.
cfg.StrOpt('directory', default='/etc/vitrage/static_datasources',
help='static data sources configuration directory')
]

View File

@ -0,0 +1,51 @@
# Copyright 2016 - Nokia, ZTE
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from vitrage.datasources.driver_base import DriverBase
from vitrage.datasources.static import STATIC_DATASOURCE
class StaticDriver(DriverBase):
def __init__(self, conf):
super(StaticDriver, self).__init__()
self.cfg = conf
self.cache = {}
@staticmethod
def get_event_types():
return []
@staticmethod
def enrich_event(event, event_type):
pass
def get_all(self, sync_mode):
"""Query all entities and send events to the vitrage events queue"""
return self.make_pickleable(self._get_all_entities(),
STATIC_DATASOURCE,
sync_mode)
def get_changes(self, sync_mode):
return self.make_pickleable(self._get_changes_entities(),
STATIC_DATASOURCE,
sync_mode)
def _get_all_entities(self):
"""Internal method to get all entities"""
return []
def _get_changes_entities(self):
"""Internal method to get changed entities"""
return []

View File

@ -0,0 +1,76 @@
# Copyright 2016 - Nokia, ZTE
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# TODO(yujunz) - skeleton only, methods to be implemented
from oslo_log import log as logging
from vitrage.common.constants import DatasourceProperties as DSProps
from vitrage.common.constants import EntityCategory
from vitrage.common.constants import VertexProperties as VProps
from vitrage.datasources.resource_transformer_base import \
ResourceTransformerBase
from vitrage.datasources.static import STATIC_DATASOURCE
from vitrage.datasources import transformer_base
import vitrage.graph.utils as graph_utils
LOG = logging.getLogger(__name__)
class StaticTransformer(ResourceTransformerBase):
def __init__(self, transformers, conf):
super(StaticTransformer, self).__init__(transformers, conf)
def _create_snapshot_entity_vertex(self, entity_event):
return self._create_vertex(entity_event)
def _create_update_entity_vertex(self, entity_event):
return self._create_vertex(entity_event)
def _create_snapshot_neighbors(self, entity_event):
return self._create_static_neighbors(entity_event)
def _create_update_neighbors(self, entity_event):
return self._create_static_neighbors(entity_event)
def _create_entity_key(self, entity_event):
entity_id = entity_event[VProps.ID]
sync_type = entity_event[VProps.TYPE]
key_fields = self._key_values(sync_type, entity_id)
return transformer_base.build_key(key_fields)
def get_type(self):
return STATIC_DATASOURCE
def _create_vertex(self, entity_event):
sync_type = entity_event[VProps.TYPE]
entity_id = entity_event[VProps.ID]
sample_timestamp = entity_event[DSProps.SAMPLE_DATE]
update_timestamp = self._format_update_timestamp(
update_timestamp=None,
sample_timestamp=sample_timestamp)
state = entity_event[VProps.STATE]
entity_key = self._create_entity_key(entity_event)
return graph_utils.create_vertex(
entity_key,
entity_id=entity_id,
entity_category=EntityCategory.RESOURCE,
entity_type=sync_type,
sample_timestamp=sample_timestamp,
update_timestamp=update_timestamp,
entity_state=state)
def _create_static_neighbors(self, entity_event):
return []

View File

@ -0,0 +1,15 @@
# Copyright 2016 - Nokia
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
__author__ = 'stack'

View File

@ -0,0 +1,94 @@
# Copyright 2016 - Nokia, ZTE
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
from oslo_log import log as logging
from vitrage.common.constants import EventAction
from vitrage.common.constants import SyncMode
from vitrage.datasources.static import driver
from vitrage.datasources.static import STATIC_DATASOURCE
from vitrage.tests import base
from vitrage.tests.mocks import utils
LOG = logging.getLogger(__name__)
class TestStaticDriver(base.BaseTest):
OPTS = [
cfg.StrOpt('transformer',
default='vitrage.datasources.static.transformer.'
'StaticTransformer'),
cfg.StrOpt('driver',
default='vitrage.datasources.static.driver.'
'StaticDriver'),
cfg.IntOpt('changes_interval',
default=30,
min=30,
help='interval between checking changes in the '
'configuration files of the static datasources'),
cfg.StrOpt('directory',
default=utils.get_resources_dir() + '/static_datasources')
]
CHANGES_OPTS = [
cfg.StrOpt('transformer',
default='vitrage.datasources.static.transformer.'
'StaticTransformer'),
cfg.StrOpt('driver',
default='vitrage.datasources.static.driver.'
'StaticDriver'),
cfg.IntOpt('changes_interval',
default=30,
min=30,
help='interval between checking changes in the static '
'datasources'),
cfg.StrOpt('directory',
default=utils.get_resources_dir() +
'/static_datasources/changes_datasources'),
]
# noinspection PyAttributeOutsideInit,PyPep8Naming
@classmethod
def setUpClass(cls):
cls.conf = cfg.ConfigOpts()
cls.conf.register_opts(cls.OPTS, group=STATIC_DATASOURCE)
cls.static_driver = driver.StaticDriver(cls.conf)
def test_get_all(self):
# Action
static_entities = self.static_driver.get_all(SyncMode.UPDATE)
# Test assertions
self.assertEqual(0, len(static_entities))
# noinspection PyAttributeOutsideInit
def test_get_changes(self):
# Setup
entities = self.static_driver.get_all(SyncMode.UPDATE)
self.assertEqual(0, len(entities))
self.conf = cfg.ConfigOpts()
self.conf.register_opts(self.CHANGES_OPTS,
group=STATIC_DATASOURCE)
self.static_driver.cfg = self.conf
# Action
changes = self.static_driver.get_changes(
EventAction.UPDATE_ENTITY)
# Test Assertions
self.assertEqual(0, len(changes))

View File

@ -0,0 +1,46 @@
# Copyright 2016 - Nokia
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
from oslo_log import log as logging
from vitrage.common.constants import UpdateMethod
from vitrage.datasources.static import STATIC_DATASOURCE
from vitrage.datasources.static.transformer import StaticTransformer
from vitrage.tests import base
LOG = logging.getLogger(__name__)
class TestStaticTransformer(base.BaseTest):
OPTS = [
cfg.StrOpt('update_method',
default=UpdateMethod.PULL),
]
# noinspection PyAttributeOutsideInit,PyPep8Naming
@classmethod
def setUpClass(cls):
cls.transformers = {}
cls.conf = cfg.ConfigOpts()
cls.conf.register_opts(cls.OPTS, group=STATIC_DATASOURCE)
cls.transformers[STATIC_DATASOURCE] = \
StaticTransformer(cls.transformers, cls.conf)
def test_snapshot_transform(self):
pass
def test_update_transform(self):
pass