<s> import utils <EOL> import manager_rest . models <EOL> import manager_rest . manager_exceptions <EOL> from manager_rest import storage_manager <EOL> from manager_rest . blueprints_manager import get_blueprints_manager <EOL> from dsl_parser . interfaces . utils import no_op_operation <EOL> from entity_context import get_entity_context <EOL> from constants import ( OPERATION_TYPE , <EOL> ENTITY_TYPES , <EOL> CHANGE_TYPE ) <EOL> class StorageClient ( object ) : <EOL> def __init__ ( self ) : <EOL> self . sm = storage_manager . get_storage_manager ( ) <EOL> class UpdateHandler ( StorageClient ) : <EOL> def handle ( self , * _ , ** __ ) : <EOL> raise NotImplementedError <EOL> def finalize ( self , * _ , ** __ ) : <EOL> raise NotImplementedError <EOL> class DeploymentUpdateNodeHandler ( UpdateHandler ) : <EOL> def __init__ ( self ) : <EOL> super ( DeploymentUpdateNodeHandler , self ) . __init__ ( ) <EOL> self . modified_entities = utils . ModifiedEntitiesDict ( ) <EOL> def handle ( self , dep_update ) : <EOL> """<STR_LIT>""" <EOL> current_nodes = self . sm . get_nodes ( <EOL> filters = { '<STR_LIT>' : dep_update . deployment_id } ) . items <EOL> nodes_dict = { node . id : node . to_dict ( ) for node in current_nodes } <EOL> entities_update_mapper = { <EOL> OPERATION_TYPE . ADD : self . _add_entity , <EOL> OPERATION_TYPE . REMOVE : self . _remove_entity , <EOL> OPERATION_TYPE . MODIFY : self . _modify_entity <EOL> } <EOL> for step in dep_update . steps : <EOL> entity_updater = entities_update_mapper [ step . operation ] <EOL> entity_context = get_entity_context ( dep_update . blueprint , <EOL> dep_update . deployment_id , <EOL> step . entity_type , <EOL> step . entity_id ) <EOL> entity_id = entity_updater ( entity_context , nodes_dict ) <EOL> self . modified_entities [ step . entity_type ] . append ( entity_id ) <EOL> return self . modified_entities , nodes_dict . values ( ) <EOL> def _add_entity ( self , ctx , current_nodes ) : <EOL> """<STR_LIT>""" <EOL> add_entity_mapper = { <EOL> ENTITY_TYPES . NODE : self . _add_node , <EOL> ENTITY_TYPES . RELATIONSHIP : self . _add_relationship , <EOL> ENTITY_TYPES . PROPERTY : self . _add_property , <EOL> ENTITY_TYPES . OPERATION : self . _add_operation <EOL> } <EOL> add_entity_handler = add_entity_mapper [ ctx . entity_type ] <EOL> entity_id = add_entity_handler ( ctx , current_nodes ) <EOL> return entity_id <EOL> def _add_node ( self , ctx , current_nodes ) : <EOL> """<STR_LIT>""" <EOL> get_blueprints_manager ( ) . _create_deployment_nodes ( <EOL> deployment_id = ctx . deployment_id , <EOL> blueprint_id = '<STR_LIT>' , <EOL> plan = ctx . blueprint , <EOL> node_ids = ctx . raw_node_id <EOL> ) <EOL> current_nodes [ ctx . raw_node_id ] = ctx . storage_node . to_dict ( ) <EOL> target_ids = [ r [ '<STR_LIT>' ] <EOL> for r in ctx . raw_node . get ( '<STR_LIT>' , [ ] ) ] <EOL> for node_id in target_ids : <EOL> self . sm . update_node ( <EOL> deployment_id = ctx . deployment_id , <EOL> node_id = node_id , <EOL> changes = { <EOL> '<STR_LIT>' : <EOL> utils . get_raw_node ( ctx . blueprint , <EOL> node_id ) [ '<STR_LIT>' ] <EOL> } ) <EOL> current_nodes [ node_id ] = self . sm . get_node ( ctx . deployment_id , node_id ) . to_dict ( ) <EOL> return ctx . raw_node_id <EOL> def _add_relationship ( self , ctx , current_nodes ) : <EOL> """<STR_LIT>""" <EOL> source_changes = { <EOL> '<STR_LIT>' : ctx . raw_node [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : ctx . raw_node [ '<STR_LIT>' ] <EOL> } <EOL> self . sm . update_node ( deployment_id = ctx . deployment_id , <EOL> node_id = ctx . raw_node_id , <EOL> changes = source_changes ) <EOL> source_node = ctx . storage_node <EOL> current_nodes [ source_node . id ] = source_node . to_dict ( ) <EOL> target_changes = { '<STR_LIT>' : ctx . raw_target_node [ '<STR_LIT>' ] } <EOL> self . sm . update_node ( deployment_id = ctx . deployment_id , <EOL> node_id = ctx . raw_target_node [ '<STR_LIT:id>' ] , <EOL> changes = target_changes ) <EOL> current_nodes [ ctx . storage_target_node . id ] = ctx . storage_target_node . to_dict ( ) <EOL> return ctx . raw_node_id , ctx . raw_target_node [ '<STR_LIT:id>' ] <EOL> def _add_property ( self , ctx , current_nodes ) : <EOL> changes = { <EOL> ctx . PROPERTIES : { <EOL> ctx . property_id : <EOL> utils . create_dict ( ctx . modification_breadcrumbs , <EOL> ctx . raw_entity_value ) <EOL> } <EOL> } <EOL> self . sm . update_node ( deployment_id = ctx . deployment_id , <EOL> node_id = ctx . raw_node_id , <EOL> changes = changes ) <EOL> properties = current_nodes [ ctx . raw_node_id ] [ ctx . PROPERTIES ] <EOL> current_node = current_nodes [ ctx . raw_node_id ] <EOL> if ctx . modification_breadcrumbs : <EOL> property_to_update = utils . traverse_object ( properties [ ctx . property_id ] , <EOL> ctx . modification_breadcrumbs [ : - <NUM_LIT:1> ] ) <EOL> property_to_update [ ctx . modification_breadcrumbs [ - <NUM_LIT:1> ] ] = ctx . raw_entity_value <EOL> else : <EOL> property_to_update = current_node [ ctx . PROPERTIES ] <EOL> property_to_update [ ctx . property_id ] = ctx . raw_entity_value <EOL> return ctx . entity_id <EOL> @ staticmethod <EOL> def _choose_and_execute_operation_handler ( ctx , <EOL> current_nodes , <EOL> relationship_executor , <EOL> node_executor ) : <EOL> if ctx . entity_id . split ( '<STR_LIT::>' ) [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> modifier = relationship_executor <EOL> else : <EOL> modifier = node_executor <EOL> return modifier ( ctx , current_nodes ) <EOL> def _add_operation ( self , ctx , current_nodes ) : <EOL> return self . _choose_and_execute_operation_handler ( <EOL> ctx , <EOL> current_nodes , <EOL> self . _add_relationship_operation , <EOL> self . _add_node_operation ) <EOL> def _add_node_operation ( self , ctx , current_nodes ) : <EOL> new_operations = utils . create_dict ( ctx . modification_breadcrumbs , <EOL> ctx . raw_entity_value ) <EOL> changes = { <EOL> ctx . OPERATIONS : { ctx . operation_id : new_operations } , <EOL> ctx . PLUGINS : ctx . raw_node [ ctx . PLUGINS ] <EOL> } <EOL> self . sm . update_node ( deployment_id = ctx . deployment_id , <EOL> node_id = ctx . raw_node_id , <EOL> changes = changes ) <EOL> current_node = current_nodes [ ctx . raw_node_id ] <EOL> if ctx . modification_breadcrumbs : <EOL> operation_to_update = utils . traverse_object ( <EOL> current_node [ ctx . OPERATIONS ] [ ctx . operation_id ] , <EOL> ctx . modification_breadcrumbs [ : - <NUM_LIT:1> ] <EOL> ) <EOL> operation_to_update [ ctx . modification_breadcrumbs [ - <NUM_LIT:1> ] ] = ctx . raw_entity_value <EOL> else : <EOL> operation_to_update = current_node [ ctx . OPERATIONS ] <EOL> operation_to_update [ ctx . operation_id ] = ctx . raw_entity_value <EOL> current_node [ ctx . PLUGINS ] = ctx . raw_node [ ctx . PLUGINS ] <EOL> return ctx . entity_id <EOL> def _add_relationship_operation ( self , ctx , current_nodes ) : <EOL> operation_value = ctx . raw_entity_value <EOL> relationships = current_nodes [ ctx . raw_node_id ] [ ctx . RELATIONSHIPS ] <EOL> operations = relationships [ ctx . relationship_index ] [ ctx . operations_key ] <EOL> operations [ ctx . operation_id ] = operation_value <EOL> changes = { ctx . RELATIONSHIPS : relationships , <EOL> ctx . PLUGINS : ctx . raw_node [ ctx . PLUGINS ] } <EOL> self . sm . update_node ( deployment_id = ctx . deployment_id , <EOL> node_id = ctx . raw_node_id , <EOL> changes = changes ) <EOL> return ctx . entity_id <EOL> def _remove_entity ( self , ctx , current_nodes ) : <EOL> """<STR_LIT>""" <EOL> remove_entity_mapper = { <EOL> ENTITY_TYPES . NODE : self . _remove_node , <EOL> ENTITY_TYPES . RELATIONSHIP : self . _remove_relationship , <EOL> ENTITY_TYPES . OPERATION : self . _remove_operation , <EOL> ENTITY_TYPES . PROPERTY : self . _remove_property <EOL> } <EOL> remove_entity_handler = remove_entity_mapper [ ctx . entity_type ] <EOL> entity_id = remove_entity_handler ( ctx , current_nodes ) <EOL> return entity_id <EOL> @ staticmethod <EOL> def _remove_node ( ctx , current_nodes ) : <EOL> """<STR_LIT>""" <EOL> del ( current_nodes [ ctx . storage_node . id ] ) <EOL> return ctx . storage_node . id <EOL> @ staticmethod <EOL> def _remove_relationship ( ctx , current_nodes ) : <EOL> """<STR_LIT>""" <EOL> current_node = current_nodes [ ctx . raw_node_id ] <EOL> current_node [ ctx . RELATIONSHIPS ] . remove ( ctx . storage_entity_value ) <EOL> return ctx . raw_node_id , ctx . raw_target_node [ '<STR_LIT:id>' ] <EOL> def _remove_operation ( self , ctx , current_nodes ) : <EOL> return self . _choose_and_execute_operation_handler ( <EOL> ctx , <EOL> current_nodes , <EOL> self . _remove_relationship_operation , <EOL> self . _remove_node_operation ) <EOL> @ staticmethod <EOL> def _remove_node_operation ( ctx , current_nodes ) : <EOL> current_node = current_nodes [ ctx . raw_node_id ] <EOL> current_node [ ctx . OPERATIONS ] [ ctx . operation_id ] = no_op_operation ( ctx . operation_id ) <EOL> return ctx . entity_id <EOL> @ staticmethod <EOL> def _remove_relationship_operation ( ctx , current_nodes ) : <EOL> current_node = current_nodes [ ctx . raw_node_id ] <EOL> modified_relationship = current_node [ ctx . RELATIONSHIPS ] [ ctx . relationship_index ] <EOL> modified_relationship [ ctx . operations_key ] [ ctx . operation_id ] = no_op_operation ( ctx . operation_id ) <EOL> return ctx . entity_id <EOL> @ staticmethod <EOL> def _remove_property ( ctx , current_nodes ) : <EOL> node_id = ctx . raw_node_id <EOL> del ( current_nodes [ node_id ] [ ctx . PROPERTIES ] [ ctx . property_id ] ) <EOL> return ctx . entity_id <EOL> def _modify_entity ( self , ctx , current_nodes ) : <EOL> """<STR_LIT>""" <EOL> modify_entity_mapper = { <EOL> ENTITY_TYPES . OPERATION : self . _modify_operation , <EOL> ENTITY_TYPES . PROPERTY : self . _modify_property <EOL> } <EOL> add_entity_handler = modify_entity_mapper [ ctx . entity_type ] <EOL> entity_id = add_entity_handler ( ctx , current_nodes ) <EOL> return entity_id <EOL> def _modify_operation ( self , ctx , current_nodes ) : <EOL> return self . _choose_and_execute_operation_handler ( <EOL> ctx , <EOL> current_nodes , <EOL> self . _modify_relationship_operation , <EOL> self . _modify_node_operation ) <EOL> def _modify_node_operation ( self , ctx , current_nodes ) : <EOL> return self . _add_node_operation ( ctx , current_nodes ) <EOL> def _modify_relationship_operation ( self , ctx , current_nodes ) : <EOL> current_relationships = current_nodes [ ctx . raw_node_id ] [ ctx . RELATIONSHIPS ] <EOL> current_relationship = current_relationships [ ctx . relationship_index ] <EOL> operation_to_update = utils . traverse_object ( <EOL> current_relationship [ ctx . operations_key ] [ ctx . operation_id ] , <EOL> ctx . modification_breadcrumbs [ : - <NUM_LIT:1> ] <EOL> ) <EOL> operation_to_update [ ctx . modification_breadcrumbs [ - <NUM_LIT:1> ] ] = ctx . raw_entity_value <EOL> changes = { ctx . RELATIONSHIPS : current_relationships } <EOL> self . sm . update_node ( deployment_id = ctx . deployment_id , <EOL> node_id = ctx . raw_node_id , <EOL> changes = changes ) <EOL> return ctx . entity_id <EOL> def _modify_property ( self , ctx , current_nodes ) : <EOL> return self . _add_property ( ctx , current_nodes ) <EOL> def finalize ( self , dep_update ) : <EOL> """<STR_LIT>""" <EOL> deleted_node_instances = dep_update . deployment_update_node_instances [ <EOL> CHANGE_TYPE . REMOVED_AND_RELATED ] . get ( CHANGE_TYPE . AFFECTED , [ ] ) <EOL> deleted_node_ids = utils . extract_ids ( deleted_node_instances , '<STR_LIT>' ) <EOL> modified_nodes = [ n for n in dep_update . deployment_update_nodes <EOL> if n [ '<STR_LIT:id>' ] not in deleted_node_ids ] <EOL> for raw_node in modified_nodes : <EOL> self . sm . delete_node ( dep_update . deployment_id , raw_node [ '<STR_LIT:id>' ] ) <EOL> node = manager_rest . models . DeploymentNode ( ** raw_node ) <EOL> self . sm . put_node ( node ) <EOL> for deleted_node_instance in deleted_node_instances : <EOL> self . sm . delete_node ( dep_update . deployment_id , <EOL> deleted_node_instance [ '<STR_LIT>' ] ) <EOL> class DeploymentUpdateNodeInstanceHandler ( UpdateHandler ) : <EOL> def __init__ ( self ) : <EOL> super ( DeploymentUpdateNodeInstanceHandler , self ) . __init__ ( ) <EOL> def handle ( self , dep_update , updated_instances ) : <EOL> """<STR_LIT>""" <EOL> handlers_mapper = { <EOL> CHANGE_TYPE . ADDED_AND_RELATED : <EOL> self . _handle_adding_node_instance , <EOL> CHANGE_TYPE . EXTENDED_AND_RELATED : <EOL> self . _handle_adding_relationship_instance , <EOL> CHANGE_TYPE . REDUCED_AND_RELATED : <EOL> self . _handle_removing_relationship_instance , <EOL> CHANGE_TYPE . REMOVED_AND_RELATED : <EOL> self . _handle_removing_node_instance <EOL> } <EOL> instances = { k : { } for k , _ in handlers_mapper . iteritems ( ) } <EOL> for change_type , handler in handlers_mapper . iteritems ( ) : <EOL> if updated_instances [ change_type ] : <EOL> instances [ change_type ] = handler ( updated_instances [ change_type ] , dep_update ) <EOL> return instances <EOL> def _handle_adding_node_instance ( self , raw_instances , dep_update ) : <EOL> """<STR_LIT>""" <EOL> added_instances = [ ] <EOL> add_related_instances = [ ] <EOL> for raw_node_instance in raw_instances : <EOL> if raw_node_instance . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> changes = { <EOL> '<STR_LIT>' : dep_update . deployment_id , <EOL> '<STR_LIT:version>' : None , <EOL> '<STR_LIT:state>' : None , <EOL> '<STR_LIT>' : { } <EOL> } <EOL> raw_node_instance . update ( changes ) <EOL> added_instances . append ( raw_node_instance ) <EOL> else : <EOL> add_related_instances . append ( raw_node_instance ) <EOL> self . _update_node_instance ( raw_node_instance ) <EOL> get_blueprints_manager ( ) . _create_deployment_node_instances ( <EOL> dep_update . deployment_id , <EOL> added_instances <EOL> ) <EOL> return { <EOL> CHANGE_TYPE . AFFECTED : added_instances , <EOL> CHANGE_TYPE . RELATED : add_related_instances <EOL> } <EOL> @ staticmethod <EOL> def _handle_removing_node_instance ( instances , * _ ) : <EOL> """<STR_LIT>""" <EOL> removed_raw_instances = [ ] <EOL> remove_related_raw_instances = [ ] <EOL> for raw_node_instance in instances : <EOL> node_instance = manager_rest . models . DeploymentNodeInstance ( ** raw_node_instance ) <EOL> if raw_node_instance . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> removed_raw_instances . append ( node_instance ) <EOL> else : <EOL> remove_related_raw_instances . append ( node_instance ) <EOL> return { <EOL> CHANGE_TYPE . AFFECTED : removed_raw_instances , <EOL> CHANGE_TYPE . RELATED : remove_related_raw_instances <EOL> } <EOL> def _handle_adding_relationship_instance ( self , instances , * _ ) : <EOL> """<STR_LIT>""" <EOL> modified_raw_instances = [ ] <EOL> modify_related_raw_instances = [ ] <EOL> for raw_node_instance in instances : <EOL> if raw_node_instance . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> modified_raw_instances . append ( raw_node_instance ) <EOL> node_instance = manager_rest . models . DeploymentNodeInstance ( <EOL> ** raw_node_instance ) <EOL> self . _update_node_instance ( node_instance . to_dict ( ) ) <EOL> else : <EOL> modify_related_raw_instances . append ( raw_node_instance ) <EOL> return { <EOL> CHANGE_TYPE . AFFECTED : modified_raw_instances , <EOL> CHANGE_TYPE . RELATED : modify_related_raw_instances <EOL> } <EOL> def _handle_removing_relationship_instance ( self , instances , * _ ) : <EOL> """<STR_LIT>""" <EOL> modified_raw_instances = [ ] <EOL> modify_related_raw_instances = [ ] <EOL> for raw_node_instance in instances : <EOL> if raw_node_instance . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> modified_node = self . sm . get_node_instance ( raw_node_instance [ '<STR_LIT:id>' ] ) . to_dict ( ) <EOL> target_ids = [ rel [ '<STR_LIT>' ] <EOL> for rel in raw_node_instance [ '<STR_LIT>' ] ] <EOL> relationships = [ rel for rel in modified_node [ '<STR_LIT>' ] <EOL> if rel [ '<STR_LIT>' ] not in target_ids ] <EOL> modified_node [ '<STR_LIT>' ] = relationships <EOL> modified_raw_instances . append ( modified_node ) <EOL> else : <EOL> modify_related_raw_instances . append ( raw_node_instance ) <EOL> return { <EOL> CHANGE_TYPE . AFFECTED : modified_raw_instances , <EOL> CHANGE_TYPE . RELATED : modify_related_raw_instances <EOL> } <EOL> def finalize ( self , dep_update ) : <EOL> """<STR_LIT>""" <EOL> reduced_node_instances = dep_update . deployment_update_node_instances [ <EOL> CHANGE_TYPE . REDUCED_AND_RELATED ] . get ( <EOL> CHANGE_TYPE . AFFECTED , [ ] ) <EOL> removed_node_instances = dep_update . deployment_update_node_instances [ <EOL> CHANGE_TYPE . REMOVED_AND_RELATED ] . get ( <EOL> CHANGE_TYPE . AFFECTED , [ ] ) <EOL> for reduced_node_instance in reduced_node_instances : <EOL> self . _update_node_instance ( reduced_node_instance , <EOL> overwrite_relationships = True ) <EOL> for removed_node_instance in removed_node_instances : <EOL> self . sm . delete_node_instance ( removed_node_instance [ '<STR_LIT:id>' ] ) <EOL> def _update_node_instance ( self , raw_node_instance , <EOL> overwrite_relationships = False ) : <EOL> current = self . sm . get_node_instance ( raw_node_instance [ '<STR_LIT:id>' ] ) <EOL> raw_node_instance [ '<STR_LIT:version>' ] = current . version <EOL> if not overwrite_relationships : <EOL> raw_relationship_target_id = [ r [ '<STR_LIT>' ] <EOL> for r in raw_node_instance . get ( '<STR_LIT>' , { } ) ] <EOL> if raw_relationship_target_id : <EOL> new_relationships = [ r for r in current . relationships <EOL> if r [ '<STR_LIT>' ] not in raw_relationship_target_id ] <EOL> raw_node_instance [ '<STR_LIT>' ] . extend ( new_relationships ) <EOL> self . sm . update_node_instance ( <EOL> manager_rest . models . DeploymentNodeInstance ( ** raw_node_instance ) <EOL> ) </s>
