nova transformer - code improvements

Change-Id: Ib25973876a4ee1802018c15f9da8d7f362952aa2
This commit is contained in:
Liat Har-Tal 2016-01-21 15:49:58 +00:00
parent 1f2ea326ac
commit 69b9b350f8
5 changed files with 111 additions and 53 deletions

View File

@ -66,7 +66,6 @@ class TransformerBase(object):
KEY_SEPARATOR = ':'
@abc.abstractmethod
def transform(self, entity_event):
"""Transform an entity event into entity wrapper.
@ -80,7 +79,34 @@ class TransformerBase(object):
:return: entity wrapper
:rtype:EntityWrapper
"""
pass
entity_vertex = self._create_entity_vertex(entity_event)
neighbors = self._create_neighbors(entity_event)
action = self._extract_action_type(entity_event)
return EntityWrapper(entity_vertex, neighbors, action)
@abc.abstractmethod
def _create_entity_vertex(self, entity_event):
"""Creates entity vertex received from given entity event.
Extracting vertex fields from a given event provided by synchronizer
:param entity_event: an event provided by synchronizer
:return: vertex - contains the entity data
:rtype:Vertex
"""
@abc.abstractmethod
def _create_neighbors(self, entity_event):
"""Extracts entity neighbors received from a given entity event.
Extracting entity neighbors from a given event provided
by synchronizer
:param entity_event: an event provided by synchronizer
:return: neigbors - a list of neighbors
:rtype:[]
"""
@abc.abstractmethod
def _key_values(self, mutable_fields=None):

View File

@ -48,7 +48,7 @@ class HostTransformer(base.TransformerBase):
def __init__(self, transformers):
self.transformers = transformers
def transform(self, entity_event):
def _create_entity_vertex(self, entity_event):
sync_mode = entity_event['sync_mode']
@ -56,9 +56,8 @@ class HostTransformer(base.TransformerBase):
entity_event,
self.HOST_NAME[sync_mode]
)
metadata = {
VertexProperties.NAME: host_name
}
metadata = {VertexProperties.NAME: host_name}
entity_key = self.extract_key(entity_event)
timestamp = extract_field_value(
@ -66,7 +65,7 @@ class HostTransformer(base.TransformerBase):
self.TIMESTAMP[sync_mode]
)
entity_vertex = graph_utils.create_vertex(
return graph_utils.create_vertex(
entity_key,
entity_id=host_name,
entity_category=EntityTypes.RESOURCE,
@ -75,41 +74,52 @@ class HostTransformer(base.TransformerBase):
metadata=metadata
)
def _create_neighbors(self, entity_event):
sync_mode = entity_event['sync_mode']
neighbors = []
timestamp = extract_field_value(
entity_event,
self.TIMESTAMP[sync_mode]
)
zone_neighbor = self._create_zone_neighbor(
entity_event,
timestamp,
self.extract_key(entity_event),
self.ZONE_NAME[sync_mode]
)
if zone_neighbor is not None:
neighbors.append(zone_neighbor)
return neighbors
def _create_zone_neighbor(
self, entity_event, timestamp, host_vertex_id, zone_name_path):
zone_transformer = self.transformers['nova.zone']
if zone_transformer:
zone_name = extract_field_value(
entity_event,
self.ZONE_NAME[sync_mode]
)
zone_neighbor = self.create_zone_neighbor(
zone_name = extract_field_value(entity_event, zone_name_path)
zone_neighbor = zone_transformer.create_placeholder_vertex(
zone_name,
timestamp,
entity_key
timestamp
)
neighbors.append(zone_neighbor)
relation_edge = graph_utils.create_edge(
source_id=zone_neighbor.vertex_id,
target_id=host_vertex_id,
relationship_type=EdgeLabels.CONTAINS
)
return base.Neighbor(zone_neighbor, relation_edge)
else:
LOG.warning('Cannot find zone transformer')
return base.EntityWrapper(
entity_vertex,
neighbors,
self._extract_action_type(entity_event))
def create_zone_neighbor(self, zone_name, timestamp, host_vertex_id):
zone_transformer = self.transformers['nova.zone']
zone_neighbor = zone_transformer.create_placeholder_vertex(
zone_name,
timestamp
)
relation_edge = graph_utils.create_edge(
source_id=zone_neighbor.vertex_id,
target_id=host_vertex_id,
relationship_type=EdgeLabels.CONTAINS
)
return base.Neighbor(zone_neighbor, relation_edge)
return None
def _key_values(self, mutable_fields):

View File

@ -79,7 +79,8 @@ class InstanceTransformer(base.TransformerBase):
def __init__(self, transformers):
self.transformers = transformers
def transform(self, entity_event):
def _create_entity_vertex(self, entity_event):
sync_mode = entity_event['sync_mode']
metadata = {
@ -106,7 +107,7 @@ class InstanceTransformer(base.TransformerBase):
self.TIMESTAMP[sync_mode]
)
entity_vertex = graph_utils.create_vertex(
return graph_utils.create_vertex(
entity_key,
entity_id=entity_id,
entity_category=EntityTypes.RESOURCE,
@ -117,12 +118,22 @@ class InstanceTransformer(base.TransformerBase):
metadata=metadata
)
def _create_neighbors(self, entity_event):
sync_mode = entity_event['sync_mode']
neighbors = []
host_transformer = self.transformers['nova.host']
if host_transformer:
host_neighbor = self.create_host_neighbor(
entity_vertex.vertex_id,
update_timestamp = extract_field_value(
entity_event,
self.TIMESTAMP[sync_mode]
)
host_neighbor = self._create_host_neighbor(
self.extract_key(entity_event),
extract_field_value(entity_event, self.HOST_NAME[sync_mode]),
update_timestamp,
host_transformer
@ -131,10 +142,7 @@ class InstanceTransformer(base.TransformerBase):
else:
LOG.warning('Cannot find host transformer')
return base.EntityWrapper(
entity_vertex,
neighbors,
self._extract_action_type(entity_event))
return neighbors
def _extract_action_type(self, entity_event):
@ -163,7 +171,7 @@ class InstanceTransformer(base.TransformerBase):
key_fields = self._key_values([instance_id])
return base.build_key(key_fields)
def create_host_neighbor(
def _create_host_neighbor(
self,
vertex_id,
host_name,

View File

@ -68,7 +68,8 @@ class ZoneTransformer(base.TransformerBase):
def __init__(self, transformers):
self.transformers = transformers
def transform(self, entity_event):
def _create_entity_vertex(self, entity_event):
sync_mode = entity_event['sync_mode']
zone_name = extract_field_value(
@ -93,7 +94,7 @@ class ZoneTransformer(base.TransformerBase):
self.TIMESTAMP[sync_mode]
)
entity_vertex = graph_utils.create_vertex(
return graph_utils.create_vertex(
entity_key,
entity_id=zone_name,
entity_category=EntityTypes.RESOURCE,
@ -103,11 +104,24 @@ class ZoneTransformer(base.TransformerBase):
metadata=metadata
)
neighbors = [self._create_node_neighbor(entity_vertex)]
def _create_neighbors(self, entity_event):
sync_mode = entity_event['sync_mode']
zone_vertex_id = self.extract_key(entity_event)
neighbors = [self._create_node_neighbor(zone_vertex_id)]
hosts = extract_field_value(entity_event, self.HOSTS[sync_mode])
host_transformer = self.transformers['nova.host']
if host_transformer:
timestamp = extract_field_value(
entity_event,
self.TIMESTAMP[sync_mode]
)
for key in hosts:
host_available = extract_field_value(
@ -125,25 +139,24 @@ class ZoneTransformer(base.TransformerBase):
host_state = self.STATE_UNAVAILABLE
host_neighbor = self._create_host_neighbor(
entity_vertex.vertex_id,
zone_vertex_id,
key,
host_state,
timestamp
)
neighbors.append(host_neighbor)
else:
LOG.warning('Cannot find host transformer')
return base.EntityWrapper(
entity_vertex,
neighbors,
self._extract_action_type(entity_event))
return neighbors
def _create_node_neighbor(self, zone_vertex):
def _create_node_neighbor(self, zone_vertex_id):
node_vertex = base.create_node_placeholder_vertex()
relation_edge = graph_utils.create_edge(
source_id=node_vertex.vertex_id,
target_id=zone_vertex.vertex_id,
target_id=zone_vertex_id,
relationship_type=EdgeLabels.CONTAINS
)
return base.Neighbor(node_vertex, relation_edge)

View File

@ -292,7 +292,8 @@ class NovaInstanceTransformerTest(base.BaseTest):
time = datetime.datetime.utcnow()
# Test action
neighbor = InstanceTransformer(self.transformers).create_host_neighbor(
instance_transformer = InstanceTransformer(self.transformers)
neighbor = instance_transformer._create_host_neighbor(
vertex_id,
host_name,
time,