<s> from neutron . db import model_base <EOL> from oslo_log import log as logging <EOL> import sqlalchemy as sa <EOL> from gbpservice . neutron . db . grouppolicy import group_policy_db as gp_db <EOL> LOG = logging . getLogger ( __name__ ) <EOL> PROVIDER = '<STR_LIT>' <EOL> CONSUMER = '<STR_LIT>' <EOL> MANAGEMENT = '<STR_LIT>' <EOL> RELATIONSHIPS = [ PROVIDER , CONSUMER , MANAGEMENT ] <EOL> class NodeToDriverMapping ( model_base . BASEV2 ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = '<STR_LIT>' <EOL> servicechain_node_id = sa . Column ( sa . String ( <NUM_LIT> ) , <EOL> sa . ForeignKey ( '<STR_LIT>' , <EOL> ondelete = '<STR_LIT>' ) , <EOL> nullable = False , primary_key = True ) <EOL> driver_name = sa . Column ( sa . String ( <NUM_LIT> ) , nullable = False ) <EOL> servicechain_instance_id = sa . Column ( sa . String ( <NUM_LIT> ) , <EOL> sa . ForeignKey ( '<STR_LIT>' , <EOL> ondelete = '<STR_LIT>' ) , <EOL> primary_key = True ) <EOL> class ServiceTarget ( model_base . BASEV2 ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = '<STR_LIT>' <EOL> policy_target_id = sa . Column ( sa . String ( <NUM_LIT> ) , <EOL> sa . ForeignKey ( gp_db . PolicyTarget . id , <EOL> ondelete = '<STR_LIT>' ) , <EOL> nullable = False , primary_key = True ) <EOL> servicechain_instance_id = sa . Column ( sa . String ( <NUM_LIT> ) , <EOL> nullable = False , primary_key = True ) <EOL> servicechain_node_id = sa . Column ( sa . String ( <NUM_LIT> ) , <EOL> nullable = False , primary_key = True ) <EOL> relationship = sa . Column ( sa . String ( <NUM_LIT> ) , nullable = False ) <EOL> position = sa . Column ( sa . Integer ) <EOL> def set_node_owner ( context , driver_name ) : <EOL> session = context . session <EOL> with session . begin ( subtransactions = True ) : <EOL> owner = NodeToDriverMapping ( <EOL> servicechain_instance_id = context . instance [ '<STR_LIT:id>' ] , <EOL> servicechain_node_id = context . current_node [ '<STR_LIT:id>' ] , <EOL> driver_name = driver_name ) <EOL> session . add ( owner ) <EOL> def get_node_owner ( context ) : <EOL> session = context . session <EOL> with session . begin ( subtransactions = True ) : <EOL> query = session . query ( NodeToDriverMapping ) <EOL> query = query . filter_by ( <EOL> servicechain_instance_id = context . instance [ '<STR_LIT:id>' ] ) <EOL> query = query . filter_by ( <EOL> servicechain_node_id = context . current_node [ '<STR_LIT:id>' ] ) <EOL> return query . all ( ) <EOL> def unset_node_owner ( context ) : <EOL> session = context . session <EOL> with session . begin ( subtransactions = True ) : <EOL> query = session . query ( NodeToDriverMapping ) <EOL> query = query . filter_by ( <EOL> servicechain_instance_id = context . instance [ '<STR_LIT:id>' ] ) <EOL> query = query . filter_by ( <EOL> servicechain_node_id = context . current_node [ '<STR_LIT:id>' ] ) <EOL> for owner in query . all ( ) : <EOL> session . delete ( owner ) <EOL> def set_service_target ( context , policy_target_id , relationship ) : <EOL> session = context . session <EOL> with session . begin ( subtransactions = True ) : <EOL> owner = ServiceTarget ( <EOL> policy_target_id = policy_target_id , <EOL> servicechain_instance_id = context . instance [ '<STR_LIT:id>' ] , <EOL> servicechain_node_id = context . current_node [ '<STR_LIT:id>' ] , <EOL> position = context . current_position , <EOL> relationship = relationship ) <EOL> session . add ( owner ) <EOL> def get_service_targets ( session , policy_target_id = None , relationship = None , <EOL> servicechain_instance_id = None , position = None , <EOL> servicechain_node_id = None ) : <EOL> with session . begin ( subtransactions = True ) : <EOL> query = _prepare_service_target_query ( <EOL> session , policy_target_id = policy_target_id , <EOL> relationship = relationship , <EOL> servicechain_instance_id = servicechain_instance_id , <EOL> position = position , servicechain_node_id = servicechain_node_id ) <EOL> return query . all ( ) <EOL> def get_service_targets_count ( session , policy_target_id = None , <EOL> relationship = None , servicechain_instance_id = None , <EOL> position = None , servicechain_node_id = None ) : <EOL> with session . begin ( subtransactions = True ) : <EOL> query = _prepare_service_target_query ( <EOL> session , policy_target_id = policy_target_id , <EOL> relationship = relationship , <EOL> servicechain_instance_id = servicechain_instance_id , <EOL> position = position , servicechain_node_id = servicechain_node_id ) <EOL> return query . count ( ) <EOL> def _prepare_service_target_query ( session , policy_target_id = None , <EOL> relationship = None , <EOL> servicechain_instance_id = None , position = None , <EOL> servicechain_node_id = None ) : <EOL> query = session . query ( ServiceTarget ) <EOL> if servicechain_instance_id : <EOL> query = query . filter_by ( <EOL> servicechain_instance_id = servicechain_instance_id ) <EOL> if servicechain_node_id : <EOL> query = query . filter_by ( <EOL> servicechain_node_id = servicechain_node_id ) <EOL> if policy_target_id : <EOL> query = query . filter_by ( policy_target_id = policy_target_id ) <EOL> if position : <EOL> query = query . filter_by ( position = position ) <EOL> if relationship : <EOL> query = query . filter_by ( relationship = relationship ) <EOL> return query </s>
